Node.js 6.x Blueprints
上QQ阅读APP看书,第一时间看更新

Installing and configuring Sequelize-cli

Sequelize-cli is a very useful command-line interface for creating models, configurations and migration files to databases. It's integrated with Sequelize middleware and operates with many relational databases such as PostgreSQL, MySQL, MSSQL, Sqlite.

Tip

You can find more information about Sequelize middleware implementation at: http://docs.sequelizejs.com/en/latest/ and full documentation of Sequelize-Cli at: https://github.com/sequelize/cli.

  1. Open terminal/shell and type:
     npm install -g sequelize-cli
    
  2. Install sequelize with the following command:
     npm install sequelize -save
    

    Tip

    Remember we always use the -save flag to add the module to our package.json file.

  3. Create a file called .sequelizerc on the  root folder and place the following code:
          var path = require('path'); 
          module.exports = { 
            'config': path.resolve('./config', 'config.json'), 
            'migrations-path': path.resolve('./config', 'migrations'), 
            'models-path': path.resolve('./', 'models'), 
            'seeders-path': path.resolve('./config', 'seeders') 
          } 
    
  4. On terminal/shell, type the following command:
    sequelize init
    
  5. After the init command, the terminal will show the following output message:
     Sequelize [Node: 6.3.0, CLI: 2.3.1, ORM: 3.19.3] Using gulpfile /usr/local/lib/node_modules/sequelize -cli/lib/gulpfile.js Starting 'init:config'... Created "config/config.json" Finished 'init:config' after 4.05 ms Successfully created migrations folder at "/chapter-02/config /migrations". Finished 'init:migrations' after 1.42 ms Successfully created seeders folder at "/chapter-02/config /seeders". Finished 'init:seeders' after 712 μs Successfully created models folder at "/chapter-02/models". Loaded configuration file "config/config.json". Using environment "development". Finished 'init:models' after 18 msStarting 'init'...
    

This command also creates the models directory to store application schema, a configuration file, and folders to hold seeders and the  migrations script. Don't worry about this now, we will look at migrations in the next section.