Rename "user" column in beerPostLikes, add ERD gen

This commit is contained in:
Aaron William Po
2023-02-13 17:05:00 -05:00
parent 249bfdaf5a
commit ea516e91b5
11 changed files with 2885 additions and 19 deletions

View File

@@ -10,7 +10,7 @@ const useUser = () => {
isLoading, isLoading,
} = useSWR('/api/users/current', async (url) => { } = useSWR('/api/users/current', async (url) => {
if (!document.cookie.includes('token')) { if (!document.cookie.includes('token')) {
return null; return undefined;
} }
const response = await fetch(url); const response = await fetch(url);

2856
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -39,6 +39,7 @@
}, },
"devDependencies": { "devDependencies": {
"@faker-js/faker": "^7.6.0", "@faker-js/faker": "^7.6.0",
"@mermaid-js/mermaid-cli": "^9.3.0",
"@types/cookie": "^0.5.1", "@types/cookie": "^0.5.1",
"@types/multer": "^1.4.7", "@types/multer": "^1.4.7",
"@types/node": "^18.13.0", "@types/node": "^18.13.0",
@@ -59,6 +60,7 @@
"prettier-plugin-jsdoc": "^0.4.2", "prettier-plugin-jsdoc": "^0.4.2",
"prettier-plugin-tailwindcss": "^0.2.2", "prettier-plugin-tailwindcss": "^0.2.2",
"prisma": "^4.9.0", "prisma": "^4.9.0",
"prisma-erd-generator": "^1.2.5",
"tailwindcss": "^3.2.4", "tailwindcss": "^3.2.4",
"ts-node": "^10.9.1", "ts-node": "^10.9.1",
"typescript": "^4.9.5" "typescript": "^4.9.5"

View File

@@ -18,7 +18,7 @@ const checkIfLiked = async (
const alreadyLiked = await DBClient.instance.beerPostLike.findFirst({ const alreadyLiked = await DBClient.instance.beerPostLike.findFirst({
where: { where: {
beerPostId: id, beerPostId: id,
userId: user.id, likedById: user.id,
}, },
}); });

View File

@@ -33,6 +33,7 @@ const RegisterUserPage: NextPage<RegisterUserProps> = () => {
const onSubmit = async (data: z.infer<typeof CreateUserValidationSchema>) => { const onSubmit = async (data: z.infer<typeof CreateUserValidationSchema>) => {
try { try {
await sendRegisterUserRequest(data); await sendRegisterUserRequest(data);
reset();
router.push('/', undefined, { shallow: true }); router.push('/', undefined, { shallow: true });
} catch (error) { } catch (error) {
setServerResponseError( setServerResponseError(

1
prisma/ERD.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 104 KiB

View File

@@ -0,0 +1,16 @@
/*
Warnings:
- You are about to drop the column `userId` on the `BeerPostLike` table. All the data in the column will be lost.
- Added the required column `likedById` to the `BeerPostLike` table without a default value. This is not possible if the table is not empty.
*/
-- DropForeignKey
ALTER TABLE "BeerPostLike" DROP CONSTRAINT "BeerPostLike_userId_fkey";
-- AlterTable
ALTER TABLE "BeerPostLike" DROP COLUMN "userId",
ADD COLUMN "likedById" TEXT NOT NULL;
-- AddForeignKey
ALTER TABLE "BeerPostLike" ADD CONSTRAINT "BeerPostLike_likedById_fkey" FOREIGN KEY ("likedById") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@@ -5,6 +5,13 @@ generator client {
provider = "prisma-client-js" provider = "prisma-client-js"
} }
generator erdSVG {
provider = "prisma-erd-generator"
output = "./ERD.svg"
includeRelationFromFields = true
theme = "neutral"
}
datasource db { datasource db {
provider = "postgresql" provider = "postgresql"
url = env("DATABASE_URL") url = env("DATABASE_URL")
@@ -53,8 +60,8 @@ model BeerPostLike {
id String @id @default(uuid()) id String @id @default(uuid())
beerPost BeerPost @relation(fields: [beerPostId], references: [id], onDelete: Cascade) beerPost BeerPost @relation(fields: [beerPostId], references: [id], onDelete: Cascade)
beerPostId String beerPostId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade) likedBy User @relation(fields: [likedById], references: [id], onDelete: Cascade)
userId String likedById String
createdAt DateTime @default(now()) @db.Timestamptz(3) createdAt DateTime @default(now()) @db.Timestamptz(3)
updatedAt DateTime? @updatedAt @db.Timestamptz(3) updatedAt DateTime? @updatedAt @db.Timestamptz(3)
} }

View File

@@ -22,7 +22,7 @@ const createNewBeerPostLikes = async ({
DBClient.instance.beerPostLike.create({ DBClient.instance.beerPostLike.create({
data: { data: {
beerPost: { connect: { id: beerPost.id } }, beerPost: { connect: { id: beerPost.id } },
user: { connect: { id: user.id } }, likedBy: { connect: { id: user.id } },
}, },
}), }),
); );

View File

@@ -12,7 +12,7 @@ const createBeerPostLike = async ({
return DBClient.instance.beerPostLike.create({ return DBClient.instance.beerPostLike.create({
data: { data: {
beerPost: { connect: { id } }, beerPost: { connect: { id } },
user: { connect: { id: user.id } }, likedBy: { connect: { id: user.id } },
}, },
}); });
}; };

View File

@@ -1,11 +1,6 @@
import DBClient from '@/prisma/DBClient'; import DBClient from '@/prisma/DBClient';
const findBeerPostLikeById = async (beerPostId: string, userId: string) => const findBeerPostLikeById = async (beerPostId: string, likedById: string) =>
DBClient.instance.beerPostLike.findFirst({ DBClient.instance.beerPostLike.findFirst({ where: { beerPostId, likedById } });
where: {
beerPostId,
userId,
},
});
export default findBeerPostLikeById; export default findBeerPostLikeById;