node.js Sequelize:如何从现有数据库导入定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10060265/
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 15:28:23  来源:igfitidea点击:

Sequelize: how to import definitions from an existing database

node.jssequelize.js

提问by dvc

Am I required to handwrite the model definitions for Sequelize even if I'm working off of an existing database.

即使我正在使用现有数据库,我是否需要为 Sequelize 手写模型定义。

If it's not required, then how does one go about using Sequelize with an existing database?

如果不需要,那么如何将 Sequelize 与现有数据库一起使用?

I've already defined the database's schema in Doctrine, so I'd rather not have to write another set of model definitions again.

我已经在 Doctrine 中定义了数据库的模式,所以我不想再次编写另一组模型定义。

采纳答案by sdepold

with Sequelize you have to define the structure of the model inside your code. Doing so, Sequelize assumes a specific database schema unless something is overwritten. So in short: No, sequelize cannot mirror the database.

使用 Sequelize,您必须在代码中定义模型的结构。这样做,除非某些内容被覆盖,否则 Sequelize 会假定特定的数据库模式。简而言之:不,sequelize 不能镜像数据库。

Hope that helps :)

希望有帮助:)

回答by Hyman Miner Ewes

This project aims to create Sequelize models from existing schema https://github.com/sequelize/sequelize-auto

该项目旨在从现有模式https://github.com/sequelize/sequelize-auto创建 Sequelize 模型

Sequelize-Auto

续集-自动

A tool to automatically generate models for SequelizeJSvia the command line.

SequelizeJS通过命令行自动生成模型的工具。

Install:

安装:

npm install -g sequelize-auto

Usage:

用法:

sequelize-auto -h <host> -d <database> -u <user> -x [password] -p [port]  --dialect [dialect] -c [/path/to/config] -o [/path/to/models]

Options:

选项:

  -h, --host      IP/Hostname for the database.                                      [required]
  -d, --database  Database name.                                                     [required]
  -u, --user      Username for database.                                             [required]
  -x, --pass      Password for database.
  -p, --port      Port number for database.
  -c, --config    JSON file for sending additional options to the Sequelize object.
  -o, --output    What directory to place the models.
  -e, --dialect   The dialect/engine that you're using: postgres, mysql, sqlite

Example

例子

sequelize-auto -o "./models" -d sequelize_auto_test -h localhost -u daniel -p 5432 -x my_password -e postgres

Produces a file/files such as ./models/Users.jswhich looks like this:

生成一个/多个文件./models/Users.js,如下所示:

/* jshint indent: 2 */

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('Users', {
    username: {
      type: DataTypes.STRING,
      allowNull: true,
      defaultValue: null
    },
    touchedAt: {
      type: DataTypes.DATE,
      allowNull: true,
      defaultValue: null
    },
    aNumber: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: null
    },
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: null
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: null
    }
  });
};

Which makes it easy for you to simply Sequelize.importit.

这使您可以轻松地简化Sequelize.import它。