Skip to content

Commit

Permalink
Merge pull request #856 from dresende/model_create_options
Browse files Browse the repository at this point in the history
Accept options in 'model.create' call
  • Loading branch information
dxg authored Jan 24, 2022
2 parents 4705473 + 4d08e89 commit ee6e8b2
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### v6.1.0
- [Feature] Accept options when calling `Model.create` ([856](../../pull/856))

### v6.0.0
- [POSSIBLY BREAKING] Set internal default property value to `undefined` instead of `null` ([855](../../pull/855)).
- 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.
Expand Down
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

## This package is not actively maintained

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

Expand Down
2 changes: 1 addition & 1 deletion lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ function Model(opts) {

return cb(err);
}
item.save(function (err) {
item.save({}, options, function (err) {
if (err) {
err.index = index;
err.instance = item;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"sqlite",
"mongodb"
],
"version": "6.0.0",
"version": "6.1.0",
"license": "MIT",
"homepage": "http://dresende.github.io/node-orm2",
"repository": "http://github.com/dresende/node-orm2.git",
Expand Down
48 changes: 47 additions & 1 deletion test/integration/model-create.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var should = require('should');
var sinon = require('sinon');
var helper = require('../support/spec_helper');

describe("Model.create()", function() {
Expand All @@ -16,7 +17,7 @@ describe("Model.create()", function() {
});
Person.hasMany("pets", Pet);

return helper.dropSync([ Person, Pet ], done);
return helper.dropSync([Person, Pet], done);
};
};

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

afterEach(function () {
sinon.restore();
});

describe("if passing an object", function () {
before(setup());

Expand Down Expand Up @@ -108,6 +113,47 @@ describe("Model.create()", function() {
return done();
});
});

describe("with 'saveAssociationsByDefault' disabled", function () {
beforeEach(function () {
sinon.stub(db.settings, 'get').withArgs('instance.saveAssociationsByDefault').returns(false);
});

it("should not save associations", function (done) {
Person.create({
name : "John Doe",
pets : [ { name: "Deco" } ]
}, function (err, John) {
should.equal(err, null);

should.equal(Array.isArray(John.pets), true);

John.pets[0].should.have.property("name", "Deco");
John.pets[0].should.not.have.property(Pet.id);

return done();
});
});

it("should save associations if 'saveAssociations' is passed", function (done) {
Person.create({
name : "John Doe",
pets : [ { name: "Deco" } ]
}, {
saveAssociations: true
}, function (err, John) {
should.equal(err, null);

should.equal(Array.isArray(John.pets), true);

John.pets[0].should.have.property("name", "Deco");
John.pets[0].should.have.property(Pet.id);
John.pets[0].saved().should.be.true;

return done();
});
});
});
});

describe("when not passing a property", function () {
Expand Down
44 changes: 44 additions & 0 deletions test/integration/model-createAsync.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var should = require('should');
var sinon = require('sinon');
var helper = require('../support/spec_helper');

describe("Model.createAsync()", function() {
Expand Down Expand Up @@ -32,6 +33,10 @@ describe("Model.createAsync()", function() {
return db.close();
});

afterEach(function () {
sinon.restore();
});

describe("if passing an object", function () {
before(setup());

Expand Down Expand Up @@ -98,6 +103,45 @@ describe("Model.createAsync()", function() {
should.equal(John.pets[0].saved(), true);
});
});

describe("with 'saveAssociationsByDefault' disabled", function () {
beforeEach(function () {
sinon.stub(db.settings, 'get').withArgs('instance.saveAssociationsByDefault').returns(false);
});

it("should not save associations", function () {
return Person.createAsync({
name : "John Doe",
pets : [ { name: "Deco" } ]
})
.then(function (John) {
John.should.have.property("name", "John Doe");

should(Array.isArray(John.pets));

John.pets[0].should.have.property("name", "Deco");
John.pets[0].should.not.have.property(Pet.id);
});
});

it("should save associations if 'saveAssociations' is passed", function () {
return Person.createAsync({
name : "John Doe",
pets : [ { name: "Deco" } ]
}, {
saveAssociations: true
})
.then(function (John) {
John.should.have.property("name", "John Doe");

should(Array.isArray(John.pets));

John.pets[0].should.have.property("name", "Deco");
John.pets[0].should.have.property(Pet.id);
should.equal(John.pets[0].saved(), true);
});
});
});
});

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

0 comments on commit ee6e8b2

Please sign in to comment.