This is my solution to the project. I've spent about 6 hours collecting on the code and debugging and it is still not particularly robust for production use. Built in documentation for the Rest endpoints is provided by built-in Swagger UI once the server is started. I decided to swap out restify for fastify to get the plugin support for swagger and validation, and made testing easier for me.
Install dependencies and start the stack, run the following:
npm install
docker compose build
docker compose up -d
To populate the database before making requests, run the following:
npm run db-migrate
npm run db-seed
This project requires a PostgreSQL database. If you are not starting the database via docker compose, make sure to run the script in ./init-db/
or the migrations will not complete.
If you do not have PostgreSQL installed, one will be started via default docker compose command.
A debug administration interface provided by pg_admin can be started using the --profile debug
with the docker compose.
If you are using the above Docker compose command, no further action is needed on your part. If you are using your own database instances, modify connectionString.js
to point to it before proceeding.
- fastify - is used for the REST API server.
- fastify-plugin - is used for the REST API server
- fastify-swagger - Provides openapi support to fastify
- fastify-swagger-ui - Provides a UI for swagger
- fastify-cors - provide cors support for fastify
- jasmine - is used for unit testing
- knexjs - is used for Database calls, query building, and migrations
server.js
- entrypoint to the server running on 8080.app.js
- contains the fastify-based API serverinit-db/0001-init.sql
- contains sql for postgres to enable uuid_v4 generation function in the databasemigrations/20210409000652_create-table-construction_sites.js
- a database migration that creates theconstruction_sites
table which for the sake of this project is a single column with a uuid-based id that materials can be associated with.migrations/20240712065256_create-table-site_materails.js
- a database migraiton that create sthesite_materials
table which contians the data model for the project.connectionString.js
- contains the Database connection string so that it can be shared by multiple files.scripts/
- contains database scripts called by package.json for seeding, migrating, and rolling back the database.spec/
- contains the tests executed by jasiminesrc/plugins/database.js
- contains some glue code to use knex in fastify as a pluginsrc/routes/materials.js
- contians the implementation of the routes for material handling in the projectsrc/routes/sites.js
- contains a route to return the list of sites seeded by the knex scriptssrc/models/material.js
contains the json schema for the material object representation
Task | Description |
---|---|
start |
Runs the server on port 8080 |
test |
Runs jasmine tests. Have the database started and seeded |
prettier |
Runs the formatter and fixes the formating |
prettier-check |
Runs the formmater ahd checks the formating |
db-migrate |
Applies all pending migrations to the database |
db-rollback |
Rolls back last set of database migrations |
db-seed |
Seeds the database with test data |
db-create-migration |
Creates a new database migration, takes a single parameter (migration name) |