Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

18.8 KB
Raw Blame
generator client {
  provider      = "prisma-client-js"
  binaryTargets = ["native", "debian-openssl-3.0.x", "linux-arm64-openssl-3.0.x", "linux-musl-openssl-3.0.x"]
}

datasource db {
  provider = "sqlite"
  url      = env("SQLITE_DATABASE_URL")
}

model Account {
  id                String   @id @default(cuid())
  userId            String   @map("user_id")
  type              String?
  provider          String
  providerAccountId String   @map("provider_account_id")
  token_type        String?
  refresh_token     String?
  access_token      String?
  expires_at        Int?
  scope             String?
  id_token          String?
  createdAt         DateTime @default(now())
  updatedAt         DateTime @updatedAt
  user              User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([provider, providerAccountId])
  @@map("accounts")
}

model Session {
  id           String   @id @default(cuid())
  userId       String?  @map("user_id")
  sessionToken String   @unique @map("session_token")
  accessToken  String?  @map("access_token")
  expires      DateTime
  user         User?    @relation(fields: [userId], references: [id], onDelete: Cascade)
  createdAt    DateTime @default(now())
  updatedAt    DateTime @updatedAt

  @@map("sessions")
}

model User {
  id                     String               @id @default(uuid())
  name                   String?
  email                  String               @unique
  emailVerified          DateTime?            @map("email_verified")
  password               String?
  image                  String?
  hasCompletedOnboarding Boolean              @default(false) @map("has_completed_onboarding")
  createdAt              DateTime             @default(now())
  updatedAt              DateTime             @updatedAt
  accounts               Account[]
  sessions               Session[]
  dbConfigs              DatabaseConfigUser[]
  createdDBConfigs       DatabaseConfig[]
  createdApiKeys         APIKeys[]            @relation("createdByUser")
  deletedApiKeys         APIKeys[]            @relation("deletedByUser")
  opengrounds            OpenGround[]
  organisations          OrganisationUser[]
  projectMemberships     ProjectUser[]

  // The instance admin is the earliest account; keep that lookup indexed.
  @@index([createdAt, id])
  @@map("users")
}

model VerificationRequest {
  id         String   @id @default(cuid())
  identifier String
  token      String   @unique
  expires    DateTime
  createdAt  DateTime @default(now())
  updatedAt  DateTime @updatedAt

  @@unique([identifier, token])
  @@map("verificationrequest")
}

// Organisation
model Organisation {
  id              String                    @id @default(cuid())
  name            String
  slug            String                    @unique
  createdAt       DateTime                  @default(now())
  updatedAt       DateTime                  @updatedAt
  createdByUserId String                    @map("created_by_user_id")
  members         OrganisationUser[]
  invitedUsers    OrganisationInvitedUser[]
  projects        Project[]
  connectorInstances ConnectorInstance[]
  apiKeys         APIKeys[]

  @@map("organisations")
}

model Project {
  id              String                    @id @default(cuid())
  organisationId  String                    @map("organisation_id")
  organisation    Organisation              @relation(fields: [organisationId], references: [id], onDelete: Cascade)
  name            String
  slug            String
  isDefault       Boolean                   @default(false) @map("is_default")
  createdAt       DateTime                  @default(now())
  updatedAt       DateTime                  @updatedAt
  databaseConfigs DatabaseConfig[]
  telemetrySources TelemetrySource[]
  telemetrySourceBindings TelemetrySourceBinding[]
  connectorInstances ConnectorInstance[]
  environments    ProjectEnvironment[]
  currentForUsers OrganisationUser[]        @relation("CurrentOrganisationProject")
  members         ProjectUser[]
  apiKeys         APIKeys[]
  realtimeFindings RealtimeFinding[]
  realtimeRules    RealtimeRule[]

  @@unique([organisationId, slug])
  @@map("projects")
}

// Generic project/organisation connector records. Existing datasource and
// alert tables remain compatible while their repositories migrate behind the
// connector abstraction.
model ConnectorInstance {
  id              String   @id @default(cuid())
  category        String
  type            String
  name            String
  environment     String   @default("production")
  organisationId  String?  @map("organisation_id")
  organisation    Organisation? @relation(fields: [organisationId], references: [id], onDelete: Cascade)
  projectId       String?  @map("project_id")
  project         Project? @relation(fields: [projectId], references: [id], onDelete: Cascade)
  settings        String   @default("{}")
  secretRef       String?  @map("secret_ref")
  status          String   @default("active")
  metadata        String   @default("{}")
  createdAt       DateTime @default(now())
  updatedAt       DateTime @updatedAt
  bindings        ConnectorBinding[]

  @@unique([name, organisationId, projectId, environment])
  @@index([category, type])
  @@map("connector_instances")
}

model ConnectorBinding {
  id           String            @id @default(cuid())
  connectorId  String            @map("connector_id")
  connector    ConnectorInstance @relation(fields: [connectorId], references: [id], onDelete: Cascade)
  consumerType String            @map("consumer_type")
  consumerKey  String            @map("consumer_key")
  environment  String            @default("production")
  metadata     String            @default("{}")
  createdAt    DateTime          @default(now())
  updatedAt    DateTime          @updatedAt

  @@unique([connectorId, consumerType, consumerKey, environment])
  @@index([consumerType, consumerKey])
  @@map("connector_bindings")
}

model OrganisationUser {
  id             String       @id @default(cuid())
  organisationId String       @map("organisation_id")
  organisation   Organisation @relation(fields: [organisationId], references: [id], onDelete: Cascade)
  userId         String       @map("user_id")
  user           User         @relation(fields: [userId], references: [id], onDelete: Cascade)
  role           String       @default("member")
  isCurrent      Boolean      @default(false)
  currentProjectId String?    @map("current_project_id")
  currentProject Project?     @relation("CurrentOrganisationProject", fields: [currentProjectId], references: [id], onDelete: SetNull)
  projects        ProjectUser[]
  createdAt      DateTime     @default(now())
  updatedAt      DateTime     @updatedAt

  @@unique([organisationId, userId])
  @@map("organisation_users")
}

model ProjectUser {
  id                   String           @id @default(cuid())
  projectId            String           @map("project_id")
  project              Project          @relation(fields: [projectId], references: [id], onDelete: Cascade)
  organisationUserId   String           @map("organisation_user_id")
  organisationUser     OrganisationUser @relation(fields: [organisationUserId], references: [id], onDelete: Cascade)
  userId               String           @map("user_id")
  user                 User             @relation(fields: [userId], references: [id], onDelete: Cascade)
  createdAt            DateTime         @default(now())

  @@unique([projectId, organisationUserId])
  @@index([userId])
  @@map("project_users")
}

model OrganisationInvitedUser {
  id              String       @id @default(cuid())
  organisationId  String       @map("organisation_id")
  organisation    Organisation @relation(fields: [organisationId], references: [id], onDelete: Cascade)
  email           String
  invitedByUserId String       @map("invited_by_user_id")
  createdAt       DateTime     @default(now())

  @@unique([organisationId, email])
  @@map("organisation_invited_users")
}

// Database Config
model DatabaseConfig {
  id              String                      @id @default(cuid())
  name            String
  environment     String                      @default("production")
  username        String                      @default("admin")
  password        String?
  host            String                      @default("127.0.0.1")
  port            String                      @default("8123")
  database        String                      @default("openlit")
  query           String?
  dbUsers         DatabaseConfigUser[]
  createdAt       DateTime                    @default(now())
  updatedAt       DateTime                    @updatedAt
  createdByUserId String                      @map("user_id")
  createdByUser   User                        @relation(fields: [createdByUserId], references: [id])
  projectId       String?                     @map("project_id")
  project         Project?                    @relation(fields: [projectId], references: [id], onDelete: Cascade)
  dbInvitedUsers  DatabaseConfigInvitedUser[]
  apiKeys         APIKeys[]
  opengrounds     OpenGround[]
  telemetryBindings TelemetrySourceBinding[]
  environmentRef  ProjectEnvironment?          @relation("ProjectEnvironmentDatabaseConfigs", fields: [projectId, environment], references: [projectId, name], onDelete: Restrict)

  @@unique([name, projectId, environment])
  @@map("databaseconfig")
}

// A pluggable raw-telemetry source for a project. The implicit built-in
// source is the project's ClickHouse (DatabaseConfig) and is NOT stored here;
// rows in this table describe external observability backends (Datadog,
// Tempo/Loki/Mimir, New Relic, Jaeger, Victoria, ...). Secrets live in the
// vault (secretRef); only non-secret settings are stored inline as JSON text.
model TelemetrySource {
  id              String   @id @default(cuid())
  projectId       String?  @map("project_id")
  project         Project? @relation(fields: [projectId], references: [id], onDelete: Cascade)
  name            String
  environment     String   @default("production")
  // Source type, e.g. "clickhouse" | "datadog" | "tempo" | "loki" | "mimir" |
  // "prometheus" | "newrelic" | "jaeger" | "victoriametrics" | ...
  type            String   @default("clickhouse")
  // Comma-separated subset of "traces,logs,metrics" this source serves.
  signals         String   @default("traces,logs,metrics")
  // JSON text of non-secret settings (URLs, site, orgId, headers).
  settings        String?
  // Vault secret id holding credentials (API/app keys), if any.
  secretRef       String?  @map("secret_ref")
  isDefault       Boolean  @default(false) @map("is_default")
  createdAt       DateTime @default(now())
  updatedAt       DateTime @updatedAt
  createdByUserId String?  @map("user_id")
  bindings        TelemetrySourceBinding[]
  environmentRef  ProjectEnvironment? @relation("ProjectEnvironmentTelemetrySources", fields: [projectId, environment], references: [projectId, name], onDelete: Restrict)

  @@unique([name, projectId, environment])
  @@map("telemetry_source")
}

model ProjectEnvironment {
  id        String   @id @default(cuid())
  projectId String   @map("project_id")
  project   Project  @relation(fields: [projectId], references: [id], onDelete: Cascade)
  name      String
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  databaseConfigs DatabaseConfig[] @relation("ProjectEnvironmentDatabaseConfigs")
  telemetrySources TelemetrySource[] @relation("ProjectEnvironmentTelemetrySources")

  @@unique([projectId, name])
  @@map("project_environments")
}

// Findings produced by the realtime engine from the NATS signal stream. Rows
// are tenant-scoped (organisation -> project -> environment) and upserted by
// `dedupeKey` so engine replays and retries never duplicate a finding.
model RealtimeFinding {
  id             String    @id @default(cuid())
  organisationId String    @map("organisation_id")
  projectId      String    @map("project_id")
  project        Project   @relation(fields: [projectId], references: [id], onDelete: Cascade)
  environment    String
  ruleId         String    @map("rule_id")
  ruleName       String    @map("rule_name")
  kind           String
  mode           String
  severity       String
  state          String
  dedupeKey      String    @unique @map("dedupe_key")
  metric         String?
  operator       String?
  value          Float
  threshold      Float
  windowSec      Int?      @map("window_sec")
  sampleCount    Int       @default(0) @map("sample_count")
  group          String    @default("{}")
  sample         String    @default("{}")
  occurrences    Int       @default(1)
  firstSeenAt    DateTime  @map("first_seen_at")
  lastSeenAt     DateTime  @map("last_seen_at")
  resolvedAt     DateTime? @map("resolved_at")
  createdAt      DateTime  @default(now())
  updatedAt      DateTime  @updatedAt

  @@index([projectId, environment, lastSeenAt])
  @@index([organisationId])
  @@map("realtime_findings")
}

// Realtime rules evaluated by the engine (`src/engine`) on the NATS signal
// stream. Each rule belongs to exactly one organisation -> project ->
// environment, which the engine receives as the rule scope. `owner` is the
// product feature that manages the rule ("alert" for threshold alerts; later
// evaluations or cost controls), so each feature lists only its own rules.
// `definition` holds the validated match/groupBy/metric/threshold JSON.
model RealtimeRule {
  id              String   @id @default(cuid())
  organisationId  String   @map("organisation_id")
  projectId       String   @map("project_id")
  project         Project  @relation(fields: [projectId], references: [id], onDelete: Cascade)
  environment     String
  owner           String   @default("alert")
  name            String
  description     String   @default("")
  kind            String
  mode            String
  severity        String
  enabled         Boolean  @default(true)
  definition      String   @default("{}")
  createdByUserId String?  @map("created_by_user_id")
  updatedByUserId String?  @map("updated_by_user_id")
  createdAt       DateTime @default(now()) @map("created_at")
  updatedAt       DateTime @updatedAt @map("updated_at")

  @@index([projectId, environment, owner])
  @@index([organisationId])
  @@index([enabled])
  @@map("realtime_rules")
}

// Per-signal routing: which TelemetrySource serves a given signal in a project
// (Grafana-style per-signal datasource binding). At most one binding per
// (project, signal). Cascades on both project and source deletion so routing
// never dangles.
model TelemetrySourceBinding {
  id        String          @id @default(cuid())
  projectId String          @map("project_id")
  project   Project         @relation(fields: [projectId], references: [id], onDelete: Cascade)
  // One of "traces" | "logs" | "metrics".
  signal    String
  environment String @default("production")
  sourceId  String?         @map("source_id")
  source    TelemetrySource? @relation(fields: [sourceId], references: [id], onDelete: Cascade)
  databaseConfigId String?  @map("database_config_id")
  databaseConfig DatabaseConfig? @relation(fields: [databaseConfigId], references: [id], onDelete: Cascade)
  createdAt DateTime        @default(now())
  updatedAt DateTime        @updatedAt

  @@unique([projectId, signal, environment])
  @@map("telemetry_source_binding")
}

model DatabaseConfigUser {
  databaseConfigId String
  userId           String          @map("user_id")
  user             User            @relation(fields: [userId], references: [id])
  databaseConfig   DatabaseConfig? @relation(fields: [databaseConfigId], references: [id], onDelete: Cascade)
  isCurrent        Boolean         @default(false)
  canEdit          Boolean         @default(false)
  canShare         Boolean         @default(false)
  canDelete        Boolean         @default(false)
  createdAt        DateTime?       @default(now())
  updatedAt        DateTime?       @updatedAt

  @@unique([databaseConfigId, userId])
  @@map("databaseconfiguser")
}

// DEPRECATED: All OpenGround data has been migrated to ClickHouse
// All tables: openlit_openground, openlit_openground_providers, openlit_openground_configs
// Old Prisma models removed - everything now in ClickHouse for better performance and scalability
// This table will be removed in future
// @deprecated - Commented out as all data migrated to ClickHouse
model OpenGround {
  id               String         @id @default(cuid())
  requestMeta      String
  responseMeta     String
  stats            String
  createdAt        DateTime       @default(now())
  createdByUserId  String         @map("user_id")
  createdByUser    User           @relation(fields: [createdByUserId], references: [id])
  databaseConfigId String         @map("database_config_id")
  databaseConfig   DatabaseConfig @relation(fields: [databaseConfigId], references: [id])

  @@map("openground")
}

model DatabaseConfigInvitedUser {
  databaseConfigId String
  databaseConfig   DatabaseConfig? @relation(fields: [databaseConfigId], references: [id])
  email            String
  canEdit          Boolean         @default(false)
  canShare         Boolean         @default(false)
  canDelete        Boolean         @default(false)

  @@unique([databaseConfigId, email])
}

model APIKeys {
  id               String          @id @default(cuid())
  name             String          @default("default")
  apiKey           String          @unique
  databaseConfigId String          @map("database_config_id")
  databaseConfig   DatabaseConfig? @relation(fields: [databaseConfigId], references: [id])
  organisationId   String?         @map("organisation_id")
  organisation     Organisation?   @relation(fields: [organisationId], references: [id], onDelete: SetNull)
  projectId        String?         @map("project_id")
  project          Project?        @relation(fields: [projectId], references: [id], onDelete: SetNull)
  environment      String          @default("production")
  isDeleted        Boolean         @default(false)
  createdAt        DateTime        @default(now())
  createdByUserId  String
  createdByUser    User            @relation("createdByUser", fields: [createdByUserId], references: [id])
  deletedAt        DateTime?
  deletedByUserId  String?
  deletedByUser    User?           @relation("deletedByUser", fields: [deletedByUserId], references: [id])

  @@index([organisationId])
  @@index([projectId, environment])
}

model ClickhouseMigrations {
  id                    String @id @default(cuid())
  databaseConfigId      String
  clickhouseMigrationId String
}

model EvaluationConfigs {
  id               String  @id @default(cuid())
  databaseConfigId String  @unique @map("database_config_id")
  provider         String
  model            String
  vaultId          String
  auto             Boolean @default(false)
  recurringTime    String
  meta             String
}

model PricingConfigs {
  id               String  @id @default(cuid())
  databaseConfigId String  @unique @map("database_config_id")
  auto             Boolean @default(false)
  recurringTime    String  @default("")
  meta             String  @default("{}")
}