From 5d7188bbaad3f2c4e7fc067632c6cab1a8daaa4b Mon Sep 17 00:00:00 2001
From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com>
Date: Tue, 18 Oct 2022 13:08:06 -0500
Subject: [PATCH] restored python scripts, database initializes with correct
descriptions
---
client/src/components/Product/ProductPage.tsx | 4 +++
db/Seed.js | 33 +++++++----------
db/util/insert_contents.py | 29 +++++++++++++++
db/util/main.py | 22 ++++++++++++
db/util/readCSV.js | 35 -------------------
package.json | 2 +-
requirements.txt | 3 ++
7 files changed, 72 insertions(+), 56 deletions(-)
create mode 100644 db/util/insert_contents.py
create mode 100644 db/util/main.py
delete mode 100644 db/util/readCSV.js
create mode 100644 requirements.txt
diff --git a/client/src/components/Product/ProductPage.tsx b/client/src/components/Product/ProductPage.tsx
index c4716a9..34d0145 100644
--- a/client/src/components/Product/ProductPage.tsx
+++ b/client/src/components/Product/ProductPage.tsx
@@ -15,6 +15,10 @@ export default function ProductPage() {
.then(res => setProductData(res));
}, [])
+ useEffect(() => {
+ console.log(productData);
+ }, [productData]);
+
if (!productData) return
Product not found.
return (
diff --git a/db/Seed.js b/db/Seed.js
index 21cdec3..9abcd45 100644
--- a/db/Seed.js
+++ b/db/Seed.js
@@ -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();
\ No newline at end of file
diff --git a/db/util/insert_contents.py b/db/util/insert_contents.py
new file mode 100644
index 0000000..1689464
--- /dev/null
+++ b/db/util/insert_contents.py
@@ -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()
\ No newline at end of file
diff --git a/db/util/main.py b/db/util/main.py
new file mode 100644
index 0000000..3147735
--- /dev/null
+++ b/db/util/main.py
@@ -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!")
diff --git a/db/util/readCSV.js b/db/util/readCSV.js
deleted file mode 100644
index 767aefc..0000000
--- a/db/util/readCSV.js
+++ /dev/null
@@ -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;
-}
diff --git a/package.json b/package.json
index 4696030..998a08a 100644
--- a/package.json
+++ b/package.json
@@ -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"
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..7647d3d
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,3 @@
+psycopg2==2.9.4
+psycopg2_binary==2.9.3
+python-decouple==3.6