restored python scripts, database initializes with correct descriptions
This commit is contained in:
@@ -15,6 +15,10 @@ export default function ProductPage() {
|
|||||||
.then(res => setProductData(res));
|
.then(res => setProductData(res));
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
console.log(productData);
|
||||||
|
}, [productData]);
|
||||||
|
|
||||||
if (!productData) return <h1>Product not found.</h1>
|
if (!productData) return <h1>Product not found.</h1>
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
33
db/Seed.js
33
db/Seed.js
@@ -97,15 +97,13 @@ async function main() {
|
|||||||
createProductsCarts, createProductsOrders
|
createProductsCarts, createProductsOrders
|
||||||
];
|
];
|
||||||
|
|
||||||
const categoryInsert = readCSV('./util/data/categories.csv', 'category');
|
// const categoryInsert = readCSV('./util/data/categories.csv', 'category');
|
||||||
const regionInsert = readCSV('./util/data/regions.csv', 'region');
|
// const regionInsert = readCSV('./util/data/regions.csv', 'region');
|
||||||
const productInsert = readCSV('./util/data/products.csv', 'product');
|
// const productInsert = readCSV('./util/data/products.csv', 'product');
|
||||||
|
|
||||||
const allInsertions = [
|
// const allInsertions = [
|
||||||
categoryInsert, regionInsert, productInsert
|
// categoryInsert, regionInsert, productInsert
|
||||||
]
|
// ]
|
||||||
|
|
||||||
let status;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await client.query("DROP SCHEMA public CASCADE; CREATE SCHEMA public");
|
await client.query("DROP SCHEMA public CASCADE; CREATE SCHEMA public");
|
||||||
@@ -114,23 +112,18 @@ async function main() {
|
|||||||
await client.query(q);
|
await client.query(q);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let section of allInsertions) {
|
// for (let section of allInsertions) {
|
||||||
for (let s of section) {
|
// for (let s of section) {
|
||||||
await client.query(s);
|
// await client.query(s);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
await client.end();
|
await client.end();
|
||||||
status = "Database initialization successful.";
|
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
status = e;
|
throw new Error(e);
|
||||||
} finally {
|
|
||||||
if (status !== "Database initialization successful.") {
|
|
||||||
throw new Error(status);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(status);
|
console.log("Database initialization successful");
|
||||||
}
|
}
|
||||||
|
|
||||||
main();
|
main();
|
||||||
29
db/util/insert_contents.py
Normal file
29
db/util/insert_contents.py
Normal 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
22
db/util/main.py
Normal 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!")
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
@@ -7,7 +7,7 @@
|
|||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
"start": "node server.js",
|
"start": "node server.js",
|
||||||
"dev": "nodemon server.js",
|
"dev": "nodemon server.js",
|
||||||
"seed": "cd db && node seed.js"
|
"seed": "cd db && node seed.js && cd util && python3 main.py"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "v16.13.1"
|
"node": "v16.13.1"
|
||||||
|
|||||||
3
requirements.txt
Normal file
3
requirements.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
psycopg2==2.9.4
|
||||||
|
psycopg2_binary==2.9.3
|
||||||
|
python-decouple==3.6
|
||||||
Reference in New Issue
Block a user