Skip to content

Commit

Permalink
Use nanoid for notebookId
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianUribe6 committed Feb 29, 2024
1 parent 9e6e3a9 commit 9a5aa12
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
23 changes: 13 additions & 10 deletions apps/front-end/src/actions/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
});
Expand All @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion apps/front-end/src/app/[notebookId]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion apps/front-end/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 9a5aa12

Please sign in to comment.