Writing bookshelf models in es6

Is there a way to write a bookshelf model using es6 classes? I see that the source of the bookshelf itself was written in es6. But all the examples and sources that I came across are written in es5. I saw a complicated github issue about this, which states that this is possible, but basically some errors are discussed regarding writing models in classes. How to write and use a bookshelf model with es6 classes?

+5
source share
1 answer

Yes, you can!

// database.js import config from '../../knexfile'; import knex from 'knex'; import bookshelf from 'bookshelf'; const Bookshelf = bookshelf(knex(config[process.env.NODE_ENV || 'development'])); Bookshelf.plugin('registry'); // Resolve circular dependencies with relations Bookshelf.plugin('visibility'); export default Bookshelf; // Administers.js import Bookshelf from '../database' import { createValidatorPromise as createValidator, required, email as isEmail } from '../../utils/validation'; import { User, Organization } from '../'; import { BasicAdministersView, DetailedAdministersView } from '../../views/index'; class Administers extends Bookshelf.Model { get tableName() { return 'administers'; } get hasTimestamps() { return true; } view(name){ return new ({ basic: BasicAdministersView, detailed: DetailedAdministersView }[name])(this); } user() { console.log(User); return this.belongsTo('User', 'user_id'); } organization() { return this.belongsTo('Organization', 'organization_id'); } } export default Bookshelf.model('Administers', Administers); 
+14
source

Source: https://habr.com/ru/post/1239397/


All Articles