Skip to content

Commit

Permalink
[#414] Creates nested schema
Browse files Browse the repository at this point in the history
lib/adapters/mongodb.js
- Updates schema links generation, deletes old ref

lib/route.js
- Makes patch use nuclear $set

test/harvester/includes.js
- Nests patch paths in links

Signed-off-by: Braden O'Guinn <braden.oguinn@mutualmobile.com>
  • Loading branch information
bradenoguinn committed Mar 19, 2015
1 parent 97791c8 commit 9137517
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
4 changes: 3 additions & 1 deletion lib/adapters/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ adapter._models = {};
adapter.schema = function (name, schema, options) {
var ObjectId = mongoose.Schema.Types.ObjectId;
var Mixed = mongoose.Schema.Types.Mixed;
schema.links = schema.links || {};

_.each(schema, function (val, key) {
var obj = {};
Expand All @@ -42,7 +43,8 @@ adapter.schema = function (name, schema, options) {
obj.ref = ref;
obj.inverse = inverse;
obj.type = ObjectId;
schema[key] = isArray ? [obj] : obj;
schema.links[key] = isArray ? [obj] : obj;
delete schema[key];
}

// Convert native object to schema type Mixed
Expand Down
14 changes: 4 additions & 10 deletions lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,22 +588,16 @@ function route(name, model, routeOptions) {
// parse patch request, only 'replace' op is supported per the json-api spec
req.body.forEach(function (operation) {
// TODO: bulk PATCH request
var field = operation.path.split('/').slice(3);
var field = operation.path.split('/').slice(3).join('.');
var value = operation.value;
var path = update;

if (operation.op === 'replace') {
field.forEach(function (key, index) {
if (index + 1 === field.length) {
path[key] = value;
} else {
path[key] = path[key] || {};
path = path[key];
}
});
update[field] = value;
}
});

update = {$set: update};

// do before transform
beforeTransform(update, req, res)

Expand Down
6 changes: 3 additions & 3 deletions test/harvester/includes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ module.exports = function(baseUrl,keys,ids) {
});
}
RSVP.all([
link('/people/' + ids.people[0], '/people/0/soulmate', ids.people[1]),
link('/people/' + ids.people[0], '/people/0/links/soulmate', ids.people[1]),
//TODO: harvester should take care about this on its own
link('/people/' + ids.people[1], '/people/0/soulmate', ids.people[0]),
link('/people/' + ids.people[1], '/people/0/links/soulmate', ids.people[0]),

link('/people/' + ids.people[0], '/people/0/lovers', [ids.people[1]])
link('/people/' + ids.people[0], '/people/0/links/lovers', [ids.people[1]])
]).then(function(){
done();
});
Expand Down

0 comments on commit 9137517

Please sign in to comment.