updates on database seed, popluates example vals

This commit is contained in:
Mikayla Dobson
2022-11-19 11:44:32 -06:00
parent 811158ec61
commit c6156e237e
5 changed files with 80 additions and 11 deletions

View File

@@ -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<string> = [
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.");
}

View File

@@ -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);
})();

10
server/db/subquery.ts Normal file
View File

@@ -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 + ')'
}

View File

@@ -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"

View File

@@ -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"]
}