restored python scripts, database initializes with correct descriptions
This commit is contained in:
33
db/Seed.js
33
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();
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user