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
經過測試 這兩命令不能看做互逆操作。如何處理自己看著辦吧。