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

bookshelf-modelbase-plus

Package Overview
Dependencies
Maintainers
6
Versions
69
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bookshelf-modelbase-plus

Extended functionality for REST operations with validation, filtering, ordering, importing records.

  • 2.7.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-57.14%
Maintainers
6
Weekly downloads
 
Created
Source

bookshelf-modelbase-plus

Version Build Status

Why

Bookshelf modelbase plugin offers good functionality, however when dealing with a number of similar REST microservices it is good to have shared codebase for common operations like paginated list, updating the records with validation, importing set of records and etc.

Install

npm i --save bookshelf-modelbase-plus

Setup

    var db        = require(knex)(require('./knexfile'));
    var bookshelf = require('bookshelf')(db);
    var ModelBase = require('bookshelf-modelbase')(bookshelf);

    // load plugin
    bookshelf.plugin(require('bookshelf-modelbase-plus'));
    // needs also pagination plugin
    bookshelf.plugin('pagination');

    var Budget = ModelBase.extend({
        tableName: 'budgets',
        validationOptions: {}, // Joi options (default options: { abortEarly: false })
    });

API

model.getList

    /**
     * Get sorted, ordered, filtered, paginated list of records. Good for GET params object passing
     * i.e. /?limit=5&page=1&order_by=-id&status=enabled
     * @param {Object} data
     * @param {Array} columns All table columns
     * @return {Promise(bookshelf.Collection)} Bookshelf Collection of all Models
     */
    var data = {
        status: 'enabled',   // where status = enabled
        order_by: '-id', // order by id desc
        limit: 5,
        page: 1,         // 1 based page number
    }
    Budget
        .getList(data, ['id', 'name', 'status'])
        .then(function(models) {
            //
        })
        .catch(function(err) { });
Complex Queries:
  • Supports Arrays for key operator/value, like
  Budget.getList(status: ['=', 'enabled'], ['status'])
  • Supports specifying logic, like
  Budget.getList({a: 2, b: 3, _logic: 'or'})
  • Supports Objects, like:
  Budget.getList({
    status: {
      operator: 'LIKE',
      value: '%enabled%',
    }}).then(models => {});
  • Supports array values, like:
  Budget.getList(status: ['IN', ['enabled', 'disabled']], ['status'])
  • Supports or/and nesting, like:
  Budget.getList({name: 'name0', _or: {name: 'name1', _and: {status: 'enabled'}}}, ['name', 'status'])
  Budget.getList({name: 'name0', _or: [{name: 'name1'}, {status: 'enabled'}]}, ['name', 'status'])
Supported Operators
  • !=
  • NOT_EQUAL_TO
  • <
  • LESS_THAN
  • <
  • LESS_THAN_OR_EQUAL_TO
  • >
  • GREATER_THAN
  • >
  • GREATER_THAN_OR_EQUAL_TO
  • =
  • EQUAL_TO
  • IS
  • IS_NOT
  • LIKE
  • NOT_LIKE
  • BETWEEN
  • NOT_BETWEEN
  • IN
  • NOT_IN
Special queries

A function may optionally be defined on the model to be used when specifying withQuery in the getList options, similar to withRelated. This function will then be called with the knex query builder so that the model can define custom code to refine the search (i.e. join, convert values, etc.)

Example:

    var Budget = ModelBase.extend({
        tableName: 'budgets'
        fancy: (qb, options) => {
          return qb.where({name: options.name + options.name2});
        },
    });
    Budget.getList({name: 'first', name2: 'last', withQuery: 'fancy'})

Use the optional select param in the getList options to only return specific columns. The value should be a comma-separated string or an array of strings.

Use the getListQuery method to get the underlying getList query without executing anything. This is useful in case you want to do something custom with the query, such as stream to a CSV somewhere instead of fetching a list of Bookshelf models.

model.createOne

/**
     * Create and save model and return all attributes
     * @param {Object} data
     * @param {Array} columns The set of columns will be used to fetch attribute values from the *data* object
     * @return {Promise(bookshelf.Model)} Bookshelf Collection of all Models
     */
    Budget
        .createOne(data, ['id', 'name', 'status'])
        .then((model) => {
            //
        })
        .catch((err) => {

        });

model.updateOneById

    /**
     * Update model through ID and revalidate all attributes before saving with modelbase Joi validation (if set)
     * returns model with all attributes
     * @param {Object} data
     * @param {Number} id
     * @param {Array} columns data The set of columns will be used to fetch attribute values from the *data* object
     * @return {Promise(bookshelf.Model)} Bookshelf Collection of all Models
     */
    Budget
        .updateOneById(data, id, ['id', 'name', 'status'])
        .then((model) => {
            //
        });

model.updateOneByCompositePKey

    /**
     * Update model through composite pKEY lookup and revalidate all attributes before saving with modelbase Joi validation (if set)
     * returns model with all attributes
     * @param {Object} data
     * @param {Array} columns data The set of columns will be used to fetch attribute values from the *data* object including the composite key
     * @return {Promise(bookshelf.Model)} Bookshelf Collection of all Models
     */
    Budget
        .updateOneByCompositePKey(data, ['id', 'name', 'status'])
        .then((model) => {
            //
        });

model.destroyOneByCompositePKey

    /**
     * Destroys  model through composite pKEY lookup
     * @param {Object} options should have the composite key fields
     */
    Budget
        .destroyOneByCompositePKey(options)
        .then((cnt) => {
            //
        });

model.importMany

    /**
     * Update model and revalidate all attributes before saving with modelbase Joi validation (if set)
     * returns model with all attributes
     * @param {Array} data Array of records to import
     * @param {Array} columns data The set of columns will be used to fetch attribute values from the *data* object
     * @param {Function} callback Optional callback with *bookshelf.Model* and *Object* params to restore soft-deleted records
     * @return {Promise(bookshelf.Model)} Bookshelf Collection of all Models
     */
    Budget
        .importMany([
            { id: 120, name: 'Test name 00' },
            { id: 139, name: 'Test name 01', status: 'enabled' },
            {
                // this will be definetely inserted
                name: 'Test name 02', status: 'disabled',
            },
        ],
        columns,
        (existingModel, updateData) => {
            if (existingModel.get('status') === 'deleted' && !updateData.status) {
                // in this particular example a callback has been raised cause one of the imported record with id = 120
                // is already in the table, so we check it's attribute *status* (might by any soft-deleting logic) and
                // decide to restore soft deleted record and clean *old* values before save new one
                updateData.status = 'enabled';
                // clear existing model attributes, since they were set before deleting the record,
                // now are obsolete
                existingModel.set('name', null);
                return true;
            } else {
                // wasn't restored
                return false;
            }
        })
        .then((rowCount) => {
            res.data = { rows: rowCount };
            return next();
        })
        .catch(err => next(err));
Import Events
Model.eventEmitter.on('import.created', function(createdModel) {...});
Model.eventEmitter.on('import.updated', function(updatedModel, prevModel) {...});

model.destroyMany

    /**
     * Destroys a set of model through where condition
     * @param {Object} options.where should have the where condtions for destroy
     */
    Budget
        .destroyMany(options)
        .then((cnt) => {
            //
        });

model.bulkSync

    /**
     * Synchronizes an existing collection to a desired collection
     * @param {Array<*>} existing Collection (or array)
     * @param {Array<*>} desired collection
     * @param {Array<string>} columns to update
     * @param options.noInsert - prevents inserting new records (default inserts)
     * @param options.noUpdate - prevents updating existing records (default updates)
     * @param options.noDestroy - prevents destroying existing records (default destroys)
     * @param options.isMatch - comparison function called with existing row and desired row. Default: (a,b) => a.id === b.id
     * @return Promise<*>
     *   {Array<*>} inserted
     *   {Array<*>} updated (models with getSavedAttributes accessible)
     *   {Array<*>} destroyed
     *   {Array<*>} unchanged
     */
  const isMatch = (a, b) => (a.id && a.id === b.id) || (a.type === b.type);

  Budget
    .fetchAll()
    .then(existing => Budget.bulkSync(existing, data, Budget.columns, { isMatch }))
    .then((synced) => {
      synced.inserted.forEach(i => events.emit('exchangeCreated', i, req));
      synced.updated.forEach(i => events.emit('exchangeUpdated', i, req));
      synced.destroyed.forEach(i => events.emit('exchangeDestroyed', i, req));
    });

model.bulkDestroyIn

    /**
     * Destroys all matching models by ID
     * @param {Array<*>} array of models with IDs to destroy
     * @param options.transacting
     * @return Promise<number> number of rows deleted
     */
  Budget
    .bulkDestroyIn({id: 1}, {id: 2})
    .then(rows => console.log(`deleted ${rows}`));

Keywords

FAQs

Package last updated on 05 Dec 2019

Did you know?

Socket

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.

Install

Related posts

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