Skip to main content

Database Reference

The API persists data in MySQL through Drizzle ORM (libs/api/db).

For setup and migration commands, see the How-To section below.

NestJS Integration

DbModule is global (@Global()). It registers DrizzleMySqlModule with the tag 'DB_PROD'.
Services that need the database inject it with @Inject('DB_PROD').

Schema Tables

Source of truth: libs/api/db/src/lib/schema.ts

user

Stores player accounts.

ColumnTypeNotes
idvarchar(36)Primary key
nametextDisplay name
emailvarchar(255)Unique
nicknamevarchar(255)Unique, required on sign-up
emailVerifiedbooleanMust be true for profile changes
imagetextNullable
createdAt, updatedAttimestampAuto-managed

session

Stores active sessions. The API deletes all rows for a user after a password change.

ColumnTypeNotes
idvarchar(36)Primary key
tokenvarchar(255)Unique session token
userIdvarchar(36)FK → user.id
expiresAttimestampExpiry managed by Better Auth

account

Stores auth provider records. For email/password auth, password holds the bcrypt hash.

ColumnTypeNotes
idvarchar(36)Primary key
accountIdtextProvider-specific account ID
providerIdtext'credential' for email/password
userIdvarchar(36)FK → user.id
passwordtextNullable — only set for credential accounts
accessToken, refreshToken, idTokentextNullable — OAuth-managed by Better Auth
accessTokenExpiresAt, refreshTokenExpiresAttimestampNullable — OAuth token expiry
scopetextNullable — OAuth scope
createdAt, updatedAttimestampAuto-managed

verification

Better Auth uses this table internally for email verification tokens.

pending_account_change

Stores in-progress profile/email/password confirmation workflows.

ColumnTypeNotes
typevarchar(32)'profile', 'email', or 'password'
stagevarchar(64)'pending', 'confirm-old-email', 'verify-new-email'
tokenHashvarchar(64)SHA-256 of the token sent in the email. Unique.
callbackUrltextFrontend URL that receives the ?token= param
newNametextNullable — set for profile changes
newNicknamevarchar(255)Nullable — set for profile changes
newEmailvarchar(255)Nullable — set for email changes
newPasswordHashtextNullable — set for password changes
expiresAttimestampTTL: 24 hours from creation

Environment Variables

getMysqlConnectionOptions() reads .env from the Nx workspace root.

VariableRequiredDefaultNotes
MYSQL_USER
MYSQL_PASSWORD
MYSQL_DB
MYSQL_HOSTlocalhost:3306Format: host:port

How To Manage the Schema

All commands run through Nx:

# Create a new migration from schema changes
pnpm nx run db:generate

# Apply pending migrations
pnpm nx run db:migrate

# Verify no schema drift exists
pnpm nx run db:check

# Open Drizzle Studio (visual DB editor)
pnpm nx run db:studio

To add a table: Update schema.ts, then run generate and migrate.
To modify a table: Edit schema.ts, then run generate and migrate.