Skip to content

Commit

Permalink
AGCMD-1807: Hapi-harvester not using data field in relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
niahmiah committed Feb 11, 2016
1 parent 5c7f3c8 commit 799370c
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*.iml
.idea
node_modules
coverage
coverage/
4 changes: 4 additions & 0 deletions lib/adapters/mongodb/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down
12 changes: 11 additions & 1 deletion lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 3 additions & 1 deletion lib/utils/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
20 changes: 15 additions & 5 deletions test/includes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
}
}
},
{
Expand All @@ -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'}]
}
}
}
],
Expand All @@ -86,15 +92,19 @@ const data = {
name: 'Horsepol'
},
relationships: {
owner: {type: 'people', id: 'abcdefff-b7f9-49dd-9842-f0a375f7dfdc'}
owner: {
data: {type: 'people', id: 'abcdefff-b7f9-49dd-9842-f0a375f7dfdc'}
}
}
}
],
collars: [
{
type: 'collars',
relationships: {
collarOwner: {type: 'pets', id: 'b344d722-b7f9-49dd-9842-f0a375f7dfdc'}
collarOwner: {
data: {type: 'pets', id: 'b344d722-b7f9-49dd-9842-f0a375f7dfdc'}
}
}
}
]
Expand Down
17 changes: 12 additions & 5 deletions test/relatedLinks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
}
}
},
{
Expand All @@ -50,7 +54,9 @@ const data = {
name: 'Paul'
},
relationships: {
pets: []
pets: {
data: []
}
}
}
],
Expand All @@ -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'}
}
}
}
]
Expand Down Expand Up @@ -174,4 +182,3 @@ describe('Related links', () => {
});
});
});

20 changes: 15 additions & 5 deletions test/relationshipCRUD.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
}
}
},
{
Expand All @@ -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'}]
}
}
}
],
Expand All @@ -86,15 +92,19 @@ const data = {
name: 'Horsepol'
},
relationships: {
owner: {type: 'people', id: 'abcdefff-b7f9-49dd-9842-f0a375f7dfdc'}
owner: {
data: {type: 'people', id: 'abcdefff-b7f9-49dd-9842-f0a375f7dfdc'}
}
}
}
],
collars: [
{
type: 'collars',
relationships: {
collarOwner: {type: 'pets', id: 'b344d722-b7f9-49dd-9842-f0a375f7dfdc'}
collarOwner: {
data: {type: 'pets', id: 'b344d722-b7f9-49dd-9842-f0a375f7dfdc'}
}
}
}
]
Expand Down
9 changes: 6 additions & 3 deletions test/remoteIncludes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions test/seeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
30 changes: 6 additions & 24 deletions test/swagger.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 799370c

Please sign in to comment.