node.js 自动生成续集模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13501431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:41:48  来源:igfitidea点击:

auto-generate models for sequelize

node.jssequelize.js

提问by LordZardeck

I'm wanting to start using Sequelize, a module that allows ORM for mysql in node.js. I was wondering if it's possible to auto-generate the models like CakePHP does. In CakePHP, it will read the table's info, and automatically create the associations and fields with their types in the model. i'd really hate to have to completely map out all my tables by hand, as some are relatively large. Is there something out there that will do this for me? Or am I on my own to hand-type all the models out?

我想开始使用 Sequelize,这是一个允许在 node.js 中对 mysql 进行 ORM 的模块。我想知道是否可以像 CakePHP 那样自动生成模型。在 CakePHP 中,它将读取表的信息,并在模型中自动创建关联和字段及其类型。我真的很不想手动完全绘制我的所有表格,因为有些表格相对较大。有什么东西可以为我做到这一点吗?还是我自己手动输入所有模型?

回答by puneet gupta

You can auto generate models through sequelize-auto. Just Follow the following link https://github.com/sequelize/sequelize-auto

您可以通过 sequelize-auto 自动生成模型。只需按照以下链接 https://github.com/sequelize/sequelize-auto

It will generate models of your table.

它将生成您的表的模型。

回答by nodejh

Now you can use sequelize-automateto generate models automatically. sequelize-autoseems to be unmaintained for a long time, and the package is out-of-date.

现在您可以使用sequelize-automate 自动生成模型。sequelize-auto好像很久没维护了,包已经过期了。

$ npm install sequelize-automate
$ ./node_modules/.bin/sequelize-automate --help

For example:

例如:

$ ./node_modules/.bin/sequelize-automate -t js -h localhost -d test -u root -p root -o models

回答by Junaid Atari

Sequelizer - A desktop application to export sequelize models automatically and visually.

Sequelizer - 一个桌面应用程序,用于自动和可视化导出续集模型。

Pretty impressive GUI client made with ElectronJS, grab here: Source: https://github.com/andyforever/sequelizer

使用 ElectronJS 制作的令人印象深刻的 GUI 客户端,在这里获取:来源:https: //github.com/andyforever/sequelizer

回答by Andrey Tyndyk

You can use a syncmethod for each of model

您可以为每个模型使用同步方法

example:

例子:

Object.keys(db).forEach((modelName) => {
    db[modelName].sync().then(result => {
       // some logic
    }).catch(err => {
       // some logic
    })
});

the logic will create a new table if the table not exist

如果表不存在,逻辑将创建一个新表

full script index.js

完整脚本 index.js

'use strict';
const fs        = require("fs");
const path      = require("path");
const Sequelize = require("sequelize");

const sequelize = new Sequelize(process.env.DB_DATEBASE, process.env.DB_USER, process.env.DB_PASS, {
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    dialect: 'mysql',
    operatorsAliases: false
});

const db = {};

fs
  .readdirSync(__dirname)
  .filter((file) => {
    return (file.indexOf(".") !== 0) && (file !== "index.js") && (file !== "migrations") && (file !== "redshift-migrations");
  })
  .forEach((file) => {
    const model = sequelize.import(path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach((modelName) => {
  if ("associate" in db[modelName]) {
    db[modelName].associate(db);
  }

  db[modelName].sync().then(result => {
    // some logic
  }).catch(err => {
    // some logic
  })
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

the script takes all files(models) in given directory where you will put the index.js file

该脚本获取给定目录中的所有文件(模型),您将在其中放置 index.js 文件

the structure looks like here:

结构如下所示:

structure

结构体

回答by sdepold

see https://github.com/sequelize/sequelize/issues/339

https://github.com/sequelize/sequelize/issues/339

Sequelize provides methods to read the existing table names of a database. Furthermore there is a method to read the structure of a table. Combined, it should be possible to automate the creation of models.

Sequelize 提供了读取数据库现有表名的方法。此外,还有一种方法可以读取表的结构。结合起来,应该可以自动创建模型。