From fa7cb923cdc9bae0adb40c82677e264e4559d713 Mon Sep 17 00:00:00 2001 From: David Cheung Date: Tue, 15 Nov 2016 17:51:22 -0500 Subject: [PATCH] add allowArray to relations' create remoteMethod this is needed because we added allowArray flag to persisted model's remoteMethod, but when relations try to rebuild such methods, it does not carry over such flags --- lib/model.js | 8 +++++- test/model.test.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/lib/model.js b/lib/model.js index 7946c232..02544979 100644 --- a/lib/model.js +++ b/lib/model.js @@ -668,7 +668,13 @@ module.exports = function(registry) { define('__create__' + scopeName, { isStatic: isStatic, http: {verb: 'post', path: '/' + pathName}, - accepts: {arg: 'data', type: 'object', model: toModelName, http: {source: 'body'}}, + accepts: { + arg: 'data', + type: 'object', + allowArray: true, + model: toModelName, + http: {source: 'body'}, + }, description: format('Creates a new instance in %s of this model.', scopeName), accessType: 'WRITE', returns: {arg: 'data', type: toModelName, root: true}, diff --git a/test/model.test.js b/test/model.test.js index 1c57d5b2..07c663f0 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -166,6 +166,72 @@ describe.onServer('Remote Methods', function() { done(); }); }); + + it('creates related models', function(done) { + User.create({first: 'Bob'}, function(err, res) { + expect(res).to.have.property('id'); + var aPost = {title: 'A story', content: 'Once upon a time'}; + request(app) + .post('/users/' + res.id + '/posts') + .send(aPost) + .expect('Content-Type', /json/) + .expect(200) + .end(function(err, result) { + if (err) return done(err); + expect(result.body).to.have.property('id'); + expect(result.body).to.have.property('title', aPost.title); + expect(result.body).to.have.property('content', aPost.content); + done(); + }); + }); + }); + + it('creates array of hasMany models', function(done) { + User.create({first: 'Bob'}, function(err, res) { + expect(res).to.have.property('id'); + var twoPosts = [ + {title: 'One story', content: 'Content #1'}, + {title: 'Two story', content: 'Content #2'}, + ]; + request(app) + .post('/users/' + res.id + '/posts') + .send(twoPosts) + .expect('Content-Type', /json/) + .expect(200) + .end(function(err, result) { + if (err) return done(err); + expect(result.body.length).to.eql(2); + expect(result.body).to.have.deep.property('[0].title', 'One story'); + expect(result.body).to.have.deep.property('[1].title', 'Two story'); + done(); + }); + }); + }); + + it('rejects array of obj input for hasOne relation', function(done) { + var Friend = app.registry.createModel('friend', {name: String}); + app.model(Friend, {dataSource: 'db'}); + User.hasOne(Friend); + + User.create({first: 'Bob'}, function(err, res) { + expect(res).to.have.property('id'); + var twoFriends = [ + {name: 'bob'}, + {name: 'rob'}, + ]; + request(app) + .post('/users/' + res.id + '/friend') + .send(twoFriends) + .expect('Content-Type', /json/) + .expect(400) + .end(function(err, result) { + if (err) return done(err); + var resError = result.body.error; + expect(resError.message).to.match(/value(.*?)not(.*?)object(\.?)/i); + done(); + }); + }); + }); }); // destoryAll is not exposed as a remoteMethod by default describe('Model.destroyAll(callback)', function() {