From 9a5aa129a5597c4f381f21fe46e55a195f8b4edf Mon Sep 17 00:00:00 2001 From: brianuribe6 Date: Wed, 28 Feb 2024 19:10:42 -0500 Subject: [PATCH] Use nanoid for notebookId --- apps/front-end/src/actions/document.ts | 23 +++++++++++-------- .../front-end/src/app/[notebookId]/layout.tsx | 2 +- apps/front-end/src/schema.ts | 2 +- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/apps/front-end/src/actions/document.ts b/apps/front-end/src/actions/document.ts index 53ed26f..e3db83b 100644 --- a/apps/front-end/src/actions/document.ts +++ b/apps/front-end/src/actions/document.ts @@ -47,17 +47,20 @@ async function createNotebook() { } return db.transaction(async (tx) => { - const name = nanoid(); - await tx.insert(documents).values({ name }); - - const [res] = await tx.insert(notebooks).values({ - documentName: name, - authorId: session.user.id, - }); + const documentName = nanoid(); + const notebookId = nanoid(); + await Promise.all([ + tx.insert(documents).values({ name: documentName }), + tx.insert(notebooks).values({ + id: notebookId, + documentName: documentName, + authorId: session.user.id, + }), + ]); return { - id: res.insertId, - documentName: name, + id: notebookId, + documentName: documentName, authorId: session.user.id, }; }); @@ -80,7 +83,7 @@ async function getNotebook() { return null; } -async function getNotebookById(id: number) { +async function getNotebookById(id: string) { const session = await getServerSession(authOptions); if (!session) { throw new Error("Unauthorized"); diff --git a/apps/front-end/src/app/[notebookId]/layout.tsx b/apps/front-end/src/app/[notebookId]/layout.tsx index 6a5cd53..2666a8e 100644 --- a/apps/front-end/src/app/[notebookId]/layout.tsx +++ b/apps/front-end/src/app/[notebookId]/layout.tsx @@ -16,7 +16,7 @@ type LayoutProps = PropsWithChildren<{ async function Layout({ children, params }: LayoutProps) { const session = await getSession(); - const notebook = await getNotebookById(Number(params.notebookId)); + const notebook = await getNotebookById(params.notebookId); if (!notebook) { return notFound(); diff --git a/apps/front-end/src/schema.ts b/apps/front-end/src/schema.ts index 4123333..e6c3272 100644 --- a/apps/front-end/src/schema.ts +++ b/apps/front-end/src/schema.ts @@ -82,7 +82,7 @@ export const documents = mysqlTable("document", { }); export const notebooks = mysqlTable("notebook", { - id: int("id").autoincrement().notNull().primaryKey(), + id: char("id", { length: 21 }).notNull().primaryKey(), documentName: char("documentName", { length: 21 }) .notNull() .references(() => documents.name),