- Node.js 6.x Blueprints
- Fernando Monteiro
- 237字
- 2021-07-14 10:35:06
Creating a User scheme
With the help of Sequelize-cli
we will create a simple scheme for application users:
Open terminal/shell at the root project folder and type the following command:
sequelize model:create --name User --attributes "name:string, email:string"
You will see the following output on your terminal window:
Sequelize [Node: 6.3.0, CLI: 2.3.1, ORM: 3.19.3] Loaded configuration file "config/config.json". Using environment "development". Using gulpfile /usr/local/lib/node_modules/sequelize- cli/lib/gulpfile.js Starting 'model:create'... Finished 'model:create' after 13 ms
Let's check the user model file present at: models/User.js
, here add sequelize
using the define()
function to create the User scheme:
'use strict'; module.exports = function(sequelize, DataTypes) { var User = sequelize.define('User', { name: DataTypes.STRING, email: DataTypes.STRING }, { classMethods: { associate: function(models) { // associations can be defined here } } }); return User; };
Note that this command created the User.js
file within the models
folder and also created a migration file containing a hash and the name of the operation to be performed on the database within the migrations
folder.
This file contains the boilerplate necessary for creation of the User table in the database.
'use strict'; module.exports = { up: function(queryInterface, Sequelize) { return queryInterface.createTable('Users', { id: { allowNull: false, autoIncrement: true, primaryKey: true, type: Sequelize.INTEGER }, name: { type: Sequelize.STRING }, email: { type: Sequelize.STRING }, createdAt: { allowNull: false, type: Sequelize.DATE }, updatedAt: { allowNull: false, type: Sequelize.DATE } }); }, down: function(queryInterface, Sequelize) { return queryInterface.dropTable('Users'); } };
推薦閱讀
- Getting Started with Citrix XenApp? 7.6
- Python程序設計教程(第2版)
- 精通Nginx(第2版)
- Responsive Web Design with HTML5 and CSS3
- Java編程指南:基礎知識、類庫應用及案例設計
- 可解釋機器學習:模型、方法與實踐
- Learning Vaadin 7(Second Edition)
- Flowable流程引擎實戰
- 編程可以很簡單
- Apache Camel Developer's Cookbook
- 硬件產品設計與開發:從原型到交付
- Node.js區塊鏈開發
- Mastering jQuery Mobile
- Sitecore Cookbook for Developers
- Wearable:Tech Projects with the Raspberry Pi Zero