diff --git a/.gitignore b/.gitignore index 491788d..b54a4cf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ node_modules/ -.DS_Store - -.env -config.env \ No newline at end of file +*.env +__pycache__ +.DS_Store \ No newline at end of file diff --git a/db/util/categories.csv b/db/util/data/categories.csv similarity index 100% rename from db/util/categories.csv rename to db/util/data/categories.csv diff --git a/db/util/products.csv b/db/util/data/products.csv similarity index 100% rename from db/util/products.csv rename to db/util/data/products.csv diff --git a/db/util/regions.csv b/db/util/data/regions.csv similarity index 100% rename from db/util/regions.csv rename to db/util/data/regions.csv diff --git a/db/util/insert_file_contents.py b/db/util/insert_file_contents.py new file mode 100644 index 0000000..680c44c --- /dev/null +++ b/db/util/insert_file_contents.py @@ -0,0 +1,33 @@ +import csv +from psycopg2 import sql + +# function to read from a given csv file into postgres +def insert_file_contents(conn, cur, file_path, table_name, column_count): + with open(file_path, 'r', encoding='utf-8-sig') as f: + reader = csv.reader(f) + header_names = "" + first_row_accessed = False + + for row in reader: + # get row values from first row of reader + if not first_row_accessed: + header_names = [item for item in row] + first_row_accessed = True + continue + + # execute table insertion for each row of csv based on number of columns + if column_count == 3: + cur.execute(sql.SQL("INSERT INTO {table} ({h1}, {h2}, {h3}) VALUES (%s, %s, %s)".format( + table=table_name, + h1=header_names[0], + h2=header_names[1], + h3=header_names[2] + )), + row + ) + elif column_count == 1: + cur.execute(sql.SQL("INSERT INTO {table} ({field}) VALUES (%s)".format(table=table_name, field=header_names[0])), row) + else: + raise + + conn.commit() \ No newline at end of file diff --git a/db/util/main.py b/db/util/main.py index aae21f1..1db62e7 100644 --- a/db/util/main.py +++ b/db/util/main.py @@ -1,7 +1,8 @@ import psycopg2 -from psycopg2 import sql import csv import os +from insert_file_contents import insert_file_contents +from psycopg2 import sql # read data from environment if present env_path = "../../.env" @@ -26,45 +27,9 @@ os.close(fd) conn = psycopg2.connect("dbname=e-commerce-092122 user=mikayladobson") cur = conn.cursor() -# read and print data from selection -cur.execute("SELECT * FROM users;") -for i in cur.fetchall(): - print(i) - -# function to read from a given csv file into postgres -def insert_file_contents(file_path, table_name, column_count): - with open(file_path, 'r', encoding='utf-8-sig') as f: - reader = csv.reader(f) - header_names = "" - first_row_accessed = False - - for row in reader: - # get row values from first row of reader - if not first_row_accessed: - header_names = [item for item in row] - first_row_accessed = True - continue - - # execute table insertion for each row of csv based on number of columns - if column_count == 3: - cur.execute(sql.SQL("INSERT INTO {table} ({h1}, {h2}, {h3}) VALUES (%s, %s, %s)".format( - table=table_name, - h1=header_names[0], - h2=header_names[1], - h3=header_names[2] - )), - row - ) - elif column_count == 1: - cur.execute(sql.SQL("INSERT INTO {table} ({field}) VALUES (%s)".format(table=table_name, field=header_names[0])), row) - else: - raise - - conn.commit() - - -insert_file_contents("./categories.csv", 'category', 1) -insert_file_contents("./regions.csv", 'region', 1) -insert_file_contents("./products.csv", 'product', 3) +# read contents of each file into postgres +insert_file_contents(conn, cur, "./data/categories.csv", 'category', 1) +insert_file_contents(conn, cur, "./data/regions.csv", 'region', 1) +insert_file_contents(conn, cur, "./data/products.csv", 'product', 3) print("Database insertions executed successfully.") \ No newline at end of file diff --git a/package.json b/package.json index d7fbe13..a1a50bd 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "nodemon server.js", - "seed": "cd db && node seed.js" + "seed": "cd db && node seed.js", + "populate": "cd db/util && python3 main.py" }, "engines": { "node": "v16.13.1"