This is a take home assignment for T.
This API backend provides the following endpoints:
Adds a user to the database. The request body should include an email
field, as shown in the example below:
{
"email": "[email protected]"
}
Retrieves a list of users with pagination capabilities. This endpoint accepts the following query string parameters:
searchStr
: The search string used to filter users (byemail
column) to return.pageSize
: The number of users returned per page. Defaults to 10, with a maximum of 100.cursor
: The pagination cursor, which must be provided to retrieve the next page of users.
A health check endpoint to verify the API's status.
Tech stack:
- Node.js 22+
- Typescript
- Fastify
- Drizzle
- Postgres
- Vitest
The .env.dev
file must contain the following environmental variables to configure the application:
Specifies the host IP address that will serve the API.
Specifies the port on which the app will serve the API. Setting PORT=0
will automatically choose a random available port.
Specifies the CORS policy for the API. This can be set to:
CORS=https://somedomain.com
to allow requests from a specific domain.CORS=https://somedomain.com https://anotherdomain.com
to allow requests from multiple domains.CORS=*
to allow requests from any domain.
The connection string to a Postgres database.
Start the local Postgres DB.
docker run -d \
--name test-db \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_DB=myapp_test \
-p 5432:5432 \
postgres:15-alpine
Run server
npm run start:dev
Build the image
docker build -t t_takehome .
Run container (this will start both a dockerized Postgres instance and the image built in previous step)
docker-compose up -d
The code has 100% code coverage as it is supposed to be mission-critical.
Unit tests cover basic functionality and use mock database instead of a real Postgres, while integration tests work with a real PostgresDB to test parts of code that read/write to DB.
npm run test:unit
- runs unit tests
npm run test:integration
- starts test DB, runs integration tests, stops test DB
npm run coverage
- generates a coverage report (for both unit and integration tests)
npm run load-test
- with DB and server running runs a basic load test with artillery (Note: these are mostly boilerplate, proper load test would require cloud staging environment to draw meaningful conclusions)