Adds relation fields to objects
This commit is contained in:
@@ -1271,4 +1271,33 @@ describe('miscellaneous', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('gets relation fields', (done) => {
|
||||||
|
let object = new Parse.Object('AnObject');
|
||||||
|
let relatedObject = new Parse.Object('RelatedObject');
|
||||||
|
Parse.Object.saveAll([object, relatedObject]).then(() => {
|
||||||
|
object.relation('related').add(relatedObject);
|
||||||
|
return object.save();
|
||||||
|
}).then(() => {
|
||||||
|
let headers = {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-Parse-Application-Id': 'test',
|
||||||
|
'X-Parse-REST-API-Key': 'rest'
|
||||||
|
};
|
||||||
|
let requestOptions = {
|
||||||
|
headers: headers,
|
||||||
|
url: 'http://localhost:8378/1/classes/AnObject',
|
||||||
|
json: true
|
||||||
|
};
|
||||||
|
request.get(requestOptions, (err, res, body) => {
|
||||||
|
expect(body.results.length).toBe(1);
|
||||||
|
let result = body.results[0];
|
||||||
|
expect(result.related).toEqual({
|
||||||
|
__type: "Relation",
|
||||||
|
className: 'RelatedObject'
|
||||||
|
})
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ var dummySchema = {
|
|||||||
return 'geopoint';
|
return 'geopoint';
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
},
|
||||||
|
getRelationFields: function() {
|
||||||
|
return {}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -634,6 +634,24 @@ class Schema {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
getRelationFields(className) {
|
||||||
|
if (this.data && this.data[className]) {
|
||||||
|
let classData = this.data[className];
|
||||||
|
return Object.keys(classData).filter((field) => {
|
||||||
|
return classData[field].startsWith('relation');
|
||||||
|
}).reduce((memo, field) => {
|
||||||
|
let type = classData[field];
|
||||||
|
let className = type.slice('relation<'.length, type.length - 1);
|
||||||
|
memo[field] = {
|
||||||
|
__type: 'Relation',
|
||||||
|
className: className
|
||||||
|
};
|
||||||
|
return memo;
|
||||||
|
}, {});
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a promise for a new Schema.
|
// Returns a promise for a new Schema.
|
||||||
|
|||||||
@@ -732,6 +732,11 @@ function untransformObject(schema, className, mongoObject, isNestedObject = fals
|
|||||||
mongoObject[key], true);
|
mongoObject[key], true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!isNestedObject) {
|
||||||
|
let relationFields = schema.getRelationFields(className);
|
||||||
|
Object.assign(restObject, relationFields);
|
||||||
|
}
|
||||||
return restObject;
|
return restObject;
|
||||||
default:
|
default:
|
||||||
throw 'unknown js type';
|
throw 'unknown js type';
|
||||||
|
|||||||
Reference in New Issue
Block a user