@protobase/server 0.129.1 → 0.130.0

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 (243) hide show
  1. package/dist/assistant/query-runner/grants-sql.js +32 -0
  2. package/dist/assistant/query-runner/query-input.js +33 -0
  3. package/dist/assistant/query-runner/query-module.js +44 -0
  4. package/dist/assistant/query-runner/query-runner.d.ts +8 -11
  5. package/dist/assistant/query-runner/query-runner.js +15 -57
  6. package/dist/assistant/query-runner/read-query.js +54 -0
  7. package/dist/assistant/query-runner/runner-sql.js +4 -20
  8. package/dist/assistant/query-runner/steps/001-role.js +19 -0
  9. package/dist/auth-store.d.ts +2 -2
  10. package/dist/auth-store.js +3 -3
  11. package/dist/better-auth/auth-schema.d.ts +13 -4
  12. package/dist/better-auth/auth-schema.js +40 -32
  13. package/dist/better-auth/auth-settings.d.ts +59 -11
  14. package/dist/better-auth/auth-settings.js +57 -13
  15. package/dist/better-auth/auth-tables.d.ts +6 -3
  16. package/dist/better-auth/auth-tables.js +12 -7
  17. package/dist/better-auth/authenticator.js +10 -2
  18. package/dist/better-auth/create-auth.d.ts +62 -7
  19. package/dist/better-auth/create-auth.js +55 -14
  20. package/dist/better-auth/device-sign-in-check.js +13 -0
  21. package/dist/better-auth/names/declaration.d.ts +18 -5
  22. package/dist/better-auth/names/declaration.js +3 -1
  23. package/dist/better-auth/names/modules/auth.js +9 -0
  24. package/dist/better-auth/names/modules/index.d.ts +34 -4
  25. package/dist/better-auth/names/modules/organizations.js +10 -3
  26. package/dist/better-auth/names/modules/personal-access-tokens.js +6 -3
  27. package/dist/better-auth/names/modules-on.js +25 -2
  28. package/dist/better-auth/names/resolve-names.d.ts +9 -1
  29. package/dist/better-auth/names/resolve-names.js +15 -9
  30. package/dist/better-auth/organizations/accept-invitation.js +5 -4
  31. package/dist/better-auth/organizations/add-every-user.js +3 -2
  32. package/dist/better-auth/organizations/host-scope.d.ts +10 -0
  33. package/dist/better-auth/organizations/host-scope.js +15 -0
  34. package/dist/better-auth/organizations/host.d.ts +12 -6
  35. package/dist/better-auth/organizations/host.js +99 -61
  36. package/dist/better-auth/organizations/kept-organizations.d.ts +21 -0
  37. package/dist/better-auth/organizations/kept-organizations.js +62 -0
  38. package/dist/better-auth/organizations/member-endpoints.js +4 -3
  39. package/dist/better-auth/organizations/options.d.ts +15 -0
  40. package/dist/better-auth/organizations/options.js +19 -1
  41. package/dist/better-auth/organizations/people-endpoint.js +3 -2
  42. package/dist/better-auth/organizations/pinned-membership.js +81 -0
  43. package/dist/better-auth/organizations/pinned-sessions.js +46 -0
  44. package/dist/better-auth/organizations/session-organization.js +3 -2
  45. package/dist/better-auth/organizations/store.js +14 -9
  46. package/dist/better-auth/personal-access-tokens/pinned-tokens.js +23 -0
  47. package/dist/better-auth/personal-access-tokens/token-plugin.js +75 -54
  48. package/dist/better-auth/personal-access-tokens/token-sessions.js +6 -3
  49. package/dist/better-auth/personal-access-tokens/token-store.js +15 -11
  50. package/dist/better-auth/store/adoption.js +25 -10
  51. package/dist/better-auth/store/drift.js +5 -4
  52. package/dist/better-auth/store/legacy-record.d.ts +10 -0
  53. package/dist/better-auth/store/legacy-record.js +56 -0
  54. package/dist/better-auth/store/migration-sql.js +31 -26
  55. package/dist/better-auth/store/offline/baseline-store.d.ts +11 -7
  56. package/dist/better-auth/store/offline/baseline-store.js +15 -12
  57. package/dist/better-auth/store/offline/module-switches.d.ts +27 -0
  58. package/dist/better-auth/store/offline/module-switches.js +94 -0
  59. package/dist/better-auth/store/offline/name-changes.d.ts +4 -1
  60. package/dist/better-auth/store/offline/name-changes.js +5 -2
  61. package/dist/better-auth/store/offline/offline-steps.d.ts +21 -7
  62. package/dist/better-auth/store/offline/offline-steps.js +29 -20
  63. package/dist/better-auth/store/offline/retired-watch.d.ts +5 -0
  64. package/dist/better-auth/store/offline/retired-watch.js +36 -0
  65. package/dist/better-auth/store/offline/settings-names.d.ts +3 -2
  66. package/dist/better-auth/store/offline/settings-names.js +10 -3
  67. package/dist/better-auth/store/offline/store-model.d.ts +9 -1
  68. package/dist/better-auth/store/offline/store-model.js +22 -5
  69. package/dist/better-auth/store/resources-outside.js +1 -1
  70. package/dist/better-auth/store/step-record.d.ts +13 -0
  71. package/dist/better-auth/store/step-record.js +59 -0
  72. package/dist/better-auth/store/steps/index.js +6 -1
  73. package/dist/better-auth/store/steps/legacy-versions.d.ts +7 -0
  74. package/dist/better-auth/store/steps/legacy-versions.js +4 -0
  75. package/dist/better-auth/store/steps/organizations/003-default-organization.js +113 -0
  76. package/dist/better-auth/store/steps/render.d.ts +3 -0
  77. package/dist/better-auth/store/steps/render.js +48 -25
  78. package/dist/better-auth/store/steps/step-lint.d.ts +14 -0
  79. package/dist/better-auth/store/steps/step-lint.js +31 -10
  80. package/dist/better-auth/store/steps/step.d.ts +18 -0
  81. package/dist/better-auth/store/steps/step.js +7 -1
  82. package/dist/better-auth/store/store-db.d.ts +6 -0
  83. package/dist/better-auth/store/store-db.js +11 -5
  84. package/dist/better-auth/store/store-names.d.ts +4 -0
  85. package/dist/better-auth/token-standing.js +12 -7
  86. package/dist/better-auth/users.d.ts +7 -2
  87. package/dist/better-auth/users.js +25 -3
  88. package/dist/built-in-resources.js +1 -1
  89. package/dist/calls/calls-module.js +42 -0
  90. package/dist/calls/calls-sql.js +4 -30
  91. package/dist/changes/capture-names.d.ts +3 -1
  92. package/dist/changes/capture-names.js +5 -2
  93. package/dist/changes/capture-schema-sql.d.ts +10 -0
  94. package/dist/changes/capture-schema-sql.js +21 -0
  95. package/dist/changes/capture-schema.d.ts +0 -18
  96. package/dist/changes/capture-schema.js +1 -74
  97. package/dist/changes/capture-sql.js +30 -24
  98. package/dist/changes/changes-input.js +36 -0
  99. package/dist/changes/changes-module.js +44 -0
  100. package/dist/changes/read-capture.js +67 -0
  101. package/dist/changes/steps/001-outbox.js +23 -0
  102. package/dist/channels/sends-module.js +37 -0
  103. package/dist/channels/sends-schema-sql.js +5 -5
  104. package/dist/comments/comments-module.js +30 -0
  105. package/dist/comments/comments-sql.js +1 -22
  106. package/dist/computed/buckets-sql.d.ts +5 -1
  107. package/dist/computed/buckets-sql.js +39 -46
  108. package/dist/computed/{buckets-schema.d.ts → buckets-targets.d.ts} +2 -3
  109. package/dist/computed/buckets-targets.js +50 -0
  110. package/dist/computed/computed-input.js +65 -0
  111. package/dist/computed/computed-module.js +102 -0
  112. package/dist/computed/formula-sql.js +14 -1
  113. package/dist/computed/read-computed.js +100 -0
  114. package/dist/computed/rollup-sql.js +13 -10
  115. package/dist/config-exports.d.ts +2 -2
  116. package/dist/create-admin.js +21 -5
  117. package/dist/derived/derived-module.js +89 -0
  118. package/dist/derived/derived-sql.js +1 -47
  119. package/dist/functions/define-function.d.ts +10 -4
  120. package/dist/functions/routes.js +5 -3
  121. package/dist/functions/supabase-routes.js +7 -5
  122. package/dist/history/history-module.js +34 -0
  123. package/dist/index.d.ts +19 -28
  124. package/dist/index.js +11 -21
  125. package/dist/internal.d.ts +26 -10
  126. package/dist/internal.js +22 -8
  127. package/dist/jobs/functions-sql.js +7 -11
  128. package/dist/jobs/job-data.js +36 -0
  129. package/dist/jobs/job-types.d.ts +6 -3
  130. package/dist/jobs/jobs-module.d.ts +5 -0
  131. package/dist/jobs/jobs-module.js +84 -0
  132. package/dist/jobs/run-job.d.ts +1 -1
  133. package/dist/jobs/run-job.js +7 -5
  134. package/dist/jobs/runtime.d.ts +9 -4
  135. package/dist/jobs/runtime.js +11 -6
  136. package/dist/jobs/schema.d.ts +0 -11
  137. package/dist/jobs/schema.js +1 -59
  138. package/dist/jobs/steps/001-pg-boss-45.js +483 -0
  139. package/dist/live/notices-module.js +30 -0
  140. package/dist/live/notices-sql.js +2 -1
  141. package/dist/mail/feedback/feedback-schema.js +3 -30
  142. package/dist/mail/feedback/mail-feedback-module.js +37 -0
  143. package/dist/mail/unsubscribe/unsubscribe-schema.js +3 -21
  144. package/dist/mail/unsubscribe/unsubscribes-module.js +31 -0
  145. package/dist/migration-record/check-record.d.ts +9 -7
  146. package/dist/migration-record/check-record.js +12 -11
  147. package/dist/migration-record/module-needs.js +30 -0
  148. package/dist/migration-record/read-record.js +27 -15
  149. package/dist/migration-record/record-model.d.ts +7 -2
  150. package/dist/migration-record/record-model.js +14 -6
  151. package/dist/migration-record/startup-decision.js +18 -7
  152. package/dist/migration-sql/additive-statements.d.ts +4 -0
  153. package/dist/migration-sql/additive-statements.js +105 -0
  154. package/dist/notifications/devices-module.js +61 -0
  155. package/dist/notifications/history-module.js +50 -0
  156. package/dist/notifications/notifications-targets.js +23 -0
  157. package/dist/notifications/read-table.js +16 -0
  158. package/dist/notifications/schema-sql.js +1 -22
  159. package/dist/notifications/schema.js +1 -101
  160. package/dist/notifications/table-names.js +40 -0
  161. package/dist/organization-scope/app-scope.js +38 -0
  162. package/dist/organization-scope/auth-request-organization.js +71 -0
  163. package/dist/organization-scope/organization-tables.js +20 -0
  164. package/dist/organization-scope/sign-in-organization.js +21 -0
  165. package/dist/package.js +1 -1
  166. package/dist/personal-mail/shared-sender-store.js +4 -2
  167. package/dist/realtime/relay/realtime-relay-module.js +37 -0
  168. package/dist/relation-found.js +6 -0
  169. package/dist/relay/read-relay.js +85 -0
  170. package/dist/relay/realtime-members-sql.js +51 -28
  171. package/dist/relay/relay-module.js +53 -0
  172. package/dist/relay/relay-targets.js +28 -0
  173. package/dist/relay/session-end.js +1 -3
  174. package/dist/relay/sessions-sql.js +4 -27
  175. package/dist/rest/table-routes.js +9 -2
  176. package/dist/schema-modules/feature-schemas-sql.d.ts +40 -0
  177. package/dist/schema-modules/feature-schemas-sql.js +51 -0
  178. package/dist/schema-modules/module-difference.d.ts +31 -0
  179. package/dist/schema-modules/module-difference.js +98 -0
  180. package/dist/schema-modules/modules-sql.js +13 -0
  181. package/dist/schema-modules/modules.d.ts +37 -0
  182. package/dist/schema-modules/modules.js +56 -0
  183. package/dist/schema-modules/needed-modules.d.ts +41 -0
  184. package/dist/schema-modules/needed-modules.js +61 -0
  185. package/dist/schema-modules/resource-difference.js +83 -0
  186. package/dist/schema-modules/schema-module.d.ts +112 -0
  187. package/dist/schema-modules/schema-module.js +8 -0
  188. package/dist/status/status-module.js +63 -0
  189. package/dist/status/status-targets.js +21 -0
  190. package/dist/types.d.ts +6 -1
  191. package/dist/user-tables/reloading-admin.d.ts +1 -0
  192. package/dist/user-tables/reloading-admin.js +1 -0
  193. package/dist/user-tables/schema-sql.js +1 -14
  194. package/dist/user-tables/schema.js +4 -27
  195. package/dist/user-tables/user-tables-module.js +38 -0
  196. package/dist/webhooks/table/delivery-log-module.js +36 -0
  197. package/dist/webhooks/table/owner-roles.js +3 -2
  198. package/dist/webhooks/table/schema-sql.js +27 -26
  199. package/dist/webhooks/table/webhooks-schema-module.js +63 -0
  200. package/dist/webhooks/table/webhooks-targets.js +26 -0
  201. package/dist/webhooks/webhooks.js +1 -1
  202. package/dist/workflows/custom-steps/custom-steps-module.js +51 -0
  203. package/dist/workflows/custom-steps/custom-steps-sql.js +10 -7
  204. package/dist/workflows/custom-steps/runners-sql.js +10 -7
  205. package/dist/workflows/read-catalogue.js +23 -0
  206. package/dist/workflows/schema-sql.js +12 -9
  207. package/dist/workflows/status-sql.js +12 -6
  208. package/dist/workflows/storage/storage-module.js +39 -0
  209. package/dist/workflows/storage/storage-sql.js +8 -5
  210. package/dist/workflows/workflows-schema-module.js +49 -0
  211. package/dist/workflows/workflows-tables-sql.js +32 -0
  212. package/dist/workflows/workflows-targets.js +16 -0
  213. package/package.json +6 -6
  214. package/dist/assistant/query-runner/runner-plan.js +0 -30
  215. package/dist/better-auth/store/migrations-table.js +0 -27
  216. package/dist/better-auth/store/modules-off.js +0 -17
  217. package/dist/calls/calls-sql.d.ts +0 -11
  218. package/dist/changes/capture-plan.js +0 -79
  219. package/dist/channels/sends-schema.d.ts +0 -7
  220. package/dist/channels/sends-schema.js +0 -18
  221. package/dist/comments/comments-sql.d.ts +0 -14
  222. package/dist/computed/buckets-plan.d.ts +0 -2
  223. package/dist/computed/buckets-plan.js +0 -88
  224. package/dist/computed/buckets-schema.js +0 -39
  225. package/dist/computed/computed-plan.js +0 -81
  226. package/dist/computed/computed-schema.d.ts +0 -16
  227. package/dist/computed/computed-schema.js +0 -63
  228. package/dist/derived/derived-sql.d.ts +0 -12
  229. package/dist/mail/feedback/feedback-schema.d.ts +0 -7
  230. package/dist/mail/unsubscribe/unsubscribe-schema.d.ts +0 -10
  231. package/dist/notifications/schema.d.ts +0 -14
  232. package/dist/realtime/relay/relay-schema.d.ts +0 -7
  233. package/dist/realtime/relay/relay-schema.js +0 -20
  234. package/dist/relay/realtime-members-schema.js +0 -67
  235. package/dist/relay/relay-schema.d.ts +0 -14
  236. package/dist/relay/relay-schema.js +0 -28
  237. package/dist/status/schema.d.ts +0 -12
  238. package/dist/status/schema.js +0 -33
  239. package/dist/user-tables/schema.d.ts +0 -11
  240. package/dist/webhooks/table/schema.d.ts +0 -16
  241. package/dist/webhooks/table/schema.js +0 -56
  242. package/dist/workflows/schema.d.ts +0 -13
  243. package/dist/workflows/schema.js +0 -88
@@ -208,6 +208,23 @@ export declare const builtInModules: readonly [{
208
208
  };
209
209
  };
210
210
  };
211
+ readonly auth_steps: {
212
+ readonly scope: "global";
213
+ readonly columns: {
214
+ readonly module: {
215
+ field: "module";
216
+ };
217
+ readonly step: {
218
+ field: "step";
219
+ };
220
+ readonly target: {
221
+ field: "target";
222
+ };
223
+ readonly applied_at: {
224
+ field: "appliedAt";
225
+ };
226
+ };
227
+ };
211
228
  };
212
229
  }, {
213
230
  readonly id: "two_factor";
@@ -506,7 +523,10 @@ export declare const builtInModules: readonly [{
506
523
  };
507
524
  };
508
525
  };
509
- readonly indexes: ["member_organization_id_idx", "member_user_id_idx"];
526
+ readonly indexes: {
527
+ readonly member_organization_id_idx: "organization";
528
+ readonly member_user_id_idx: "global";
529
+ };
510
530
  };
511
531
  readonly invitation: {
512
532
  readonly model: "invitation";
@@ -543,7 +563,10 @@ export declare const builtInModules: readonly [{
543
563
  };
544
564
  };
545
565
  };
546
- readonly indexes: ["invitation_organization_id_idx", "invitation_email_idx"];
566
+ readonly indexes: {
567
+ readonly invitation_organization_id_idx: "organization";
568
+ readonly invitation_email_idx: "global";
569
+ };
547
570
  };
548
571
  };
549
572
  readonly columns: {
@@ -572,6 +595,11 @@ export declare const builtInModules: readonly [{
572
595
  };
573
596
  };
574
597
  };
598
+ readonly indexes: {
599
+ readonly personal_access_token: {
600
+ readonly personal_access_token_organization_id_user_id_idx: "organization";
601
+ };
602
+ };
575
603
  }, {
576
604
  readonly id: "mcp";
577
605
  readonly requires: ["auth"];
@@ -661,7 +689,7 @@ export declare const builtInModules: readonly [{
661
689
  readonly tables: {
662
690
  readonly personal_access_token: {
663
691
  readonly model: "personalAccessToken";
664
- readonly scope: "global";
692
+ readonly scope: "organization";
665
693
  readonly columns: {
666
694
  readonly user_id: {
667
695
  field: "userId";
@@ -713,7 +741,9 @@ export declare const builtInModules: readonly [{
713
741
  };
714
742
  };
715
743
  };
716
- readonly indexes: ["personal_access_token_user_id_idx"];
744
+ readonly indexes: {
745
+ readonly personal_access_token_user_id_idx: "global";
746
+ };
717
747
  };
718
748
  };
719
749
  }, {
@@ -32,7 +32,10 @@ const organizations = {
32
32
  input: false
33
33
  })
34
34
  },
35
- indexes: ["member_organization_id_idx", "member_user_id_idx"]
35
+ indexes: {
36
+ member_organization_id_idx: "organization",
37
+ member_user_id_idx: "global"
38
+ }
36
39
  },
37
40
  invitation: {
38
41
  model: "invitation",
@@ -52,7 +55,10 @@ const organizations = {
52
55
  input: true
53
56
  })
54
57
  },
55
- indexes: ["invitation_organization_id_idx", "invitation_email_idx"]
58
+ indexes: {
59
+ invitation_organization_id_idx: "organization",
60
+ invitation_email_idx: "global"
61
+ }
56
62
  }
57
63
  },
58
64
  columns: {
@@ -66,7 +72,8 @@ const organizations = {
66
72
  type: "string",
67
73
  required: false
68
74
  }) }
69
- }
75
+ },
76
+ indexes: { personal_access_token: { personal_access_token_organization_id_user_id_idx: "organization" } }
70
77
  };
71
78
  //#endregion
72
79
  export { organizations };
@@ -1,12 +1,15 @@
1
1
  import { own } from "../declaration.js";
2
2
  //#region src/better-auth/names/modules/personal-access-tokens.ts
3
- /** The personal access tokens people make for their scripts: one row per token, holding its hash and never the token. */
3
+ /**
4
+ * The personal access tokens people make for their scripts: one row per token, holding its hash and never the token.
5
+ * Each belongs to the organization it works in, whose column organizations add; the hash stays unique app-wide.
6
+ */
4
7
  const personalAccessTokens = {
5
8
  id: "personal_access_tokens",
6
9
  requires: ["auth"],
7
10
  tables: { personal_access_token: {
8
11
  model: "personalAccessToken",
9
- scope: "global",
12
+ scope: "organization",
10
13
  columns: {
11
14
  user_id: own("userId", {
12
15
  type: "string",
@@ -37,7 +40,7 @@ const personalAccessTokens = {
37
40
  required: false
38
41
  })
39
42
  },
40
- indexes: ["personal_access_token_user_id_idx"]
43
+ indexes: { personal_access_token_user_id_idx: "global" }
41
44
  } }
42
45
  };
43
46
  //#endregion
@@ -7,7 +7,30 @@ const checkRequiredModules = (on) => {
7
7
  if (off !== void 0) throw new Error(`The ${module.id} module is on while ${off}, which it requires, is off: turn ${off} on, or ${module.id} off`);
8
8
  }
9
9
  };
10
- /** Every module: each store has every module's tables, whether the app uses the feature or not. */
10
+ /** Every module, in the order they require one another. */
11
11
  const allModules = builtInModules.map(({ id }) => id);
12
+ /**
13
+ * The modules the app's auth turns on, the one place the CLI's migrations and the runtime read them from: each
14
+ * feature's module is on while its switch is, `mcp` with the MCP endpoint or the OpenID provider, whose tables are the
15
+ * same, `mail_senders` with personal mail, and `platform_devices` with `signInDevices` or a provider of
16
+ * `signInProviders` that signs in devices: the config alone, as a migration is written without the platform's
17
+ * environment. The others are always on: sign-in itself, two-factor authentication, passkeys, the sign-in policy and
18
+ * staff sign-in have no switch.
19
+ */
20
+ const modulesOf = (settings) => {
21
+ const on = {
22
+ auth: true,
23
+ two_factor: true,
24
+ passkeys: true,
25
+ sign_in_policy: true,
26
+ staff_sign_in: true,
27
+ platform_devices: settings.signInDevices === true || (settings.signInProviders ?? []).some(({ devices }) => devices === true),
28
+ organizations: Boolean(settings.organizations),
29
+ mcp: Boolean(settings.mcp) || Boolean(settings.openIdProvider),
30
+ personal_access_tokens: settings.personalAccessTokens !== void 0,
31
+ mail_senders: (settings.personalMail ?? []).length > 0
32
+ };
33
+ return allModules.filter((module) => on[module]);
34
+ };
12
35
  //#endregion
13
- export { allModules, checkRequiredModules };
36
+ export { allModules, checkRequiredModules, modulesOf };
@@ -1,4 +1,4 @@
1
- import { TableDeclaration } from "./declaration.js";
1
+ import { IndexScope, TableDeclaration } from "./declaration.js";
2
2
  import { ColumnId, IndexId, TableId } from "./identifiers.js";
3
3
  import { BuiltInTables, ModuleId } from "./modules/index.js";
4
4
  import { DBFieldAttribute } from "@better-auth/core/db";
@@ -18,6 +18,8 @@ export type ResolvedTable<Column extends string = string> = TableId & {
18
18
  columns: Record<Column, ResolvedColumn>;
19
19
  /** The columns by the Better Auth field each holds. */
20
20
  fields: Record<string, ResolvedColumn>;
21
+ /** Its declared indexes, by internal name, with what each serves (`IndexScope`), those of the modules that are on included. */
22
+ indexes: Record<string, IndexScope>;
21
23
  /** A declared index of the table, by internal name. */
22
24
  index: (internal: string) => IndexId;
23
25
  };
@@ -56,4 +58,10 @@ export type ResolvedNames = {
56
58
  /** What the names were resolved from, to resolve them again in another schema. */
57
59
  input: NamesInput;
58
60
  };
61
+ /**
62
+ * The store's names: every table of the modules that are on, under the name `tables` gives it or its built-in one, each
63
+ * column likewise. Refuses a module that is on while one it requires is off, a table or column `tables` names that
64
+ * the store has not, a configured name Postgres would cut, and a name two tables, a table and an index, or two columns of one, share.
65
+ */
66
+ export declare const resolveNames: (input: NamesInput) => ResolvedNames;
59
67
  //#endregion
@@ -1,6 +1,7 @@
1
+ import { indexScopes } from "./declaration.js";
2
+ import { builtInModules } from "./modules/index.js";
1
3
  import { checkConfiguredName, generatedName } from "./identifier-length.js";
2
4
  import { columnId, indexId, tableId } from "./identifiers.js";
3
- import { builtInModules } from "./modules/index.js";
4
5
  import { allModules, checkRequiredModules } from "./modules-on.js";
5
6
  //#region src/better-auth/names/resolve-names.ts
6
7
  const declaredTables = (on) => {
@@ -9,7 +10,8 @@ const declaredTables = (on) => {
9
10
  module,
10
11
  internal,
11
12
  table,
12
- columns: Object.assign({}, table.columns, ...modules.map((other) => other.columns?.[internal] ?? {}))
13
+ columns: Object.assign({}, table.columns, ...modules.map((other) => other.columns?.[internal] ?? {})),
14
+ indexes: Object.assign({}, indexScopes(table), ...modules.map((other) => other.indexes?.[internal] ?? {}))
13
15
  })));
14
16
  };
15
17
  const checkConfig = (tables, declared) => {
@@ -26,11 +28,15 @@ const checkConfig = (tables, declared) => {
26
28
  }
27
29
  }
28
30
  };
31
+ /** The refusal of a name the store's names give twice, such as a table named as another module's. */
32
+ var NameClash = class extends Error {};
29
33
  const refuseTwice = (names, what) => {
30
34
  const twice = names.find((name, i) => names.indexOf(name) !== i);
31
- if (twice !== void 0) throw new Error(`auth.tables gives ${what} the name "${twice}" twice: give each its own`);
35
+ if (twice !== void 0) throw new NameClash(`auth.tables gives ${what} the name "${twice}" twice: give each its own`);
32
36
  };
33
- const resolveTable = (schema, { module, internal, table, columns }, prefix, config = {}) => {
37
+ /** The name `tables` gives a table, by internal name: its own `name`, or its built-in one after the prefix. */
38
+ const tableNameOf = (tables, internal) => tables[internal]?.name ?? generatedName(`${tables.prefix ?? ""}${internal}`);
39
+ const resolveTable = (schema, { module, internal, table, columns, indexes }, prefix, config = {}) => {
34
40
  const id = tableId(schema, config.name ?? generatedName(`${prefix}${internal}`));
35
41
  const resolved = Object.fromEntries(Object.entries(columns).map(([column, { field, attribute }]) => [column, {
36
42
  ...columnId(id, config.columns?.[column] ?? column),
@@ -39,7 +45,6 @@ const resolveTable = (schema, { module, internal, table, columns }, prefix, conf
39
45
  ...attribute && { attribute }
40
46
  }]));
41
47
  refuseTwice(["id", ...Object.values(resolved).map(({ name }) => name)], `two columns of ${internal}`);
42
- const indexes = table.indexes ?? [];
43
48
  return {
44
49
  ...id,
45
50
  internal,
@@ -48,8 +53,9 @@ const resolveTable = (schema, { module, internal, table, columns }, prefix, conf
48
53
  scope: table.scope,
49
54
  columns: resolved,
50
55
  fields: Object.fromEntries(Object.values(resolved).map((column) => [column.field, column])),
56
+ indexes,
51
57
  index: (index) => {
52
- if (!indexes.includes(index) || !index.startsWith(`${internal}_`)) throw new Error(`${index} is not an index ${internal} declares`);
58
+ if (!(index in indexes) || !index.startsWith(`${internal}_`)) throw new Error(`${index} is not an index ${internal} declares`);
53
59
  return indexId(id, generatedName(`${id.name}${index.slice(internal.length)}`));
54
60
  }
55
61
  };
@@ -63,10 +69,10 @@ const resolveNames = (input) => {
63
69
  const { schema, tables = {}, modules = allModules } = input;
64
70
  checkRequiredModules(modules);
65
71
  const declared = declaredTables(modules);
66
- checkConfig(tables, declared);
72
+ checkConfig(tables, declaredTables(allModules));
67
73
  const configs = tables;
68
74
  const each = declared.map((table) => ({
69
- indexes: table.table.indexes ?? [],
75
+ indexes: Object.keys(table.indexes),
70
76
  resolved: resolveTable(schema, table, tables.prefix ?? "", configs[table.internal])
71
77
  }));
72
78
  const resolved = Object.fromEntries(each.map((table) => [table.resolved.internal, table.resolved]));
@@ -86,4 +92,4 @@ const resolveNames = (input) => {
86
92
  };
87
93
  };
88
94
  //#endregion
89
- export { resolveNames };
95
+ export { NameClash, resolveNames, tableNameOf };
@@ -1,16 +1,17 @@
1
1
  import { parseAppRoles } from "./app-roles.js";
2
+ import { acrossOrganizations } from "../../organization-scope/sign-in-organization.js";
2
3
  import { storeSqlOf } from "../store/store-sql.js";
3
4
  import { invalidInvitation, invitationIdOf, verifyInvitationToken } from "../invitations/invitation-token.js";
4
5
  import { sql } from "kysely";
5
6
  import { APIError } from "better-auth/api";
6
7
  //#region src/better-auth/organizations/accept-invitation.ts
7
- const findInvitation = (context, id) => context.adapter.findOne({
8
+ const findInvitation = (context, id) => acrossOrganizations("an invitation is found by its id for the person it invites, before they are a member", () => context.adapter.findOne({
8
9
  model: "invitation",
9
10
  where: [{
10
11
  field: "id",
11
12
  value: id
12
13
  }]
13
- });
14
+ }));
14
15
  const pending = (row) => {
15
16
  if (row.status !== "pending" || new Date(row.expiresAt).getTime() < Date.now()) throw new APIError("BAD_REQUEST", invalidInvitation);
16
17
  return {
@@ -51,7 +52,7 @@ const acceptInvitation = async (context, invitation, user, membershipLimit) => {
51
52
  const member = store.table("member");
52
53
  const m = member.columns;
53
54
  const invitations = store.table("invitation");
54
- return store.db.transaction().execute(async (trx) => {
55
+ return acrossOrganizations("accepting an invitation makes its person a member of the invitation's organization", () => store.db.transaction().execute(async (trx) => {
55
56
  await sql`select from ${store.table("organization")} where id = ${invitation.organizationId} for update`.execute(trx);
56
57
  const { rows: [counted] } = await sql`select count(*)::int as members, count(*) filter (where ${m.user_id} = ${user.id}) > 0 as joined
57
58
  from ${member} where ${m.organization_id} = ${invitation.organizationId}`.execute(trx);
@@ -72,7 +73,7 @@ const acceptInvitation = async (context, invitation, user, membershipLimit) => {
72
73
  ...made,
73
74
  appRoles: invitation.appRoles
74
75
  };
75
- });
76
+ }));
76
77
  };
77
78
  //#endregion
78
79
  export { acceptInvitation, invitationOfId, invitationOfToken };
@@ -1,4 +1,5 @@
1
1
  import { membershipRoles } from "./role-definitions.js";
2
+ import { acrossOrganizations } from "../../organization-scope/sign-in-organization.js";
2
3
  import { storeSqlOf } from "../store/store-sql.js";
3
4
  import { roleNames } from "../store/active-admin.js";
4
5
  import { findOrganizationByKey } from "./host.js";
@@ -23,7 +24,7 @@ const addEveryUser = async (auth, organizationKey) => {
23
24
  const m = member.columns;
24
25
  const names = roleNames(sql`u.${u.role}`);
25
26
  const now = /* @__PURE__ */ new Date();
26
- return store.db.transaction().execute(async (trx) => {
27
+ return acrossOrganizations("everyone with an account joins the organization, whichever others they are a member of", () => store.db.transaction().execute(async (trx) => {
27
28
  await sql`select from ${store.table("organization")} where id = ${organization.id} for update`.execute(trx);
28
29
  const { rows } = await sql`with joined as (
29
30
  insert into ${member} (id, ${m.organization_id}, ${m.user_id}, ${m.role}, ${m.app_roles}, ${m.created_at})
@@ -42,7 +43,7 @@ const addEveryUser = async (auth, organizationKey) => {
42
43
  join ${user} u on u.id = joined.user_id
43
44
  order by u.${u.created_at}, u.id`.execute(trx);
44
45
  return rows;
45
- });
46
+ }));
46
47
  };
47
48
  //#endregion
48
49
  export { addEveryUser };
@@ -0,0 +1,10 @@
1
+ import "../names/resolve-names.js";
2
+ import "../store/store-sql.js";
3
+ import { EveryOrganization, Organization } from "@protobase/query";
4
+ //#region src/better-auth/organizations/host-scope.d.ts
5
+ /**
6
+ * Where a host function works: one organization, by its id, or every one (`everyOrganization('<reason>')`), the reviewed
7
+ * way out that its caller names, such as `protobase organizations list --all-organizations`.
8
+ */
9
+ export type HostScope = Organization | EveryOrganization;
10
+ //#endregion
@@ -0,0 +1,15 @@
1
+ import { isEveryOrganization } from "@protobase/query";
2
+ //#region src/better-auth/organizations/host-scope.ts
3
+ /** A table of the store as Kysely's builder names it: `schema.table`, or the table alone where the search path finds it. */
4
+ const builderName = (table) => table.schema === void 0 ? table.name : `${table.schema}.${table.name}`;
5
+ /**
6
+ * A table of the store whose rows belong to one organization, for raw SQL in `scope`: the table itself across every
7
+ * organization, or the organization's rows, a query of Kysely's builder that names it, so a store kept to the
8
+ * organization (`ScopedDb`) checks it too, while one without the scope, such as a pg Pool's, reads it as written.
9
+ */
10
+ const rowsIn = (store, scope, table) => {
11
+ if (isEveryOrganization(scope)) return table;
12
+ return store.db.selectFrom(builderName(table)).selectAll().where(table.columns.organization_id.name, "=", scope);
13
+ };
14
+ //#endregion
15
+ export { builderName, rowsIn };
@@ -1,6 +1,9 @@
1
1
  import { OrganizationRole, StoredMember, StoredOrganization } from "./store.js";
2
2
  import { AdminAuth } from "../create-auth.js";
3
+ import { HostScope } from "./host-scope.js";
3
4
  //#region src/better-auth/organizations/host.d.ts
5
+ /** An organization by its id or slug; null when there is none. */
6
+ export declare const organizationByKey: (auth: AdminAuth, key: string) => Promise<StoredOrganization | null>;
4
7
  export type NewOrganization = {
5
8
  name: string;
6
9
  slug: string;
@@ -30,6 +33,8 @@ export type MemberInput = {
30
33
  };
31
34
  /** Adds someone with an account as a member: organization role `member` unless given, and the app roles given. */
32
35
  export declare const addMember: (auth: AdminAuth, input: MemberInput) => Promise<StoredMember>;
36
+ /** Whether someone with `email` has an account and is a member of the organization with id `organizationId`. */
37
+ export declare const isMemberByEmail: (auth: AdminAuth, organizationId: string, email: string) => Promise<boolean>;
33
38
  /** Changes a member's organization role, app roles, or both. */
34
39
  export declare const setMemberRoles: (auth: AdminAuth, input: MemberInput) => Promise<StoredMember | undefined>;
35
40
  /** Removes a member; refuses the last owner. */
@@ -59,16 +64,17 @@ export type OrganizationsPage = {
59
64
  /** The organizations a page of `listOrganizations` holds when it is not told. */
60
65
  export declare const organizationsPageSize = 100;
61
66
  /**
62
- * A page of the organizations, oldest first, each with its members' emails and roles, oldest member first: `size` of
63
- * them from `after`, the `next` of the page before, as Postgres pages them, and their members in one join.
67
+ * A page of the organizations `scope` holds, oldest first, each with its members' emails and roles, oldest member
68
+ * first: `size` of them from `after`, the `next` of the page before, as Postgres pages them, and their members in one
69
+ * join.
64
70
  */
65
- export declare const listOrganizations: (auth: AdminAuth, { after, size }?: {
71
+ export declare const listOrganizations: (auth: AdminAuth, scope: HostScope, page?: {
66
72
  after?: OrganizationsCursor;
67
73
  size?: number;
68
74
  }) => Promise<OrganizationsPage>;
69
75
  /**
70
- * Gives an app role to every owner of every organization who lacks it, in one statement, for a role the project adds
71
- * once organizations exist. Returns how many owners got it.
76
+ * Gives an app role to every owner in `scope` who lacks it, in one statement, for a role the project adds once
77
+ * organizations exist. Returns how many owners got it.
72
78
  */
73
- export declare const addAppRoleToOwners: (auth: AdminAuth, appRole: string) => Promise<number>;
79
+ export declare const addAppRoleToOwners: (auth: AdminAuth, scope: HostScope, appRole: string) => Promise<number>;
74
80
  //#endregion