bookshelf-modelbase
Why
Bookshelf.js is awesome. However,
we found ourselves extending bookshelf.Model
for the same reasons over and
over - parsing and formatting (to and from DB) niceties, adding timestamps, and
validating data on save, for example. Since these are problems you'll likely
have to solve for most use cases of Bookshelf, it made sense to provide a
convenient set of core model features.
Please note
-
bookshelf-modelbase
will not force you to use it for all your models.
If you want to use it for some and not others, nothing bad will happen.
-
bookshelf-modelbase
requires you to pass in an initialized instance
of bookshelf, meaning that you can configure bookshelf however you please.
Outside of overriding bookshelf.Model
, there is nothing you can do to
your bookshelf instance that will break bookshelf-modelbase
.
Features
-
Adds timestamps (created_at
and updated_at
)
-
Validate own attributes on save using Joi.
You can pass in a validation object as a class attribute when you extend
bookshelf-modelbase
- see below for usage.
-
Naive CRUD methods - findAll
, findOne
, findOrCreate
, create
, update
, and destroy
Usage
var db = require(knex)(require('./knexfile'));
var bookshelf = require('bookshelf')(db);
var Joi = require('joi');
var ModelBase = require('bookshelf-modelbase')(bookshelf);
bookshelf.plugin(require('bookshelf-modelbase').pluggable);
var User = ModelBase.extend({
tableName: 'users',
validate: {
firstName: Joi.string()
}
});
User.create({ firstName: 'Grayson' })
.then(function () {
return User.findOne({ firstName: 'Grayson' }, { require: true });
})
.then(function (grayson) {
return User.update({ firstName: 'Basil' }, { id: grayson.id });
})
.then(function (basil) {
return User.destroy({ id: basil.id });
})
.then(function () {
return User.findAll();
})
.then(function (collection) {
console.log(collection.models.length);
})
API
model.create
create: function (data, options) {
return this.forge(data).save(null, options);
}
model.destroy
destroy: function (options) {
options = extend({ require: true }, options);
return this.forge({ [this.prototype.idAttribute]: options.id })
.destroy(options);
}
model.findAll
findAll: function (filter, options) {
return this.forge().where(extend({}, filter)).fetchAll(options);
}
model.findById
findById: function (id, options) {
return this.findOne({ [this.prototype.idAttribute]: id }, options);
}
model.findOne
findOne: function (query, options) {
options = extend({ require: true }, options);
return this.forge(query).fetch(options);
}
model.findOrCreate
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);
});
}
model.update
update: function (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
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' })
)
});
}