attempted to refactor db seed, still needs more updating

This commit is contained in:
Mikayla Dobson
2022-11-26 18:26:32 -06:00
parent 9bd1704da9
commit 9d0c8a8782
22 changed files with 340 additions and 68 deletions

142
server/db/newPopulate.ts Normal file
View File

@@ -0,0 +1,142 @@
import pool from ".";
import { IIngredient, IRecipe, IUser } from "../schemas";
import { User } from "../models/user";
import { Recipe } from "../models/recipe";
import { Collection } from "../models/collection";
import { Ingredient } from "../models/ingredient";
import AuthService from "../auth";
import now from "../util/now";
const auth = new AuthService();
const user = new User();
const collection = new Collection();
const ingredient = new Ingredient();
export default async function populate() {
// register new users; initialize default collection
const usr1: IUser = {
firstname: "Mikayla",
lastname: "Dobson",
handle: "innocuoussymmetry",
password: "coolpassword",
email: "mikayla@mikayla.com",
active: true,
isadmin: true
}
const usr2: IUser = {
firstname: "Emily",
lastname: "Dobson",
handle: "emjdobson",
password: "coolpassword",
email: "emily@emily.com",
active: true,
isadmin: false
}
const usr3: IUser = {
firstname: "Montanna",
lastname: "Dobson",
handle: "delayedlemon",
password: "coolpassword",
email: "montanna@montanna.com",
active: true,
isadmin: false
}
const usr4: IUser = {
firstname: 'someother',
lastname: 'person',
handle: 'someperson',
password: 'coolpassword',
email: 'someperson@email.com',
active: false,
isadmin: false
}
for (let usr of [usr1, usr2, usr3, usr4]) {
await auth.register(usr);
}
/*
// recipes onto new user profiles; insert into new default collections
const rec1: IRecipe = {
name: "Pad Thai",
preptime: '1 hour',
authoruserid: 1
}
const rec2: IRecipe = {
name: "Bo ssam",
preptime: '8 hours',
authoruserid: 3
}
const rec3: IRecipe = {
name: "Banana bread",
preptime: '90 minutes',
authoruserid: 2
}
const rec4: IRecipe = {
name: "garlic confit",
preptime: "2 hours",
authoruserid: 3
}
for (let rec of [rec1, rec2, rec3, rec4]) {
await recipe.post(rec.authoruserid as string, rec);
}
*/
// set up a couple of friendships
// await user.addFriendship(1, 2);
// await user.addFriendship(1, 3);
// await user.addFriendship(2, 3);
// await user.updateFriendship(1, 2, {
// active: true,
// pending: false
// });
// await user.updateFriendship(2, 3, {
// active: true,
// pending: false
// })
// // request and validate subscriptions
// const usr1default = await collection.getUserDefault(1);
// await collection.postSubscription(usr1default.id, 2);
// const usr3default = await collection.getUserDefault(3);
// await collection.postSubscription(usr3default.id, 1);
// add some ingredients
const ing1: IIngredient = {
name: "cucumber",
description: "vegetal",
datecreated: now,
createdbyid: 1
}
const ing2: IIngredient = {
name: "tofu",
description: "soy protein",
datecreated: now,
createdbyid: 1
}
const ing3: IIngredient = {
name: 'garlic',
description: "lifeblood",
datecreated: now,
createdbyid: 3
}
for (let ing of [ing1, ing2, ing3]) {
await ingredient.post(ing);
}
}

View File

@@ -59,7 +59,7 @@ export default async function populate() {
const populateFriendships = `
INSERT INTO recipin.cmp_userfriendships
(datecreated, active, pending, firstuserid, seconduserid)
(datecreated, active, pending, senderid, targetid)
VALUES
($1, true, false, 1, 2),
($1, true, false, 1, 4),

View File

@@ -1,4 +1,5 @@
import dotenv from 'dotenv';
import newPopulate from './newPopulate';
import populate from "./populate";
import pool from ".";
import fs from "fs";
@@ -41,6 +42,7 @@ dotenv.config();
console.log("Database seed successful. Attempting to populate...");
await populate();
// await newPopulate();
process.exit(1);
})();

View File

@@ -4,6 +4,6 @@ CREATE TABLE IF NOT EXISTS recipin.cmp_userfriendships (
active boolean NOT NULL,
pending boolean NOT NULL,
dateterminated varchar,
firstuserid int REFERENCES recipin.appusers (id),
seconduserid int REFERENCES recipin.appusers (id)
senderid int REFERENCES recipin.appusers (id),
targetid int REFERENCES recipin.appusers (id)
);

View File

@@ -1,5 +1,6 @@
CREATE TABLE IF NOT EXISTS recipin.cmp_usersubscriptions (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
collectionid int REFERENCES recipin.collection (id),
usermemberid int REFERENCES recipin.appusers (id)
usermemberid int REFERENCES recipin.appusers (id),
active boolean NOT NULL
);

View File

@@ -5,5 +5,5 @@ CREATE TABLE IF NOT EXISTS recipin.recipe (
datecreated varchar NOT NULL,
datemodified varchar NOT NULL,
description varchar,
authoruserid int REFERENCES recipin.appusers (id) NOT NULL
authoruserid int REFERENCES recipin.appusers (id)
);

View File

@@ -8,6 +8,6 @@ SELECT
recipin.appusers.email
FROM recipin.cmp_userfriendships
INNER JOIN recipin.appusers
ON recipin.appusers.id = recipin.cmp_userfriendships.seconduserid
WHERE firstuserid = $1 OR seconduserid = $1
ON recipin.appusers.id = recipin.cmp_userfriendships.targetid
WHERE senderid = $1 OR targetid = $1
AND cmp_userfriendships.active = true;

View File

@@ -0,0 +1,2 @@
SELECT * FROM recipin.recipe
WHERE authorid = $1;

View File

@@ -8,7 +8,7 @@ SELECT
recipin.appusers.email
FROM recipin.cmp_userfriendships
INNER JOIN recipin.appusers
ON recipin.appusers.id = recipin.cmp_userfriendships.firstuserid
ON recipin.appusers.id = recipin.cmp_userfriendships.senderid
WHERE recipin.cmp_userfriendships.id = $1
UNION
SELECT
@@ -21,5 +21,5 @@ SELECT
recipin.appusers.email
FROM recipin.cmp_userfriendships
INNER JOIN recipin.appusers
ON recipin.appusers.id = recipin.cmp_userfriendships.seconduserid
ON recipin.appusers.id = recipin.cmp_userfriendships.targetid
WHERE recipin.cmp_userfriendships.id = $1;