Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bookshelf-modelbase

Package Overview
Dependencies
Maintainers
2
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bookshelf-modelbase - npm Package Compare versions

Comparing version 2.10.4 to 2.11.0

75

lib/index.js

@@ -1,2 +0,1 @@

var extend = require('xtend')
var Joi = require('joi')

@@ -25,4 +24,4 @@ var difference = require('lodash.difference')

this.validate = this.validate.isJoi
? this.validate.keys(baseValidation)
: Joi.object(this.validate).keys(baseValidation)
? this.validate.keys(baseValidation)
: Joi.object(this.validate).keys(baseValidation)

@@ -70,3 +69,3 @@ this.on('saving', this.validateSave)

* Select a collection based on a query
* @param {Object} [query]
* @param {Object} [filter]
* @param {Object} [options] Options used of model.fetchAll

@@ -76,3 +75,3 @@ * @return {Promise(bookshelf.Collection)} Bookshelf Collection of Models

findAll: function (filter, options) {
return this.forge().where(extend({}, filter)).fetchAll(options)
return this.forge().where(filter || {}).fetchAll(options)
},

@@ -97,4 +96,4 @@

*/
findOne: function (query, options) {
options = extend({ require: true }, options)
findOne: function (query, options = {}) {
options = Object.assign({ require: true }, options)
return this.forge(query).fetch(options)

@@ -111,3 +110,3 @@ },

return this.forge(data)
.save(null, options)
.save(null, options)
},

@@ -124,8 +123,8 @@

*/
update: function (data, options) {
options = extend({ patch: true, require: true }, options)
update: function (data, options = {}) {
options = Object.assign({ patch: true, require: true }, options)
return this.forge({ [this.prototype.idAttribute]: options.id }).fetch(options)
.then(function (model) {
return model ? model.save(data, options) : undefined
})
.then(function (model) {
return model ? model.save(data, options) : undefined
})
},

@@ -137,9 +136,9 @@

* @param {String|Integer} options.id The id of the model to destroy
* @param {Boolean} [options.require=false]
* @param {Boolean} [options.require=true]
* @return {Promise(bookshelf.Model)} empty model
*/
destroy: function (options) {
options = extend({ require: true }, options)
destroy: function (options = {}) {
options = Object.assign({ require: true }, options)
return this.forge({ [this.prototype.idAttribute]: options.id })
.destroy(options)
.destroy(options)
},

@@ -154,9 +153,9 @@

*/
findOrCreate: function (data, options) {
return this.findOne(data, extend(options, { require: false }))
.bind(this)
.then(function (model) {
var defaults = options && options.defaults
return model || this.create(extend(defaults, data), options)
})
findOrCreate: function (data, options = {}) {
return this.findOne(data, Object.assign({}, options, { require: false }))
.bind(this)
.then(function (model) {
var defaults = (options && options.defaults) || {}
return model || this.create(Object.assign(defaults, data), options)
})
},

@@ -170,16 +169,16 @@

*/
upsert: function (selectData, updateData, options) {
return this.findOne(selectData, extend(options, { require: false }))
.bind(this)
.then(function (model) {
return model
? model.save(
updateData,
extend({ patch: true, method: 'update' }, options)
)
: this.create(
extend(selectData, updateData),
extend(options, { method: 'insert' })
)
})
upsert: function (selectData, updateData, options = {}) {
return this.findOne(selectData, Object.assign({}, options, { require: false }))
.bind(this)
.then(function (model) {
return model
? model.save(
updateData,
Object.assign({ patch: true, method: 'update' }, options)
)
: this.create(
Object.assign({}, selectData, updateData),
Object.assign({}, options, { method: 'insert' })
)
})
}

@@ -186,0 +185,0 @@ })

{
"name": "bookshelf-modelbase",
"version": "2.10.4",
"version": "2.11.0",
"description": "Extensible ModelBase for bookshelf-based model layers",

@@ -20,17 +20,15 @@ "main": "./lib",

"dependencies": {
"joi": "^9.0.4",
"lodash.difference": "^4.4.0",
"xtend": "^4.0.1"
"joi": "^14.3.1",
"lodash.difference": "^4.5.0"
},
"devDependencies": {
"bluebird": "^3.4.1",
"bookshelf": "^0.10.0",
"knex": "^0.11.9",
"chai": "^3.5.0",
"bookshelf": "^1.1.0",
"chai": "^4.2.0",
"istanbul": "^0.4.4",
"mocha": "^3.0.0",
"sinon": "^1.17.5",
"sqlite3": "^3.1.4",
"standard": "^7.1.2"
"knex": "^0.20.10",
"mocha": "^5.2.0",
"sinon": "^6.3.4",
"sqlite3": "^4.0.2",
"standard": "^12.0.1"
}
}

@@ -34,2 +34,3 @@ # bookshelf-modelbase

var bookshelf = require('bookshelf')(db);
var Joi = require('joi');
// Pass an initialized bookshelf instance

@@ -179,3 +180,3 @@ var ModelBase = require('bookshelf-modelbase')(bookshelf);

options = extend({ patch: true, require: true }, options);
return this.forge({ [this.prototype.idAttribute]: options.id }).fetch(options);
return this.forge({ [this.prototype.idAttribute]: options.id }).fetch(options)
.then(function (model) {

@@ -182,0 +183,0 @@ return model ? model.save(data, options) : undefined;

@@ -34,2 +34,6 @@ /* global describe, before, after, beforeEach, it */

after(function () {
return db.destroy()
})
describe('initialize', function () {

@@ -80,6 +84,6 @@ var origModelBase

return specimen.save()
.then(function (model) {
expect(model).to.not.be.an('undefined')
expect(model.get('last_name')).to.be.equal('world')
})
.then(function (model) {
expect(model).to.not.be.an('undefined')
expect(model.get('last_name')).to.be.equal('world')
})
})

@@ -108,7 +112,7 @@

return SpecimenClass
.where({ first_name: 'hello' })
.save({ last_name: 'world' }, { patch: true, method: 'update', require: false })
.then(function (model) {
return expect(model.get('last_name')).to.equal('world')
})
.where({ first_name: 'hello' })
.save({ last_name: 'world' }, { patch: true, method: 'update', require: false })
.then(function (model) {
return expect(model.get('last_name')).to.equal('world')
})
})

@@ -118,6 +122,6 @@

return SpecimenClass.forge({ id: 1 })
.save({ last_name: 'world' }, { patch: true, require: false })
.then(function (model) {
return expect(model.get('last_name')).to.equal('world')
})
.save({ last_name: 'world' }, { patch: true, require: false })
.then(function (model) {
return expect(model.get('last_name')).to.equal('world')
})
})

@@ -128,6 +132,6 @@

return Model.forge({ id: 1 })
.save('first_name', 'notYoName')
.then(function (model) {
return expect(model.get('first_name')).to.equal('notYoName')
})
.save('first_name', 'notYoName')
.then(function (model) {
return expect(model.get('first_name')).to.equal('notYoName')
})
})

@@ -146,5 +150,5 @@ })

return SpecimenClass.findAll()
.then(function (collection) {
return expect(collection).to.be.instanceof(bookshelf.Collection)
})
.then(function (collection) {
return expect(collection).to.be.instanceof(bookshelf.Collection)
})
})

@@ -157,9 +161,9 @@ })

return SpecimenClass.create({ first_name: 'yo' })
.then(function (model) {
created = model
return SpecimenClass.findById(model.id)
})
.then(function (model) {
expect(model.id).to.deep.equal(created.id)
})
.then(function (model) {
created = model
return SpecimenClass.findById(model.id)
})
.then(function (model) {
expect(model.id).to.deep.equal(created.id)
})
})

@@ -171,5 +175,5 @@ })

return SpecimenClass.findOne()
.then(function (model) {
expect(model).to.be.instanceof(SpecimenClass)
})
.then(function (model) {
expect(model).to.be.instanceof(SpecimenClass)
})
})

@@ -183,5 +187,5 @@ })

})
.then(function (model) {
expect(model.id).to.not.eql(specimen.id)
})
.then(function (model) {
expect(model.id).to.not.eql(specimen.id)
})
})

@@ -198,6 +202,6 @@ })

})
.then(function (model) {
expect(model.get('id')).to.eql(specimen.get('id'))
expect(model.get('first_name')).to.eql('goodbye')
})
.then(function (model) {
expect(model.get('id')).to.eql(specimen.get('id'))
expect(model.get('first_name')).to.eql('goodbye')
})
})

@@ -212,5 +216,5 @@

})
.then(function (model) {
expect(model).to.eql(undefined)
})
.then(function (model) {
expect(model).to.eql(undefined)
})
})

@@ -222,14 +226,14 @@ })

return SpecimenClass.forge({ first_name: 'hello' })
.save()
.bind({})
.then(function (model) {
this.modelId = model.id
return SpecimenClass.destroy({ id: this.modelId })
})
.then(function (model) {
return SpecimenClass.findOne({ id: this.modelId })
})
.catch(function (err) {
expect(err.message).to.eql('EmptyResponse')
})
.save()
.bind({})
.then(function (model) {
this.modelId = model.id
return SpecimenClass.destroy({ id: this.modelId })
})
.then(function (model) {
return SpecimenClass.findOne({ id: this.modelId })
})
.catch(function (err) {
expect(err.message).to.eql('EmptyResponse')
})
})

@@ -241,6 +245,6 @@ })

return SpecimenClass.findOrCreate({ id: specimen.id })
.then(function (model) {
expect(model.id).to.eql(specimen.id)
expect(model.get('first_name')).to.equal('hello')
})
.then(function (model) {
expect(model.id).to.eql(specimen.id)
expect(model.get('first_name')).to.equal('hello')
})
})

@@ -250,6 +254,6 @@

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)
})
.then(function (model) {
expect(model.id).to.eql(specimen.id)
expect(model.get('first_name')).to.equal(undefined)
})
})

@@ -259,7 +263,7 @@

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.a('null')
})
.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.a('null')
})
})

@@ -272,5 +276,5 @@

})
.then(function (model) {
expect(model.id).to.not.eql(specimen.id)
})
.then(function (model) {
expect(model.id).to.not.eql(specimen.id)
})
})

@@ -286,7 +290,7 @@

})
.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)
})
.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)
})
})

@@ -301,7 +305,7 @@

})
.then(function (model) {
expect(model.get('id')).to.equal(specimen.id)
expect(model.get('first_name')).to.be.an('undefined')
expect(model.get('last_name')).to.be.a('null')
})
.then(function (model) {
expect(model.get('id')).to.equal(specimen.id)
expect(model.get('first_name')).to.be.an('undefined')
expect(model.get('last_name')).to.be.a('null')
})
})

@@ -316,16 +320,16 @@ })

})
.bind({})
.then(function (model) {
this.createdModelId = model.id
return SpecimenClass.upsert({
last_name: 'upsert'
}, {
last_name: 'success'
.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)
})
.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)
})
})

@@ -340,6 +344,6 @@

})
.then(function (model) {
expect(model.get('first_name')).to.equal('goodbye')
expect(model.get('last_name')).to.equal('updated')
})
.then(function (model) {
expect(model.get('first_name')).to.equal('goodbye')
expect(model.get('last_name')).to.equal('updated')
})
})

@@ -355,9 +359,9 @@

})
.then(function (model) {
expect(model.id).to.equal(0)
expect(model.get('first_name')).to.equal('goodbye')
expect(model.get('last_name')).to.equal('updated')
})
.then(function (model) {
expect(model.id).to.equal(0)
expect(model.get('first_name')).to.equal('goodbye')
expect(model.get('last_name')).to.equal('updated')
})
})
})
})

@@ -7,5 +7,8 @@ var path = require('path')

client: 'sqlite3',
connection: {
filename: path.resolve(__dirname, 'dev.sqlite3')
connection: ':memory:',
pool: {
max: 1,
min: 1
},
useNullAsDefault: true,
migrations: {

@@ -12,0 +15,0 @@ directory: path.resolve(__dirname, 'migrations')

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc