在单独的文件夹中打破续集模型 [英] Break sequelize models in separate folders

查看:52
本文介绍了在单独的文件夹中打破续集模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真正的问题是:是否可以使用 sequelize 将模型放在单独的文件夹中?

My real question is: Is it possible to work with sequelize having the Models in separate folders?

我问这个是因为我试图在我的应用程序中使用模块化结构,以实现我需要将模型、控制器和路由放在同一文件夹中,这就是我的意思:

I'm asking this because i'm trying to work with a modular structure in my application, to accomplish that i need to have the model, the controller and the routes in the same folder, here's what i mean:

├── 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

我怎么能这样做?我如何将模型拆分到单独的文件夹中?

How could i do that? how could i split the models into separate folders?

也许我要问的是:如何配置 models/index.js 脚本以从每个目录读取模型?

Maybe what i'm asking for is: How could i configure the models/index.js script to read the models from each directory?

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 ,其结构如下:

Something i was thinking is to look for the xxx.model.js under each folder of the modules directory having a structure like this:

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 中有何不同,因为当您定义模型时,您会告诉列的类型等,与迁移相同...

NOTE: I come from a Laravel background, i'm guessing how are migrations and models different in sequelize since when you define a model you tell the type, etc.. of the column, the same as the migration...

推荐答案

您必须读取给定目录中存在的不同文件夹中的所有模型.

You have to read all the models from different folders exist in your given directory.

让我们将 sortDir 函数添加到 models/index.js

Let's add sortDir function into models/index.js

sortDir 会将我们目录中每个文件夹中存在的所有 js 文件排序到一个名为 files 的数组中.调用该函数后,我们可以轻松地从文件数组中读取我们的模型.

sortDir will sort all the js files exist in each folder we have in our directory into an array called files. After calling the function we can easily read our models from files array.

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

现在,您可以在对数组文件中的每个模型进行排序后读取它们.

Now, you can read each model after sorting them from array files.

files
  .forEach((file) => {
    const model = require(file)(sequelize, Sequelize.DataTypes);
    db[model.name] = model;
  });

希望有帮助!

这篇关于在单独的文件夹中打破续集模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆