NWO.ai assessment
Installation:
- Clone the repository (git clone https://github.com/emmagawd/subscription-api.git)
- Navigate to project directory (cd subscription-api)
- Install the necessary packages (npm install)
Environment Setup:
- Copy the ‘.env.example’ file to ‘.env’ (cp .env.example .env)
- Open the
.env
file and replace the placeholders with your actual PostgreSQL database credentials for both the application database and the test database.
Create Database Schema:
Main Database:
- Log in to your PostgreSQL command line tool (psql –U your_username –d your_database)
- Execute the following SQL commands:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL
);
CREATE TABLE subscriptions (
id SERIAL PRIMARY KEY,
user_id INT NOT NULL,
industry VARCHAR(255),
source VARCHAR(255),
subcategory VARCHAR(255),
active BOOLEAN DEFAULT true,
CONSTRAINT fk_user
FOREIGN KEY(user_id)
REFERENCES users(id)
ON DELETE CASCADE
);
INSERT INTO users (email) VALUES ('[email protected]');
Test Database:
- Ensure you have a separate test database created (createdb -U your_username your_test_database)
- Log in to your PostgreSQL command line tool for the test database (psql -U your_username -d your_test_database)
- Repeat the SQL commands from the Main Database section to set up the schema in your test database.
Running the program:
- Start the PostgreSQL server (ensure it's configured as per the
.env
settings). - Test to see if database is properly connected using ‘node dbTest.js’. If you aren't getting 'Connection Test Successful', verify that your PostgreSQL service is running and that
.env
contains the correct credentials for both databases. - Launch the application (npm start)
- The server should now be running on http://localhost:3000
Testing the Application:
- Run the automated test (npm test)
The subscriptions
table contains a foreign key that links each subscription to a user in the users
table. This foreign key ensures that:
- Each subscription is associated with an existing user.
- Deleting a user from the
users
table will automatically remove all their subscriptions (due to theON DELETE CASCADE
rule). It is crucial to maintain these relationships to ensure data integrity and correct application behavior.