官术网_书友最值得收藏!

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'); 
        } 
      }; 
主站蜘蛛池模板: 庆城县| 象州县| 灵山县| 威海市| 大理市| 华亭县| 平定县| 奉化市| 淮安市| 深水埗区| 新营市| 云南省| 玉林市| 盖州市| 香港 | 康平县| 浙江省| 鄯善县| 霍邱县| 张掖市| 迁西县| 岳西县| 大邑县| 东城区| 宣城市| 文化| 富锦市| 达州市| 忻州市| 河池市| 土默特左旗| 泰和县| 会泽县| 霍城县| 马边| 丽水市| 新乡县| 海晏县| 固原市| 石棉县| 宁乡县|