How To Create MySQL Table with SequelizeJS

Hello, in this post I will be showing how you can create an SQL table using JavaScript without the knowledge of MySQL.

Sequelize is a promise-based Node.js ORM tool for PostgresMySQLMariaDBSQLiteMicrosoft SQL ServerAmazon Redshift and Snowflake’s Data Cloud. It features solid transaction support, relations, eager and lazy loading, read replication and more.


Before you try this, make sure you have prior knowledge of JavaScript, NodeJS and NPM.

Docs you'll need to refer throughout this post:

In this post, we will create a simple Student table with columns as ID, RollNo, Name and Marks.

Steps:

=> In the terminal, make sure you are typing following code in the directory where your project is:
Command: npm i --save sequelize

Here, we are downloading one module: sequelize from the Node Package Manager (npm).

As of today, version of Sequelize is 6.17.0

=> Let's now create a JS file: database.js
This file will import the sequelize module and also make an instance of sequelize with details of your database name, database username and password (passing parameters separately) in order to connect to the database. Here, we are going to include the type of dialect (which database you have) and the address to the host.

Refer this page if you are not sure about which dialect to use: Dialects | Sequelize

Sequelize is independent from specific dialects. This means that you'll have to install the respective connector library to your project yourself.

At the end, we'll be adding module.exports so that this file can be imported into other JS files as well.
The module is a variable that represents the current module, and exports is an object that will be exposed as a module. So, whatever we give to module.exports, it will be exposed as a module.

The syntax for MySQL databases is: 
const sequelize = new Sequelize('database-name', 'username', 'password', {
  dialect: 'mysql'
})

For database.js:

Here,
* Class - name of the database
* root - username of the database
* root - password of the database
* localhost - represents the system we are working on

=> Let's now create a model which will help us to create the table: Student.js


Refer this document to know which datatype suits best for your columns: Datatypes | Sequelize

Here, database.define is used to model our table. Internally, database.define calls Model.init.

Inside database.define method, 'student' is the name of the table while id, rollno, name and marks are the names of the columns.

=> Next, let's create another .js file in our project folder as: app.js

If you want to test the connection to the database first, you can refer these lines of code: Testing the connection



If you see the above code carefully, you will see that we have used .sync() method. It is used because if we want Sequelize to automatically create the table or modify the table according to our model definition then we can use the sync method. But wait! we have actually used sequelize.sync(), that is because instead of calling sync() for every other model, we just call this which will automatically sync all models.

Run your project  using - node app.js
You will then see in the console that a table is being created with columns that you have mentioned.

Tip: Go to https://npmjs.com to explore other node packages.

If you have any query, feel free to comment it below.

Comments

Popular

How To Read CSV Files in Java as Maven Project

How To Create A Hibernate Project in Maven