Sequelize One-To-One association – NodeJS/Express, MySQL

In the post, we got started with Sequelize ORM. Today we’re gonna create Sequelize One-To-One association models with NodeJS/Express, MySQL.

Related posts:
Sequelize ORM – Build CRUD RestAPIs with NodeJs/Express, Sequelize, MySQL
Sequelize One-To-Many association – NodeJS/Express, MySQL
Sequelize Many-to-Many association – NodeJS/Express, MySQL

Sequelize One-to-One association

Note: When calling a method such as Customer.hasOne(Address), we say that the Customer model is the source and the Address model is the target.

We define 2 models:

How to create One-to-One association between them?
-> Sequelize provides 2 ways:

belongsTo

hasOne

What is difference between belongsTo and hasOne?
-> hasOne inserts the association key in target model whereas BelongsTo inserts the association key in the source model.

We can create a solution by combine foreignKey and targetKey with belongsTo and hasOne relation as below code:

Foreign Key

– In belongsTo relation, foreign key will be generated from the target model name and the target primary key name. Sequelize provides a foreignKey option to override defaultValue.

Target Key

– The target key is the column on the target model that the foreign key column on the source model points to. In belongsTo relation, by default the target key will be the target model’s primary key. Sequelize provides a targetKey option to define a custom column.

How to save it?

How to fetch entities?

-> Way to get all Customers that includes Addresses:

With attributes option, we can select only some attributes:

And attributes can be renamed using a nested array:

Practice

We create a NodeJs/Express project as below structure:

Setting up NodeJs/Express project

Following the guide to create a NodeJS/Express project.

Install Express, Sequelize, MySQL:

-> package.json file:

Setting up Sequelize MySQL connection

– Create ‘./app/config/env.js’ file:

– Setup Sequelize-MySQL connection in ‘./app/config/db.config.js’ file:

Define Sequelize models

– Address model:

– Customer model:

Express RestAPIs
Route

Define Customer’s routes in ‘./app/controller/customer.route.js’ file:

Controller

Implement Customer’s controller in ‘./app/controller/customer.controller.js’ file:

Server.js

Run & Check results

Start NodeJs server:

Use Postman to check result ->

– POST customers:

Sequelize One-to-One association - CRUD RestAPI NodeJSExpress MySQL - post request

Check database after do POST:

Sequelize One-to-One association - CRUD RestAPI NodeJSExpress MySQL - customer table

Sequelize One-to-One association - CRUD RestAPI NodeJSExpress MySQL - address table

– Find All Customers:

Sequelize One-to-One association - CRUD RestAPI NodeJSExpress MySQL - get all customers request

Sourcecode

Sequelize-One-to-One-NodeJS-Express-MySQL

By grokonez | April 20, 2018.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*