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.3 to 2.10.4

8

lib/index.js

@@ -44,3 +44,9 @@ var extend = require('xtend')

// only validate the keys that are being updated
validation = Joi.validate(attrs, this.validate.optionalKeys(optionalKeys))
validation = Joi.validate(
attrs,
optionalKeys.length
// optionalKeys() doesn't like empty arrays
? this.validate.optionalKeys(optionalKeys)
: this.validate
)
} else {

@@ -47,0 +53,0 @@ validation = Joi.validate(this.attributes, this.validate)

2

package.json
{
"name": "bookshelf-modelbase",
"version": "2.10.3",
"version": "2.10.4",
"description": "Extensible ModelBase for bookshelf-based model layers",

@@ -5,0 +5,0 @@ "main": "./lib",

@@ -40,3 +40,3 @@ # bookshelf-modelbase

var User = ModelBase.extend({
tableName: 'users'
tableName: 'users',

@@ -75,10 +75,9 @@ // validation is passed to Joi.object(), so use a raw object

/**
* 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) {
return this.forge(data)
.save(null, options);
return this.forge(data).save(null, options);
}

@@ -91,9 +90,12 @@ ```

/**
* 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) {
options = extend({ require: true }, options);
return this.forge({ [this.prototype.idAttribute]: options.id })
.destroy(options);
.destroy(options);
}

@@ -106,8 +108,9 @@ ```

/**
* Naive findAll - fetches all data for `this`
* @param {Object} options (optional)
* @return {Promise(bookshelf.Collection)} Bookshelf Collection of all Models
*/
findAll: function (options) {
return bookshelf.Collection.forge([], { model: this }).fetch(options);
* 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) {
return this.forge().where(extend({}, filter)).fetchAll(options);
}

@@ -126,3 +129,3 @@ ```

findById: function (id, options) {
return this.findOne({ [this.prototype.idAttribute]: id }, options)
return this.findOne({ [this.prototype.idAttribute]: id }, options);
}

@@ -135,9 +138,11 @@ ```

/**
* 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) {
return this.forge(data).fetch(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(query).fetch(options);
}

@@ -149,14 +154,15 @@ ```

/**
* 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 model.save
* @param {Object} [options.defaults] Defaults to apply to a create
* @return {Promise(bookshelf.Model)} single Model
*/
findOrCreate: function (data, options) {
var self = this;
return self.findOne(data, options)
.then(function (model) {
return model ? model : self.create(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);
});
}

@@ -169,38 +175,42 @@ ```

/**
* 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) {
_.defaults(options, {
patch: true
});
return this.forge({ [this.prototype.idAttribute]: options.id }).fetch(options)
.then(function (model) {
if (model) {
return model.save(data, options);
}
})
options = extend({ 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;
});
}
```
### model.upsert
#### model.upsert
```js
/**
* Upsert - 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
* @return {Promise(bookshelf.Model)} edited Model
*/
* 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, extend(options, { require: false }))
.bind(this)
.then(function (model) {
return model
? model.save(updateData, extend({ patch: true }, options))
: this.create(extend(selectData, updateData), options)
})
.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' })
)
});
}
```
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