restored python scripts, database initializes with correct descriptions

This commit is contained in:
Mikayla Dobson
2022-10-18 13:08:06 -05:00
parent ebb1dc842c
commit 5d7188bbaa
7 changed files with 72 additions and 56 deletions

View File

@@ -15,6 +15,10 @@ export default function ProductPage() {
.then(res => setProductData(res));
}, [])
useEffect(() => {
console.log(productData);
}, [productData]);
if (!productData) return <h1>Product not found.</h1>
return (

View File

@@ -97,15 +97,13 @@ async function main() {
createProductsCarts, createProductsOrders
];
const categoryInsert = readCSV('./util/data/categories.csv', 'category');
const regionInsert = readCSV('./util/data/regions.csv', 'region');
const productInsert = readCSV('./util/data/products.csv', 'product');
// const categoryInsert = readCSV('./util/data/categories.csv', 'category');
// const regionInsert = readCSV('./util/data/regions.csv', 'region');
// const productInsert = readCSV('./util/data/products.csv', 'product');
const allInsertions = [
categoryInsert, regionInsert, productInsert
]
let status;
// const allInsertions = [
// categoryInsert, regionInsert, productInsert
// ]
try {
await client.query("DROP SCHEMA public CASCADE; CREATE SCHEMA public");
@@ -114,23 +112,18 @@ async function main() {
await client.query(q);
}
for (let section of allInsertions) {
for (let s of section) {
await client.query(s);
}
}
// for (let section of allInsertions) {
// for (let s of section) {
// await client.query(s);
// }
// }
await client.end();
status = "Database initialization successful.";
} catch(e) {
status = e;
} finally {
if (status !== "Database initialization successful.") {
throw new Error(status);
}
throw new Error(e);
}
console.log(status);
console.log("Database initialization successful");
}
main();

View File

@@ -0,0 +1,29 @@
import csv
from psycopg2 import sql
# function to read from a given csv file into postgres
def insert_contents(conn, cur, file_path, table_name):
with open(file_path, 'r', encoding='utf-8-sig') as f:
reader = csv.reader(f)
first_row_accessed = False
header_names = ""
num_columns = 0
for row in reader:
# get row values from first row of reader
if not first_row_accessed:
header_names = [item for item in row]
num_columns = len(header_names)
first_row_accessed = True
continue
mapped_columns = [header_names[i] for i in range(num_columns)]
prepared_q = sql.SQL("INSERT INTO {TABLE} ({COLS}) VALUES ({VALS})").format(
TABLE=sql.Identifier(table_name),
COLS=sql.SQL(', ').join(map(sql.Identifier, mapped_columns)),
VALS=sql.SQL(', ').join(sql.Placeholder() * len(mapped_columns))
)
cur.execute(prepared_q, [item for item in row])
conn.commit()

22
db/util/main.py Normal file
View File

@@ -0,0 +1,22 @@
import psycopg2
from decouple import config
from insert_contents import insert_contents
# read data from environment if present
env_path = "../../.env"
DB_CONN = config('CONNECTION')
USER = "mikayladobson"
# connect to local database instance and open a cursor
conn = psycopg2.connect(DB_CONN)
cur = conn.cursor()
print("Now attempting to populate database...")
# read contents of each file into postgres
insert_contents(conn, cur, "./data/categories.csv", 'category')
insert_contents(conn, cur, "./data/regions.csv", 'region')
insert_contents(conn, cur, "./data/products.csv", 'product')
print("Insertions executed successfully.")
print("Database preparations complete!")

View File

@@ -1,35 +0,0 @@
const { readFileSync } = require('fs');
const pgp = require('pg-promise')({ capSQL: true });
module.exports = (path, tableName) => {
const arr = readFileSync(path)
.toString()
.split('\n')
.map(s => s.trim())
.map(s => s.split(',').map(s => s.trim()));
let data = [];
let queries = [];
let cols;
for (let row of arr) {
if (!cols) {
cols = row;
} else {
let formattedData = {};
for (let j = 0; j < row.length; j++) {
const key = cols[j];
const value = row[j];
formattedData[key] = value;
}
data.push(formattedData);
}
}
for (let each of data) {
queries.push(pgp.helpers.insert(each, cols, tableName));
}
return queries;
}

View File

@@ -7,7 +7,7 @@
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js",
"dev": "nodemon server.js",
"seed": "cd db && node seed.js"
"seed": "cd db && node seed.js && cd util && python3 main.py"
},
"engines": {
"node": "v16.13.1"

3
requirements.txt Normal file
View File

@@ -0,0 +1,3 @@
psycopg2==2.9.4
psycopg2_binary==2.9.3
python-decouple==3.6