The NodeJS ExpressJS framework is fantastic for creating performant web applications. This post details how to integrate the Sequelize NodeJS package with ExpressJS to achieve ORM capability.
Versions
At thhe time of this post, the following relevant versions of packages were used:
- co (4.6.0)
- express (4.13.4)
- pg (6.1.0)
- pg-hstore (2.3.2)
- sequelize (3.24.1)
- umzug (1.11.0)
Assumptions
This tutorial assumes that you already have an ExpressJS application created and you are working within the directory of the project. The tutorial also assumes you are using something similar to dotenv for tracking configuration parameters, or at least provide the parameters as environment variables when running the application.
Process
First, install the Sequelize package and required dependencies:
sequelize
is package which will provide the ORM capability.co
is used for serializing the database creation/migration prior to launch of the ExpressJS listener. Given JavaScript’s asynchronous nature and callbacks, it’s best to serialize this initial launch process.pg
is used for creating the database itself if it has not yet been created (sequelize
does not currently provide this capability).umzug
provides the ability to run migrations within the code itself (rather than by using the sequelize command-line binary). It natively creates a database table to keep track of which migrations have been run for Sequelize.
Next, update your bin/www
file to create two functions prior to binding the listener. Note that the co
package expects a Promise resolve() for each step in the serial sequence:
Once the above functions have been created in the file, wrap the listen invocation in the same
bin/www
file to ensure that the database is created and migrations are run prior to the application
launching and binding to the interface:
The application will then be available for use following the server.listen(port);
line of code.
Extra - User Tracking
Assuming that you already have a migration for adding user information which includes email address
and number of logins as logins
, the following code snippett is a nice way to create a user upon
first login, and increment the total number of logins each time they log in (if they already exist):
Summary
With the above setup, each time the application launches, it will run through the following process (in sequence) prior to the application being available. Although the startup time may be a bit longer, it is actually a quite nice automated way to ensure that your application deployments always include the latest up to date migrations:
- Create the database user (ignore error if exists)
- Create the database (ignore error if exists)
- Grant the database user privileges to the database (ignore error if exists)
- Run any migration in the
migrations/
folder that have no yet been run - Launch the listener for the application, making it available
Credit
Contributions to some of the above were gleaned from: