more database updates

This commit is contained in:
Mikayla Dobson
2022-11-19 17:37:07 -06:00
parent e5e3d9a761
commit f35ed55806
2 changed files with 84 additions and 33 deletions

View File

@@ -1,47 +1,83 @@
import pool from '.';
export default async function populate() {
const now = new Intl.DateTimeFormat('en-US', { dateStyle: 'medium', timeStyle: 'long' }).format(Date.now())
const setup = `SET ROLE postgres`
const populateUsers = `
INSERT INTO recipin.appusers
(firstname, lastname, handle, email, password, active)
(firstname, lastname, handle, email, password, active, dateregistered, datelastactive)
VALUES
('Mikayla', 'Dobson', 'innocuoussymmetry', 'mikaylaherself@gmail.com', 'password1', true),
('Emily', 'Dobson', 'emjdobson', 'emily@email.com', 'password2', true),
('Montanna', 'Dobson', 'delayedlemon', 'montanna@email.com', 'password3', true),
('Christine', 'Riley', 'christine', 'christine@email.com', 'password4', true),
('Someone', 'Not active', 'someone', 'someone@email.com', 'notactive', false)
('Mikayla', 'Dobson', 'innocuoussymmetry', 'mikaylaherself@gmail.com', 'password1', true, $1, $1),
('Emily', 'Dobson', 'emjdobson', 'emily@email.com', 'password2', true, $1, $1),
('Montanna', 'Dobson', 'delayedlemon', 'montanna@email.com', 'password3', true, $1, $1),
('Christine', 'Riley', 'christine', 'christine@email.com', 'password4', true, $1, $1),
('Someone', 'Not active', 'someone', 'someone@email.com', 'notactive', false, $1, $1)
;
`
const populateRecipes = `
INSERT INTO recipin.recipe
(name, description, preptime, authoruserid)
(name, description, preptime, authoruserid, datecreated, datemodified)
VALUES
('Pad Thai', 'noodles', '1 hour', 1),
('Tacos', null, '30 minutes', 1),
('Garlic knots', null, '1 hour', 4),
('Cacio e pepe', 'stinky pasta', '1 hour', 3)
('Pad Thai', 'noodles', '1 hour', 1, $1, $1),
('Tacos', null, '30 minutes', 1, $1, $1),
('Garlic knots', null, '1 hour', 4, $1, $1),
('Cacio e pepe', 'stinky pasta', '1 hour', 3, $1, $1)
;
`
const populateCollection = `
INSERT INTO recipin.collection
(name, active, ismaincollection, ownerid)
(name, active, ismaincollection, ownerid, datecreated, datemodified)
VALUES
('Mikayla''s collection', true, true, 1),
('Emily''s collection', true, true, 2)
('Mikayla''s collection', true, true, 1, $1, $1),
('Emily''s collection', true, true, 2, $1, $1)
;
`
const populateIngredients = `
INSERT INTO recipin.ingredient
(name, description, datecreated, createdbyid)
VALUES
('cucumber', 'vegetal', $1, 1),
('tofu', 'soy protein', $1, 1),
('cilantro', 'aromatic culinary herb', $1, 1),
('coffee', 'lifeblood', $1, 1)
;
`
const populateGroceryList = `
INSERT INTO recipin.grocerylist
(name, active, datecreated, datemodified, ownerid)
VALUES
('Mikayla List 1', true, $1, $1, 1),
('Mikayla List 2', true, $1, $1, 1),
('Mom List 1', true, $1, $1, 2)
;
`
const populateFriendships = `
INSERT INTO recipin.cmp_userfriendships
(datecreated, active, firstuserid, seconduserid)
VALUES
($1, true, 1, 2),
($1, true, 1, 4),
($1, true, 2, 3),
($1, true, 1, 3)
;
`
const allStatements: Array<string> = [
setup, populateUsers, populateRecipes, populateCollection
]
populateUsers, populateRecipes, populateCollection,
populateIngredients, populateGroceryList, populateFriendships
];
await pool.query(setup);
for (let s of allStatements) {
try {
await pool.query(s);
await pool.query(s, [now]);
} catch(e: any) {
console.log('Last executed: ' + s);
throw new Error(e);

View File

@@ -1,6 +1,7 @@
import { Client } from 'pg';
import dotenv from 'dotenv';
import populate from "./examplevals";
import pool from ".";
dotenv.config();
@@ -19,7 +20,9 @@ dotenv.config();
handle varchar NOT NULL UNIQUE,
email varchar NOT NULL UNIQUE,
password varchar NOT NULL,
active boolean NOT NULL
active boolean NOT NULL,
dateregistered varchar NOT NULL,
datelastactive varchar NOT NULL
);
`
@@ -27,7 +30,9 @@ dotenv.config();
CREATE TABLE IF NOT EXISTS recipin.ingredient (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name varchar NOT NULL,
description varchar
description varchar,
datecreated varchar NOT NULL,
createdbyid int REFERENCES recipin.appusers (id)
);
`
@@ -37,6 +42,8 @@ dotenv.config();
name varchar NOT NULL,
active boolean NOT NULL,
ismaincollection boolean NOT NULL,
datecreated varchar NOT NULL,
datemodified varchar NOT NULL,
ownerid int REFERENCES recipin.appusers (id)
);
`
@@ -46,6 +53,8 @@ dotenv.config();
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name varchar NOT NULL,
active boolean NOT NULL,
datecreated varchar NOT NULL,
datemodified varchar NOT NULL,
ownerid int REFERENCES recipin.appusers (id)
);
`
@@ -55,10 +64,10 @@ dotenv.config();
CREATE TABLE IF NOT EXISTS recipin.recipe (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name varchar NOT NULL,
preptime varchar NOT NULL,
datecreated varchar NOT NULL,
datemodified varchar NOT NULL,
description varchar,
preptime varchar,
datecreated varchar,
datemodified varchar,
authoruserid int REFERENCES recipin.appusers (id) NOT NULL
);
`
@@ -66,8 +75,8 @@ dotenv.config();
const recipeingredient = `
CREATE TABLE IF NOT EXISTS recipin.cmp_recipeingredient (
recipeingredientid INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
quantity decimal,
unit varchar,
quantity decimal NOT NULL,
unit varchar NOT NULL,
ingredientid int REFERENCES recipin.ingredient (id),
recipeid int REFERENCES recipin.recipe (id),
collectionid int REFERENCES recipin.collection (id)
@@ -82,21 +91,27 @@ dotenv.config();
);
`;
const allStatements = [
setRole, appusers, ingredient, collection, recipe, groceryList, recipeingredient, userscollections
]
const userfriendships = `
CREATE TABLE IF NOT EXISTS recipin.cmp_userfriendships (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
datecreated varchar NOT NULL,
active boolean NOT NULL,
dateterminated varchar,
firstuserid int REFERENCES recipin.appusers (id),
seconduserid int REFERENCES recipin.appusers (id)
);
`
const client = new Client({ connectionString: process.env.CONSTRING });
await client.connect().then(() => console.log('connected to pg. seeding...'));
const allStatements = [
setRole, appusers, ingredient, collection, recipe,
groceryList, recipeingredient, userscollections, userfriendships
]
try {
for (let s of allStatements) {
await client.query(s);
await pool.query(s);
}
await client.end();
} catch(e: any) {
await client.end();
throw new Error(e);
}