bookshelf-modelbase
Advanced tools
Comparing version 2.7.0 to 2.8.1
@@ -124,14 +124,14 @@ var extend = require('xtend') | ||
* @param {Object} data | ||
* @param {Object} [options] Options for model.fetch and modl.save | ||
* @param {Boolean} [options.require=false] | ||
* @param {Object} [options] Options for model.fetch and model.save | ||
* @param {Object} [options.defaults] Defaults to apply to a create | ||
* @return {Promise(bookshelf.Model)} single Model | ||
*/ | ||
findOrCreate: function (data, options) { | ||
options = extend({ require: false }, options) | ||
return this.findOne(data, options) | ||
return this.findOne(data, extend(options, { require: false })) | ||
.bind(this) | ||
.then(function (model) { | ||
var defaults = options && options.defaults | ||
return model ? | ||
model : | ||
this.create(data, options) | ||
this.create(extend(defaults, data), options) | ||
}) | ||
@@ -147,3 +147,3 @@ }, | ||
upsert: function (selectData, updateData, options) { | ||
return this.findOne(selectData, { require: false }) | ||
return this.findOne(selectData, extend(options, { require: false })) | ||
.bind(this) | ||
@@ -150,0 +150,0 @@ .then(function (model) { |
{ | ||
"name": "bookshelf-modelbase", | ||
"version": "2.7.0", | ||
"version": "2.8.1", | ||
"description": "Extensible ModelBase for bookshelf-based model layers", | ||
@@ -5,0 +5,0 @@ "main": "./lib", |
@@ -186,9 +186,27 @@ /* global describe, before, beforeEach, it */ | ||
.then(function (model) { | ||
return expect(model.id).to.eql(specimen.id) | ||
expect(model.id).to.eql(specimen.id) | ||
expect(model.get('first_name')).to.equal('hello') | ||
}) | ||
}) | ||
it('should find with options', function () { | ||
return SpecimenClass.findOrCreate({ id: specimen.id }, { columns: 'id' }) | ||
.then(function (model) { | ||
expect(model.id).to.eql(specimen.id) | ||
expect(model.get('first_name')).to.equal(undefined) | ||
}) | ||
}) | ||
it('should not apply defaults when model found', function () { | ||
return SpecimenClass.findOrCreate({ id: specimen.id }, { defaults: { last_name: 'world' } }) | ||
.then(function (model) { | ||
expect(model.id).to.eql(specimen.id) | ||
expect(model.get('first_name')).to.equal('hello') | ||
expect(model.get('last_name')).to.be.null() | ||
}) | ||
}) | ||
it('should create when model not found', function () { | ||
return SpecimenClass.findOrCreate({ | ||
first_name: 'yo', | ||
first_name: 'hello', | ||
last_name: '' + new Date() | ||
@@ -200,2 +218,31 @@ }) | ||
}) | ||
it('should apply defaults if creating', function () { | ||
var date = '' + new Date() | ||
return SpecimenClass.findOrCreate({ | ||
last_name: date | ||
}, { | ||
defaults: { first_name: 'hello' } | ||
}) | ||
.then(function (model) { | ||
expect(model.id).to.not.eql(specimen.id) | ||
expect(model.get('first_name')).to.equal('hello') | ||
expect(model.get('last_name')).to.equal(date) | ||
}) | ||
}) | ||
it('should work with defaults and options', function () { | ||
return SpecimenClass.findOrCreate({ | ||
id: specimen.id | ||
}, { | ||
defaults: { last_name: 'hello' }, | ||
columns: ['id', 'last_name'] | ||
}) | ||
.then(function (model) { | ||
expect(model.get('id')).to.equal(specimen.id) | ||
expect(model.get('first_name')).to.be.undefined() | ||
expect(model.get('last_name')).to.be.null() | ||
}) | ||
}) | ||
}) | ||
@@ -202,0 +249,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21595
422