Skip to content

Commit

Permalink
add seeding script and pagination adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
bercivarga committed Mar 12, 2024
1 parent 40983f3 commit d2df948
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 9 deletions.
6 changes: 3 additions & 3 deletions app/(platform)/notes/[noteId]/note-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default function NoteActions({ note }: Props) {

return (
<>
<div className="flex w-80 flex-col bg-slate-100/50 p-6 text-sm text-slate-400">
<div className="flex h-full w-80 flex-col overflow-y-scroll bg-slate-100/50 p-6 text-sm text-slate-400">
<div className="flex flex-col items-stretch gap-4">
<span className="block">Connected notes</span>
{noteRelations.length === 0 ? (
Expand All @@ -83,8 +83,8 @@ export default function NoteActions({ note }: Props) {
variant={"link"}
size={"sm"}
>
{relatedNote.title.length > 32
? `${relatedNote.title.slice(0, 32)}...`
{relatedNote.title.length > 20
? `${relatedNote.title.slice(0, 20)}...`
: relatedNote.title}
</Button>
</Link>
Expand Down
13 changes: 8 additions & 5 deletions app/(platform)/notes/notes-pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default function NotesPagination({

const prevPageHref = `${path}?page=${currentPage - 1}`;
const nextPageHref = `${path}?page=${currentPage + 1}`;
const lastPageHref = `${path}?page=${totalPages}`;

return (
<Pagination>
Expand All @@ -51,18 +52,20 @@ export default function NotesPagination({
<PaginationItem className="pointer-events-none opacity-50">
<PaginationLink href="#">{currentPage}</PaginationLink>
</PaginationItem>
{totalPages && totalPages > currentPage + 2 && (
<PaginationItem>
<PaginationEllipsis />
</PaginationItem>
)}
{isThereANextPage && (
<>
<PaginationItem>
<PaginationLink href={nextPageHref}>
{currentPage + 1}
</PaginationLink>
</PaginationItem>
{totalPages && totalPages > currentPage + 2 && (
<PaginationItem>
<PaginationLink href={lastPageHref}>
<PaginationEllipsis />
</PaginationLink>
</PaginationItem>
)}
<PaginationItem>
<PaginationNext href={nextPageHref} />
</PaginationItem>
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"test": "vitest --passWithNoTests",
"lint": "next lint --fix",
"prepare": "husky",
"prettier": "prettier --write ."
"prettier": "prettier --write .",
"seed": "npx ts-node prisma/seed.ts"
},
"dependencies": {
"@clerk/nextjs": "^4.29.9",
Expand Down
82 changes: 82 additions & 0 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const { PrismaClient } = require("@prisma/client");

const prisma = new PrismaClient();

const topics = [
"concerts",
"painting",
"cat",
"creative coding",
"groceries",
"cooking",
"planning trips",
"running",
"bouldering",
"listening to and discovering new music",
"reading",
"socialising",
"buying gifts",
"shopping for clothes",
"watching movies",
];

const TEST_USER_ID = process.env.TEST_USER_ID;
const SEED_COUNT = 200;

async function seedDatabase() {
console.log(`Seeding database for user ${TEST_USER_ID}...`);

try {
// Clear current notes
await prisma.note.deleteMany({
where: {
authorId: TEST_USER_ID,
},
});

for (let i = 0; i < SEED_COUNT; i++) {
const title = topics[Math.floor(Math.random() * topics.length)];
const content =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
const authorId = TEST_USER_ID as string;

// Randomly decide if the note should connect to others
let relatedNotes = undefined;
if (Math.random() < 0.8) {
// Determine how many notes to connect to (1-5 randomly)
const numRelated = Math.floor(Math.random() * 5) + 1;
// Find numRelated random notes that already exist in the database
const randomNotes = await prisma.note.findMany({
take: numRelated,
skip: Math.floor(Math.random() * i), // Randomly skip existing notes
});
relatedNotes = {
// @ts-ignore
connect: randomNotes.map((note) => ({ id: note.id })),
};
}

const note = await prisma.note.create({
data: {
title: title,
content: content,
authorId: authorId,
relatedNotes: relatedNotes,
},
});

console.log(
`[${i + 1} / ${SEED_COUNT}] - `,
`Created note with id: ${note.id}`
);
}

console.log("Database seeded successfully.");
} catch (error) {
console.error("Error seeding database:", error);
} finally {
await prisma.$disconnect();
}
}

seedDatabase();

0 comments on commit d2df948

Please sign in to comment.