Files
SYSTEM/docs/architecture/database-architecture.md
2025-09-01 01:52:06 +03:00

60 lines
1.9 KiB
Markdown

# 4. Database Architecture (Revised with Prisma)
The database schema will be defined declaratively using the Prisma schema language in a single source of truth file: `prisma/schema.prisma`. Prisma will be responsible for generating SQL migrations and providing a type-safe client for all database interactions.
## Prisma Schema (`prisma/schema.prisma`)
```prisma
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
// User-friendly metadata associated with a public key.
model Registry {
publicKey String @id
citizenName String
avatarUrl String?
isActive Boolean @default(true)
createdAt DateTime @default(now())
}
// The core, immutable data model.
// Once a row is written, it is never updated or deleted.
model LedgerEntry {
id BigInt @id @default(autoincrement())
previousHash String?
entryHash String @unique
citizenPublicKey String
signature String
actionType String
payload Json? // The actual data for the action, e.g., cell details
createdAt DateTime @default(now())
@@index([citizenPublicKey])
@@index([payload], name: "idx_ledger_payload_entity") // Example for indexing specific payload keys
}
```
## Migrations Workflow
Database migrations will be managed exclusively by Prisma Migrate.
1. **Development:** After any change to `schema.prisma`, a new migration will be created and applied using the command:
```bash
npx prisma migrate dev --name <descriptive_migration_name>
```
2. **Production:** Migrations will be applied during the deployment process using:
```bash
npx prisma migrate deploy
```
This ensures that the database schema is always in sync with the Prisma schema definition.
---