
Research
Security News
Lazarus Strikes npm Again with New Wave of Malicious Packages
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
bookshelf-modelbase
Advanced tools
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.
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
.
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
var db = require(knex)(require('./knexfile'));
var bookshelf = require('bookshelf')(db);
// Pass an initialized bookshelf instance
var ModelBase = require('bookshelf-modelbase')(bookshelf);
// Or initialize as a bookshelf plugin
bookshelf.plugin(require('bookshelf-modelbase').pluggable);
var User = ModelBase.extend({
tableName: 'users'
// validation is passed to Joi.object(), so use a raw object
validate: {
firstName: Joi.string()
}
});
User.create({ firstName: 'Grayson' })
.then(function () {
return User.findOne({ firstName: 'Grayson' }, { require: true });
})
.then(function (grayson) {
// passes patch: true to .save() by default
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); // => 0
})
/**
* Naive add - create and save a model based on data
* @param {Object} data
* @param {Object} options (optional)
* @return {Promise(bookshelf.Model)} single Model
*/
create: function (data, options) {
return this.forge(data)
.save(null, options);
}
/**
* Naive destroy
* @param {Object} options
* @return {Promise(bookshelf.Model)} empty Model
*/
destroy: function (options) {
return this.forge({ id: options.id })
.destroy(options);
}
/**
* 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);
}
/**
* 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);
}
/**
* Find or create - try and find the model, create one if not found
* @param {Object} data
* @param {Object} options
* @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);
})
}
/**
* Naive update - update a model based on data
* @param {Object} data
* @param {Object} options
* @return {Promise(bookshelf.Model)} edited Model
*/
update: function (data, options) {
_.defaults(options, {
patch: true
});
return this.forge({ id: options.id }).fetch(options)
.then(function (model) {
if (model) {
return model.save(data, options);
}
})
}
FAQs
Extensible ModelBase for bookshelf-based model layers
The npm package bookshelf-modelbase receives a total of 1,685 weekly downloads. As such, bookshelf-modelbase popularity was classified as popular.
We found that bookshelf-modelbase demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.
Security News
Opengrep continues building momentum with the alpha release of its Playground tool, demonstrating the project's rapid evolution just two months after its initial launch.