Implementing the Data Access Layer
Layered Architecture·4 min read·Jan 1, 2025
The Data Access Layer is the layer responsible for performing input/output operations outside of the application's boundaries, such as communicating with the database.
One of the easiest ways to implement this layer is to use an Object-Relational Mapper (ORM), such as Sequelize, to create an object that exposes generic handlers for performing CRUD operations on the database, acting as an intermediary between the Service Layer and the database instance.
Setting up the MySQL database
Assuming we're using MySQL, we have to first set it up by creating a new database for the application at hand, a new database table to store user's information, and a new database user with limited privileges to administer this table.
Creating a new database
Let's start by connecting to the MySQL server using the root account, or any other account with sufficient privileges to create databases and users:
$ mysql -u root -pOnce logged in, let's create a new database named app:
mysql> CREATE DATABASE app;And let's switch to that database:
mysql> USE app;Creating a new table
Within the app database, let's create a new table named users:
mysql> CREATE TABLE users ( -> id INT AUTO_INCREMENT NOT NULL PRIMARY KEY, -> email VARCHAR(320) NOT NULL UNIQUE,Unlock the Build Layered Services in Node.js module
Learn how to build well-structured, easily maintainable, and resilient microservices with the three-layer architecture.
You get immediate access to:
- 10 focused lessons across the three-layered architecture
- 1 real-world commented, step-by-step project
- Ongoing updates to this bundle
- Lifetime access to this bundle