Database architecture
The database is structured to:- Store user authentication data (email/password and OAuth)
- Track game state and synchronize with on-chain data
- Maintain player statistics and bid history
- Handle OTP verification for email authentication
- Store off-chain metadata and relationships
The database works alongside the Solana program. Game-critical data is stored on-chain, while user accounts and metadata are stored off-chain in PostgreSQL.
Schema overview
The database schema is defined innext-app/prisma/schema.prisma and consists of six main models:
User model
Stores user account information and authentication data.Field descriptions
Field descriptions
- id: Auto-incrementing primary key
- email: Unique email address (required)
- name: Display name (optional)
- password: Hashed password for email/password auth (optional, null for OAuth users)
- imageUrl: Profile image URL from Cloudinary or OAuth provider
- provider: OAuth provider name (“google”, null for email/password)
- players: One-to-many relationship with Player model
OTP model
Temporary storage for one-time passwords used in email verification.OTP records should be cleaned up periodically. Consider implementing a cron job to delete records older than 10-15 minutes.
Game model
Stores game state synchronized with the on-chain Solana program.Field descriptions
Field descriptions
- gameId: Unique game identifier
- pda: Program Derived Address for the game on Solana
- initialBidAmount: Starting bid amount in lamports
- highestBid: Current highest bid amount
- lastBidTime: Timestamp of the last bid
- totalBids: Total number of bids placed
- lastBidderId: Public key of the last bidder
- prizePool: Total prize pool in lamports
- platformFeePercent: Platform fee percentage
- gameEnded: Whether the game has ended
- players: One-to-many relationship with Player model
Player model
Tracks individual player participation in games.Field descriptions
Field descriptions
- playerPubkey: Player’s Solana wallet public key
- pda: Player’s Program Derived Address
- totalBidAmount: Total amount bid by this player
- safe: Whether the player is marked as “safe”
- royaltyEarned: Royalties earned by the player
- bidCount: Number of bids placed
- role: Player role enum (PLAYER, WINNER, or FINISHER)
- userId: Foreign key to User model
- gameId: Foreign key to Game model
- bid: One-to-one relationship with Bid model
Bid model
Stores individual bid transactions.Field descriptions
Field descriptions
- pda: Bid’s Program Derived Address
- amount: Bid amount in lamports
- timestamp: When the bid was placed
- playerId: Foreign key to Player (one-to-one relationship)
- txId: Solana transaction ID (optional)
GameId model
Stores the current game ID counter.This model maintains a single record that tracks the current game ID. It’s initialized with ID 1 during database seeding.
PlayerRole enum
Running migrations
Prisma manages database schema changes through migrations.Development migrations
Create a migration
When you modify This will:
schema.prisma, create a new migration:- Create a new migration file in
prisma/migrations/ - Apply the migration to your database
- Regenerate the Prisma client
Production migrations
Existing migrations
The project includes these migrations:20241014200533_restucture- Initial schema restructure20241030184217_add_txid- Added transaction ID to Bid model20241031194616_remove_name_unique_constrant- Removed unique constraint from User name
Seeding the database
The seed script initializes theGameId table with a starting value.
Seed script
Running the seed
The seed script uses
upsert to avoid duplicate entries. You can run it multiple times safely.Prisma client
The Prisma client is automatically generated from the schema.Regenerate the client
When to regenerate
When to regenerate
The client is automatically regenerated:
- After running
pnpm install(postinstall hook) - After running
pnpm run build(prebuild hook) - After running
prisma migrate dev
schema.prisma without running migrations.Using the client
The project includes a singleton Prisma client instance:Prisma Studio
Prisma Studio provides a visual database browser.- View and edit database records
- Run queries
- Explore relationships
- Test data integrity
Database management tasks
Reset the database
Reset the database
Warning: This deletes all data!This will:
- Drop the database
- Create a new database
- Apply all migrations
- Run the seed script
View current schema
View current schema
schema.prisma to match.Validate schema
Validate schema
schema.prisma.Format schema
Format schema
schema.prisma file.Database connection
The database connection is configured via theDATABASE_URL environment variable in .env:
Best practices
- Always create migrations for schema changes, never edit the database directly
- Use descriptive migration names:
add_user_avatar, notupdate1 - Review generated SQL before applying to production
- Back up production databases before running migrations
- Use Prisma Studio for development debugging, not production data editing
- Index frequently queried fields (Prisma handles some automatically)
- Use transactions for operations that modify multiple tables
Next steps
- Complete the local development setup
- Configure environment variables
- Learn about the Solana program architecture