Overview

This is Part II in the continuation of “Setting up and ORM in Node JS”. In the previous post, we did the following:

• created an “Employees” database in either Postgres and/or MySQL
• generated an entity model and repository layer

In this post, we’ll be going over on how to integrate the entity model and repository layer generated in the last post.

If you want to skip this post entirely, and go straight for the code here you go:

https://github.com/keunlee/blog-code-examples/tree/master/nodejs-orm-sample

Otherwise, keep going forward.

Prerequisites

Fullfill requirements from:
Setting up an ORM in Node JS - Part I

Integrating our Entity Model and Repository Layer

In previous posts we generated code for an entity model and repository layer. The files generated:

To integrate these, we’re going to want to create a test application.

(1) create the application skeleton

Create a directory. Let’s call it nodejs-orm-sample.

With inside of that folder, create a package.json file and copy the following contents into it.

Notice we’ve got some node package dependencies that we’ve already added. For our purposes, this is our minimal set of node dependencies we’ll be using, however, feel free to add more.

In addition to that, pay close attention to the node databaseConfig, in packages.json. That node is used to specify the database(s) we’ll be using for this demonstration.

In this example, I’m using postgres.

Since we’re using typescript, we’ll want to add a minimal set of typescript definition files for use.

create a typings.json file and add the following:

to instruct the typescript transpiler how we want our typescript files transpiled, create a tsconfig.json file and add the following contents:

next, we’ll want to copy the generated files (generated in our last post) into our source tree.

Copy the following files :

into the following directory:

nodejs-orm-sample/src/domain

next we’ll want to modify a few of the generated files above. In our case, sequelize-models.ts and sequelize-types.ts.

Open both of the files in a text editor, and locate the /// <reference path="..." /> line located near the top of each file.

change that line to this:

and save both files.

At this point, run the following:

if all is well, you should not have any build errors.

(2) create domain repositories

For this exercise, we’ll be creating a domain repository which implements an abstract repository.

The abstract repository will leverage the domain model and data access code which we generated from previous steps.

Starting with the abstract repository, create a new file:

nodejs-orm-sample/src/repository/AbstractRepository.ts

Afterwards, we’ll want to create a domain specific repository which implements the abstract repository we’ve just created.

Let’s create an “Employees” repository. To do this, create a new file:

nodejs-orm-sample/src/repository/EmployeesRepository.ts

(3) testing the repository

To test the “Employees” repository, we’ll be using mocha to run our unit tests.

Create the following file:

nodejs-orm-sample/test/EmployeesRepository.spec.ts

npm run build
npm run test