Bookshelf ORM如何定义模型属性和数据types

我正在使用Bookshelf.js ORM作为我的Express.js应用程序,在Bookshelf ORM中只给出了定义模型和关联的示例。

我没有find任何地方,如何定义模型属性和数据types

name: { type: string } age: { type: number } 

需要帮助..在此先感谢..

你不能。 属性及其types取自数据库模式 。 在书架上定义属性最接近的是关键定义:主键和外键。 但是,即使这样,你也只能限于他们的名字和关键的语义,这些types是根据他们的数据库types决定的。

如果你想在代码中定义它们,你可以使用Knex Schema Builder和Migrationsfunction。 喜欢:

 // 0001_people.js 'use strict'; exports.up = function(knex, Promise) { return knex.schema.createTable('people', function(table) { table.increments('id').primary(); table.string('name').unique().notNullable(); table.integer('age').notNullable(); }); }; exports.down = function(knex, Promise) { return knex.schema.dropTable('people'); }; 

但请记住,这些定义并不与相应的书架模型相关联。 你的责任是保持同步。