Skip to content

Commit ae4d53d

Browse files
authored
Merge pull request #2879 from strongloop/batch-create
allow batch create for persisted models
2 parents 8cc71a4 + 5252fba commit ae4d53d

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

lib/persisted-model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ module.exports = function(registry) {
640640
description: 'Create a new instance of the model and persist it into the data source.',
641641
accessType: 'WRITE',
642642
accepts: {
643-
arg: 'data', type: 'object', model: typeName,
643+
arg: 'data', type: 'object', model: typeName, allowArray: true,
644644
description: 'Model instance data',
645645
http: { source: 'body' },
646646
},

test/model.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,44 @@ describe.onServer('Remote Methods', function() {
124124
app.use(loopback.rest());
125125
});
126126

127+
describe('Model.create(data, callback)', function() {
128+
it('creates model', function(done) {
129+
var anObject = { first: 'June' };
130+
request(app)
131+
.post('/users')
132+
// sends an object
133+
.send(anObject)
134+
.expect('Content-Type', /json/)
135+
.expect(200)
136+
.end(function(err, res) {
137+
if (err) return done(err);
138+
expect(res.body).to.have.property('id');
139+
expect(res.body).to.have.property('first', 'June');
140+
done();
141+
});
142+
});
143+
// batch create must be tested with a remote request because there are
144+
// coercion being done on strong-remoting side
145+
it('creates array of models', function(done) {
146+
var arrayOfObjects = [
147+
{ first: 'John' }, { first: 'Jane' },
148+
];
149+
request(app)
150+
.post('/users')
151+
// sends an array of objects
152+
.send(arrayOfObjects)
153+
.expect('Content-Type', /json/)
154+
.expect(200)
155+
.end(function(err, res) {
156+
if (err) return done(err);
157+
expect(res.body.length).to.eql(2);
158+
expect(res.body).to.have.deep.property('[0].first', 'John');
159+
expect(res.body).to.have.deep.property('[1].first', 'Jane');
160+
done();
161+
});
162+
});
163+
});
164+
// destoryAll is not exposed as a remoteMethod by default
127165
describe('Model.destroyAll(callback)', function() {
128166
it('Delete all Model instances from data source', function(done) {
129167
(new TaskEmitter())

0 commit comments

Comments
 (0)