some code and stuff

This commit is contained in:
Smigz 2025-01-16 16:28:53 -05:00
parent 75b83e30ea
commit 17d664eaeb
19 changed files with 762 additions and 4 deletions

11
inventory/src/db/pool.js Normal file
View file

@ -0,0 +1,11 @@
const { Pool } = require("pg");
// Todo make this env variables
module.exports = new Pool({
host: "smig-ca04.lab.smig.tech",
user: "odin",
database: "odin",
password: "odinproject",
port: 32343,
});
// 32343;

120
inventory/src/db/queries.js Normal file
View file

@ -0,0 +1,120 @@
const pool = require("./pool");
async function getAllItems() {
const { rows } = await pool.query("SELECT * FROM items");
return rows;
}
async function getItemByName(name) {
const { rows } = await pool.query("SELECT * FROM items WHERE name = $1", [
name,
]);
return rows[0];
}
async function getItemById(id) {
const { rows } = await pool.query(
"SELECT id, items.name, price, qty, category.name AS category, store.name AS store FROM items LEFT JOIN category on items.category_id = category.category_id LEFT JOIN store ON items.store_id = store.store_id WHERE items.id = $1 ",
[id],
);
return rows[0];
}
async function insertItem(data) {
const { name, price, qty, category_id, store_id } = data;
try {
await pool.query(
`INSERT INTO items (name, price, qty, category_id, store_id) VALUES ($1, $2, $3, $4, $5)`,
[name, price, qty, category_id, store_id],
);
return null;
} catch (e) {
return e;
}
}
async function deleteItem(item) {
try {
const res = await pool.query("DELETE FROM items WHERE id = $1", [item.id]);
return null;
} catch (e) {
return e;
}
}
async function getAllItemsWithRelationships() {
const { rows } = await pool.query(
`SELECT items.id, items.name, items.price, items.qty, category.name AS category_name, store.name AS store_name FROM items
LEFT JOIN category ON items.category_id = category.category_id
LEFT JOIN store ON items.store_id = store.store_id;`,
);
return rows;
}
async function getCategories() {
const { rows } = await pool.query("SELECT * FROM category");
return rows;
}
async function getCategoryId(item) {
const { rows } = await pool.query("SELECT * FROM category WHERE name = $1", [
item.name,
]);
return rows[0];
}
async function insertCategory(data) {
try {
await pool.query("INSERT INTO category (name) VALUES ($1)", [data]);
return null;
} catch (e) {
return e;
}
}
async function deleteCategory(itemId) {
try {
await pool.query("DELETE FROM category WHERE category_id = $1", [itemId]);
return null;
} catch (e) {
return e;
}
}
async function getStores() {
const { rows } = await pool.query("SELECT * FROM store");
return rows;
}
async function getStoreId(item) {
const { rows } = await pool.query("SELECT * FROM store WHERE name = $1", [
item.name,
]);
return rows[0];
}
async function insertStore(data) {
try {
await pool.query("INSERT INTO store (name) VALUES ($1)", [data]);
return null;
} catch (e) {
return e;
}
}
module.exports = {
insertItem,
getItemById,
getAllItems,
getItemByName,
deleteItem,
getCategories,
getCategoryId,
getStores,
getStoreId,
insertStore,
insertCategory,
deleteCategory,
getAllItemsWithRelationships,
};

View file

@ -0,0 +1,64 @@
#!/usr/bin/env node
const { Client } = require("pg");
const CREATE_DB = `
CREATE DATABASE inventory_app;
`;
const SQL = `
CREATE TABLE IF NOT EXISTS store (
store_id INTEGER PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
name varchar(80) NOT NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS category (
category_id INTEGER PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
name varchar(40) NOT NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS items (
id INTEGER PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
name VARCHAR(80) NOT NULL,
price INTEGER,
qty INTEGER,
store_id INTEGER REFERENCES store (store_id),
category_id INTEGER REFERENCES category (category_id)
);
`;
const POPULATE = `
INSERT INTO category (name)
VALUES ('grocery'), ('clothing'), ('household'), ('cleaning'), ('misc');
INSERT INTO store (name)
VALUES ('target'), ('wegmans'), ('giant');
INSERT INTO items (name, price, qty, store_id, category_id)
VALUES ('paper towels', '399', '3', '2','3' );
`;
async function main() {
console.log("DB Creation script");
const client = new Client({
host: "smig-ca05.lab.smig.tech",
user: "odin",
database: "odin",
password: "odinproject",
port: 32343,
});
try {
await client.connect();
await client.query(POPULATE);
await client.end();
} catch (e) {
console.log(`ERROR: ${e}`);
process.exit(1);
}
console.log("Done");
}
main();