A node API for a store built with Typescript & PostgreSQL.
- Node.js Runtime.
- Express for writing node server;.
- pg client for managing database connections & running queries.
- Jasmine for writing unit tests.
You need the following modules and dependencies installed to run this project:
- Docker for the pdotgresql database container.
- Node 16 for the backend server to run.
- npm for managing project dependencies.
- I have added two sql files for data migrations under the data folder; one for seeding the database with initial data, for testing purposes, and the second one for cleaning the database from any data.
- I also added two node script files under the scripts folder; each script would run a data migration sql file on the specified database.
-
Run
npm installto install project dependencies. -
Rename
.env.exampleto.envand provide values for the variables. See example configurations below:# Server PORT=3000 # Environment NODE_ENV=test # Database settings POSTGRES_PORT=5432 POSTGRES_DB=db_dev POSTGRES_HOST=0.0.0.0 POSTGRES_USER=db_user POSTGRES_DB_TEST=db_test POSTGRES_PASSWORD=db_password # JWT & Bcrypt settings SALT=10 PEPPER=I-am-beyonce-always SECRET=I-declare-bankruptcy
-
Run
docker compose upto get the postgresql container up and ready for development. -
Access the posgresql instance on the docker container using
psql -U postgres. -
Create two databses one for development and one for testing by running
CREATE DATABASE <database_name>. Make sure to replace<database_name>with the database name. -
Create a new databse role using
CREATE USER <user> WITH PASSWORD <password>. Make sure to replace both<user>&<password>with the user name and password you want. -
Connect to each databse using
\c <database_name>. Make sure to replace<database_namewith the actual value. -
Grant all permissions on the dev and test databases to the newly created user using
GRANT ALL PRIVILEGES ON DATABASE <database_name> TO <user>. Make sure to replace the<database_name>and<user>with the actual values. -
Run the database migrations using
NODE_ENV=<env> npm run migrate:up. Replace<env>with the actual value which can be either test or dev. -
Run the data migrations using
NODE_ENV=<env> npm run seedto seed the database with initial data. -
Run
npm run devto run the development server and runnpm startto run the production server. -
Finally, the data migrations inserted two users found below. You can choose any one for authentciation to get a token to use for most of the API endpoints.
- email: [email protected], password: date_mike
- email: [email protected], password: battlestar_galactica
These users only work for the bcrypt secret provided in the example .env config above
The tests for the server is written in Jasmine. I have added two environemnts for database dev and test, see
database.json for more info about each database environment. The steps below show how to run the unit
tests for the testing database environment. To run it on the dev environemnt just set NODE_ENV to dev like
this NODE_ENV=dev
NODE_ENV=test npm run buildto build the project with the testing database as default.NODE_ENV=test npm run seedto seed the testing database with initial data.NODE_ENV=test npm testto run unit tests on the testing database.NODE_ENV=test npm run teardownto clean up the testing database from any data.
Important Note
The commnads
seed & teardownwill prompt you to provide the database password.
I have provided 69 unit tests that covers:
- pg database client.
- each database model.
- each data model handler.
dev: to start the development serverstart: to start the production servertest: to run jasmine tests on chosen database envlint: to run eslint on the project's typescript filesclean: to remove old build folder before building a new onelint:fix: to fix issues identified by eslintprebuild: to runnpm run cleanformat: to run prettier on the project's typescript filesmigrate:upto run database migrations on the chosen database envmigrate:downto drop the last migration on the chosen database envseedto run the seedup.js to populate the chosen database env with seed datateardownto run the teardown.js to clean the chosen database env from seed and any data
See REQUIREMENTS.md file.
