Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
66 changes: 66 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be changed to :
expect(result.body.title).to.eql(aPost.title)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has discussion with @gunjpan decided to keep it the same

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() {
Expand Down