allow batch create for persisted models

In strong-remoting 3.x, we have stricken the coercion of inputs
methods that are expecting an Object will nolonger accept an array
as input, to preserve backwards compatibility we have added flag
allowArray in remote arguments, which would accept an array of objects
This commit is contained in:
David Cheung 2016-10-19 17:59:39 -04:00
parent 8cc71a4dc0
commit 5252fba376
2 changed files with 39 additions and 1 deletions

View File

@ -640,7 +640,7 @@ module.exports = function(registry) {
description: 'Create a new instance of the model and persist it into the data source.', description: 'Create a new instance of the model and persist it into the data source.',
accessType: 'WRITE', accessType: 'WRITE',
accepts: { accepts: {
arg: 'data', type: 'object', model: typeName, arg: 'data', type: 'object', model: typeName, allowArray: true,
description: 'Model instance data', description: 'Model instance data',
http: { source: 'body' }, http: { source: 'body' },
}, },

View File

@ -124,6 +124,44 @@ describe.onServer('Remote Methods', function() {
app.use(loopback.rest()); app.use(loopback.rest());
}); });
describe('Model.create(data, callback)', function() {
it('creates model', function(done) {
var anObject = { first: 'June' };
request(app)
.post('/users')
// sends an object
.send(anObject)
.expect('Content-Type', /json/)
.expect(200)
.end(function(err, res) {
if (err) return done(err);
expect(res.body).to.have.property('id');
expect(res.body).to.have.property('first', 'June');
done();
});
});
// batch create must be tested with a remote request because there are
// coercion being done on strong-remoting side
it('creates array of models', function(done) {
var arrayOfObjects = [
{ first: 'John' }, { first: 'Jane' },
];
request(app)
.post('/users')
// sends an array of objects
.send(arrayOfObjects)
.expect('Content-Type', /json/)
.expect(200)
.end(function(err, res) {
if (err) return done(err);
expect(res.body.length).to.eql(2);
expect(res.body).to.have.deep.property('[0].first', 'John');
expect(res.body).to.have.deep.property('[1].first', 'Jane');
done();
});
});
});
// destoryAll is not exposed as a remoteMethod by default
describe('Model.destroyAll(callback)', function() { describe('Model.destroyAll(callback)', function() {
it('Delete all Model instances from data source', function(done) { it('Delete all Model instances from data source', function(done) {
(new TaskEmitter()) (new TaskEmitter())