Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
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

  • 0.4.0
  • npm
  • Socket score

Version published
Weekly downloads
11
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),
    });
  },
});

save-refresh

This feature may be supported after bookshelf v0.14.x: https://github.com/bookshelf/bookshelf/issues/1665

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

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'],
});

License

MIT © Yoctol

Keywords

FAQs

Package last updated on 10 Apr 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