Sequelize
avitesh
15 Dec 2017 05:48 AM
What is Sequelize? Sequelize is a javascript library used in NodeJS as ORM to access relational database schemas as objects. The library is written entirely in Javascript to be used in NodeJS environment. Features
- Sequelize is used in NodeJS and is available via npm.
- Sequelize is an ORM (Object Relational Mapper)
- It provides easy access to various databases like MySQL, MariaDB, SQLite, or PostgreSQL.
- It has good support for database synchronization, eager loading, associations, transactions and migrations.
- It is easy to test using frameworks like Mocha
- Its powerful migration mechanism helps transforming existing database schema into a new version and Its database synchronization mechanism helps in creating database structure by specifying the model structure.
- Node/Express already installed/configured
- To install sequelize/sequelize-cli
- To configure Sequelize-cli
- The Sequelize CLI helps to run the sequelize commands on the terminal directly.
- As with any npm package, you can use the global flag (-g) to install the CLI globally. If you have installed the CLI without the global flag, use node_modules/.bin/sequelize (command), instead of sequelize (command).
- The Sequelize CLI allows you to set up and run migrations directly from your terminal. However, it uses a default file structure which may not work for all applications. To configure, create a file called .sequelizerc in the root directory and save the following with correct information:
- To setup Sequelize Connection (with DB)
- To connect DB using tool
- To Initialize sequelize
- Feel free to name the migration whatever you like: sequelize-cli will automatically create a migration file with whatever name you choose, prepended with a timestamp. You can then open up that file and update it as you like.
- You can check out the inline docs for this command with sequelize help:model:create
- Need to use queryInterface object
- You need another migration file for adding the column. In the up function you need to add
- model: http://sequelizejs.com/docs/latest/models
- Data insertion: http://sequelizejs.com/docs/latest/instances
- Migration: http://sequelizejs.com/docs/latest/migrations
- To check connection:
- Data insertion
- Data retrieval