Socket
Book a DemoInstallSign in
Socket

@yoctol/bookshelf-plugin

Package Overview
Dependencies
Maintainers
6
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@yoctol/bookshelf-plugin

Yoctol specific plugins for Bookshelf

latest
npmnpm
Version
1.0.0
Version published
Weekly downloads
10
Maintainers
6
Weekly downloads
 
Created
Source

bookshelf-plugin

npm

Install

npm install @yoctol/bookshelf-plugin

Usage

const plugin = require('@yoctol/bookshelf-plugin');

bookshelf.plugin(plugin);

Include plugins

You can pass caseConverter: false option to disable case-converter:

const plugin = require('@yoctol/bookshelf-plugin');

bookshelf.plugin(plugin, { caseConverter: false });

touch

const Project = bookshelf.Model.extend({
  tableName: 'projects',
  touches: ['user'],

  user() {
    return this.belongsTo(User);
  },
});
project.touch();

You can use touchMethod option to avoid naming conflict when it happened:

const plugin = require('@yoctol/bookshelf-plugin');

bookshelf.plugin(plugin, { touchMethod: 'touchModel' });

You can use timestamps option to overwrite the timestamps key:

const plugin = require('@yoctol/bookshelf-plugin');

bookshelf.plugin(plugin, { timestamps: ['created_at', 'updated_at'] });

soft-delete

const User = bookshelf.Model.extend({
  tableName: 'users',

  softDelete: true,
  dependents: ['projects'],

  projects() {
    return this.hasMany(Project);
  },
});
// This will update deleted_at and cascade delete dependents
user.destroy();

accessible-attributes

const User = bookshelf.Model.extend({
  tableName: 'users',

  attrs: ['name', 'type', 'age'],
});
user.name; // Same as user.get('name');
user.type; // Same as user.get('type');
user.age; // Same as user.get('age');

You can also overwrite attr on Model.

bookshelf.Model = bookshelf.Model.extend({
  constructor(...args) {
    proto.constructor.apply(this, args);

    Object.defineProperty(this, 'custom', {
      get: () => 'custom-value',
      set: (value, key) => this.set(key, value),
    });
  },
});

encrypt-columns

Use encryptColumns options to enable encrypt-columns plugin.
The idea of this plugin mainly comes from bookshelf-encrypt-columns

const plugin = require('@yoctol/bookshelf-plugin');

bookshelf.plugin(plugin, {
  encryptColumns: {
    algorithm: 'aes-256-cbc', // default to 'aes-256-cbc'
    ivLength: 16, // IV length for the selected algorithm
    key: '<YOUR_KEY>',
  },
});

This plugin will automatically encrypt when save to database and decrypt on query from database.

const User = bookshelf.Model.extend({
  tableName: 'users',
  encryptedCoulmns: ['secret'],
});

Migration guide from 0.6.x to 1.0.0

withRefresh

before

user.save(null, { withRefresh: true });
user.save({ key: value }, { withRefresh: true });
user.save(key, value, { withRefresh: true });

after

user.save(null);
user.save({ key: value });
user.save(key, value);

before

user.save(null);
user.save({ key: value });
user.save(key, value);

after

user.save(null, { autoRefresh: false });
user.save({ key: value }, { autoRefresh: false });
user.save(key, value, { autoRefresh: false });

License

MIT © Yoctol

Keywords

bookshelf

FAQs

Package last updated on 30 Mar 2021

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