bookshelf-update

1.2.1 • Public • Published

bookshelf-update

Simple Bookshelf.js plugin that allows simple patching of models and skips updating if no values have changed.

See also: bookshelf-spotparse, a plugin that makes formatting, parsing and finding models easier.

Installation

  • Install with npm using npm i bookshelf-update
  • After creating a bookshelf instance, call the plugin method:
const bookshelf = require('bookshelf')(knex);
bookshelf.plugin('bookshelf-update');
  • The update method can now be used on all your models.

Example

/**
 * Save new user 'ernie' to database.
 * Since the user isNew(), the update function
 * will perform a full insert operation.
 */
new User({
    username: 'ernie',
    password: 'meepmoop',
}).update()
.then(usr => console.dir(usr))
.catch(err => console.log(err));

/**
 * Get user 'ernie', and update its password to 'meepmeep'.
 * Only the password field is updated and on second attempt no
 * update query is run.
 */
User.where({username: 'ernie'}).fetch()
.then(usr => usr.set('password', 'meepmeep'))
.then(usr => usr.update())
.then(usr => console.dir(usr))
.catch(err => console.log(err));
});

/**
 * When creating a user with the ID already specified
 * you might need to force an insert like so:
 */
new User({
    id: '11',
    username: 'ernie',
    password: 'meepmoop',
}).update({forceInsert: true})
.then(usr => console.dir(usr))
.catch(err => console.log(err));

Working

This is a simple wrapper around model.save(model.changed, {patch: true});.

Package Sidebar

Install

npm i bookshelf-update

Weekly Downloads

6

Version

1.2.1

License

MIT

Unpacked Size

3.96 kB

Total Files

4

Last publish

Collaborators

  • 7kasper