From c6156e237e221700c2f5e846401ec22225edfe45 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Sat, 19 Nov 2022 11:44:32 -0600 Subject: [PATCH] updates on database seed, popluates example vals --- server/db/examplevals.ts | 52 ++++++++++++++++++++++++++++++++++++++++ server/db/seed.ts | 14 +++++++---- server/db/subquery.ts | 10 ++++++++ server/package.json | 1 + server/schemas/index.ts | 14 +++++------ 5 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 server/db/subquery.ts diff --git a/server/db/examplevals.ts b/server/db/examplevals.ts index e69de29..957d120 100644 --- a/server/db/examplevals.ts +++ b/server/db/examplevals.ts @@ -0,0 +1,52 @@ +import pool from '.'; + +export default async function populate() { + const setup = `SET ROLE postgres` + + const populateUsers = ` + INSERT INTO recipin.appusers + (firstname, lastname, handle, email, password, active) + 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) + ; + ` + + const populateRecipes = ` + INSERT INTO recipin.recipe + (name, description, preptime, authoruserid) + 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) + ; + ` + + const populateCollection = ` + INSERT INTO recipin.collection + (name, active, ismaincollection, ownerid) + VALUES + ('Mikayla''s collection', true, true, 1), + ('Emily''s collection', true, true, 2) + ; + ` + + const allStatements: Array = [ + setup, populateUsers, populateRecipes, populateCollection + ] + + for (let s of allStatements) { + try { + await pool.query(s); + } catch(e: any) { + console.log('Last executed: ' + s); + throw new Error(e); + } + } + + console.log("Example values inserted successfully. DB seed complete."); +} \ No newline at end of file diff --git a/server/db/seed.ts b/server/db/seed.ts index adca384..1408f52 100644 --- a/server/db/seed.ts +++ b/server/db/seed.ts @@ -1,5 +1,7 @@ import { Client } from 'pg'; import dotenv from 'dotenv'; +import populate from "./examplevals"; + dotenv.config(); (async function() { @@ -14,9 +16,10 @@ dotenv.config(); id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, firstname varchar NOT NULL, lastname varchar NOT NULL, - handle varchar NOT NULL, - email varchar NOT NULL, - password varchar NOT NULL + handle varchar NOT NULL UNIQUE, + email varchar NOT NULL UNIQUE, + password varchar NOT NULL, + active boolean NOT NULL ); ` @@ -97,5 +100,8 @@ dotenv.config(); throw new Error(e); } - console.log("Database seed successful."); + console.log("Database seed successful. Attempting to populate..."); + await populate(); + + process.exit(1); })(); \ No newline at end of file diff --git a/server/db/subquery.ts b/server/db/subquery.ts new file mode 100644 index 0000000..5c71704 --- /dev/null +++ b/server/db/subquery.ts @@ -0,0 +1,10 @@ +import { QueryResult } from "pg"; +import pool from "./"; + +export default function subquery(table: string, target: string, value: string | number): string { + // const statement = `SELECT $1 FROM $2 WHERE $1 = $3` + // const values = [target, table, value]; + // return await pool.query(statement, values); + + return '(SELECT ' + target + ' FROM ' + table + ' WHERE ' + target + ' = ' + value + ')' +} \ No newline at end of file diff --git a/server/package.json b/server/package.json index c1798aa..f3e8ee1 100644 --- a/server/package.json +++ b/server/package.json @@ -6,6 +6,7 @@ "scripts": { "build": "rm -rf dist && ./node_modules/.bin/tsc --project ./tsconfig.json", "seed": "npm run build && ts-node-dev db/seed.ts", + "populate": "npm run build && node dist/db/examplevals.js", "dev": "rm -rf dist && ./node_modules/.bin/tsc --project ./tsconfig.json --watch & ts-node-dev index.ts", "prod": "npm run build && node dist/index.js", "test": "echo \"Error: no test specified\" && exit 1" diff --git a/server/schemas/index.ts b/server/schemas/index.ts index 903c8a7..7809fb8 100644 --- a/server/schemas/index.ts +++ b/server/schemas/index.ts @@ -1,5 +1,5 @@ export interface IUser { - id: string | number + id: number firstname: string lastname: string handle: string @@ -9,7 +9,7 @@ export interface IUser { } export interface IRecipe { - id: string | number + id: number name: string description?: string preptime: string @@ -18,23 +18,23 @@ export interface IRecipe { } export interface IIngredient { - id: string | number + id: number name: string description?: string } export interface ICollection { - id: string | number + id: number name: string active: string ismaincollection: boolean - owner: IUser["id"] + ownerid: IUser["id"] } export interface IGroceryList { - id: string | number - owner: IUser + id: number listname: string recipes?: IRecipe["id"][] active: boolean + ownerid: IUser["id"] } \ No newline at end of file