DEV Community

Cover image for Sequelize, your favorite database library
BunnyDunker
BunnyDunker

Posted on

Sequelize, your favorite database library

Alright, welcome. I am here to give you a brief introduction to sequelize and tell you why you should be using it if you are using some of the most popular SQL databases out there.

But why?

First off, why would you want to use Sequelize? Well I'm glad you asked. Sequelize is a promise based ORM for popular databases such as Postgres, MariaDB, MySQL, SQlite, and Microsoft SQL Server. ORM stands for Object-relational-mapper and what that means is that it is a way of making those long SQL queries:

SELECT * FROM users WHERE email = 'test@test.com';

into a more manageable object oriented style. This ORM will let you skip the process of writing out these long and complicated queries out by hand and can bring down the complexity of using a database while still keeping its depth.

Ok but how?

To start the only thing you need to install is sequelize using either npm or yarn, apart from the database you want to use if you want it to run locally. Once you have it downloaded you can set up your sequelize connection to the database you are using like so:

const Sequelize = require('sequelize');

const sequelize = new Sequelize('database', 'username', 'password', {
  host: 'localhost',
  dialect: /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */
});

The passed parameters are fairly self explanatory, database corresponds to the name of the database in the server running, username and password are the credentials needed to connect to the database server. The object in the last place has two key value pairs, the host is the location of the database server (either local host of a locally run database or the ip address of the server that is hosting it for you), and the dialect corresponds to which SQL database you are using.

Model time

Now that we have the connection to our database lets setup some models. To do that we just need to take the connection we made to our database and call its define method. By assigning that to a variable we are able to keep an easy reference to each model we have for creation and searches. This can be done in the following way:

const User = sequelize.define('users', {
  id: {
    type: type.INTEGER,
    primaryKey: true,
    autoIncrement: true,
  },
  firstName: type.STRING,
  lastName: {
    type: Sequelize.STRING
  }
});

When defining a model the keys always correspond to the name of the column and the value determines the specific details that value can be. The values can not only be type descriptions but whole objects with other details for the value such as whether or not it's the primary key, or what its default value is. In this object you are also able to determine the connection between tables if you pass in this key value pair:

references: {
  model: 'users',
  key: 'id',
},

When passing using this key value pair, model corresponds to the model you want to reference and id being the column from that table this value references.

Query time

Ok, before we can make full use of these models we need to make sure they are created in our database and for that we need to synchronize it. We can call the .sync method on each of our models or on the database connection itself to apply it to all models. A little tip here, you can pass an object with the key value pair of {force: true} to make the database drop the models if they exist already and recreate them. Now to show off the beauty of Sequelize, each query is performed by calling a method on the model you want to reference. You have methods like .create, .findAll, .update, .destroy, .findOne, and many more. For the sake of time let's just go over .create and .findOne. For the most part each of these methods only takes in an object as its argument, and in the case of .create the key value pair correspond to the direct values you want to insert into the table and under what column. Now for .findOne you can pass in the key 'where' and give it an object where the key value pairs inside would correspond to the entry with that exact pair in the table you are looking for.

There are too many specific keys to pass in to each method so go check them out in their documentation and have fun writing efficient databases!

Top comments (0)