implementing supabase. troubleshooting database creation
This commit is contained in:
13
db/Seed.js
13
db/Seed.js
@@ -1,10 +1,12 @@
|
|||||||
const { Client } = require('pg');
|
const { Client } = require('pg');
|
||||||
|
const insertAll = require('./util/insertAll');
|
||||||
require('dotenv').config({ path: "../.env" });
|
require('dotenv').config({ path: "../.env" });
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
console.log("Beginning database setup.");
|
console.log("Beginning database setup.");
|
||||||
|
|
||||||
const client = new Client({ connectionString: process.env.CONNECTION });
|
const client = new Client({ connectionString: process.env.CONNECTION });
|
||||||
|
console.log(process.env.CONNECTION);
|
||||||
await client.connect().then(console.log("Now connected to postgres"));
|
await client.connect().then(console.log("Now connected to postgres"));
|
||||||
|
|
||||||
// user
|
// 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
|
// products_carts
|
||||||
const createProductsCarts = `
|
const createProductsCarts = `
|
||||||
CREATE TABLE IF NOT EXISTS products_carts (
|
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;
|
let status;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
11
db/util/insertAll.js
Normal file
11
db/util/insertAll.js
Normal file
@@ -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()));
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import csv
|
import csv
|
||||||
|
import psycopg2
|
||||||
from psycopg2 import sql
|
from psycopg2 import sql
|
||||||
|
|
||||||
# function to read from a given csv file into postgres
|
# function to read from a given csv file into postgres
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ import psycopg2
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
# read data from environment if present
|
# read data from environment if present
|
||||||
|
supabase_string = os.getenv('CONNECTION')
|
||||||
|
|
||||||
env_path = "../../.env"
|
env_path = "../../.env"
|
||||||
fd = os.open(env_path, os.O_RDONLY)
|
fd = os.open(env_path, os.O_RDONLY)
|
||||||
n = 300
|
n = 300
|
||||||
|
|||||||
26
db/util/parseOutput.js
Normal file
26
db/util/parseOutput.js
Normal file
@@ -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');
|
||||||
Reference in New Issue
Block a user