Skip to content

Commit fa7cb92

Browse files
author
David Cheung
committed
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
1 parent d4bee2b commit fa7cb92

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

lib/model.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,13 @@ module.exports = function(registry) {
668668
define('__create__' + scopeName, {
669669
isStatic: isStatic,
670670
http: {verb: 'post', path: '/' + pathName},
671-
accepts: {arg: 'data', type: 'object', model: toModelName, http: {source: 'body'}},
671+
accepts: {
672+
arg: 'data',
673+
type: 'object',
674+
allowArray: true,
675+
model: toModelName,
676+
http: {source: 'body'},
677+
},
672678
description: format('Creates a new instance in %s of this model.', scopeName),
673679
accessType: 'WRITE',
674680
returns: {arg: 'data', type: toModelName, root: true},

test/model.test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,72 @@ describe.onServer('Remote Methods', function() {
166166
done();
167167
});
168168
});
169+
170+
it('creates related models', function(done) {
171+
User.create({first: 'Bob'}, function(err, res) {
172+
expect(res).to.have.property('id');
173+
var aPost = {title: 'A story', content: 'Once upon a time'};
174+
request(app)
175+
.post('/users/' + res.id + '/posts')
176+
.send(aPost)
177+
.expect('Content-Type', /json/)
178+
.expect(200)
179+
.end(function(err, result) {
180+
if (err) return done(err);
181+
expect(result.body).to.have.property('id');
182+
expect(result.body).to.have.property('title', aPost.title);
183+
expect(result.body).to.have.property('content', aPost.content);
184+
done();
185+
});
186+
});
187+
});
188+
189+
it('creates array of hasMany models', function(done) {
190+
User.create({first: 'Bob'}, function(err, res) {
191+
expect(res).to.have.property('id');
192+
var twoPosts = [
193+
{title: 'One story', content: 'Content #1'},
194+
{title: 'Two story', content: 'Content #2'},
195+
];
196+
request(app)
197+
.post('/users/' + res.id + '/posts')
198+
.send(twoPosts)
199+
.expect('Content-Type', /json/)
200+
.expect(200)
201+
.end(function(err, result) {
202+
if (err) return done(err);
203+
expect(result.body.length).to.eql(2);
204+
expect(result.body).to.have.deep.property('[0].title', 'One story');
205+
expect(result.body).to.have.deep.property('[1].title', 'Two story');
206+
done();
207+
});
208+
});
209+
});
210+
211+
it('rejects array of obj input for hasOne relation', function(done) {
212+
var Friend = app.registry.createModel('friend', {name: String});
213+
app.model(Friend, {dataSource: 'db'});
214+
User.hasOne(Friend);
215+
216+
User.create({first: 'Bob'}, function(err, res) {
217+
expect(res).to.have.property('id');
218+
var twoFriends = [
219+
{name: 'bob'},
220+
{name: 'rob'},
221+
];
222+
request(app)
223+
.post('/users/' + res.id + '/friend')
224+
.send(twoFriends)
225+
.expect('Content-Type', /json/)
226+
.expect(400)
227+
.end(function(err, result) {
228+
if (err) return done(err);
229+
var resError = result.body.error;
230+
expect(resError.message).to.match(/value(.*?)not(.*?)object(\.?)/i);
231+
done();
232+
});
233+
});
234+
});
169235
});
170236
// destoryAll is not exposed as a remoteMethod by default
171237
describe('Model.destroyAll(callback)', function() {

0 commit comments

Comments
 (0)