https://github.com/yoctol/bookshelf-plugin
Yoctol specific plugins for Bookshelf
https://github.com/yoctol/bookshelf-plugin
Last synced: 7 days ago
JSON representation
Yoctol specific plugins for Bookshelf
- Host: GitHub
- URL: https://github.com/yoctol/bookshelf-plugin
- Owner: Yoctol
- License: mit
- Created: 2018-10-08T10:26:58.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-11T03:25:44.000Z (over 3 years ago)
- Last Synced: 2025-02-27T00:16:16.651Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 1010 KB
- Stars: 2
- Watchers: 10
- Forks: 1
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bookshelf-plugin
[](https://www.npmjs.com/package/@yoctol/bookshelf-plugin)
## Install
```sh
npm install @yoctol/bookshelf-plugin
```
## Usage
```js
const plugin = require('@yoctol/bookshelf-plugin');
bookshelf.plugin(plugin);
```
## Include plugins
- [case-converter](https://github.com/bookshelf/case-converter-plugin)
- [virtuals](https://github.com/bookshelf/virtuals-plugin)
- [modelbase](https://github.com/bsiddiqui/bookshelf-modelbase)
- [json-columns](https://github.com/seegno/bookshelf-json-columns)
- touch
- soft-delete
- accessible-attributes
- encrypt-columns
You can pass `caseConverter: false` option to disable `case-converter`:
```js
const plugin = require('@yoctol/bookshelf-plugin');
bookshelf.plugin(plugin, { caseConverter: false });
```
### touch
```js
const Project = bookshelf.Model.extend({
tableName: 'projects',
touches: ['user'],
user() {
return this.belongsTo(User);
},
});
```
```js
project.touch();
```
You can use `touchMethod` option to avoid naming conflict when it happened:
```js
const plugin = require('@yoctol/bookshelf-plugin');
bookshelf.plugin(plugin, { touchMethod: 'touchModel' });
```
You can use `timestamps` option to overwrite the timestamps key:
```js
const plugin = require('@yoctol/bookshelf-plugin');
bookshelf.plugin(plugin, { timestamps: ['created_at', 'updated_at'] });
```
### soft-delete
```js
const User = bookshelf.Model.extend({
tableName: 'users',
softDelete: true,
dependents: ['projects'],
projects() {
return this.hasMany(Project);
},
});
```
```js
// This will update deleted_at and cascade delete dependents
user.destroy();
```
### accessible-attributes
```js
const User = bookshelf.Model.extend({
tableName: 'users',
attrs: ['name', 'type', 'age'],
});
```
```js
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.
```js
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`](https://github.com/scoutforpets/bookshelf-encrypt-columns/)
```js
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: '',
},
});
```
This plugin will automatically encrypt when save to database and decrypt on query from database.
```js
const User = bookshelf.Model.extend({
tableName: 'users',
encryptedCoulmns: ['secret'],
});
```
## Migration guide from 0.6.x to 1.0.0
### withRefresh
#### before
```js
user.save(null, { withRefresh: true });
user.save({ key: value }, { withRefresh: true });
user.save(key, value, { withRefresh: true });
```
#### after
```js
user.save(null);
user.save({ key: value });
user.save(key, value);
```
#### before
```js
user.save(null);
user.save({ key: value });
user.save(key, value);
```
#### after
```js
user.save(null, { autoRefresh: false });
user.save({ key: value }, { autoRefresh: false });
user.save(key, value, { autoRefresh: false });
```
- `autoRefresh` is supported by bookshelf, and its default is `true`
- You should also take a look for bookshelf migration guide https://github.com/bookshelf/bookshelf/wiki/Migrating-from-0.15.1-to-1.0.0
## License
MIT © [Yoctol](https://github.com/Yoctol/bookshelf-plugin)