Skip to content

Commit ee6e8b2

Browse files
authored
Merge pull request #856 from dresende/model_create_options
Accept options in 'model.create' call
2 parents 4705473 + 4d08e89 commit ee6e8b2

File tree

7 files changed

+98
-5
lines changed

7 files changed

+98
-5
lines changed

Changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### v6.1.0
2+
- [Feature] Accept options when calling `Model.create` ([856](../../pull/856))
3+
14
### v6.0.0
25
- [POSSIBLY BREAKING] Set internal default property value to `undefined` instead of `null` ([855](../../pull/855)).
36
- This will prevent `null` values being explicitly sent to the database when no value was assigned and instead result in the database setting the column to null, or generating a default value.

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
## This package is not actively maintained
1010

11-
If you're starting a new project, please consider using one of the following instead as they have a much more active community:
11+
If you're starting a new project, consider using one of the following instead as they have a more active community:
1212
* https://github.com/bookshelf/bookshelf
1313
* https://github.com/sequelize/sequelize
1414

lib/Model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ function Model(opts) {
616616

617617
return cb(err);
618618
}
619-
item.save(function (err) {
619+
item.save({}, options, function (err) {
620620
if (err) {
621621
err.index = index;
622622
err.instance = item;

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"sqlite",
1313
"mongodb"
1414
],
15-
"version": "6.0.0",
15+
"version": "6.1.0",
1616
"license": "MIT",
1717
"homepage": "http://dresende.github.io/node-orm2",
1818
"repository": "http://github.com/dresende/node-orm2.git",

test/integration/model-create.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var should = require('should');
2+
var sinon = require('sinon');
23
var helper = require('../support/spec_helper');
34

45
describe("Model.create()", function() {
@@ -16,7 +17,7 @@ describe("Model.create()", function() {
1617
});
1718
Person.hasMany("pets", Pet);
1819

19-
return helper.dropSync([ Person, Pet ], done);
20+
return helper.dropSync([Person, Pet], done);
2021
};
2122
};
2223

@@ -32,6 +33,10 @@ describe("Model.create()", function() {
3233
return db.close();
3334
});
3435

36+
afterEach(function () {
37+
sinon.restore();
38+
});
39+
3540
describe("if passing an object", function () {
3641
before(setup());
3742

@@ -108,6 +113,47 @@ describe("Model.create()", function() {
108113
return done();
109114
});
110115
});
116+
117+
describe("with 'saveAssociationsByDefault' disabled", function () {
118+
beforeEach(function () {
119+
sinon.stub(db.settings, 'get').withArgs('instance.saveAssociationsByDefault').returns(false);
120+
});
121+
122+
it("should not save associations", function (done) {
123+
Person.create({
124+
name : "John Doe",
125+
pets : [ { name: "Deco" } ]
126+
}, function (err, John) {
127+
should.equal(err, null);
128+
129+
should.equal(Array.isArray(John.pets), true);
130+
131+
John.pets[0].should.have.property("name", "Deco");
132+
John.pets[0].should.not.have.property(Pet.id);
133+
134+
return done();
135+
});
136+
});
137+
138+
it("should save associations if 'saveAssociations' is passed", function (done) {
139+
Person.create({
140+
name : "John Doe",
141+
pets : [ { name: "Deco" } ]
142+
}, {
143+
saveAssociations: true
144+
}, function (err, John) {
145+
should.equal(err, null);
146+
147+
should.equal(Array.isArray(John.pets), true);
148+
149+
John.pets[0].should.have.property("name", "Deco");
150+
John.pets[0].should.have.property(Pet.id);
151+
John.pets[0].saved().should.be.true;
152+
153+
return done();
154+
});
155+
});
156+
});
111157
});
112158

113159
describe("when not passing a property", function () {

test/integration/model-createAsync.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var should = require('should');
2+
var sinon = require('sinon');
23
var helper = require('../support/spec_helper');
34

45
describe("Model.createAsync()", function() {
@@ -32,6 +33,10 @@ describe("Model.createAsync()", function() {
3233
return db.close();
3334
});
3435

36+
afterEach(function () {
37+
sinon.restore();
38+
});
39+
3540
describe("if passing an object", function () {
3641
before(setup());
3742

@@ -98,6 +103,45 @@ describe("Model.createAsync()", function() {
98103
should.equal(John.pets[0].saved(), true);
99104
});
100105
});
106+
107+
describe("with 'saveAssociationsByDefault' disabled", function () {
108+
beforeEach(function () {
109+
sinon.stub(db.settings, 'get').withArgs('instance.saveAssociationsByDefault').returns(false);
110+
});
111+
112+
it("should not save associations", function () {
113+
return Person.createAsync({
114+
name : "John Doe",
115+
pets : [ { name: "Deco" } ]
116+
})
117+
.then(function (John) {
118+
John.should.have.property("name", "John Doe");
119+
120+
should(Array.isArray(John.pets));
121+
122+
John.pets[0].should.have.property("name", "Deco");
123+
John.pets[0].should.not.have.property(Pet.id);
124+
});
125+
});
126+
127+
it("should save associations if 'saveAssociations' is passed", function () {
128+
return Person.createAsync({
129+
name : "John Doe",
130+
pets : [ { name: "Deco" } ]
131+
}, {
132+
saveAssociations: true
133+
})
134+
.then(function (John) {
135+
John.should.have.property("name", "John Doe");
136+
137+
should(Array.isArray(John.pets));
138+
139+
John.pets[0].should.have.property("name", "Deco");
140+
John.pets[0].should.have.property(Pet.id);
141+
should.equal(John.pets[0].saved(), true);
142+
});
143+
});
144+
});
101145
});
102146

103147
describe("when not passing a property", function () {

0 commit comments

Comments
 (0)