diff --git a/db/Seed.js b/db/Seed.js index 6ddbdec..88da143 100644 --- a/db/Seed.js +++ b/db/Seed.js @@ -1,10 +1,12 @@ const { Client } = require('pg'); +const insertAll = require('./util/insertAll'); require('dotenv').config({ path: "../.env" }); async function main() { console.log("Beginning database setup."); const client = new Client({ connectionString: process.env.CONNECTION }); + console.log(process.env.CONNECTION); await client.connect().then(console.log("Now connected to postgres")); // user @@ -70,6 +72,10 @@ async function main() { ); `; + // const populateProductTable = ` + // \copy products(name, regionid, categoryid, price, inventory, unit, description) FROM './data/products.csv' DELIMITER ',' CSV HEADER + // `; + // products_carts const createProductsCarts = ` CREATE TABLE IF NOT EXISTS products_carts ( @@ -89,7 +95,12 @@ async function main() { ); `; - const allQueries = [createUserTable, createCartTable, createCategoryTable, createRegionTable, createProductTable, createOrderTable, createProductsCarts, createProductsOrders]; + const allQueries = [ + createUserTable, createCartTable, createCategoryTable, createRegionTable, + createProductTable, /* populateProductTable, */ createOrderTable, + createProductsCarts, createProductsOrders + ]; + let status; try { diff --git a/db/util/insertAll.js b/db/util/insertAll.js new file mode 100644 index 0000000..ba10fd1 --- /dev/null +++ b/db/util/insertAll.js @@ -0,0 +1,11 @@ +const { readFileSync } = require('fs'); + +// insertFromCSV('./data/products.csv'); + +module.exports = (path) => { + return readFileSync(path) + .toString() + .split('\n') + .map(s => s.trim()) + .map(s => s.split(',').map(s => s.trim())); +} \ No newline at end of file diff --git a/db/util/insert_file_contents.py b/db/util/insert_file_contents.py index 0849c2f..6c15439 100644 --- a/db/util/insert_file_contents.py +++ b/db/util/insert_file_contents.py @@ -1,4 +1,5 @@ import csv +import psycopg2 from psycopg2 import sql # function to read from a given csv file into postgres diff --git a/db/util/main.py b/db/util/main.py index b3751e4..a289432 100644 --- a/db/util/main.py +++ b/db/util/main.py @@ -3,6 +3,8 @@ import psycopg2 import os # read data from environment if present +supabase_string = os.getenv('CONNECTION') + env_path = "../../.env" fd = os.open(env_path, os.O_RDONLY) n = 300 diff --git a/db/util/parseOutput.js b/db/util/parseOutput.js new file mode 100644 index 0000000..1068bfc --- /dev/null +++ b/db/util/parseOutput.js @@ -0,0 +1,26 @@ +const insertAll = require('./insertAll'); +const pgp = require('pg-promise')({ capSQL: true }); + +const parseOutput = (arr, tableName) => { + let i = 0; + let data; + let cols = null + + for (let row of arr) { + if (i == 0) { + cols = row; + i++; + } else { + if (cols == null) { + cols = row; + } else { + data.concat(row); + } + } + } + + let query = pgp.helpers.insert(data, cols, tableName); + console.log(query); +} + +parseOutput(insertAll('./data/products.csv'), 'products'); \ No newline at end of file