Refactored schema and added link model.

This commit is contained in:
Daniel 2023-02-14 03:39:13 +03:30
parent cefeb5e7a9
commit d19204f4c0
10 changed files with 101 additions and 79 deletions

View File

@ -27,15 +27,11 @@ export default function Layout({ children }: Props) {
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="/favicon.ico" /> <link rel="icon" href="/favicon.ico" />
</Head> </Head>
<div className="flex">
<Sidebar /> <Sidebar />
<div className="ml-80 w-full"> <div className="ml-80 w-full">
<div className="mx-auto w-full">
<Navbar /> <Navbar />
{children} {children}
</div> </div>
</div>
</div>
</> </>
); );
else if ((status === "unauthenticated" && !redirection) || !routeExists) else if ((status === "unauthenticated" && !redirection) || !routeExists)

View File

@ -1,9 +1,9 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
interface Collections { interface Collections {
id: any; id: number;
name: string; name: string;
role: string; createdAt: string;
} }
export default function Collections() { export default function Collections() {

View File

@ -14,7 +14,7 @@ export const authOptions: AuthOptions = {
credentials: {}, credentials: {},
async authorize(credentials, req) { async authorize(credentials, req) {
const { email, password } = credentials as { const { email, password } = credentials as {
id: string; id: number;
email: string; email: string;
password: string; password: string;
}; };
@ -50,7 +50,7 @@ export const authOptions: AuthOptions = {
}, },
callbacks: { callbacks: {
session: async ({ session, token }) => { session: async ({ session, token }) => {
session.user.id = token?.sub; session.user.id = parseInt(token?.sub as any);
return session; return session;
}, },

View File

@ -38,18 +38,11 @@ export default async function handler(
email: body.email, email: body.email,
password: hashedPassword, password: hashedPassword,
collections: { collections: {
create: [
{
role: "owner",
collection: {
create: { create: {
name: "First Collection", name: "First Collection",
}, },
}, },
}, },
],
},
},
}); });
res.status(201).json({ message: "User successfully created." }); res.status(201).json({ message: "User successfully created." });

View File

@ -24,16 +24,12 @@ export default async function handler(
email: email, email: email,
}, },
include: { include: {
collections: { collections: true,
include: {
collection: true,
},
},
}, },
}); });
const collections = findCollection?.collections.map((e) => { const collections = findCollection?.collections.map((e) => {
return { id: e.collection.id, name: e.collection.name, role: e.role }; return { id: e.id, name: e.name, createdAt: e.createdAt };
}); });
// console.log(session?.user?.email); // console.log(session?.user?.email);

View File

@ -30,17 +30,17 @@ export default async function handler(
where: { where: {
email, email,
}, },
include: { select: {
collections: { collections: {
where: { where: {
collection: {
name: collectionName, name: collectionName,
}, },
}, },
}, },
},
}); });
console.log(typeof session.user.id);
const checkIfCollectionExists = findCollection?.collections[0]; const checkIfCollectionExists = findCollection?.collections[0];
if (checkIfCollectionExists) { if (checkIfCollectionExists) {
@ -69,13 +69,8 @@ export default async function handler(
collections: { collections: {
create: [ create: [
{ {
role: "owner",
collection: {
create: {
name: collectionName, name: collectionName,
}, },
},
},
], ],
}, },
}, },

View File

@ -1,32 +0,0 @@
-- CreateTable
CREATE TABLE "User" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"email" TEXT NOT NULL,
"password" TEXT NOT NULL,
CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "Collection" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
CONSTRAINT "Collection_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "UserAndCollection" (
"userId" TEXT NOT NULL,
"collectionId" TEXT NOT NULL,
"role" TEXT NOT NULL,
CONSTRAINT "UserAndCollection_pkey" PRIMARY KEY ("userId","collectionId")
);
-- AddForeignKey
ALTER TABLE "UserAndCollection" ADD CONSTRAINT "UserAndCollection_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "UserAndCollection" ADD CONSTRAINT "UserAndCollection_collectionId_fkey" FOREIGN KEY ("collectionId") REFERENCES "Collection"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

View File

@ -0,0 +1,57 @@
-- CreateTable
CREATE TABLE "User" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"email" TEXT NOT NULL,
"password" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "Collection" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"ownerId" INTEGER NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "Collection_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "UserAndCollection" (
"userId" INTEGER NOT NULL,
"collectionId" INTEGER NOT NULL,
"canCreate" BOOLEAN NOT NULL,
"canRead" BOOLEAN NOT NULL,
"canUpdate" BOOLEAN NOT NULL,
"canDelete" BOOLEAN NOT NULL,
CONSTRAINT "UserAndCollection_pkey" PRIMARY KEY ("userId","collectionId")
);
-- CreateTable
CREATE TABLE "Link" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"url" TEXT NOT NULL,
"collectionId" INTEGER NOT NULL,
CONSTRAINT "Link_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
-- AddForeignKey
ALTER TABLE "Collection" ADD CONSTRAINT "Collection_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "UserAndCollection" ADD CONSTRAINT "UserAndCollection_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "UserAndCollection" ADD CONSTRAINT "UserAndCollection_collectionId_fkey" FOREIGN KEY ("collectionId") REFERENCES "Collection"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Link" ADD CONSTRAINT "Link_collectionId_fkey" FOREIGN KEY ("collectionId") REFERENCES "Collection"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

View File

@ -11,27 +11,44 @@ datasource db {
} }
model User { model User {
id String @id @default(cuid()) id Int @id @default(autoincrement())
name String name String
email String email String @unique
password String password String
collections UserAndCollection[] collections Collection[]
collectionsJoined UserAndCollection[]
createdAt DateTime @default(now())
} }
model Collection { model Collection {
id String @id @default(cuid()) id Int @id @default(autoincrement())
name String name String
users UserAndCollection[] owner User @relation(fields: [ownerId], references: [id])
ownerId Int
members UserAndCollection[]
links Link[]
createdAt DateTime @default(now())
} }
model UserAndCollection { model UserAndCollection {
user User @relation(fields: [userId], references: [id]) user User @relation(fields: [userId], references: [id])
userId String userId Int
collection Collection @relation(fields: [collectionId], references: [id]) collection Collection @relation(fields: [collectionId], references: [id])
collectionId String collectionId Int
role String canCreate Boolean
canRead Boolean
canUpdate Boolean
canDelete Boolean
@@id([userId, collectionId]) @@id([userId, collectionId])
} }
model Link {
id Int @id @default(autoincrement())
name String
url String
collection Collection @relation(fields: [collectionId], references: [id])
collectionId Int
}

View File

@ -3,7 +3,7 @@ import NextAuth, { DefaultSession } from "next-auth";
declare module "next-auth" { declare module "next-auth" {
interface Session { interface Session {
user: { user: {
id: any; id: number;
} & DefaultSession["user"]; } & DefaultSession["user"];
} }
} }