CHAPTER 1Setting Up the Database
-
Create a project with
bun init .
-
Edit
package.json
: version, description, etc. -
Install packages:
bun i express morgan cors bun i -D @types/express @types/cors @types/morgan prisma
-
Initialize Prisma:
bunx prisma init --datasource-provider sqlite
-
Check that
.gitignore
contains.env
(it should with Bun). -
Edit
schema.prisma
to add theForum
entity:model Forum { id Int @id @default(autoincrement()) name String @unique createdAt DateTime @default(now()) }
-
Push the
schema.prisma
to the database to apply changes:bunx prisma db push
-
Open Prisma Studio (
bunx prisma studio
) and add a couple of Forums to check. -
Add two more entities:
User
andMessage
.model User { userId Int @id @default(autoincrement()) @map("id") nick String @unique fullName String? createdAt DateTime @default(now()) admin Boolean @default(false) messages Message[] }
model Message { id Int @id @default(autoincrement()) text String userId Int User User @relation(fields: [userId], references: [userId]) forumId Int Forum Forum @relation(fields: [forumId], references: [forumId]) }
-
Push changes in the schema to the database.
-
Create a
prisma/seed.ts
file and add test data that you would like to have in the initial database (after a reset). -
Add the
"prisma"
section to thepackage.json
file to invoke the seeding process:{ // ... "prisma": { "seed": "bun prisma/seed.ts" } }
-
Now reset the database to check that it can be re-created from scratch:
rm prisma/dev.db bunx prisma db push bunx prisma db seed