Skip to content

Yoctol/bookshelf-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

78 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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