From 4d08e89214a9cbc8e39d41bb788db064e6ebcd4a Mon Sep 17 00:00:00 2001 From: Arek W Date: Mon, 24 Jan 2022 17:31:20 +0100 Subject: [PATCH] Accept options in 'model.create' call --- Changelog.md | 3 ++ Readme.md | 2 +- lib/Model.js | 2 +- package-lock.json | 2 +- package.json | 2 +- test/integration/model-create.js | 48 ++++++++++++++++++++++++++- test/integration/model-createAsync.js | 44 ++++++++++++++++++++++++ 7 files changed, 98 insertions(+), 5 deletions(-) diff --git a/Changelog.md b/Changelog.md index 57df3cc3..feeb3e1e 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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. diff --git a/Readme.md b/Readme.md index 6ea3d768..fbf65827 100755 --- a/Readme.md +++ b/Readme.md @@ -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 diff --git a/lib/Model.js b/lib/Model.js index 6cafd884..d5df52fe 100644 --- a/lib/Model.js +++ b/lib/Model.js @@ -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; diff --git a/package-lock.json b/package-lock.json index 8f372ea1..0833ef49 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "orm", - "version": "6.0.0", + "version": "6.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index c0c6b6a1..68c18e20 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/integration/model-create.js b/test/integration/model-create.js index 9217c957..c9e7b6f5 100644 --- a/test/integration/model-create.js +++ b/test/integration/model-create.js @@ -1,4 +1,5 @@ var should = require('should'); +var sinon = require('sinon'); var helper = require('../support/spec_helper'); describe("Model.create()", function() { @@ -16,7 +17,7 @@ describe("Model.create()", function() { }); Person.hasMany("pets", Pet); - return helper.dropSync([ Person, Pet ], done); + return helper.dropSync([Person, Pet], done); }; }; @@ -32,6 +33,10 @@ describe("Model.create()", function() { return db.close(); }); + afterEach(function () { + sinon.restore(); + }); + describe("if passing an object", function () { before(setup()); @@ -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 () { diff --git a/test/integration/model-createAsync.js b/test/integration/model-createAsync.js index cb95d53c..8c60768c 100644 --- a/test/integration/model-createAsync.js +++ b/test/integration/model-createAsync.js @@ -1,4 +1,5 @@ var should = require('should'); +var sinon = require('sinon'); var helper = require('../support/spec_helper'); describe("Model.createAsync()", function() { @@ -32,6 +33,10 @@ describe("Model.createAsync()", function() { return db.close(); }); + afterEach(function () { + sinon.restore(); + }); + describe("if passing an object", function () { before(setup()); @@ -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 () {