support for database seed
This commit is contained in:
@@ -13,11 +13,11 @@ The project directory is organized as follows:
|
||||
- ``routes``: this hosts the Express router which powers the project's REST API
|
||||
- ``server.js``: the backend "entryway" for the application, which hosts the server and the integration for the API.
|
||||
|
||||
## Installing and Running the Project Locally
|
||||
## Installing and Running the Development Build Locally
|
||||
1. Clone the project repository from the root level.
|
||||
2. Node modules are hosted both at root directory level and within the client directory.
|
||||
3. At the root directory level, run ``npm install`` to install backend dependencies.
|
||||
4. Change into the ``/client`` directory and run ``npm install`` to install frontend dependencies.
|
||||
5. Still within ``/client``, run ``npm run dev`` to start the Vite development server.
|
||||
6. In another terminal window at the root directory level, run ``node server.js`` to start the localhost server.
|
||||
6. In another terminal window at the root directory level, run ``npm start`` to start the localhost server.
|
||||
7. It should indicate that the server is listening on its designated port, and log each database transaction to the console.
|
||||
76
db/Seed.js
Normal file
76
db/Seed.js
Normal file
@@ -0,0 +1,76 @@
|
||||
const { Client } = require('pg');
|
||||
require('dotenv').config({ path: "../.env" });
|
||||
|
||||
(async () => {
|
||||
const client = new Client({ connectionString: process.env.CONNECTION });
|
||||
await client.connect().then(console.log("Connection successful."));
|
||||
|
||||
const createUserTable = `
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY NOT NULL,
|
||||
username VARCHAR NOT NULL,
|
||||
password VARCHAR NOT NULL,
|
||||
firstname VARCHAR NOT NULL,
|
||||
lastname VARCHAR NOT NULL
|
||||
);
|
||||
`;
|
||||
|
||||
const createCartTable = `
|
||||
CREATE TABLE IF NOT EXISTS cart (
|
||||
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY NOT NULL,
|
||||
appUserId INT REFERENCES users(id),
|
||||
subtotal NUMERIC
|
||||
);
|
||||
`;
|
||||
|
||||
const createOrderTable = `
|
||||
CREATE TABLE IF NOT EXISTS orders (
|
||||
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY NOT NULL,
|
||||
cartId INT REFERENCES cart(id),
|
||||
delivered BOOLEAN,
|
||||
processed BOOLEAN,
|
||||
shipped BOOLEAN
|
||||
);
|
||||
`;
|
||||
|
||||
const createCategoryTable = `
|
||||
CREATE TABLE IF NOT EXISTS category (
|
||||
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY NOT NULL,
|
||||
name VARCHAR NOT NULL,
|
||||
description VARCHAR NOT NULL
|
||||
);
|
||||
`;
|
||||
|
||||
const createProductTable = `
|
||||
CREATE TABLE IF NOT EXISTS product (
|
||||
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY NOT NULL,
|
||||
name VARCHAR NOT NULL,
|
||||
description VARCHAR NOT NULL,
|
||||
category VARCHAR NOT NULL,
|
||||
categoryId INT REFERENCES category(id),
|
||||
price NUMERIC NOT NULL
|
||||
);
|
||||
`;
|
||||
|
||||
const createProductsCarts = `
|
||||
CREATE TABLE IF NOT EXISTS products_carts (
|
||||
productId INT REFERENCES product(id),
|
||||
cartId INT REFERENCES cart(id)
|
||||
);
|
||||
`;
|
||||
|
||||
const allQueries = [createUserTable, createCartTable, createCategoryTable, createProductTable, createOrderTable, createProductsCarts];
|
||||
let status;
|
||||
|
||||
try {
|
||||
for (let q of allQueries) {
|
||||
await client.query(q);
|
||||
}
|
||||
await client.end();
|
||||
status = "Database setup successful!";
|
||||
} catch(e) {
|
||||
status = e;
|
||||
}
|
||||
|
||||
console.log(status);
|
||||
})();
|
||||
@@ -5,7 +5,8 @@
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "nodemon server.js"
|
||||
"start": "nodemon server.js",
|
||||
"seed": "cd db && node Seed.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": "v16.13.1"
|
||||
|
||||
Reference in New Issue
Block a user