javascript Sequelize:模型和关联index.js文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript Sequelize:模型和关联index.js文件相关的知识,希望对你有一定的参考价值。

"use strict";

const fs = require("fs");
const path = require("path");
const Sequelize = require("sequelize");
const basename = path.basename(__filename);
const config = require(`${process.cwd()}/config`);
const postgresqlConfig = config.postgresql; // TODO: Change this to actual postgres object path.
const db = {};

// Declare a sequelize variable
const sequelize = new Sequelize(postgresqlConfig.database, postgresqlConfig.username, postgresqlConfig.password, postgresqlConfig);

// Find models and add them to our database object.
fs.readdirSync(__dirname).filter(file => {
	return file === basename ? false : file.match(/(\.js)$/);
}).forEach(file => {
	const model = sequelize.import(path.join(__dirname, file));

	db[model.name] = model;
});

// Associate keys of model
Object.keys(db).forEach(modelName => {
	if (db[modelName].associate) {
		db[modelName].associate(db);
	}
});

// Save instances
db.sequelize = sequelize;

module.exports = db;

以上是关于javascript Sequelize:模型和关联index.js文件的主要内容,如果未能解决你的问题,请参考以下文章

javascript Sequelize:模型和关联index.js文件

javascript 这是Sequelize用户模型,具有令牌授权。通用盐与密码一起存储。

Node.js学习13~基于node-sequelize的查询器,附代码举例

Sequelize 模型重复值检查

同步 sequelize 模型:sequelize.import() 不是函数(并且已弃用)

Sequelize 模型关联不会创建新列