More work on beer image upload

patFix schema so beer image and brewery image have createdBy column. Rename 'url' to 'path' in schema, add 'caption' column.
This commit is contained in:
Aaron William Po
2023-02-11 21:42:22 -05:00
parent 45cc10a009
commit 912008e68d
17 changed files with 193 additions and 58 deletions

View File

@@ -0,0 +1,30 @@
/*
Warnings:
- You are about to drop the column `url` on the `BeerImage` table. All the data in the column will be lost.
- You are about to drop the column `url` on the `BreweryImage` table. All the data in the column will be lost.
- Added the required column `caption` to the `BeerImage` table without a default value. This is not possible if the table is not empty.
- Added the required column `path` to the `BeerImage` table without a default value. This is not possible if the table is not empty.
- Added the required column `postedById` to the `BeerImage` table without a default value. This is not possible if the table is not empty.
- Added the required column `caption` to the `BreweryImage` table without a default value. This is not possible if the table is not empty.
- Added the required column `path` to the `BreweryImage` table without a default value. This is not possible if the table is not empty.
- Added the required column `postedById` to the `BreweryImage` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "BeerImage" DROP COLUMN "url",
ADD COLUMN "caption" TEXT NOT NULL,
ADD COLUMN "path" TEXT NOT NULL,
ADD COLUMN "postedById" TEXT NOT NULL;
-- AlterTable
ALTER TABLE "BreweryImage" DROP COLUMN "url",
ADD COLUMN "caption" TEXT NOT NULL,
ADD COLUMN "path" TEXT NOT NULL,
ADD COLUMN "postedById" TEXT NOT NULL;
-- AddForeignKey
ALTER TABLE "BeerImage" ADD CONSTRAINT "BeerImage_postedById_fkey" FOREIGN KEY ("postedById") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "BreweryImage" ADD CONSTRAINT "BreweryImage_postedById_fkey" FOREIGN KEY ("postedById") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@@ -26,6 +26,8 @@ model User {
beerComments BeerComment[]
breweryComments BreweryComment[]
BeerPostLikes BeerPostLike[]
BeerImage BeerImage[]
BreweryImage BreweryImage[]
}
model BeerPost {
@@ -109,18 +111,24 @@ model BeerImage {
id String @id @default(uuid())
beerPost BeerPost @relation(fields: [beerPostId], references: [id], onDelete: Cascade)
beerPostId String
url String
path String
alt String
caption String
createdAt DateTime @default(now()) @db.Timestamptz(3)
updatedAt DateTime? @updatedAt @db.Timestamptz(3)
postedBy User @relation(fields: [postedById], references: [id], onDelete: Cascade)
postedById String
}
model BreweryImage {
id String @id @default(uuid())
breweryPost BreweryPost @relation(fields: [breweryPostId], references: [id], onDelete: Cascade)
breweryPostId String
url String
path String
createdAt DateTime @default(now()) @db.Timestamptz(3)
updatedAt DateTime? @updatedAt @db.Timestamptz(3)
caption String
alt String
postedBy User @relation(fields: [postedById], references: [id], onDelete: Cascade)
postedById String
}

View File

@@ -8,6 +8,11 @@ const cleanDatabase = async () => {
await prisma.$executeRaw`TRUNCATE TABLE "BreweryPost" CASCADE`;
await prisma.$executeRaw`TRUNCATE TABLE "BeerComment" CASCADE`;
await prisma.$executeRaw`TRUNCATE TABLE "BreweryComment" CASCADE`;
await prisma.$executeRaw`TRUNCATE TABLE "BeerPostLike" CASCADE`;
await prisma.$executeRaw`TRUNCATE TABLE "BeerImage" CASCADE`;
await prisma.$executeRaw`TRUNCATE TABLE "BreweryImage" CASCADE`;
await prisma.$disconnect();
};
export default cleanDatabase;

View File

@@ -1,15 +1,19 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { faker } from '@faker-js/faker';
import { BeerPost, BeerImage } from '@prisma/client';
import { BeerPost, BeerImage, User } from '@prisma/client';
import DBClient from '../../DBClient';
interface CreateNewBeerImagesArgs {
numberOfImages: number;
beerPosts: BeerPost[];
joinData: {
beerPosts: BeerPost[];
users: User[];
};
}
const createNewBeerImages = async ({
numberOfImages,
beerPosts,
joinData: { beerPosts, users },
}: CreateNewBeerImagesArgs) => {
const prisma = DBClient.instance;
const createdAt = faker.date.past(1);
@@ -18,12 +22,15 @@ const createNewBeerImages = async ({
// eslint-disable-next-line no-plusplus
for (let i = 0; i < numberOfImages; i++) {
const beerPost = beerPosts[Math.floor(Math.random() * beerPosts.length)];
const user = users[Math.floor(Math.random() * users.length)];
beerImagesPromises.push(
prisma.beerImage.create({
data: {
url: 'https://picsum.photos/900/1600',
path: 'https://picsum.photos/900/1600',
alt: 'Placeholder beer image.',
caption: 'Placeholder beer image caption.',
beerPost: { connect: { id: beerPost.id } },
postedBy: { connect: { id: user.id } },
createdAt,
},
}),

View File

@@ -1,15 +1,19 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { faker } from '@faker-js/faker';
import { BreweryPost, BreweryImage } from '@prisma/client';
import { BreweryPost, BreweryImage, User } from '@prisma/client';
import DBClient from '../../DBClient';
interface CreateBreweryImagesArgs {
numberOfImages: number;
breweryPosts: BreweryPost[];
joinData: {
breweryPosts: BreweryPost[];
users: User[];
};
}
const createNewBreweryImages = async ({
numberOfImages,
breweryPosts,
joinData: { breweryPosts, users },
}: CreateBreweryImagesArgs) => {
const prisma = DBClient.instance;
const createdAt = faker.date.past(1);
@@ -18,13 +22,16 @@ const createNewBreweryImages = async ({
// eslint-disable-next-line no-plusplus
for (let i = 0; i < numberOfImages; i++) {
const breweryPost = breweryPosts[Math.floor(Math.random() * breweryPosts.length)];
const user = users[Math.floor(Math.random() * users.length)];
breweryImagesPromises.push(
prisma.breweryImage.create({
data: {
url: 'https://picsum.photos/900/1600',
path: 'https://picsum.photos/900/1600',
alt: 'Placeholder brewery image.',
caption: 'Placeholder brewery image caption.',
breweryPost: { connect: { id: breweryPost.id } },
postedBy: { connect: { id: user.id } },
createdAt,
},
}),

View File

@@ -54,11 +54,11 @@ import createNewUsers from './create/createNewUsers';
}),
createNewBeerImages({
numberOfImages: 1000,
beerPosts,
joinData: { beerPosts, users },
}),
createNewBreweryImages({
numberOfImages: 1000,
breweryPosts,
joinData: { breweryPosts, users },
}),
]);