Sequelize One-to-Many Association example with Node.js & MySQL
Sequelize One-to-Many example Overview
In systems analysis, a one-to-many relationship refers to the relationship between two entities A and B in which an element of A may be linked to many elements of B, but a member of B is linked to only one element of A.
For example, assume that you want to design a Tutorial Blog data model, you can think about one Tutorial has many Comments, but one Comment only belongs to one Tutorial.
So the relationship between Tutorial entity and Comment entity is one-to-many. Thatโs what weโre gonna make in this article, and here are the step by step:
First, we setup Node.js App
Next, configure MySQL database & Sequelize
Define the Sequelize Model and initialize Sequelize
Then we create the Controller for creating and retrieving Entities
Finally we run the app to check the result
Use Sequelize for One-to-Many Association
Weโre gonna define the models using Sequelize Model:
In models folder, create tutorial.model.js file like this:
This Sequelize Model represents tutorials table in MySQL database. These columns will be generated automatically: id, title, description, createdAt, updatedAt.
After initializing Sequelize, we donโt need to write CRUD functions, Sequelize supports all of them:
Weโre gonna use Sequelize Model functions in our Controller.
Now continue create Comment model in comment.model.js:
Initialize Sequelize
Letโs initialize Sequelize in app/models folder. Now create app/models/index.js with the following code:
We use hasMany() to help one Tutorial have many Comments, and belongsTo() to indicate that one Comment only belongs to one Tutorial.
Create the Controller
Inside app/controllers folder, letโs create tutorial.controller.js and export these functions:
createTutorial
createComment
findTutorialById
findCommentById
findAll
First we need to import database object:
Create and Save new Tutorials
Create and Save new Comments
Get the comments for a given tutorial
Get the comments for a given comment id
Get all Tutorials include comments
Check the result
Open server.js and write the following code:
โ First, we import our database object and controller above.
โ Then we call Sequelize sync() method.
In development, you may need to drop existing tables and re-sync database. Just use force: true as following code:
Now, weโre gonna test the result by putting the next lines of code inside run() function.
You can run the this Sequelize one-to-many Node.js application with command: node server.js.
$ mkdir nodejs-sequelize-one-to-many
$ cd nodejs-sequelize-one-to-many
name: (nodejs-sequelize-one-to-many)
version: (1.0.0)
description: Node.js Sequelize One to Many Relationship
entry point: (index.js) server.js
test command:
git repository:
keywords: nodejs, sequelize, one-to-many, association
author: bezkoder
license: (ISC)
Is this ok? (yes) yes
npm install sequelize mysql2 --save
{
"name": "nodejs-sequelize-one-to-many",
"version": "1.0.0",
"description": "Node.js Sequelize One to Many Relationship",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"nodejs",
"sequelize",
"one-to-many",
"association"
],
"author": "bezkoder",
"license": "ISC",
"dependencies": {
"mysql2": "^2.1.0",
"sequelize": "^5.21.6"
}
}