bookshelf-modelbase
Advanced tools
Comparing version 2.6.9 to 2.7.0
@@ -35,4 +35,2 @@ var extend = require('xtend') | ||
validateSave: function (model, attrs, options) { | ||
if (!this.validate) return | ||
var validation | ||
@@ -61,32 +59,30 @@ // model is not new or update method explicitly set | ||
/* Model CRUD */ | ||
/** | ||
* Naive findAll - fetches all data for `this` | ||
* @param {Object} filter (optional) | ||
* @param {Object} options (optional) | ||
* @return {Promise(bookshelf.Collection)} Bookshelf Collection of Models | ||
*/ | ||
* Select a collection based on a query | ||
* @param {Object} [query] | ||
* @param {Object} [options] Options used of model.fetchAll | ||
* @return {Promise(bookshelf.Collection)} Bookshelf Collection of Models | ||
*/ | ||
findAll: function (filter, options) { | ||
filter = extend({}, filter) | ||
return this.forge().query({ where: filter }).fetchAll(options) | ||
return this.forge().where(extend({}, filter)).fetchAll(options) | ||
}, | ||
/** | ||
* Naive findOne - fetch data for `this` matching data | ||
* @param {Object} data | ||
* @param {Object} options (optional) | ||
* @return {Promise(bookshelf.Model)} single Model | ||
*/ | ||
findOne: function (data, options) { | ||
* Select a model based on a query | ||
* @param {Object} [query] | ||
* @param {Object} [options] Options for model.fetch | ||
* @param {Boolean} [options.require=false] | ||
* @return {Promise(bookshelf.Model)} | ||
*/ | ||
findOne: function (query, options) { | ||
options = extend({ require: true }, options) | ||
return this.forge(data).fetch(options) | ||
return this.forge(query).fetch(options) | ||
}, | ||
/** | ||
* Naive add - create and save a model based on data | ||
* @param {Object} data | ||
* @param {Object} options (optional) | ||
* @return {Promise(bookshelf.Model)} single Model | ||
*/ | ||
* Insert a model based on data | ||
* @param {Object} data | ||
* @param {Object} [options] Options for model.save | ||
* @return {Promise(bookshelf.Model)} | ||
*/ | ||
create: function (data, options) { | ||
@@ -98,7 +94,10 @@ return this.forge(data) | ||
/** | ||
* Naive update - update a model based on data | ||
* @param {Object} data | ||
* @param {Object} options | ||
* @return {Promise(bookshelf.Model)} edited Model | ||
*/ | ||
* Update a model based on data | ||
* @param {Object} data | ||
* @param {Object} options Options for model.fetch and model.save | ||
* @param {String|Integer} options.id The id of the model to update | ||
* @param {Boolean} [options.patch=true] | ||
* @param {Boolean} [options.require=true] | ||
* @return {Promise(bookshelf.Model)} | ||
*/ | ||
update: function (data, options) { | ||
@@ -113,6 +112,8 @@ options = extend({ patch: true, require: true }, options) | ||
/** | ||
* Naive destroy | ||
* @param {Object} options | ||
* @return {Promise(bookshelf.Model)} empty Model | ||
*/ | ||
* Destroy a model by id | ||
* @param {Object} options | ||
* @param {String|Integer} options.id The id of the model to destroy | ||
* @param {Boolean} [options.require=false] | ||
* @return {Promise(bookshelf.Model)} empty model | ||
*/ | ||
destroy: function (options) { | ||
@@ -125,5 +126,6 @@ options = extend({ require: true }, options) | ||
/** | ||
* Find or create - try and find the model, create one if not found | ||
* Select a model based on data and insert if not found | ||
* @param {Object} data | ||
* @param {Object} options | ||
* @param {Object} [options] Options for model.fetch and modl.save | ||
* @param {Boolean} [options.require=false] | ||
* @return {Promise(bookshelf.Model)} single Model | ||
@@ -136,6 +138,23 @@ */ | ||
.then(function (model) { | ||
return model ? model : this.create(data, options) | ||
return model ? | ||
model : | ||
this.create(data, options) | ||
}) | ||
}, | ||
/** | ||
* Select a model based on data and update if found, insert if not found | ||
* @param {Object} selectData Data for select | ||
* @param {Object} updateData Data for update | ||
* @param {Object} [options] Options for model.save | ||
*/ | ||
upsert: function (selectData, updateData, options) { | ||
return this.findOne(selectData, { require: false }) | ||
.bind(this) | ||
.then(function (model) { | ||
return model ? | ||
model.save(updateData, extend({ patch: true }, options)) : | ||
this.create(extend(selectData, updateData), options) | ||
}) | ||
} | ||
}) | ||
@@ -142,0 +161,0 @@ |
{ | ||
"name": "bookshelf-modelbase", | ||
"version": "2.6.9", | ||
"version": "2.7.0", | ||
"description": "Extensible ModelBase for bookshelf-based model layers", | ||
@@ -5,0 +5,0 @@ "main": "./lib", |
@@ -192,9 +192,46 @@ /* global describe, before, beforeEach, it */ | ||
return SpecimenClass.findOrCreate({ | ||
first_name: 'yo' | ||
first_name: 'yo', | ||
last_name: '' + new Date() | ||
}) | ||
.then(function (model) { | ||
return expect(model.id).to.not.eql(specimen.id) | ||
expect(model.id).to.not.eql(specimen.id) | ||
}) | ||
}) | ||
}) | ||
describe('upsert', function () { | ||
it('should update if existing model found', function () { | ||
return SpecimenClass.create({ | ||
first_name: 'hello', | ||
last_name: 'upsert' | ||
}) | ||
.bind({}) | ||
.then(function (model) { | ||
this.createdModelId = model.id | ||
return SpecimenClass.upsert({ | ||
last_name: 'upsert' | ||
}, { | ||
last_name: 'success' | ||
}) | ||
}) | ||
.then(function (model) { | ||
expect(model.get('first_name')).to.equal('hello') | ||
expect(model.get('last_name')).to.equal('success') | ||
expect(model.id).to.equal(this.createdModelId) | ||
}) | ||
}) | ||
it('should create if existing model not found', function () { | ||
return SpecimenClass.upsert({ | ||
first_name: 'goodbye', | ||
last_name: 'update' | ||
}, { | ||
last_name: 'updated' | ||
}) | ||
.then(function (model) { | ||
expect(model.get('first_name')).to.equal('goodbye') | ||
expect(model.get('last_name')).to.equal('updated') | ||
}) | ||
}) | ||
}) | ||
}) |
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
19935
380