Skip to content

Demo for an NodeJS API backed by PostgreSQL, orchestrated with Docker Compose, and validated with Supertest & Mocha

Notifications You must be signed in to change notification settings

DesertFrogSolutions/AcronymApiDemo

Repository files navigation

Acronym API Demo

Test API implementation

Benchmark API implementations w/ Postgres Service Container

This is a tech demo demonstrating an API built on Node.JS in 2022, and now serves as a case study in the maintenance burden for an API built in NodeJS without significant feature work. For the purposes of comparing performance and ergonomics, we completed multiple implementations of the same API in this repository using different Node.JS web server frameworks.

Note - Neither the PostgreSQL backend nor the web server implementations are configured to be production-ready. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

For more information, reach out to the team at [email protected] or desertfrogsolutions.com

Install & Initialize PostgreSQL DB

  1. Install PostgreSQL.

  2. Start a database instance

/usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgres

By default, the database listens to localhost:5432.

  1. Initialize acronymapi user with password password and database defaultdb
CREATE USER acronymapi PASSWORD 'password';
CREATE DATABASE defaultdb WITH OWNER = acronymapi;
GRANT ALL PRIVILEGES ON DATABASE defaultdb TO acronymapi;
  1. Run initdb script (generated from acronym.json by src/generate-initdb.js with npm run generate-initdb.)
PGPASSWORD=password psql --file=./initdb.sql --username=acronymapi --dbname=defaultdb

Configure, Start, and Test NodeJS server

Configuration

Default configuration aligned with the setup above is provided - see example.config.json for a template. The default node-postgres environment variables are used to interface with the database. The authentication information for the Put and Delete endpoints is API_USER and API_PASSWORD.

Since the server uses nconf, it supports hierarchical configuration:

  • command line arguments, parsed by yargs (e.g. --foo baz, also nested: --foo.bar=baz)
  • environment variables
  • config.json in the project root directory

All configuration sources that were found will be flattened into one object, so that sources earlier in this list override later ones.

Start the Server

Start the server you select (e.g. express) with Node:

npm run start:express

If actively developing the server (or you just want hot reloading), use nodemon to start the server with

npm run dev:express

Testing

The spec.js file contains functional tests in Mocha for the server with the PostgreSQL access stubbed out with Sinon. Run them with

npm run test

If actively developing the code, run Mocha in "watch" mode with

npm run test:watch

If running with a "live" and initialized PostgreSQL database (e.g. started with docker-compose up database, run tests without mocked DB calls with

npm --test_live_pg_server=true run test

The test results can be validated in the database by using e.g. PGPASSWORD=password psql -h localhost -U acronymapi -d defaultdb and querying SELECT * FROM acronym WHERE name LIKE '%ACRONYM%'; to find a PUTACRONYM and a POSTACRONYM

If running with a "live" NodeJS application (e.g. started with docker-compose up backend or npm run start:express etc.), run tests against the live endpoint with

npm --test_live_node_server=true run test:express

Benchmarks

Benchmarks are automated using Autocannon; with a server running in another process, run benchmarks with the command

About

Demo for an NodeJS API backed by PostgreSQL, orchestrated with Docker Compose, and validated with Supertest & Mocha

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published