在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/數(shù)據(jù)庫  HTML/ sequelize的文檔為什么建議用sequelize.import()的方式將

sequelize的文檔為什么建議用sequelize.import()的方式將model來分成模塊

koaexpress中用sequelize一般都要把一個(gè)個(gè)model放到單文件,官方文檔給了一個(gè)奇怪的做法,它自己提供了個(gè)import的方法,然后讓model單文件導(dǎo)出一個(gè)函數(shù)

// in your server file - e.g. app.js
const Project = sequelize.import(__dirname + "/path/to/models/project")

// The model definition is done in /path/to/models/project.js
// As you might notice, the DataTypes are the very same as explained above
module.exports = (sequelize, DataTypes) => {
  return sequelize.define("project", {
    name: DataTypes.STRING,
    description: DataTypes.TEXT
  })
}

https://github.com/sequelize/...
這個(gè)官方樣例更詳細(xì)

但是我覺得我這樣寫,模塊化會(huì)更舒服一點(diǎn)

// sequelize.js
import Sequelize from 'sequelize'
const sequelize = new Sequelize({
    ...
})
export default sequelize

// model/modelA.js
import Sequelize from 'sequelize'
import sequelize from './sequelize'
export default sequelize.define("project", {
    name: Sequelize.STRING,
    description: Sequelize.TEXT
})


// in route
import modelA from 'path/to/model/modelA'

// or by using model/index.js
import { modelA } from 'path/to/model'

不知道我的想法會(huì)有什么問題

回答
編輯回答
念舊

解決nodejs require module時(shí)循環(huán)引用會(huì)導(dǎo)致undefined的問題
這個(gè)一般在定義關(guān)聯(lián)的時(shí)候會(huì)用。
目前我的做法是把所有model的關(guān)聯(lián)放到一個(gè)js去做

import { Authorize, AuthorizeAttributes, AuthorizeInstance } from './authorize';
import { Comment, CommentAttributes, CommentInstance } from './comment';
import { Hit, HitAttributes, HitInstance } from './hit';
import { Moneylog, MoneylogAction, MoneylogAttributes, MoneylogInstance } from './moneylog';
import { Order, OrderAttributes, OrderInstance } from './order';
import { Post, PostAttributes, PostContentType, PostInstance } from './post';
import { Poundage, PoundageAttributes, PoundageInstance } from './poundage';
import { User, UserAttributes, UserInstance } from './user';
import { Withdrawal, WithdrawalAttributes, WithdrawalInstance } from './withdrawal';

Comment.belongsTo(Post, { foreignKey: 'post_id', as: 'post' });
Comment.belongsTo(User, { foreignKey: 'user_id', as: 'user' });

Order.belongsTo(User, { foreignKey: 'user_id', as: 'user' });
Order.belongsTo(Post, { foreignKey: 'post_id', as: 'post' });

Post.belongsTo(User, { foreignKey: 'user_id', as: 'user' });
Post.hasMany(Comment, { foreignKey: 'post_id', as: 'commentList' });

User.hasMany(Post, { foreignKey: 'user_id', as: 'posts' });
User.hasMany(Order, { foreignKey: 'user_id', as: 'orders' });
User.hasMany(Comment, { foreignKey: 'user_id', as: 'comment' });
2017年9月27日 19:32