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

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 (244) hide show
  1. package/CHANGELOG.md +74 -0
  2. package/LICENSE +21 -0
  3. package/README.md +525 -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 +66 -0
  8. package/dist/baseline/deps.js.map +1 -0
  9. package/dist/baseline/errors.d.ts +36 -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 +127 -0
  34. package/dist/baseline/report.d.ts.map +1 -0
  35. package/dist/baseline/report.js +89 -0
  36. package/dist/baseline/report.js.map +1 -0
  37. package/dist/baseline/run.d.ts +117 -0
  38. package/dist/baseline/run.d.ts.map +1 -0
  39. package/dist/baseline/run.js +358 -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 +311 -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/app-objects.d.ts +107 -0
  78. package/dist/drift/app-objects.d.ts.map +1 -0
  79. package/dist/drift/app-objects.js +193 -0
  80. package/dist/drift/app-objects.js.map +1 -0
  81. package/dist/drift/db-conformance.d.ts +52 -0
  82. package/dist/drift/db-conformance.d.ts.map +1 -0
  83. package/dist/drift/db-conformance.js +89 -0
  84. package/dist/drift/db-conformance.js.map +1 -0
  85. package/dist/drift/index.d.ts +8 -0
  86. package/dist/drift/index.d.ts.map +1 -0
  87. package/dist/drift/index.js +43 -0
  88. package/dist/drift/index.js.map +1 -0
  89. package/dist/drift/raw-sql-indexes.d.ts +106 -0
  90. package/dist/drift/raw-sql-indexes.d.ts.map +1 -0
  91. package/dist/drift/raw-sql-indexes.js +113 -0
  92. package/dist/drift/raw-sql-indexes.js.map +1 -0
  93. package/dist/drift/raw-sql-tripwire.d.ts +191 -0
  94. package/dist/drift/raw-sql-tripwire.d.ts.map +1 -0
  95. package/dist/drift/raw-sql-tripwire.js +422 -0
  96. package/dist/drift/raw-sql-tripwire.js.map +1 -0
  97. package/dist/drift/rls-policies.d.ts +226 -0
  98. package/dist/drift/rls-policies.d.ts.map +1 -0
  99. package/dist/drift/rls-policies.js +317 -0
  100. package/dist/drift/rls-policies.js.map +1 -0
  101. package/dist/drift/run.d.ts +152 -0
  102. package/dist/drift/run.d.ts.map +1 -0
  103. package/dist/drift/run.js +252 -0
  104. package/dist/drift/run.js.map +1 -0
  105. package/dist/drift/sql-expression.d.ts +27 -0
  106. package/dist/drift/sql-expression.d.ts.map +1 -0
  107. package/dist/drift/sql-expression.js +64 -0
  108. package/dist/drift/sql-expression.js.map +1 -0
  109. package/dist/index.d.ts +16 -0
  110. package/dist/index.d.ts.map +1 -0
  111. package/dist/index.js +33 -0
  112. package/dist/index.js.map +1 -0
  113. package/dist/lock/hash.d.ts +13 -0
  114. package/dist/lock/hash.d.ts.map +1 -0
  115. package/dist/lock/hash.js +19 -0
  116. package/dist/lock/hash.js.map +1 -0
  117. package/dist/lock/index.d.ts +5 -0
  118. package/dist/lock/index.d.ts.map +1 -0
  119. package/dist/lock/index.js +20 -0
  120. package/dist/lock/index.js.map +1 -0
  121. package/dist/lock/lock.d.ts +154 -0
  122. package/dist/lock/lock.d.ts.map +1 -0
  123. package/dist/lock/lock.js +151 -0
  124. package/dist/lock/lock.js.map +1 -0
  125. package/dist/lock/manifest.d.ts +68 -0
  126. package/dist/lock/manifest.d.ts.map +1 -0
  127. package/dist/lock/manifest.js +96 -0
  128. package/dist/lock/manifest.js.map +1 -0
  129. package/dist/lock/semver.d.ts +10 -0
  130. package/dist/lock/semver.d.ts.map +1 -0
  131. package/dist/lock/semver.js +13 -0
  132. package/dist/lock/semver.js.map +1 -0
  133. package/dist/seed/index.d.ts +5 -0
  134. package/dist/seed/index.d.ts.map +1 -0
  135. package/dist/seed/index.js +16 -0
  136. package/dist/seed/index.js.map +1 -0
  137. package/dist/seed/input.d.ts +52 -0
  138. package/dist/seed/input.d.ts.map +1 -0
  139. package/dist/seed/input.js +77 -0
  140. package/dist/seed/input.js.map +1 -0
  141. package/dist/seed/seed-platform.d.ts +41 -0
  142. package/dist/seed/seed-platform.d.ts.map +1 -0
  143. package/dist/seed/seed-platform.js +165 -0
  144. package/dist/seed/seed-platform.js.map +1 -0
  145. package/dist/seed/steps.d.ts +172 -0
  146. package/dist/seed/steps.d.ts.map +1 -0
  147. package/dist/seed/steps.js +136 -0
  148. package/dist/seed/steps.js.map +1 -0
  149. package/dist/seed/types.d.ts +287 -0
  150. package/dist/seed/types.d.ts.map +1 -0
  151. package/dist/seed/types.js +5 -0
  152. package/dist/seed/types.js.map +1 -0
  153. package/dist/sync/app-migrations.d.ts +52 -0
  154. package/dist/sync/app-migrations.d.ts.map +1 -0
  155. package/dist/sync/app-migrations.js +62 -0
  156. package/dist/sync/app-migrations.js.map +1 -0
  157. package/dist/sync/apply.d.ts +29 -0
  158. package/dist/sync/apply.d.ts.map +1 -0
  159. package/dist/sync/apply.js +38 -0
  160. package/dist/sync/apply.js.map +1 -0
  161. package/dist/sync/check.d.ts +59 -0
  162. package/dist/sync/check.d.ts.map +1 -0
  163. package/dist/sync/check.js +115 -0
  164. package/dist/sync/check.js.map +1 -0
  165. package/dist/sync/errors.d.ts +32 -0
  166. package/dist/sync/errors.d.ts.map +1 -0
  167. package/dist/sync/errors.js +23 -0
  168. package/dist/sync/errors.js.map +1 -0
  169. package/dist/sync/index.d.ts +10 -0
  170. package/dist/sync/index.d.ts.map +1 -0
  171. package/dist/sync/index.js +27 -0
  172. package/dist/sync/index.js.map +1 -0
  173. package/dist/sync/ledger.d.ts +65 -0
  174. package/dist/sync/ledger.d.ts.map +1 -0
  175. package/dist/sync/ledger.js +49 -0
  176. package/dist/sync/ledger.js.map +1 -0
  177. package/dist/sync/plan.d.ts +63 -0
  178. package/dist/sync/plan.d.ts.map +1 -0
  179. package/dist/sync/plan.js +86 -0
  180. package/dist/sync/plan.js.map +1 -0
  181. package/dist/sync/promote.d.ts +62 -0
  182. package/dist/sync/promote.d.ts.map +1 -0
  183. package/dist/sync/promote.js +95 -0
  184. package/dist/sync/promote.js.map +1 -0
  185. package/dist/sync/timestamps.d.ts +41 -0
  186. package/dist/sync/timestamps.d.ts.map +1 -0
  187. package/dist/sync/timestamps.js +68 -0
  188. package/dist/sync/timestamps.js.map +1 -0
  189. package/dist/sync/version.d.ts +28 -0
  190. package/dist/sync/version.d.ts.map +1 -0
  191. package/dist/sync/version.js +66 -0
  192. package/dist/sync/version.js.map +1 -0
  193. package/migrations/0001_initial/migration.sql +297 -0
  194. package/migrations/0002_add_personal_access_tokens/migration.sql +31 -0
  195. package/migrations/0003_add_credentials/migration.sql +20 -0
  196. package/migrations/0004_add_notification_deliveries/migration.sql +27 -0
  197. package/migrations/0005_drop_stale_uuid_defaults/migration.sql +35 -0
  198. package/migrations/0006_add_notifications/migration.sql +22 -0
  199. package/migrations/0007_add_push_subscriptions/migration.sql +25 -0
  200. package/migrations/0008_add_jobs/migration.sql +110 -0
  201. package/migrations/0009_add_worker_nodes/migration.sql +90 -0
  202. package/migrations/0010_add_database_backup_runs/migration.sql +104 -0
  203. package/migrations/0011_add_notification_broadcasts/migration.sql +57 -0
  204. package/migrations/0012_add_backup_run_job_link/migration.sql +87 -0
  205. package/migrations/0013_add_job_node_secrets/migration.sql +61 -0
  206. package/migrations/0014_add_backup_run_pg_dump_version/migration.sql +26 -0
  207. package/migrations/0015_add_job_claim_token/migration.sql +92 -0
  208. package/migrations/0016_add_ai_platform/migration.sql +108 -0
  209. package/migrations/0017_revoke_viewer_ai_use/migration.sql +32 -0
  210. package/migrations/0018_add_user_credentials/migration.sql +29 -0
  211. package/migrations/0019_add_device_session_credential_link/migration.sql +52 -0
  212. package/migrations/0020_add_worker_node_vitals/migration.sql +3 -0
  213. package/migrations/0021_add_job_trace_context/migration.sql +6 -0
  214. package/migrations/0022_add_retention_created_at_indexes/migration.sql +13 -0
  215. package/migrations/0023_add_organizations/migration.sql +142 -0
  216. package/migrations/0024_split_system_org_roles/migration.sql +131 -0
  217. package/migrations/0025_org_scoped_rls/migration.sql +215 -0
  218. package/migrations/0026_add_groups/migration.sql +165 -0
  219. package/migrations/0027_add_grants/migration.sql +117 -0
  220. package/migrations/0028_add_org_settings/migration.sql +49 -0
  221. package/migrations/0029_add_org_credentials/migration.sql +54 -0
  222. package/migrations/0030_add_jobs_org_id/migration.sql +18 -0
  223. package/migrations/0031_add_jobs_org_id_status_index/migration.sql +6 -0
  224. package/migrations/0032_add_broadcast_target_org/migration.sql +21 -0
  225. package/migrations/0033_add_android_app/migration.sql +60 -0
  226. package/migrations/0034_open_job_reason/migration.sql +16 -0
  227. package/migrations/0035_ai_usage_feature/migration.sql +37 -0
  228. package/migrations/0036_add_app_metadata/migration.sql +49 -0
  229. package/migrations/0037_open_sharing_vocabularies/migration.sql +97 -0
  230. package/migrations/manifest.json +379 -0
  231. package/package.json +78 -4
  232. package/raw-sql-indexes.json +94 -0
  233. package/rls-policies.json +74 -0
  234. package/schema/ai.prisma +444 -0
  235. package/schema/android-app.prisma +47 -0
  236. package/schema/base.prisma +11 -0
  237. package/schema/credentials.prisma +170 -0
  238. package/schema/db-backup.prisma +376 -0
  239. package/schema/identity.prisma +439 -0
  240. package/schema/jobs.prisma +760 -0
  241. package/schema/notifications.prisma +649 -0
  242. package/schema/settings.prisma +60 -0
  243. package/schema/sharing.prisma +223 -0
  244. package/schema/storage.prisma +84 -0
@@ -0,0 +1,60 @@
1
+ // =============================================================================
2
+ // Settings
3
+ // =============================================================================
4
+
5
+ model SystemSettings {
6
+ id String @id @default(uuid()) @db.Uuid
7
+ key String @unique
8
+ value Json
9
+ version Int @default(1)
10
+ updatedByUserId String? @map("updated_by_user_id") @db.Uuid
11
+ updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
12
+
13
+ // Relations
14
+ updatedByUser User? @relation("SettingsUpdater", fields: [updatedByUserId], references: [id], onDelete: SetNull)
15
+
16
+ @@map("system_settings")
17
+ }
18
+
19
+ model UserSettings {
20
+ id String @id @default(uuid()) @db.Uuid
21
+ userId String @unique @map("user_id") @db.Uuid
22
+ value Json
23
+ version Int @default(1)
24
+ updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
25
+
26
+ // Relations
27
+ user User @relation(fields: [userId], references: [id], onDelete: Cascade)
28
+
29
+ @@map("user_settings")
30
+ }
31
+
32
+ // One organization's overrides of the org-overridable system settings
33
+ // namespaces (#733): `{ "<namespace>": { "<field>": value } }`, only the fields
34
+ // each namespace's `org.schema` allows. An org table: FORCEd row-level security
35
+ // on org_id (`org_settings_org_isolation`, migration 0028_add_org_settings),
36
+ // reached only through `runInOrg`. Its own `version` for If-Match.
37
+ model OrgSettings {
38
+ id String @id @default(uuid()) @db.Uuid
39
+ orgId String @unique @map("org_id") @db.Uuid
40
+ value Json @db.JsonB
41
+ version Int @default(1)
42
+ updatedByUserId String? @map("updated_by_user_id") @db.Uuid
43
+ updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
44
+
45
+ // Relations
46
+ org Organization @relation(fields: [orgId], references: [id], onDelete: Cascade)
47
+ updatedByUser User? @relation("OrgSettingsUpdater", fields: [updatedByUserId], references: [id], onDelete: SetNull)
48
+
49
+ @@map("org_settings")
50
+ }
51
+
52
+ extend model User {
53
+ userSettings UserSettings?
54
+ settingsUpdates SystemSettings[] @relation("SettingsUpdater")
55
+ orgSettingsUpdates OrgSettings[] @relation("OrgSettingsUpdater")
56
+ }
57
+
58
+ extend model Organization {
59
+ settings OrgSettings?
60
+ }
@@ -0,0 +1,223 @@
1
+ // =============================================================================
2
+ // Sharing: groups (PP-7.1, issue #728) and grants (PP-7.2, issue #729)
3
+ // =============================================================================
4
+ //
5
+ // A GROUP is a set of users INSIDE ONE ORGANIZATION that can own and share
6
+ // content (spec decision D6: groups are sharing groups, never tenants). Every
7
+ // row carries `org_id` and falls under the organization row-level-security
8
+ // model of `0025_org_scoped_rls`: the three tables `ENABLE` and `FORCE ROW
9
+ // LEVEL SECURITY` with one `<table>_org_isolation` policy each (migration
10
+ // `0026_add_groups`, listed in `rls-policies.json`).
11
+ //
12
+ // A member row and an invite row reach their group through the COMPOSITE key
13
+ // `(group_id, org_id)` -> `groups (id, org_id)`: a foreign-key check bypasses
14
+ // row-level security, so a plain key would let one organization store a
15
+ // reference to another organization's group, and the composite key also
16
+ // guarantees `group_members.org_id = groups.org_id`.
17
+ //
18
+ // `group_invites_pending_uniq_idx` (one PENDING invite per group and address)
19
+ // exists only in migration SQL: it is a partial unique index, which Prisma
20
+ // cannot express. Never "fix" the drift with `@@unique([groupId, email])`: a
21
+ // declined or revoked address must be invitable again.
22
+ //
23
+ // The names below are part of the public contract once released.
24
+ // =============================================================================
25
+
26
+ // The member and invite `role` columns are plain text (migration
27
+ // `0037_open_sharing_vocabularies`): the vocabulary is the API's group-role
28
+ // registry (`registerGroupRole`), which ships `admin` (manage the group, its
29
+ // members and invites; full rights on group-owned resources), `editor` (write
30
+ // on group-owned resources, MemoriaHub "collaborator") and `viewer` (read).
31
+ // A row must outlive the registry that produced it, so there is no CHECK.
32
+
33
+ // @extensible — back-relations only, added with "extend model Group { ... }" in a fragment
34
+ model Group {
35
+ id String @id @default(uuid()) @db.Uuid
36
+ orgId String @map("org_id") @db.Uuid
37
+ name String
38
+ description String?
39
+ /// Apps keep extra fields here (spec: never add columns to package-owned tables).
40
+ metadata Json? @db.JsonB
41
+ createdById String? @map("created_by_id") @db.Uuid
42
+ // Optimistic concurrency for PATCH (If-Match), per docs/API.md.
43
+ version Int @default(1)
44
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
45
+ updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
46
+
47
+ // Relations
48
+ org Organization @relation(fields: [orgId], references: [id], onDelete: Cascade)
49
+ createdBy User? @relation("GroupCreatedBy", fields: [createdById], references: [id], onDelete: SetNull)
50
+ members GroupMember[]
51
+ invites GroupInvite[]
52
+ grants Grant[]
53
+
54
+ // The target of the members' and invites' composite foreign keys.
55
+ @@unique([id, orgId])
56
+ @@index([orgId])
57
+ @@map("groups")
58
+ }
59
+
60
+ model GroupMember {
61
+ id String @id @default(uuid()) @db.Uuid
62
+ groupId String @map("group_id") @db.Uuid
63
+ userId String @map("user_id") @db.Uuid
64
+ // Denormalised for row-level security; equal to groups.org_id by the
65
+ // composite foreign key below.
66
+ orgId String @map("org_id") @db.Uuid
67
+ /// A registered group role id (`registerGroupRole`). A plain string: a row must outlive the registry that produced it.
68
+ role String @default("viewer")
69
+ addedById String? @map("added_by_id") @db.Uuid
70
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
71
+ updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
72
+
73
+ // Relations
74
+ //
75
+ // Composite: (group_id, org_id) -> groups (id, org_id).
76
+ group Group @relation(fields: [groupId, orgId], references: [id, orgId], onDelete: Cascade)
77
+ org Organization @relation(fields: [orgId], references: [id], onDelete: Cascade)
78
+ user User @relation("GroupMemberUser", fields: [userId], references: [id], onDelete: Cascade)
79
+ addedBy User? @relation("GroupMemberAddedBy", fields: [addedById], references: [id], onDelete: SetNull)
80
+
81
+ @@unique([groupId, userId])
82
+ @@index([userId])
83
+ @@index([orgId])
84
+ @@map("group_members")
85
+ }
86
+
87
+ model GroupInvite {
88
+ id String @id @default(uuid()) @db.Uuid
89
+ groupId String @map("group_id") @db.Uuid
90
+ orgId String @map("org_id") @db.Uuid
91
+ // Stored lower-cased. At most one PENDING invite per (group, email):
92
+ // group_invites_pending_uniq_idx, raw SQL only.
93
+ email String
94
+ /// The role the invitee gets on acceptance: a registered group role id. A plain string.
95
+ role String @default("viewer")
96
+ invitedById String? @map("invited_by_id") @db.Uuid
97
+ // NULL: no expiry. Expiry is evaluated when the invite is read; no sweep.
98
+ expiresAt DateTime? @map("expires_at") @db.Timestamptz
99
+ acceptedAt DateTime? @map("accepted_at") @db.Timestamptz
100
+ acceptedById String? @map("accepted_by_id") @db.Uuid
101
+ declinedAt DateTime? @map("declined_at") @db.Timestamptz
102
+ revokedAt DateTime? @map("revoked_at") @db.Timestamptz
103
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
104
+
105
+ // Relations
106
+ //
107
+ // Composite: (group_id, org_id) -> groups (id, org_id).
108
+ group Group @relation(fields: [groupId, orgId], references: [id, orgId], onDelete: Cascade)
109
+ org Organization @relation(fields: [orgId], references: [id], onDelete: Cascade)
110
+ invitedBy User? @relation("GroupInviteInvitedBy", fields: [invitedById], references: [id], onDelete: SetNull)
111
+ acceptedBy User? @relation("GroupInviteAcceptedBy", fields: [acceptedById], references: [id], onDelete: SetNull)
112
+
113
+ @@index([email])
114
+ @@index([groupId])
115
+ @@index([orgId])
116
+ @@map("group_invites")
117
+ }
118
+
119
+ // =============================================================================
120
+ // Sharing: grants (PP-7.2, issue #729)
121
+ // =============================================================================
122
+ //
123
+ // A GRANT shares ONE record of a registered resource type with a user, a
124
+ // group of the same organization or (#730) a link, with a role and an
125
+ // optional expiry. The resource is POLYMORPHIC, `(resource_type,
126
+ // resource_id)`, with no foreign key: the platform cannot know app tables.
127
+ // Integrity comes from `GrantsService.deleteForResources()` (called inside the
128
+ // app's delete transaction) and the `sharing.grants.prune` job.
129
+ //
130
+ // Revocation is SOFT (`revoked_at`, `revoked_by_id`) so the audit trail
131
+ // survives; the prune job deletes revoked and expired rows after the
132
+ // retention period.
133
+ //
134
+ // Raw SQL only (migration `0027_add_grants`, intentional schema drift):
135
+ // grants_active_user_uniq_idx one ACTIVE grant per (resource, user)
136
+ // grants_active_group_uniq_idx one ACTIVE grant per (resource, group)
137
+ // grants_grantee_consistency_check the grantee columns match grantee_kind
138
+ // grants_active_ref_uniq_idx one ACTIVE grant per (resource, app kind, reference) (0037)
139
+ // Never "fix" the drift with `@@unique`, and never replace the indexes with a
140
+ // `findFirst` pre-check: the service upserts on them.
141
+ //
142
+ // The group grantee reaches its group through the COMPOSITE key
143
+ // `(grantee_group_id, org_id)` -> `groups (id, org_id)`, so a grant can never
144
+ // point at another organization's group (a foreign-key check bypasses
145
+ // row-level security).
146
+ // =============================================================================
147
+
148
+ // `grants.grantee_kind` is plain text (migration `0037_open_sharing_vocabularies`):
149
+ // the platform ships `user` (one user of the resource's organization), `group`
150
+ // (one group of the organization) and `link` (anyone holding the link token,
151
+ // #730), and an app registers more with `registerGranteeKind`. A grant of an
152
+ // app kind stores its reference in `grantee_ref` and leaves the three
153
+ // built-in grantee columns NULL; `grants_grantee_consistency_check` allows
154
+ // exactly those four shapes and `grants_active_ref_uniq_idx` keeps one ACTIVE
155
+ // grant per (resource, kind, reference).
156
+
157
+ // @extensible — back-relations only, added with "extend model Grant { ... }" in a fragment
158
+ // (an app's own annotation of a grant, for example an approval record). The table is
159
+ // org-owned with row-level security forced, so a table of the app's that holds `org_id`
160
+ // links with a composite foreign key `(grant_id, org_id)` -> `grants (id, org_id)`; the
161
+ // `@@unique([id, orgId])` below exists for it. The two partial unique indexes of the
162
+ // table stay raw SQL (never an `@@unique` on their columns).
163
+ model Grant {
164
+ id String @id @default(uuid()) @db.Uuid
165
+ orgId String @map("org_id") @db.Uuid
166
+ /// Registered resource type id. Permanent once rows exist, like a job type string.
167
+ resourceType String @map("resource_type")
168
+ resourceId String @map("resource_id") @db.Uuid
169
+ /// `user`, `group`, `link` or a registered app kind. A plain string: a row must outlive the registry that produced it.
170
+ granteeKind String @map("grantee_kind")
171
+ granteeUserId String? @map("grantee_user_id") @db.Uuid
172
+ granteeGroupId String? @map("grantee_group_id") @db.Uuid
173
+ /// The reference of an app-registered grantee kind (a domain, a team id), as its `validateGrantee` normalised it. NULL for the built-in kinds.
174
+ granteeRef String? @map("grantee_ref")
175
+ /// One of the resource type's registered roles. A plain string: a row must outlive the registry that produced it.
176
+ role String
177
+ /// Link grants only (#730): SHA-256 of the token, and the token encrypted with the platform secret cipher.
178
+ linkTokenHash String? @unique @map("link_token_hash")
179
+ linkTokenCiphertext String? @map("link_token_ciphertext") @db.Text
180
+ linkLabel String? @map("link_label")
181
+ expiresAt DateTime? @map("expires_at") @db.Timestamptz
182
+ revokedAt DateTime? @map("revoked_at") @db.Timestamptz
183
+ revokedById String? @map("revoked_by_id") @db.Uuid
184
+ grantedById String? @map("granted_by_id") @db.Uuid
185
+ /// Apps keep extra fields here (spec: never add columns to package-owned tables).
186
+ metadata Json? @db.JsonB
187
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
188
+ updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
189
+
190
+ // Relations
191
+ org Organization @relation(fields: [orgId], references: [id], onDelete: Cascade)
192
+ granteeUser User? @relation("GrantGranteeUser", fields: [granteeUserId], references: [id], onDelete: Cascade)
193
+ // Composite: (grantee_group_id, org_id) -> groups (id, org_id).
194
+ granteeGroup Group? @relation(fields: [granteeGroupId, orgId], references: [id, orgId], onDelete: Cascade)
195
+ revokedBy User? @relation("GrantRevokedBy", fields: [revokedById], references: [id], onDelete: SetNull)
196
+ grantedBy User? @relation("GrantGrantedBy", fields: [grantedById], references: [id], onDelete: SetNull)
197
+
198
+ @@index([resourceType, resourceId])
199
+ @@index([granteeUserId])
200
+ @@index([granteeGroupId])
201
+ @@index([orgId])
202
+ // The target of an app table's composite foreign key (id, org_id); see the note above the model.
203
+ @@unique([id, orgId])
204
+ @@map("grants")
205
+ }
206
+
207
+ extend model User {
208
+ groupsCreated Group[] @relation("GroupCreatedBy")
209
+ groupMemberships GroupMember[] @relation("GroupMemberUser")
210
+ groupMembersAdded GroupMember[] @relation("GroupMemberAddedBy")
211
+ groupInvitesSent GroupInvite[] @relation("GroupInviteInvitedBy")
212
+ groupInvitesClaimed GroupInvite[] @relation("GroupInviteAcceptedBy")
213
+ grantsReceived Grant[] @relation("GrantGranteeUser")
214
+ grantsRevoked Grant[] @relation("GrantRevokedBy")
215
+ grantsGiven Grant[] @relation("GrantGrantedBy")
216
+ }
217
+
218
+ extend model Organization {
219
+ groups Group[]
220
+ groupMembers GroupMember[]
221
+ groupInvites GroupInvite[]
222
+ grants Grant[]
223
+ }
@@ -0,0 +1,84 @@
1
+ // =============================================================================
2
+ // Storage Objects
3
+ // =============================================================================
4
+
5
+ enum StorageObjectStatus {
6
+ pending // Upload initiated, awaiting chunks
7
+ uploading // Chunks being uploaded
8
+ processing // Post-upload processing in progress
9
+ ready // Available for use
10
+ failed // Upload or processing failed
11
+ }
12
+
13
+ // @extensible — back-relations only, added with "extend model StorageObject { ... }" in a fragment
14
+ model StorageObject {
15
+ id String @id @default(uuid()) @db.Uuid
16
+ name String // Original filename
17
+ size BigInt // Total size in bytes (BigInt for GB-scale)
18
+ mimeType String @map("mime_type")
19
+ storageKey String @unique @map("storage_key") // S3 object key
20
+ storageProvider String @default("s3") @map("storage_provider")
21
+ bucket String?
22
+ status StorageObjectStatus @default(pending)
23
+ s3UploadId String? @map("s3_upload_id") // S3 multipart upload ID
24
+ metadata Json? // JSONB for flexible metadata
25
+ uploadedById String? @map("uploaded_by_id") @db.Uuid
26
+ // The organization the object belongs to (#725). NOT NULL, row-level
27
+ // security forced (policy `storage_objects_org_isolation`, see
28
+ // `0025_org_scoped_rls`). `Restrict`: an organization with objects cannot be
29
+ // deleted until they are purged deliberately.
30
+ orgId String @map("org_id") @db.Uuid
31
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
32
+ updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
33
+
34
+ // Relations
35
+ uploadedBy User? @relation("UserStorageObjects", fields: [uploadedById], references: [id], onDelete: SetNull)
36
+ org Organization @relation(fields: [orgId], references: [id], onDelete: Restrict)
37
+ chunks StorageObjectChunk[]
38
+
39
+ // The target of the chunks' composite foreign key (object_id, org_id): a
40
+ // reference between two org-owned tables is composite, because a foreign-key
41
+ // check bypasses row-level security (ADR 0002 D5).
42
+ @@unique([id, orgId])
43
+ @@index([uploadedById])
44
+ @@index([status])
45
+ @@index([createdAt])
46
+ // The policy filter `org_id = X` leads every query; these serve the hot paths
47
+ // behind it (a user's list, newest first; the cleanup sweep by status).
48
+ @@index([orgId, uploadedById, createdAt])
49
+ @@index([orgId, status])
50
+ @@map("storage_objects")
51
+ }
52
+
53
+ model StorageObjectChunk {
54
+ id String @id @default(uuid()) @db.Uuid
55
+ objectId String @map("object_id") @db.Uuid
56
+ partNumber Int @map("part_number") // S3 part number (1-10000)
57
+ eTag String @map("e_tag") // S3 ETag for verification
58
+ size BigInt // Chunk size in bytes
59
+ // The organization of the parent object (#725), carried here because a policy
60
+ // cannot join through the parent. NOT NULL, row-level security forced.
61
+ orgId String @map("org_id") @db.Uuid
62
+ uploadedAt DateTime @default(now()) @map("uploaded_at") @db.Timestamptz
63
+
64
+ // Relations
65
+ //
66
+ // Composite: (object_id, org_id) -> storage_objects (id, org_id), so a chunk
67
+ // can never name another organization's object. Cascade, as before.
68
+ object StorageObject @relation(fields: [objectId, orgId], references: [id, orgId], onDelete: Cascade)
69
+ org Organization @relation(fields: [orgId], references: [id], onDelete: Restrict)
70
+
71
+ @@unique([objectId, partNumber])
72
+ @@index([objectId])
73
+ @@index([orgId, objectId])
74
+ @@map("storage_object_chunks")
75
+ }
76
+
77
+ extend model User {
78
+ storageObjects StorageObject[] @relation("UserStorageObjects")
79
+ }
80
+
81
+ extend model Organization {
82
+ storageObjects StorageObject[]
83
+ storageObjectChunks StorageObjectChunk[]
84
+ }