Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

12.0 KB
Raw Blame
// 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"
  previewFeatures = ["views"]
  // see commands.ts
  // <output-placeholder-for-migrations>
  output          = "../src/__generated__"
  // </output-placeholder-for-migrations>

  binaryTargets = env("PRISMA_BINARY_TARGET")
}

datasource db {
  provider  = "postgres"
  url       = env("DATABASE_URL")
  directUrl = env("DIRECT_URL")
}

enum UploadStatus {
  UPLOADING
  UPLOADED
}

model File {
  name              String       @id
  format            String
  size              Int
  description       String?
  createdAt         DateTime     @default(now()) @db.Timestamptz(3)
  updatedAt         DateTime     @default(now()) @updatedAt @db.Timestamptz(3)
  meta              String       @default("{}")
  status            UploadStatus @default(UPLOADING)
  isDeleted         Boolean      @default(false)
  uploaderProject   Project?     @relation(fields: [uploaderProjectId], references: [id])
  uploaderProjectId String?
  contentHash       String?      @db.VarChar(64)
  assets            Asset[]
}

model Asset {
  id               String              @default(uuid()) // not unique!
  projectId        String
  file             File                @relation(fields: [name], references: [name])
  name             String
  filename         String?
  description      String?
  folderId         String?
  folder           AssetFolder?        @relation(fields: [folderId, projectId], references: [id, projectId], onDelete: NoAction)
  // The child primary key permits one row. Prisma represents this as a list
  // because the relation field order differs from the project-first key and a
  // singular relation would require a redundant reverse-order unique index.
  fileMetadata     AssetFileMetadata[]
  Project          Project[]
  DashboardProject DashboardProject[]

  @@id([id, projectId])
}

model AssetFileMetadata {
  projectId          String
  assetId            String
  revision           String
  document           Json
  createdAt          DateTime @default(now()) @db.Timestamptz(3)
  updatedAt          DateTime @default(now()) @updatedAt @db.Timestamptz(3)
  asset              Asset    @relation(fields: [assetId, projectId], references: [id, projectId], onDelete: Cascade)

  @@id([projectId, assetId])
}

model AssetFolder {
  id        String        @default(uuid())
  projectId String
  project   Project       @relation(fields: [projectId], references: [id], onDelete: Cascade)
  name      String
  parentId  String?
  parent    AssetFolder?  @relation("AssetFolderChildren", fields: [parentId, projectId], references: [id, projectId], onDelete: NoAction)
  children  AssetFolder[] @relation("AssetFolderChildren")
  assets    Asset[]
  createdAt DateTime      @default(now()) @db.Timestamptz(3)

  @@id([id, projectId])
  @@index([projectId, parentId])
}

model User {
  id               String             @id @default(uuid())
  email            String?            @unique
  provider         String?
  image            String?
  username         String?
  createdAt        DateTime           @default(now()) @db.Timestamptz(3)
  projects         Project[]
  workspaces       Workspace[]
  projectsTags     Json               @default("[]")
  clientReferences ClientReferences[]
  checkout         TransactionLog[]
  products         UserProduct[]
}

// User client references in external services like stripe etc
model ClientReferences {
  reference String   @default(dbgenerated())
  service   String
  createdAt DateTime @default(now()) @db.Timestamptz(3)
  user      User     @relation(fields: [userId], references: [id])
  userId    String

  @@id([userId, service])
  @@unique([reference, service])
}

model Product {
  id          String   @id
  name        String
  description String?
  features    String[]
  images      String[]
  meta        Json

  createdAt    DateTime         @default(now()) @db.Timestamptz(3)
  checkout     TransactionLog[]
  userProducts UserProduct[]
}

model TransactionLog {
  // Stripe event id (debug and idempotency purposes)
  eventId String  @id
  userId  String?
  user    User?   @relation(fields: [userId], references: [id])

  productId String?
  product   Product? @relation(fields: [productId], references: [id])

  createdAt DateTime @default(now()) @db.Timestamptz(3)

  eventData Json? @default(dbgenerated())

  eventType    String? @default(dbgenerated())
  eventCreated Int?    @default(dbgenerated())

  // paid
  status String? @default(dbgenerated())

  customerId    String? @default(dbgenerated())
  customerEmail String? @default(dbgenerated())

  subscriptionId String? @default(dbgenerated())

  // Used for Refund
  paymentIntent String? @default(dbgenerated())

  @@unique([eventId, productId])
}

view UserProduct {
  userId         String
  user           User    @relation(fields: [userId], references: [id])
  productId      String
  product        Product @relation(fields: [productId], references: [id])
  // subscriptionId and customerId not null for subscriptions
  subscriptionId String?
  customerId     String?
  // Easier to debug
  customerEmail  String?

  @@unique([userId, productId])
}

model Project {
  id                        String                       @id @default(uuid())
  createdAt                 DateTime                     @default(now()) @db.Timestamptz(3)
  title                     String
  // Tag ids used in User.projectTags
  tags                      String[]
  domain                    String                       @unique
  user                      User?                        @relation(fields: [userId], references: [id])
  userId                    String?
  workspace                 Workspace?                   @relation(fields: [workspaceId], references: [id])
  workspaceId               String?
  build                     Build[]
  isDeleted                 Boolean                      @default(false)
  files                     File[]
  assetFolders              AssetFolder[]
  projectDomain             ProjectDomain[]
  authorizationToken        AuthorizationToken[]
  previewImageAsset         Asset?                       @relation(fields: [previewImageAssetId, id], references: [id, projectId])
  previewImageAssetId       String?
  marketplaceApprovalStatus MarketplaceApprovalStatus    @default(UNLISTED)
  latestStaticBuild         LatestStaticBuildPerProject?

  @@unique([id, isDeleted])
  @@unique([domain, isDeleted])
  @@unique([id, domain])
  // ApprovedMarketplaceProduct view performance index
  @@index([isDeleted, marketplaceApprovalStatus])
}

enum PublishStatus {
  PENDING
  PUBLISHED
  FAILED
}

enum MarketplaceApprovalStatus {
  UNLISTED
  PENDING
  APPROVED
  REJECTED
}

model Build {
  id                String   @unique @default(uuid())
  version           Int      @default(0)
  lastTransactionId String?
  createdAt         DateTime @default(now()) @db.Timestamptz(3)
  updatedAt         DateTime @default(now()) @updatedAt @db.Timestamptz(3)

  pages String

  project   Project @relation(fields: [projectId], references: [id])
  projectId String

  breakpoints           String @default("[]")
  styles                String @default("[]")
  styleSources          String @default("[]")
  styleSourceSelections String @default("[]")
  props                 String @default("[]")
  dataSources           String @default("[]")
  resources             String @default("[]")
  instances             String @default("[]")
  marketplaceProduct    String @default("{}")
  projectSettings       String @default("{\"meta\":{},\"compiler\":{}}")

  deployment    String?
  publishStatus PublishStatus @default(PENDING)

  latestStaticBuildPerProject LatestStaticBuildPerProject?

  @@id([id, projectId])
  // ApprovedMarketplaceProduct view performance index
  @@index([projectId, createdAt(sort: Desc)])
}

enum AuthorizationRelation {
  viewers
  editors
  builders
  administrators
}

model AuthorizationToken {
  token     String                @default(uuid())
  projectId String
  project   Project               @relation(fields: [projectId], references: [id])
  name      String                @default("")
  relation  AuthorizationRelation @default(viewers)
  createdAt DateTime              @default(now()) @db.Timestamptz(3)

  canClone  Boolean @default(true)
  canCopy   Boolean @default(true)
  canUseApi Boolean @default(false)

  @@id([token, projectId])
  @@unique([token])
}

enum DomainStatus {
  INITIALIZING
  ACTIVE
  ERROR
  PENDING
}

// Domains  + last known status and last known txtRecord
// In the future we can update this table using queue, n8n or temporal workflows.
// As of now updates are done during UI interactions
model Domain {
  id        String   @id @default(uuid())
  domain    String   @unique
  createdAt DateTime @default(now()) @db.Timestamptz(3)
  updatedAt DateTime @default(now()) @updatedAt @db.Timestamptz(3)

  ProjectDomain ProjectDomain[]
  // Last known txtRecord of the domain (to check domain ownership)
  txtRecord     String?
  // create, init, pending, active, error
  status        DomainStatus    @default(INITIALIZING)
  // In case of status="error", this will contain the error message
  error         String?
}

model ProjectDomain {
  projectId String
  project   Project  @relation(fields: [projectId], references: [id])
  domainId  String
  domain    Domain   @relation(fields: [domainId], references: [id])
  createdAt DateTime @default(now()) @db.Timestamptz(3)
  // Generated txt record to check domain ownership
  txtRecord String   @unique @default(uuid())

  // CNAME record to point to the domain
  cname String

  @@id([projectId, domainId])
  @@index([domainId])
}

view LatestStaticBuildPerProject {
  buildId String
  build   Build  @relation(fields: [buildId, projectId], references: [id, projectId])

  projectId String
  project   Project @relation(fields: [projectId], references: [id])

  publishStatus PublishStatus
  updatedAt     DateTime      @db.Timestamptz(3)

  @@id([projectId])
  @@unique([buildId, projectId])
}

model Workspace {
  id        String            @id @default(uuid())
  name      String
  isDefault Boolean           @default(false)
  isDeleted Boolean           @default(false)
  createdAt DateTime          @default(now()) @db.Timestamptz(3)
  user      User              @relation(fields: [userId], references: [id])
  userId    String
  projects  Project[]
  members   WorkspaceMember[]

  // Only one default workspace per user
  @@unique([userId, isDefault])
}

model WorkspaceMember {
  workspace   Workspace             @relation(fields: [workspaceId], references: [id], onDelete: Cascade)
  workspaceId String
  userId      String
  relation    AuthorizationRelation @default(administrators)
  createdAt   DateTime              @default(now()) @db.Timestamptz(3)
  removedAt   DateTime?             @db.Timestamptz(3)

  @@id([workspaceId, userId])
}

model Notification {
  id          String    @id @default(uuid())
  type        String
  recipientId String
  senderId    String
  status      String    @default("pending")
  payload     Json      @default("{}")
  createdAt   DateTime  @default(now()) @db.Timestamptz(3)
  respondedAt DateTime? @db.Timestamptz(3)

  @@index([recipientId, status])
}

view WorkspaceProjectAuthorization {
  userId    String
  projectId String
  relation  String

  @@id([userId, projectId, relation])
}

// Dashboard
view DashboardProject {
  id                        String                    @id @default(uuid())
  createdAt                 DateTime                  @default(now()) @db.Timestamptz(3)
  title                     String
  domain                    String
  userId                    String?
  workspaceId               String?
  previewImageAsset         Asset?                    @relation(fields: [previewImageAssetId, id], references: [id, projectId])
  previewImageAssetId       String?
  isDeleted                 Boolean                   @default(false)
  isPublished               Boolean
  marketplaceApprovalStatus MarketplaceApprovalStatus @default(UNLISTED)
}

view ApprovedMarketplaceProduct {
  projectId          String  @id
  marketplaceProduct String
  authorizationToken String?
}