【问题标题】:Writing bookshelf models in es6在 es6 中编写书架模型
【发布时间】:2015-12-29 05:44:15
【问题描述】:

有没有办法使用 es6 类编写书架模型?可以看到书架源码本身是用es6写的。但是我遇到的所有例子和源码都是用 es5 写的。我看到一个详细的github issue 说明这是可能的,但它主要讨论了关于在类中编写模型的一些错误。如何编写和使用带有 es6 类的书架模型?

【问题讨论】:

  • 虽然这个问题还有待回答,但我发现并一直专门使用objection.js,它也是基于knex

标签: javascript orm bookshelf.js


【解决方案1】:

是的,你可以!

// 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);

【讨论】:

    猜你喜欢
    • 2016-04-06
    • 2012-06-06
    • 2021-10-15
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多