@marinoscar/platform-db 0.0.0-stage → 0.1.0-next.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (208) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/LICENSE +21 -0
  3. package/README.md +382 -2
  4. package/bin/platform.js +28 -0
  5. package/dist/baseline/deps.d.ts +32 -0
  6. package/dist/baseline/deps.d.ts.map +1 -0
  7. package/dist/baseline/deps.js +64 -0
  8. package/dist/baseline/deps.js.map +1 -0
  9. package/dist/baseline/errors.d.ts +34 -0
  10. package/dist/baseline/errors.d.ts.map +1 -0
  11. package/dist/baseline/errors.js +23 -0
  12. package/dist/baseline/errors.js.map +1 -0
  13. package/dist/baseline/fs.d.ts +9 -0
  14. package/dist/baseline/fs.d.ts.map +1 -0
  15. package/dist/baseline/fs.js +39 -0
  16. package/dist/baseline/fs.js.map +1 -0
  17. package/dist/baseline/index.d.ts +8 -0
  18. package/dist/baseline/index.d.ts.map +1 -0
  19. package/dist/baseline/index.js +24 -0
  20. package/dist/baseline/index.js.map +1 -0
  21. package/dist/baseline/mapping.d.ts +43 -0
  22. package/dist/baseline/mapping.d.ts.map +1 -0
  23. package/dist/baseline/mapping.js +80 -0
  24. package/dist/baseline/mapping.js.map +1 -0
  25. package/dist/baseline/normalise.d.ts +42 -0
  26. package/dist/baseline/normalise.d.ts.map +1 -0
  27. package/dist/baseline/normalise.js +133 -0
  28. package/dist/baseline/normalise.js.map +1 -0
  29. package/dist/baseline/plan.d.ts +66 -0
  30. package/dist/baseline/plan.d.ts.map +1 -0
  31. package/dist/baseline/plan.js +105 -0
  32. package/dist/baseline/plan.js.map +1 -0
  33. package/dist/baseline/report.d.ts +125 -0
  34. package/dist/baseline/report.d.ts.map +1 -0
  35. package/dist/baseline/report.js +85 -0
  36. package/dist/baseline/report.js.map +1 -0
  37. package/dist/baseline/run.d.ts +106 -0
  38. package/dist/baseline/run.d.ts.map +1 -0
  39. package/dist/baseline/run.js +339 -0
  40. package/dist/baseline/run.js.map +1 -0
  41. package/dist/bin/db-commands.d.ts +31 -0
  42. package/dist/bin/db-commands.d.ts.map +1 -0
  43. package/dist/bin/db-commands.js +276 -0
  44. package/dist/bin/db-commands.js.map +1 -0
  45. package/dist/bin/fs-io.d.ts +7 -0
  46. package/dist/bin/fs-io.d.ts.map +1 -0
  47. package/dist/bin/fs-io.js +45 -0
  48. package/dist/bin/fs-io.js.map +1 -0
  49. package/dist/bin/platform.d.ts +21 -0
  50. package/dist/bin/platform.d.ts.map +1 -0
  51. package/dist/bin/platform.js +99 -0
  52. package/dist/bin/platform.js.map +1 -0
  53. package/dist/compose/api.d.ts +84 -0
  54. package/dist/compose/api.d.ts.map +1 -0
  55. package/dist/compose/api.js +101 -0
  56. package/dist/compose/api.js.map +1 -0
  57. package/dist/compose/compose.d.ts +51 -0
  58. package/dist/compose/compose.d.ts.map +1 -0
  59. package/dist/compose/compose.js +177 -0
  60. package/dist/compose/compose.js.map +1 -0
  61. package/dist/compose/diff.d.ts +7 -0
  62. package/dist/compose/diff.d.ts.map +1 -0
  63. package/dist/compose/diff.js +68 -0
  64. package/dist/compose/diff.js.map +1 -0
  65. package/dist/compose/errors.d.ts +31 -0
  66. package/dist/compose/errors.d.ts.map +1 -0
  67. package/dist/compose/errors.js +35 -0
  68. package/dist/compose/errors.js.map +1 -0
  69. package/dist/compose/index.d.ts +3 -0
  70. package/dist/compose/index.d.ts.map +1 -0
  71. package/dist/compose/index.js +11 -0
  72. package/dist/compose/index.js.map +1 -0
  73. package/dist/compose/parse.d.ts +65 -0
  74. package/dist/compose/parse.d.ts.map +1 -0
  75. package/dist/compose/parse.js +122 -0
  76. package/dist/compose/parse.js.map +1 -0
  77. package/dist/drift/db-conformance.d.ts +49 -0
  78. package/dist/drift/db-conformance.d.ts.map +1 -0
  79. package/dist/drift/db-conformance.js +65 -0
  80. package/dist/drift/db-conformance.js.map +1 -0
  81. package/dist/drift/index.d.ts +5 -0
  82. package/dist/drift/index.d.ts.map +1 -0
  83. package/dist/drift/index.js +26 -0
  84. package/dist/drift/index.js.map +1 -0
  85. package/dist/drift/raw-sql-indexes.d.ts +106 -0
  86. package/dist/drift/raw-sql-indexes.d.ts.map +1 -0
  87. package/dist/drift/raw-sql-indexes.js +113 -0
  88. package/dist/drift/raw-sql-indexes.js.map +1 -0
  89. package/dist/drift/raw-sql-tripwire.d.ts +150 -0
  90. package/dist/drift/raw-sql-tripwire.d.ts.map +1 -0
  91. package/dist/drift/raw-sql-tripwire.js +394 -0
  92. package/dist/drift/raw-sql-tripwire.js.map +1 -0
  93. package/dist/drift/run.d.ts +128 -0
  94. package/dist/drift/run.d.ts.map +1 -0
  95. package/dist/drift/run.js +201 -0
  96. package/dist/drift/run.js.map +1 -0
  97. package/dist/index.d.ts +16 -0
  98. package/dist/index.d.ts.map +1 -0
  99. package/dist/index.js +33 -0
  100. package/dist/index.js.map +1 -0
  101. package/dist/lock/hash.d.ts +13 -0
  102. package/dist/lock/hash.d.ts.map +1 -0
  103. package/dist/lock/hash.js +19 -0
  104. package/dist/lock/hash.js.map +1 -0
  105. package/dist/lock/index.d.ts +5 -0
  106. package/dist/lock/index.d.ts.map +1 -0
  107. package/dist/lock/index.js +20 -0
  108. package/dist/lock/index.js.map +1 -0
  109. package/dist/lock/lock.d.ts +117 -0
  110. package/dist/lock/lock.d.ts.map +1 -0
  111. package/dist/lock/lock.js +140 -0
  112. package/dist/lock/lock.js.map +1 -0
  113. package/dist/lock/manifest.d.ts +60 -0
  114. package/dist/lock/manifest.d.ts.map +1 -0
  115. package/dist/lock/manifest.js +95 -0
  116. package/dist/lock/manifest.js.map +1 -0
  117. package/dist/lock/semver.d.ts +10 -0
  118. package/dist/lock/semver.d.ts.map +1 -0
  119. package/dist/lock/semver.js +13 -0
  120. package/dist/lock/semver.js.map +1 -0
  121. package/dist/seed/index.d.ts +4 -0
  122. package/dist/seed/index.d.ts.map +1 -0
  123. package/dist/seed/index.js +10 -0
  124. package/dist/seed/index.js.map +1 -0
  125. package/dist/seed/input.d.ts +52 -0
  126. package/dist/seed/input.d.ts.map +1 -0
  127. package/dist/seed/input.js +77 -0
  128. package/dist/seed/input.js.map +1 -0
  129. package/dist/seed/seed-platform.d.ts +28 -0
  130. package/dist/seed/seed-platform.d.ts.map +1 -0
  131. package/dist/seed/seed-platform.js +133 -0
  132. package/dist/seed/seed-platform.js.map +1 -0
  133. package/dist/seed/types.d.ts +277 -0
  134. package/dist/seed/types.d.ts.map +1 -0
  135. package/dist/seed/types.js +5 -0
  136. package/dist/seed/types.js.map +1 -0
  137. package/dist/sync/apply.d.ts +29 -0
  138. package/dist/sync/apply.d.ts.map +1 -0
  139. package/dist/sync/apply.js +37 -0
  140. package/dist/sync/apply.js.map +1 -0
  141. package/dist/sync/check.d.ts +56 -0
  142. package/dist/sync/check.d.ts.map +1 -0
  143. package/dist/sync/check.js +108 -0
  144. package/dist/sync/check.js.map +1 -0
  145. package/dist/sync/errors.d.ts +31 -0
  146. package/dist/sync/errors.d.ts.map +1 -0
  147. package/dist/sync/errors.js +23 -0
  148. package/dist/sync/errors.js.map +1 -0
  149. package/dist/sync/index.d.ts +9 -0
  150. package/dist/sync/index.d.ts.map +1 -0
  151. package/dist/sync/index.js +25 -0
  152. package/dist/sync/index.js.map +1 -0
  153. package/dist/sync/ledger.d.ts +65 -0
  154. package/dist/sync/ledger.d.ts.map +1 -0
  155. package/dist/sync/ledger.js +49 -0
  156. package/dist/sync/ledger.js.map +1 -0
  157. package/dist/sync/plan.d.ts +61 -0
  158. package/dist/sync/plan.d.ts.map +1 -0
  159. package/dist/sync/plan.js +79 -0
  160. package/dist/sync/plan.js.map +1 -0
  161. package/dist/sync/promote.d.ts +58 -0
  162. package/dist/sync/promote.d.ts.map +1 -0
  163. package/dist/sync/promote.js +93 -0
  164. package/dist/sync/promote.js.map +1 -0
  165. package/dist/sync/timestamps.d.ts +41 -0
  166. package/dist/sync/timestamps.d.ts.map +1 -0
  167. package/dist/sync/timestamps.js +68 -0
  168. package/dist/sync/timestamps.js.map +1 -0
  169. package/dist/sync/version.d.ts +28 -0
  170. package/dist/sync/version.d.ts.map +1 -0
  171. package/dist/sync/version.js +66 -0
  172. package/dist/sync/version.js.map +1 -0
  173. package/migrations/0001_initial/migration.sql +297 -0
  174. package/migrations/0002_add_personal_access_tokens/migration.sql +31 -0
  175. package/migrations/0003_add_credentials/migration.sql +20 -0
  176. package/migrations/0004_add_notification_deliveries/migration.sql +27 -0
  177. package/migrations/0005_drop_stale_uuid_defaults/migration.sql +35 -0
  178. package/migrations/0006_add_notifications/migration.sql +22 -0
  179. package/migrations/0007_add_push_subscriptions/migration.sql +25 -0
  180. package/migrations/0008_add_jobs/migration.sql +110 -0
  181. package/migrations/0009_add_worker_nodes/migration.sql +90 -0
  182. package/migrations/0010_add_database_backup_runs/migration.sql +104 -0
  183. package/migrations/0011_add_notification_broadcasts/migration.sql +57 -0
  184. package/migrations/0012_add_backup_run_job_link/migration.sql +87 -0
  185. package/migrations/0013_add_job_node_secrets/migration.sql +61 -0
  186. package/migrations/0014_add_backup_run_pg_dump_version/migration.sql +26 -0
  187. package/migrations/0015_add_job_claim_token/migration.sql +92 -0
  188. package/migrations/0016_add_ai_platform/migration.sql +108 -0
  189. package/migrations/0017_revoke_viewer_ai_use/migration.sql +32 -0
  190. package/migrations/0018_add_user_credentials/migration.sql +29 -0
  191. package/migrations/0019_add_device_session_credential_link/migration.sql +52 -0
  192. package/migrations/0020_add_worker_node_vitals/migration.sql +3 -0
  193. package/migrations/0021_add_job_trace_context/migration.sql +6 -0
  194. package/migrations/0022_add_retention_created_at_indexes/migration.sql +13 -0
  195. package/migrations/0023_add_organizations/migration.sql +142 -0
  196. package/migrations/0024_split_system_org_roles/migration.sql +131 -0
  197. package/migrations/manifest.json +240 -0
  198. package/package.json +75 -4
  199. package/raw-sql-indexes.json +49 -0
  200. package/schema/ai.prisma +387 -0
  201. package/schema/base.prisma +11 -0
  202. package/schema/credentials.prisma +108 -0
  203. package/schema/db-backup.prisma +376 -0
  204. package/schema/identity.prisma +407 -0
  205. package/schema/jobs.prisma +731 -0
  206. package/schema/notifications.prisma +610 -0
  207. package/schema/settings.prisma +35 -0
  208. package/schema/storage.prisma +57 -0
@@ -0,0 +1,407 @@
1
+ // =============================================================================
2
+ // User & Identity
3
+ // =============================================================================
4
+
5
+ // @extensible — back-relations only, added with "extend model User { ... }" in a fragment
6
+ model User {
7
+ id String @id @default(uuid()) @db.Uuid
8
+ email String @unique
9
+ displayName String? @map("display_name")
10
+ providerDisplayName String? @map("provider_display_name")
11
+ profileImageUrl String? @map("profile_image_url")
12
+ providerProfileImageUrl String? @map("provider_profile_image_url")
13
+ isActive Boolean @default(true) @map("is_active")
14
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
15
+ updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
16
+
17
+ // Relations
18
+ identities UserIdentity[]
19
+ userRoles UserRole[]
20
+ auditEvents AuditEvent[] @relation("ActorEvents")
21
+ refreshTokens RefreshToken[] @relation("UserRefreshTokens")
22
+ allowlistEntriesAdded AllowedEmail[] @relation("AllowlistAddedBy")
23
+ allowlistEntry AllowedEmail? @relation("AllowlistClaimedBy")
24
+ deviceCodes DeviceCode[] @relation("UserDeviceCodes")
25
+ personalAccessTokens PersonalAccessToken[] @relation("UserPersonalAccessTokens")
26
+ memberships Membership[]
27
+ organizationsCreated Organization[] @relation("OrganizationCreatedBy")
28
+ invitesSent Invite[] @relation("InviteInvitedBy")
29
+ invitesAccepted Invite[] @relation("InviteAcceptedBy")
30
+
31
+ @@map("users")
32
+ }
33
+
34
+ model UserIdentity {
35
+ id String @id @default(uuid()) @db.Uuid
36
+ userId String @map("user_id") @db.Uuid
37
+ provider String
38
+ providerSubject String @map("provider_subject")
39
+ providerEmail String? @map("provider_email")
40
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
41
+
42
+ // Relations
43
+ user User @relation(fields: [userId], references: [id], onDelete: Cascade)
44
+
45
+ @@unique([provider, providerSubject])
46
+ @@map("user_identities")
47
+ }
48
+
49
+ // =============================================================================
50
+ // RBAC - Roles & Permissions
51
+ // =============================================================================
52
+ //
53
+ // Two kinds of role (PP-6.3): SYSTEM roles operate the deployment and are held
54
+ // in `user_roles`; ORG roles operate one organization and are held on a
55
+ // membership (`memberships.role_id`). A role is granted only permissions of its
56
+ // own scope (enforced by the app's permission registry, seeded by
57
+ // `seedPlatform`). Roles stay global rows; per-organization custom roles are a
58
+ // later seam.
59
+
60
+ enum RoleScope {
61
+ system
62
+ org
63
+ }
64
+
65
+ model Role {
66
+ id String @id @default(uuid()) @db.Uuid
67
+ name String @unique
68
+ description String?
69
+ scope RoleScope @default(system)
70
+
71
+ // Relations
72
+ rolePermissions RolePermission[]
73
+ userRoles UserRole[]
74
+ memberships Membership[]
75
+ invites Invite[]
76
+
77
+ @@map("roles")
78
+ }
79
+
80
+ model Permission {
81
+ id String @id @default(uuid()) @db.Uuid
82
+ name String @unique
83
+ description String?
84
+ scope RoleScope @default(system)
85
+
86
+ // Relations
87
+ rolePermissions RolePermission[]
88
+
89
+ @@map("permissions")
90
+ }
91
+
92
+ model RolePermission {
93
+ roleId String @map("role_id") @db.Uuid
94
+ permissionId String @map("permission_id") @db.Uuid
95
+
96
+ // Relations
97
+ role Role @relation(fields: [roleId], references: [id], onDelete: Cascade)
98
+ permission Permission @relation(fields: [permissionId], references: [id], onDelete: Cascade)
99
+
100
+ @@id([roleId, permissionId])
101
+ @@map("role_permissions")
102
+ }
103
+
104
+ model UserRole {
105
+ userId String @map("user_id") @db.Uuid
106
+ roleId String @map("role_id") @db.Uuid
107
+
108
+ // Relations
109
+ user User @relation(fields: [userId], references: [id], onDelete: Cascade)
110
+ role Role @relation(fields: [roleId], references: [id], onDelete: Cascade)
111
+
112
+ @@id([userId, roleId])
113
+ @@map("user_roles")
114
+ }
115
+
116
+ // =============================================================================
117
+ // Audit
118
+ // =============================================================================
119
+
120
+ model AuditEvent {
121
+ id String @id @default(uuid()) @db.Uuid
122
+ actorUserId String? @map("actor_user_id") @db.Uuid
123
+ action String
124
+ targetType String @map("target_type")
125
+ targetId String @map("target_id")
126
+ meta Json?
127
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
128
+
129
+ // Relations
130
+ actorUser User? @relation("ActorEvents", fields: [actorUserId], references: [id], onDelete: SetNull)
131
+
132
+ @@index([actorUserId])
133
+ @@index([targetType, targetId])
134
+ @@index([createdAt])
135
+ @@map("audit_events")
136
+ }
137
+
138
+ // =============================================================================
139
+ // Refresh Tokens (for token rotation)
140
+ // =============================================================================
141
+
142
+ model RefreshToken {
143
+ id String @id @default(uuid()) @db.Uuid
144
+ userId String @map("user_id") @db.Uuid
145
+ tokenHash String @unique @map("token_hash")
146
+ expiresAt DateTime @map("expires_at") @db.Timestamptz
147
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
148
+ revokedAt DateTime? @map("revoked_at") @db.Timestamptz
149
+ // The device-authorization session that minted this refresh token, if any
150
+ // (issue #518). Lets revoking a `DeviceCode` session reach the refresh
151
+ // token it handed out. Nullable: most refresh tokens come from the
152
+ // interactive login flow, not the device flow. `SetNull` because a device
153
+ // code can be purged independently of the token lifecycle it kicked off.
154
+ deviceCodeId String? @map("device_code_id") @db.Uuid
155
+ // The organization this session is active in (PP-6.1). Nullable (expand
156
+ // step); backfilled to the default organization. Carried by the active-org
157
+ // token (PP-6.4). `Cascade`: a credential has no meaning once its org is gone.
158
+ orgId String? @map("org_id") @db.Uuid
159
+
160
+ // Relations
161
+ user User @relation("UserRefreshTokens", fields: [userId], references: [id], onDelete: Cascade)
162
+ deviceCode DeviceCode? @relation(fields: [deviceCodeId], references: [id], onDelete: SetNull)
163
+ org Organization? @relation(fields: [orgId], references: [id], onDelete: Cascade)
164
+
165
+ @@index([userId])
166
+ @@index([expiresAt])
167
+ @@index([deviceCodeId])
168
+ @@index([orgId])
169
+ @@map("refresh_tokens")
170
+ }
171
+
172
+ // =============================================================================
173
+ // Personal Access Tokens (PAT)
174
+ // =============================================================================
175
+
176
+ enum PatDurationUnit {
177
+ minutes
178
+ days
179
+ months
180
+ }
181
+
182
+ model PersonalAccessToken {
183
+ id String @id @default(uuid()) @db.Uuid
184
+ userId String @map("user_id") @db.Uuid
185
+ name String
186
+ tokenHash String @unique @map("token_hash")
187
+ tokenPrefix String @map("token_prefix")
188
+ durationValue Int @map("duration_value")
189
+ durationUnit PatDurationUnit @map("duration_unit")
190
+ expiresAt DateTime @map("expires_at") @db.Timestamptz
191
+ lastUsedAt DateTime? @map("last_used_at") @db.Timestamptz
192
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
193
+ revokedAt DateTime? @map("revoked_at") @db.Timestamptz
194
+ // The organization the token acts in (PP-6.1). Nullable (expand step);
195
+ // backfilled to the default organization. `Cascade`: see `RefreshToken.orgId`.
196
+ orgId String? @map("org_id") @db.Uuid
197
+
198
+ // Relations
199
+ user User @relation("UserPersonalAccessTokens", fields: [userId], references: [id], onDelete: Cascade)
200
+ org Organization? @relation(fields: [orgId], references: [id], onDelete: Cascade)
201
+ // The device-authorization sessions that issued this PAT (issue #518).
202
+ // Almost always at most one, but not constrained `@unique` here — see
203
+ // `DeviceCode.patId`.
204
+ deviceCodes DeviceCode[]
205
+
206
+ @@index([userId])
207
+ @@index([expiresAt])
208
+ @@index([orgId])
209
+ @@map("personal_access_tokens")
210
+ }
211
+
212
+ // =============================================================================
213
+ // Allowlist (Email Access Control)
214
+ // =============================================================================
215
+
216
+ model AllowedEmail {
217
+ id String @id @default(uuid()) @db.Uuid
218
+ email String @unique
219
+ addedById String? @map("added_by_id") @db.Uuid
220
+ addedAt DateTime @default(now()) @map("added_at") @db.Timestamptz
221
+ claimedById String? @unique @map("claimed_by_id") @db.Uuid
222
+ claimedAt DateTime? @map("claimed_at") @db.Timestamptz
223
+ notes String?
224
+
225
+ // Relations
226
+ addedBy User? @relation("AllowlistAddedBy", fields: [addedById], references: [id], onDelete: SetNull)
227
+ claimedBy User? @relation("AllowlistClaimedBy", fields: [claimedById], references: [id], onDelete: SetNull)
228
+
229
+ @@map("allowed_emails")
230
+ }
231
+
232
+ // =============================================================================
233
+ // Device Authorization Flow (RFC 8628)
234
+ // =============================================================================
235
+
236
+ enum DeviceCodeStatus {
237
+ pending
238
+ approved
239
+ denied
240
+ expired
241
+ }
242
+
243
+ model DeviceCode {
244
+ id String @id @default(uuid()) @db.Uuid
245
+ deviceCode String @unique @map("device_code")
246
+ userCode String @unique @map("user_code")
247
+ userId String? @map("user_id") @db.Uuid
248
+ status DeviceCodeStatus @default(pending)
249
+ clientInfo Json? @map("client_info")
250
+ scopes String[]
251
+ // The code's own expiry (RFC 8628 `expires_in`) — when the user/device
252
+ // codes stop being redeemable if never approved. Distinct from
253
+ // `credentialExpiresAt`, which is the lifetime of what got issued once
254
+ // approval happened (issue #518).
255
+ expiresAt DateTime @map("expires_at") @db.Timestamptz
256
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
257
+ updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
258
+
259
+ // The PAT minted for this device once the polling client collected it
260
+ // (issue #518). Nullable until collection; `SetNull` so deleting the PAT
261
+ // (or the PAT row aging out) never cascades into deleting the device-code
262
+ // history. This is what lets "revoke this device session" reach the
263
+ // credential the device is actually holding.
264
+ patId String? @map("pat_id") @db.Uuid
265
+
266
+ // When the polling client (`POST /api/auth/device/token`) collected the
267
+ // credential above. Null until collection. Distinguishes "approved,
268
+ // waiting to be picked up" from "picked up and in active use" for the
269
+ // sessions list.
270
+ collectedAt DateTime? @map("collected_at") @db.Timestamptz
271
+
272
+ // The collected credential's own expiry, copied from the PAT at
273
+ // collection time so the sessions list can filter "still valid" without a
274
+ // join. Null until collection.
275
+ credentialExpiresAt DateTime? @map("credential_expires_at") @db.Timestamptz
276
+
277
+ // Set when this device session (and the credential it issued, if any) is
278
+ // revoked — from the code itself if revoked before collection, from the
279
+ // session-revocation endpoint after. Null means still active.
280
+ revokedAt DateTime? @map("revoked_at") @db.Timestamptz
281
+
282
+ // The organization the approved device session acts in (PP-6.1). Nullable
283
+ // (expand step); backfilled to the default organization. `Cascade`: see
284
+ // `RefreshToken.orgId`.
285
+ orgId String? @map("org_id") @db.Uuid
286
+
287
+ // Relations
288
+ user User? @relation("UserDeviceCodes", fields: [userId], references: [id], onDelete: Cascade)
289
+ org Organization? @relation(fields: [orgId], references: [id], onDelete: Cascade)
290
+ pat PersonalAccessToken? @relation(fields: [patId], references: [id], onDelete: SetNull)
291
+
292
+ // Back-relation: refresh tokens minted through this device-authorization
293
+ // session (issue #518). See `RefreshToken.deviceCodeId`.
294
+ refreshTokens RefreshToken[]
295
+
296
+ @@index([deviceCode])
297
+ @@index([userCode])
298
+ @@index([status, expiresAt])
299
+ @@index([userId])
300
+ // Serves the sessions list (`GET /api/auth/device/sessions`, issue #518):
301
+ // "this user's device sessions that are not revoked" —
302
+ // approved-but-uncollected and collected-unexpired both live under
303
+ // `revokedAt IS NULL`; the remaining collected/expiry filtering happens
304
+ // in the query predicate, not a second index.
305
+ @@index([userId, revokedAt])
306
+ @@index([orgId])
307
+ @@map("device_codes")
308
+ }
309
+
310
+ // =============================================================================
311
+ // Organizations (tenancy foundation, PP-6.1)
312
+ // =============================================================================
313
+ //
314
+ // Identity tables, read across organizations at login and guarded by service
315
+ // code: no RLS here (ADR 0002). The default organization is unique by the
316
+ // raw-SQL partial index `organizations_default_uniq_idx` (migration SQL only;
317
+ // Prisma cannot express it). A membership carries its org role (PP-6.3); an
318
+ // invite may name the org role the invitee will get.
319
+
320
+ enum MembershipStatus {
321
+ active
322
+ suspended
323
+ }
324
+
325
+ enum InviteStatus {
326
+ pending
327
+ accepted
328
+ revoked
329
+ expired
330
+ }
331
+
332
+ model Organization {
333
+ id String @id @default(uuid()) @db.Uuid
334
+ name String
335
+ // Lower-case [a-z0-9-], 2..63 characters (validated by the service).
336
+ slug String @unique
337
+ // Exactly one row is true: organizations_default_uniq_idx.
338
+ isDefault Boolean @default(false) @map("is_default")
339
+ createdById String? @map("created_by_id") @db.Uuid
340
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
341
+ updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
342
+
343
+ // Relations
344
+ createdBy User? @relation("OrganizationCreatedBy", fields: [createdById], references: [id], onDelete: SetNull)
345
+ memberships Membership[]
346
+ invites Invite[]
347
+ refreshTokens RefreshToken[]
348
+ personalAccessTokens PersonalAccessToken[]
349
+ deviceCodes DeviceCode[]
350
+
351
+ @@map("organizations")
352
+ }
353
+
354
+ model Membership {
355
+ id String @id @default(uuid()) @db.Uuid
356
+ orgId String @map("org_id") @db.Uuid
357
+ userId String @map("user_id") @db.Uuid
358
+ status MembershipStatus @default(active)
359
+ // Picks the default active organization at login (PP-6.4).
360
+ lastActiveAt DateTime? @map("last_active_at") @db.Timestamptz
361
+ // The member's org-scoped role (PP-6.3). `Restrict`: a role in use cannot be
362
+ // deleted out from under its members.
363
+ roleId String @map("role_id") @db.Uuid
364
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
365
+ updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
366
+
367
+ // Relations
368
+ org Organization @relation(fields: [orgId], references: [id], onDelete: Cascade)
369
+ user User @relation(fields: [userId], references: [id], onDelete: Cascade)
370
+ role Role @relation(fields: [roleId], references: [id], onDelete: Restrict)
371
+
372
+ @@unique([orgId, userId])
373
+ @@index([userId])
374
+ @@index([roleId])
375
+ @@map("memberships")
376
+ }
377
+
378
+ model Invite {
379
+ id String @id @default(uuid()) @db.Uuid
380
+ orgId String @map("org_id") @db.Uuid
381
+ // Stored lower-cased.
382
+ email String
383
+ status InviteStatus @default(pending)
384
+ // sha256 of an emailed accept token, if one is used.
385
+ tokenHash String? @unique @map("token_hash")
386
+ expiresAt DateTime? @map("expires_at") @db.Timestamptz
387
+ invitedById String? @map("invited_by_id") @db.Uuid
388
+ acceptedById String? @map("accepted_by_id") @db.Uuid
389
+ acceptedAt DateTime? @map("accepted_at") @db.Timestamptz
390
+ notes String?
391
+ // The org role the invitee gets on acceptance (PP-6.3). NULL means the
392
+ // default org role (`viewer`). `Restrict`, as on `Membership.roleId`.
393
+ roleId String? @map("role_id") @db.Uuid
394
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
395
+
396
+ // Relations
397
+ org Organization @relation(fields: [orgId], references: [id], onDelete: Cascade)
398
+ role Role? @relation(fields: [roleId], references: [id], onDelete: Restrict)
399
+ invitedBy User? @relation("InviteInvitedBy", fields: [invitedById], references: [id], onDelete: SetNull)
400
+ acceptedBy User? @relation("InviteAcceptedBy", fields: [acceptedById], references: [id], onDelete: SetNull)
401
+
402
+ // Re-inviting updates the row.
403
+ @@unique([orgId, email])
404
+ @@index([email])
405
+ @@index([roleId])
406
+ @@map("org_invites")
407
+ }