Skip to content

Domain Layer Reference

Complete reference for USL's domain layer.

Entities

Entities are mutable objects with identity:

entity User {
  id: UserId @primary
  email: Email @unique
  name: String
  age: Int

  invariant adult {
    this.age >= 18
  }
}

Annotations

  • @primary - Primary key
  • @unique - Unique constraint
  • @secret - Sensitive data
  • @indexed - Database index

Invariants

Business rules that must always hold:

invariant name {
  condition
}

Value Objects

Immutable, validated types:

value Email {
  address: String

  invariant valid {
    matches(this.address, EMAIL_REGEX)
  }
}

Enums

Simple enumerations:

enum Role {
  Admin
  User
  Guest
}

Variants

Sum types with associated data:

variant Status {
  Active
  Suspended { reason: String, until: Date }
  Banned
}

Relationships

entity Post {
  id: PostId @primary
  authorId: UserId  // Foreign key
}

Computed Fields

entity User {
  firstName: String
  lastName: String

  computed fullName: String {
    this.firstName + " " + this.lastName
  }
}

Global Invariants

global invariant user_emails_unique {
  forall u1, u2 in User {
    u1.id != u2.id -> u1.email != u2.email
  }
}

Back to Overview