From 799370c05464a98b921642025ed9629dfb95b96c Mon Sep 17 00:00:00 2001 From: Ian Patton Date: Wed, 10 Feb 2016 16:28:58 -0500 Subject: [PATCH] AGCMD-1807: Hapi-harvester not using data field in relationships --- .gitignore | 2 +- lib/adapters/mongodb/connection.js | 4 ++++ lib/plugin.js | 12 +++++++++++- lib/utils/schema.js | 4 +++- test/includes.spec.js | 20 +++++++++++++++----- test/relatedLinks.spec.js | 17 ++++++++++++----- test/relationshipCRUD.spec.js | 20 +++++++++++++++----- test/remoteIncludes.spec.js | 9 ++++++--- test/seeder.js | 6 +++--- test/swagger.spec.js | 30 ++++++------------------------ 10 files changed, 76 insertions(+), 48 deletions(-) diff --git a/.gitignore b/.gitignore index 41fb4ec..09167e7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ *.iml .idea node_modules -coverage \ No newline at end of file +coverage/ diff --git a/lib/adapters/mongodb/connection.js b/lib/adapters/mongodb/connection.js index e0aa233..f410b75 100644 --- a/lib/adapters/mongodb/connection.js +++ b/lib/adapters/mongodb/connection.js @@ -20,6 +20,10 @@ module.exports.connect = function (url, options) { module.exports.disconnect = function (db) { return new Promise((resolve, reject) => { + if(db.base.connections[0] && db.base.connections[0]._readyState === 0) { + return resolve(); + } + db.on('close', () => { resolve() }) diff --git a/lib/plugin.js b/lib/plugin.js index b0310c1..402b4e9 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -98,11 +98,21 @@ exports.register = function (server, opts, next) { }) } + const formatRelationships = function(data) { + if(data.relationships) { + _.each(data.relationships, (val, key) => { + data.relationships[key] = data.relationships[key].data; + }) + } + return data; + } + const post = function (schema) { onRouteRegister(schema) return _.merge(routes.post(schema), { handler: (req, reply) => { - reply(adapter.create(schema.type, req.payload.data).then((data)=> { + const formattedData = formatRelationships(req.payload.data); + reply(adapter.create(schema.type, formattedData).then((data)=> { return {data: data} })).code(201) } diff --git a/lib/utils/schema.js b/lib/utils/schema.js index 1724edf..10ca43c 100644 --- a/lib/utils/schema.js +++ b/lib/utils/schema.js @@ -16,7 +16,9 @@ module.exports = function () { id: Joi.string().required().regex(idPattern).description(idDescription), type: Joi.string().required().valid(itemSchemaType) } - relationshipsScheme[key] = isArray ? Joi.array().items(itemSchema) : itemSchema; + relationshipsScheme[key] = Joi.object().keys({ + data: isArray ? Joi.array().items(itemSchema) : Joi.object().keys(itemSchema) + }); }); return Joi.object().keys({ data: Joi.object().keys({ diff --git a/test/includes.spec.js b/test/includes.spec.js index 438785d..191cc0d 100644 --- a/test/includes.spec.js +++ b/test/includes.spec.js @@ -49,8 +49,12 @@ const data = { appearances: 2007 }, relationships: { - pets: [{type: 'pets', id: 'c344d722-b7f9-49dd-9842-f0a375f7dfdc'}, {type: 'pets', id: 'a344d722-b7f9-49dd-9842-f0a375f7dfdc'}], - soulmate: {type: 'people', id: 'c344d722-b7f9-49dd-9842-f0a375f7dfdc'} + pets: { + data: [{type: 'pets', id: 'c344d722-b7f9-49dd-9842-f0a375f7dfdc'}, {type: 'pets', id: 'a344d722-b7f9-49dd-9842-f0a375f7dfdc'}] + }, + soulmate: { + data: {type: 'people', id: 'c344d722-b7f9-49dd-9842-f0a375f7dfdc'} + } } }, { @@ -60,7 +64,9 @@ const data = { name: 'Paul' }, relationships: { - pets: [{type: 'pets', id: 'b344d722-b7f9-49dd-9842-f0a375f7dfdc'}] + pets: { + data: [{type: 'pets', id: 'b344d722-b7f9-49dd-9842-f0a375f7dfdc'}] + } } } ], @@ -86,7 +92,9 @@ const data = { name: 'Horsepol' }, relationships: { - owner: {type: 'people', id: 'abcdefff-b7f9-49dd-9842-f0a375f7dfdc'} + owner: { + data: {type: 'people', id: 'abcdefff-b7f9-49dd-9842-f0a375f7dfdc'} + } } } ], @@ -94,7 +102,9 @@ const data = { { type: 'collars', relationships: { - collarOwner: {type: 'pets', id: 'b344d722-b7f9-49dd-9842-f0a375f7dfdc'} + collarOwner: { + data: {type: 'pets', id: 'b344d722-b7f9-49dd-9842-f0a375f7dfdc'} + } } } ] diff --git a/test/relatedLinks.spec.js b/test/relatedLinks.spec.js index aca322d..ade3e5f 100644 --- a/test/relatedLinks.spec.js +++ b/test/relatedLinks.spec.js @@ -39,8 +39,12 @@ const data = { appearances: 2007 }, relationships: { - pets: [{type: 'pets', id: 'c344d722-b7f9-49dd-9842-f0a375f7dfdc'}, {type: 'pets', id: 'a344d722-b7f9-49dd-9842-f0a375f7dfdc'}], - soulmate: {type: 'people', id: 'c344d722-b7f9-49dd-9842-f0a375f7dfdc'} + pets: { + data: [{type: 'pets', id: 'c344d722-b7f9-49dd-9842-f0a375f7dfdc'}, {type: 'pets', id: 'a344d722-b7f9-49dd-9842-f0a375f7dfdc'}] + }, + soulmate: { + data: {type: 'people', id: 'c344d722-b7f9-49dd-9842-f0a375f7dfdc'} + } } }, { @@ -50,7 +54,9 @@ const data = { name: 'Paul' }, relationships: { - pets: [] + pets: { + data: [] + } } } ], @@ -76,7 +82,9 @@ const data = { name: 'Horsepol' }, relationships: { - owner: {type: 'people', id: 'abcdefff-b7f9-49dd-9842-f0a375f7dfdc'} + owner: { + data: {type: 'people', id: 'abcdefff-b7f9-49dd-9842-f0a375f7dfdc'} + } } } ] @@ -174,4 +182,3 @@ describe('Related links', () => { }); }); }); - diff --git a/test/relationshipCRUD.spec.js b/test/relationshipCRUD.spec.js index cdff088..fbbd4c5 100644 --- a/test/relationshipCRUD.spec.js +++ b/test/relationshipCRUD.spec.js @@ -49,8 +49,12 @@ const data = { appearances: 2007 }, relationships: { - pets: [{type: 'pets', id: 'c344d722-b7f9-49dd-9842-f0a375f7dfdc'}, {type: 'pets', id: 'a344d722-b7f9-49dd-9842-f0a375f7dfdc'}], - soulmate: {type: 'people', id: 'c344d722-b7f9-49dd-9842-f0a375f7dfdc'} + pets: { + data: [{type: 'pets', id: 'c344d722-b7f9-49dd-9842-f0a375f7dfdc'}, {type: 'pets', id: 'a344d722-b7f9-49dd-9842-f0a375f7dfdc'}] + }, + soulmate: { + data: {type: 'people', id: 'c344d722-b7f9-49dd-9842-f0a375f7dfdc'} + } } }, { @@ -60,7 +64,9 @@ const data = { name: 'Paul' }, relationships: { - pets: [{type: 'pets', id: 'b344d722-b7f9-49dd-9842-f0a375f7dfdc'}] + pets: { + data: [{type: 'pets', id: 'b344d722-b7f9-49dd-9842-f0a375f7dfdc'}] + } } } ], @@ -86,7 +92,9 @@ const data = { name: 'Horsepol' }, relationships: { - owner: {type: 'people', id: 'abcdefff-b7f9-49dd-9842-f0a375f7dfdc'} + owner: { + data: {type: 'people', id: 'abcdefff-b7f9-49dd-9842-f0a375f7dfdc'} + } } } ], @@ -94,7 +102,9 @@ const data = { { type: 'collars', relationships: { - collarOwner: {type: 'pets', id: 'b344d722-b7f9-49dd-9842-f0a375f7dfdc'} + collarOwner: { + data: {type: 'pets', id: 'b344d722-b7f9-49dd-9842-f0a375f7dfdc'} + } } } ] diff --git a/test/remoteIncludes.spec.js b/test/remoteIncludes.spec.js index 47bfaee..0f5927c 100644 --- a/test/remoteIncludes.spec.js +++ b/test/remoteIncludes.spec.js @@ -87,7 +87,7 @@ describe('remote link', function () { const data = { type: 'people', attributes: {firstName: 'Tony', lastName: 'Maley'}, - relationships: {country: {type: 'countries', id: that.countryId}} + relationships: {country: {data: {type: 'countries', id: that.countryId}}} }; return server2.injectThen({method: 'post', url: '/people', payload: {data: data}}); }).then(function (response) { @@ -104,7 +104,10 @@ describe('remote link', function () { const data = { type: 'posts', attributes: {}, - relationships: {author: {type: 'people', id: that.authorId}, comments: [{type: 'comments', id: that.commentId}]} + relationships: { + author: {data: {type: 'people', id: that.authorId}}, + comments: {data: [{type: 'comments', id: that.commentId}]} + } }; return server1.injectThen({method: 'post', url: '/posts', payload: {data: data}}); }).then(function (response) { @@ -171,7 +174,7 @@ describe('remote link', function () { const data = { type: 'posts', attributes: {}, - relationships: {comments: [{type: 'comments', id: '00000000-0000-4000-b000-000000000000'}]} + relationships: {comments: {data: [{type: 'comments', id: '00000000-0000-4000-b000-000000000000'}]}} }; return server1.injectThen({method: 'post', url: '/posts', payload: {data: data}}).then(function (result) { expect(result.statusCode).to.equal(201) diff --git a/test/seeder.js b/test/seeder.js index 6eb5caa..6397900 100644 --- a/test/seeder.js +++ b/test/seeder.js @@ -16,9 +16,9 @@ module.exports = function (harvesterInstance) { function post(key, items) { return _(items).map(function (item) { return harvesterInstance.injectThen({method: 'post', url: '/' + key, payload: {data: item}}).then(function (response) { - //if (response.statusCode !== 201) { - // console.log(JSON.stringify(response.result, null, ' ')); - //} + if (response.statusCode !== 201) { + console.log(JSON.stringify(response.result, null, ' ')); + } expect(response.statusCode).to.equal(201); return response.result.data.id; }); diff --git a/test/swagger.spec.js b/test/swagger.spec.js index 8966c2f..98939a8 100644 --- a/test/swagger.spec.js +++ b/test/swagger.spec.js @@ -221,16 +221,11 @@ describe('Swagger docs', function () { type: 'object', properties: { pets: { - type: 'array', - defaultValue: undefined, + type: 'pets', + defaultValue: null, description: undefined, - maxItems: undefined, - minItems: undefined, notes: undefined, - tags: undefined, - items: { - $ref: 'pets' - } + tags: undefined }, soulmate: { type: 'soulmate', @@ -247,7 +242,8 @@ describe('Swagger docs', function () { properties: { id: { type: 'string', - defaultValue: null, + required: true, + defaultValue: undefined, description: 'RFC4122 v4 UUID', notes: undefined, tags: undefined @@ -256,21 +252,7 @@ describe('Swagger docs', function () { type: 'string', required: true, defaultValue: undefined, - enum: ['people'], - description: undefined, - notes: undefined, - tags: undefined - }, - attributes: { - type: 'attributes', - defaultValue: null, - description: undefined, - notes: undefined, - tags: undefined - }, - relationships: { - type: 'relationships', - defaultValue: null, + enum: ['pets'], description: undefined, notes: undefined, tags: undefined