The Data Access Layer (DAL) is the most critical part of any application. This is where the code integrates with an external source of truth like a database.
In this take, we'll show you how to build a DAL that talks to Postgres via Node. Then we'll go into best practices like Clean Architecture and what this means in practical terms.
Ready? Let’s go!
First, a quick note about the code: The code will have modern niceties like TypeScript, unit tests, and will tackle potential scalability issues. It will run on Node, with a local copy of Postgres running on a machine. The unit tests run in isolation, so they will work even when the database isn’t available, which is perfect for build servers.
Feel free to nab the working code from GitHub, or follow along!
Project Scaffold to Build a Node and Postgres DAL
The main project has many sub-folders. So, fire up the console and type:
Be sure to create the following files within the folder structure:
Here's what each file is for:
ActorDto
- Entity type which defines the actor modelpg/Actor
- Postgres implementation to find and update actor dataActorDb
- Interface that's useful outside the DALActor.test
- Unit tests — no Postgres database necessary
The index
file is mostly there to glue the code together. The index
file under the root folder works as the entry point.
Fire up npm init
to create a basic package.json
file. Once complete, execute the following NPM commands:
You can initialize the tsconfig.json
file via tsc --init
. Be sure to run this command at the project's root. The tsc
command is available when you install TypeScript globally or in the node_modules/.bin
folder. Enable "outDir": "./dist"
, and add "exclude": ["test"]
to the default config file.
Most of these packages are developer dependencies that make our lives easier. The only real dependency is pg
which is the node-postgres
package that talks to Postgres from a Node codebase.
In the package.json
under scripts
, put the following commands:
Lastly, be sure to have a working copy of Postgres running on your local machine. The database is a sample relational db called dvdrental
.
Go ahead and download the zip file, unpack it, and restore the dvdrental
database on your local machine. This DAL only focuses on the actor
, film_actor
, and film
relational tables. It is also possible to explore the rest of the schema via this PDF download.
Set Up the Bare Necessities in TypeScript
Next, write the bare skeleton code to be fleshed out later. To make things easier, the file name goes on top of the implementation. To follow along, simply find the file in the project structure and copy-paste the code. All these files are in the db
folder.
ActorDto
:
pg/Actor
:
ActorDb
:
index
:
Be sure to include the correct connection string. It follows this format: <username>:<password>@<server>:<port>/<database>
.
Actor.test
:
Clean Architecture Principles in Node
The basic skeleton above follows Clean Architecture as dependencies physically pull the entity away from implementation details.
In Node, dependencies should flow in a single direction, and ActorDto
, for example, sits in the innermost layer. This is the Data Transfer Object (DTO) which declares the data contract that the business understands.
At the outermost layer is pg/Actor
, which actually talks to the Postgres database. The projection in mapActorResult
translates from table columns in the db into the DTO. The interface ActorDb
declares the external interface, which is useful outside the DAL. This way, it is possible to swap out databases, from Postgres to Mongo for example, without risky invasive surgery. As long as the contracts hold, the changes will theoretically be minimal.
Since TypeScript strongly-typed pg/Actor
, the code then becomes testable. In Actor.test
, we bring in the index
file in db
to test the DAL. Stubs and mocks are put in place, so tests never actually talk to Postgres but just verify the code we wrote.
To illustrate, this is what the “onion” looks like in Clean Architecture:
Source for original diagram: Uncle Bob's blog - The Clean Architecture
In practical terms, this is what the implementation looks like in Node:
Dependencies are unidirectional, and the DTO is the business entity. Implementation details in pg/Actor
adhere to a strict interface that acts much like a contract. This, in some way, follows the dependency of inversion principle because the contract declares what the code must do. The use cases outside the DAL can then take the contract and not care about how it finds or updates data.
Queries in TypeScript
To practice TDD, we simply write the test first, and look for a failure. Put this in Actor.test
:
Add in a passing implementation by replacing findByYearAndLastName
in pg/Actor
:
Notice the call at the end that returns a Promise<ActorDto>
. This is a projection that contains the DTO, an external entity, and is how people who are not engineers reason about the business domain.
The #pool
object remains hidden, even during runtime, and abstracts implementation details away from use cases.
Transactions in TypeScript
For transactions, let's say there is a list of actor ids with a new last name.
In the rollback test, the client
mock expects the code to execute query
exactly twice. Then, it rejects the promise to throw an exception and resolves the subsequent promise when the exception gets caught. This technique shows how sinon
can really help to keep tests fluent and somewhat easy to follow.
There is the potential of a failure mid-changes, so this tests both the happy path and the rollback. Here are the implementation details:
The map
method iterates through the list of ids. A promise fires all requests in parallel, so the client waits less. Once all requests complete, it returns the result array used to gather a count via a reduce.
If there are any issues mid-changes, an exception is thrown that rollbacks the changes and rethrows this same exception to unwind the call stack. It is important not to swallow the error, or mask the error with a different exception.
The finally
block must release the client back to the pool otherwise the connection pool might run dry.
Scalability in Postgres
Too many queries in a single transaction can cause a performance bottleneck in Postgres. One technique to combat this is to shorten long-running connections by sending a single query instead.
This time, the query updates the first name instead of the last name. But, this uses an array of integers as a parameter via ANY instead of a transaction. The unit test is similar to what has already been shown, so feel free to take a peek at the GitHub repo.
AppSignal offers instrumentation for Postgres, with a magic dashboard on top that visualizes your database performance metrics. Magic dashboards are automatically created for you when you integrate AppSignal into your app, so there is no need to configure the graphs manually.
You can check for starving pool connections via a Postgres magic dashboard.
The Postgres Pool metric captures idle and waiting connections in the pool. This is one effective way to monitor database performance and find ways to reduce SQL pressure.
Putting It All Together: Execute the DAL
Lastly, in the index
file under the root folder, do:
With this in place, run npm start
to execute the entire DAL. Notice we bring in the strongly-typed actor
dependency. In Clean Architecture, the business use cases can use this same technique to interact with persisted data.
Data Types
You may come across a gotcha in the output after running the code:
The rentalRate
is declared as a number, but the DAL actually returns a string. To tackle this problem, simply add this to db/index
:
This tells node-postgres to parse from a numeric type in Postgres, based on an OID, into a number type in Node.
Wrap Up: Now Our DAL Talks to Postgres via Node
In this post, we built a DAL that communicates to Postgres through Node. We saw that connecting to Postgres from Node is possible via node-postgres. This package supports a connection pool, transactions, and parsing db types. We also looked at Clean Architecture principles and how they work in practice.
Until next time, happy coding!
P.S. If you liked this post, subscribe to our JavaScript Sorcery list for a monthly deep dive into more magical JavaScript tips and tricks.
P.P.S. If you need an APM for your Node.js app, go and check out the AppSignal APM for Node.js.