Creating database model - synthofficial/discord-idle-game-bot-template GitHub Wiki
A Prisma model represents a table in your database. Each model maps to a table and defines the structure of your data.
Here's a simple User model for an idle game:
model User {
userId String @id @unique
level Int @default(1)
experience Int @default(0)
money Int @default(1000)
@@map("users") // This is how the table will be named in your database
}-
userId- Unique identifier for each user (using Discord user ID)-
String- Data type -
@id- Primary key -
@unique- Ensures no duplicates
-
-
level,experience,money- Game stats-
Int- Integer data type -
@default()- Sets initial values when creating a new user
-
-
@@map("users")- Names the actual database table as "users"
For larger projects, you can split your models into separate files inside a schema/ folder:
โโโ src/
โโโ prisma/
โ โโโ schema.prisma
โ โโโ schema/
โ โ โโโ user.prisma
โ โ โโโ guild.prisma
โ โ โโโ inventory.prisma
โ โ โโโ tools.prisma
In your main schema.prisma file, import the separate schema files:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql" # This can be changed to "mysql"
url = env("PRISMA_URL")
}
// Import your models
import "schema/user.prisma"
import "schema/guild.prisma"
import "schema/inventory.prisma"
import "schema/tools.prisma"Note: This keeps your codebase clean and makes it easier to manage multiple models!
| Type | Description | Example Use |
|---|---|---|
String |
Text data | User IDs, names |
Int |
Whole numbers | Level, experience, money |
Boolean |
True/false | Premium status, banned |
DateTime |
Date and time | Created at, last active |
Float |
Decimal numbers | Multipliers, percentages |
Once you've created or modified your models, run:
๐ฆ npm / npx
npm run db:gen # Generate Prisma Client
npm run db:push # Push changes to your database๐ฅ Bun
bun db:gen # Generate Prisma Client
bun db:push # Push changes to your database๐ฆ pnpm
pnpm db:gen # Generate Prisma Client
pnpm db:push # Push changes to your database๐งถ Yarn
yarn db:gen # Generate Prisma Client
yarn db:push # Push changes to your database