Skip to content

Commit 63d8b73

Browse files
committed
Allow passing a single object to Model.create.
Addresses dresende#159
1 parent 54f272f commit 63d8b73

File tree

3 files changed

+60
-19
lines changed

3 files changed

+60
-19
lines changed

lib/Model.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,10 @@ function Model(opts) {
398398
model.create = function () {
399399
var Instances = [];
400400
var options = {};
401-
var cb = null, idx = 0;
401+
var cb = null, idx = 0, single = false;
402402
var createNext = function () {
403403
if (idx >= Instances.length) {
404-
return cb(null, Instances);
404+
return cb(null, single ? Instances[0] : Instances);
405405
}
406406

407407
Instances[idx] = createInstance(Instances[idx], {
@@ -423,14 +423,16 @@ function Model(opts) {
423423
};
424424

425425
for (var i = 0; i < arguments.length; i++) {
426-
if (Array.isArray(arguments[i])) {
427-
Instances = Instances.concat(arguments[i]);
428-
continue;
429-
}
430-
431426
switch (typeof arguments[i]) {
432427
case "object":
433-
options = arguments[i];
428+
if ( !single && Array.isArray(arguments[i]) ) {
429+
Instances = Instances.concat(arguments[i]);
430+
} else if (i == 0) {
431+
single = true;
432+
Instances.push(arguments[i]);
433+
} else {
434+
options = arguments[i];
435+
}
434436
break;
435437
case "function":
436438
cb = arguments[i];

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"urun" : "0.0.6",
4444
"mysql" : "2.0.0-alpha7",
4545
"pg" : "1.0.0",
46-
"sqlite3" : "2.1.5"
46+
"sqlite3" : "2.1.5",
47+
"async" : "*"
4748
},
4849
"optionalDependencies": {}
4950
}

test/integration/test-create.js

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,58 @@
11
var common = require('../common');
22
var assert = require('assert');
3+
var async = require('async');
34

45
common.createConnection(function (err, db) {
56
common.createModelTable('test_create', db.driver.db, function () {
67
var TestModel = db.define('test_create', common.getModelProperties());
78

8-
TestModel.create([
9-
{ name: 'test1' },
10-
{ name: 'test2' },
11-
{ name: 'test3' }
12-
], function (err) {
13-
TestModel.count(function (err, count) {
14-
assert.equal(err, null);
15-
assert.equal(count, 3);
16-
db.close();
17-
});
9+
async.series([
10+
// Test that items are actually created
11+
function (done) {
12+
TestModel.create([
13+
{ name: 'test1' },
14+
{ name: 'test2' },
15+
{ name: 'test3' }
16+
], function (err) {
17+
TestModel.count(function (err, count) {
18+
assert.equal(err, null);
19+
assert.equal(count, 3);
20+
done();
21+
});
22+
});
23+
},
24+
function (done) {
25+
TestModel.create({ name: 'test4' }, function (err) {
26+
TestModel.count(function (err, count) {
27+
assert.equal(err, null);
28+
assert.equal(count, 4);
29+
done();
30+
});
31+
});
32+
},
33+
// Test callback arguments
34+
function (done) {
35+
TestModel.create([
36+
{ name: 'test5' },
37+
{ name: 'test6' }
38+
], function (err, items) {
39+
assert.equal(err, null);
40+
assert.equal(Array.isArray(items), true);
41+
assert.equal(items.length, 2);
42+
done();
43+
});
44+
},
45+
function (done) {
46+
TestModel.create({ name: 'test7' }, function (err, item) {
47+
assert.equal(err, null);
48+
assert.equal(Array.isArray(item), false);
49+
assert.equal(item.name, 'test7');
50+
assert.equal(item.hasOwnProperty('id'), true);
51+
done();
52+
});
53+
}
54+
], function complete() {
55+
db.close();
1856
});
1957
});
2058
});

0 commit comments

Comments
 (0)