How to connect Node.JS to database using ORM ?
We will use the Sequelize library of Node.JS to connect Node.JS to the database.
Sequelize is like the ORM tool to connect Node.JS to the database. ORM allows developers to interact with a database using the programming language's syntax and constructs rather than writing raw queries. It increases the readability of the code to a developer without knowing databases. It also increases the flexibility in switching the databases.
Installation of the package
Run the following command in your project directory to install the dependency.
Importing the package
Connecting Node.JS to database
Following is the code to connect to the database
Disabling the timestamps
By default, Sequelize creates two columns i.e. createdAt and updatedAt, for every model you define. It uses the following Date data type to store the date and time when something is updated or created in the model. If you don't want it, it can be disabled on the instance of the Sequelize.
How to define the model ?
Following code shows how the model is defined.
In the above code, we use the database object to define a model which corresponding to the table. Database object has the function name
"define" to define a table. It creates the table automatically if the table does not exist.
It takes two arguments i.e. name of the table and an object defining the name and properties of the columns like its type, default value, etc. You can find the column data types for the model
here.Saving the data in Model.
To create the row in the table, we use the instance of the model which has "create" method. It takes the JSON object defines the values in the row. The JSON object's keys should be the exact names of the columns, and their values should be the corresponding values.
Fetching the data from Model.
The instance of the model has two methods, "findAll" and "findOne".
The"findAll" method returns all the rows in the table or the rows that satisfy the specified query if provided.
The "findOne" method returns a single row that satisfies the specified query.
For querying, a JSON object is created just like shown in the code below and explained in detail in the following post.
Deleting the data from Model.
The instance of the model has the"destroy" method. It takes the JSON object defining the query and deletes the rows satisfying it.
Updating the data from Model.
The instance of the model has the "update" method in which you have to pass to parameters first the JSON object defining the new data and another JSON object interpreting the query.
How to create the query object ?
Following shows some sample queries with the basic operators to get to know about more operators you can follow this
link.To use the operators we need to import "Op" class from sequelize.
Comparison operators :
We use comparison operators to compare values. Following are the comparison operators
- Greater than: Op.gt
- Greater than or equal to: Op.gte
- Less than: Op.lt
- Less than or equal to: Op.lte
SQL QUERY OF ABOVE WILL LOOK LIKE BELOW.
Like operators :
Like and Not Like operator is use to check weather value matches the patter or not.
SQL QUERY OF ABOVE WILL LOOK LIKE BELOW.
Not Like operators :
SQL QUERY OF ABOVE WILL LOOK LIKE BELOW.
Between operators :
The Between and Not Between operators are used to check if a value is between two values.
Not Between operators :