在单独的文件夹中打破续集模型
Posted
技术标签:
【中文标题】在单独的文件夹中打破续集模型【英文标题】:Break sequelize models in separate folders 【发布时间】:2015-12-09 08:29:23 【问题描述】:我真正的问题是:是否可以使用 sequelize 将模型放在单独的文件夹中?
我问这个是因为我正在尝试在我的应用程序中使用模块化结构,为了实现我需要在同一个文件夹中拥有模型、控制器和路由,这就是我的意思:
├── node_modules
├── src
│ ├── client
│ │ └── ... //Frontend things managed by angular (like views, etc...)
│ └── server
| ├── components
| | ├── db
| | | ├── migrations
| | | | ├── users.js
| | | | └── ...
| | | ├── seeders
| | | ├── config.json
| | | ├── options.json
| | | └── index.js
| | ├── env
| | └── ...
│ ├── modules //By module i mean an entity
| | ├── users
| | | ├── users.model.js
| | | ├── users.controller.js
| | | └── index.js //Here i define the routes
| | └── ...
| └── ...
|
├── package.json
└── server.js
我怎么能这样做?如何将模型拆分到单独的文件夹中?
也许我要问的是:如何配置 models/index.js 脚本以从每个目录中读取模型?
models/index.js
'use strict';
var fs = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var basename = path.basename(module.filename);
var env = process.env.NODE_ENV || 'development';
var config = require(__dirname + '/../config.json')[env];
var db = ;
if (config.use_env_variable)
var sequelize = new Sequelize(process.env[config.use_env_variable]);
else
var sequelize = new Sequelize(config.database, config.username, config.password, config);
fs
.readdirSync(__dirname)
.filter(function(file)
return (file.indexOf('.') !== 0) && (file !== basename);
)
.forEach(function(file)
if (file.slice(-3) !== '.js') return;
var model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
);
Object.keys(db).forEach(function(modelName)
if (db[modelName].associate)
db[modelName].associate(db);
);
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
我的想法是在 modules 目录的每个文件夹下查找 xxx.model.js,其结构如下:
modules
├── index.js //The above script configured to read the x.model.js in each folder
├── users
| ├── users.model.js
| ├── users.controller.js
| └── ...
├── questions
| ├── questions.model.js
| ├── questions.controller.js
| └── ...
└── ...
注意:我来自 Laravel 背景,我猜迁移和模型在 sequelize 中有何不同,因为当您定义模型时,您会告诉列的类型等,与迁移相同...
【问题讨论】:
我的大部分问题都通过the docs 解决了,但模型文件夹的破坏没有解决 您能告诉我您是如何解决您的问题的吗?我没有从文档中得到太多。 @JonathanSolorzano,您找到解决问题的方法了吗? @VijayDev 不,在一个单独的文件夹中工作 【参考方案1】:您必须从给定目录中的不同文件夹中读取所有模型。
让我们将 sortDir 函数添加到 models/index.js
sortDir 会将我们目录中每个文件夹中存在的所有 js 文件排序到一个名为 files 的数组中。 调用该函数后,我们可以轻松地从文件数组中读取我们的模型。
const files = [];
const sortDir = (maniDir) =>
const folders = [];
const CheckFile = filePath => (fs.statSync(filePath).isFile());
const sortPath = (dir) =>
fs
.readdirSync(dir)
.filter(file => (file.indexOf(".") !== 0) && (file !== "index.js"))
.forEach((res) =>
const filePath = path.join(dir, res);
if (CheckFile(filePath))
files.push(filePath);
else
folders.push(filePath);
);
;
folders.push(maniDir);
let i = 0;
do
sortPath(folders[i]);
i += 1;
while (i < folders.length);
;
sortDir(__dirname);
现在,您可以在对数组文件进行排序后读取每个模型。
files
.forEach((file) =>
const model = require(file)(sequelize, Sequelize.DataTypes);
db[model.name] = model;
);
希望对你有帮助!
【讨论】:
【参考方案2】: 'use strict';
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/database.js')[env];
const db = ;
let sequelize = null;
const Op = Sequelize;
const operatorsAliases =
$gt: Op.gt,
$gte: Op.gte,
$ne: Op.ne,
$in: Op.in,
$or: Op.or,
$and: Op.and,
$like: Op.like,
;
if (config.use_env_variable)
sequelize = new Sequelize(process.env[config.use_env_variable], config);
else
sequelize = new Sequelize(config.database, config.username, config.password, config);
const files = [];
const sortDir = maniDir =>
const folders = [];
const CheckFile = filePath => fs.statSync(filePath).isFile();
const sortPath = dir =>
fs
.readdirSync(dir)
.filter(file => file.indexOf(".") !== 0 && file !== "index.js")
.forEach(res =>
const filePath = path.join(dir, res);
if (filePath.indexOf("Schema") === -1)
if (CheckFile(filePath))
files.push(filePath);
else
folders.push(filePath);
);
;
folders.push(maniDir);
let i = 0;
do
sortPath(folders[i]);
i += 1;
while (i < folders.length);
;
sortDir(__dirname);
files.forEach(file =>
console.log(file)
const model = require(path.join( file))(sequelize, Sequelize.DataTypes);
db[model.name] = model;
console.log(model)
);
Object.keys(db).forEach(modelName =>
if (db[modelName].associate)
db[modelName].associate(db);
);
db.sequelize = sequelize;
db.Sequelize = Sequelize;
db.sequelize.sync(
logging: false,
);
module.exports = db;
【讨论】:
【参考方案3】:我希望,这会节省您的时间。
import fs from "fs";
import path from "path";
import Sequelize from "sequelize";
import getConfig from "../../lib/config";
const db = ;
// Load config file based on the enviroment
const config = getConfig();
let sequelize = null;
const Op = Sequelize;
const operatorsAliases =
$gt: Op.gt,
$gte: Op.gte,
$ne: Op.ne,
$in: Op.in,
$or: Op.or,
$and: Op.and,
$like: Op.like,
;
if (config.useEnvVariable)
sequelize = new Sequelize(process.env[config.useEnvVariable], config);
else
const databaseConf = config[process.env.NODE_ENV];
sequelize = new Sequelize(
databaseConf.database,
databaseConf.username,
databaseConf.password,
host: databaseConf.host,
dialect: databaseConf.dialect,
operatorsAliases,
,
);
const files = [];
const sortDir = maniDir =>
const folders = [];
const CheckFile = filePath => fs.statSync(filePath).isFile();
const sortPath = dir =>
fs
.readdirSync(dir)
.filter(file => file.indexOf(".") !== 0 && file !== "index.js")
.forEach(res =>
const filePath = path.join(dir, res);
if (filePath.indexOf("Schema") === -1)
if (CheckFile(filePath))
files.push(filePath);
else
folders.push(filePath);
);
;
folders.push(maniDir);
let i = 0;
do
sortPath(folders[i]);
i += 1;
while (i < folders.length);
;
sortDir(__dirname);
files.forEach(file =>
const model = sequelize.import(file);
db[model.name] = model;
);
Object.keys(db).forEach(modelName =>
console.log(modelName);
if (db[modelName].associate)
db[modelName].associate(db);
);
db.sequelize = sequelize;
db.Sequelize = Sequelize;
db.sequelize.sync(
logging: false,
);
export default db;
【讨论】:
【参考方案4】:你可以随意使用节点模块glob
自定义文件夹
【讨论】:
该模块帮助您分析文件夹并通过前缀或后缀获取文件,您无法轻松获取模型文件 哦,我明白了,你能举例说明我如何使用这个结构来处理它吗? npmjs.com/package/jerryjs 我的迷你项目使用 glob 来构建文件夹 我想使用我发布的结构,我明白你使用glob
的意思,但是你能举一个使用glob
的index.js
脚本的例子吗?并回答帖子中的其他问题?以上是关于在单独的文件夹中打破续集模型的主要内容,如果未能解决你的问题,请参考以下文章