sequelize 是Node.js中应用最广泛的orm开源库,github:https://github.com/sequelize/sequelize。
中文文档:https://github.com/demopark/sequelize-docs-Zh-CN
Sequelize 是一个基于 promise 的 Node.js ORM, 目前支持 Postgres, MySQL, SQLite 和 Microsoft SQL Server. 它具有强大的事务支持, 关联关系, 读取和复制等功能.
按照官方文档做,总是觉得不对劲,做的非常累,总有劳动力没有解放的感觉。后来发现这个库还有两个关键的配套工具sequelize-cli,和sequelize-auto。
sequelize-cli用来通过命令产生对应的代码模型和数据库迁移。
sequelize-auto 用来通过已有的数据库结构生成代码模型和数据库迁移。
常用命令
sequelize-auto -h localhost -d login_demo -u root -x root -p 3306 -t user
Options:
-h, --host IP/Hostname for the database. [required]
-d, --database Database name. [required]
-u, --user Username for database.
-x, --pass Password for database.
-p, --port Port number for database.
-c, --config JSON file for Sequelize's constructor "options" flag object as defined here: https://sequelize.readthedocs.org/en/latest/api/sequelize/
-o, --output What directory to place the models.
-e, --dialect The dialect/engine that you're using: postgres, mysql, sqlite
-a, --additional Path to a json file containing model definitions (for all tables) which are to be defined within a model's configuration parameter. For more info: https://sequelize.readthedocs.org/en/latest/docs/models-definition/#configuration
-t, --tables Comma-separated names of tables to import
-T, --skip-tables Comma-separated names of tables to skip
-C, --camel Use camel case to name models and fields
-n, --no-write Prevent writing the models to disk.
-s, --schema Database schema from which to retrieve tables
node_modules/.bin/sequelize model:generate --name User --attributes firstName:string,lastName:string,email:string
node_modules/.bin/sequelize db:migrate
node_modules/.bin/sequelize db:migrate:undo
经过测试 这两命令不能看做互逆操作。如何处理自己看着办吧。