@protobase/server 0.129.0 → 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 +35 -0
  56. package/dist/better-auth/store/offline/baseline-store.js +65 -0
  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 -6
  62. package/dist/better-auth/store/offline/offline-steps.js +30 -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 +13 -4
  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 +27 -10
  126. package/dist/internal.js +22 -7
  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
@@ -0,0 +1,53 @@
1
+ import { qualified } from "../notifications/schema-sql.js";
2
+ import { sessionsSql } from "./sessions-sql.js";
3
+ import { dropListedSql, listedTableSql, membersFunctionsSql, membersTableSql } from "./realtime-members-sql.js";
4
+ import { readRelay } from "./read-relay.js";
5
+ //#region src/relay/relay-module.ts
6
+ const statements = (list) => `${list.join(";\n\n")};`;
7
+ /** The triggers that keep `realtime.members` from one channel resource's records that list their members. */
8
+ const members = {
9
+ name: "members",
10
+ title: "The triggers that keep realtime.members",
11
+ version: 1,
12
+ compatibleFrom: 1,
13
+ sql: (_names, input) => statements(listedTableSql(input)),
14
+ drop: (_names, table) => statements(dropListedSql(qualified({
15
+ schema: table.schema,
16
+ table: table.name
17
+ }))),
18
+ keptWhileOthers: false
19
+ };
20
+ /**
21
+ * What the realtime relay needs of the app's database: the trigger that closes a deleted sign-in session's sockets, on
22
+ * the sessions table the auth store names (`names.sessions`, as SQL names it), which queues a job, so it is written
23
+ * after the auth store's steps and the jobs schema; and `realtime.members` with the triggers that keep it from the
24
+ * channel records that list their members. Its table is never dropped.
25
+ */
26
+ const relayModule = {
27
+ label: "the realtime relay",
28
+ title: "The trigger that closes a deleted sign-in session's sockets on the realtime relay, and realtime.members with the triggers that keep it from the channel records that list their members",
29
+ steps: [{
30
+ step: 1,
31
+ name: "members",
32
+ title: "The realtime.members table",
33
+ additive: true,
34
+ sql: () => statements(membersTableSql)
35
+ }],
36
+ replaceables: [{
37
+ name: "sessions",
38
+ title: "The trigger that closes a deleted session's sockets",
39
+ version: 1,
40
+ compatibleFrom: 1,
41
+ sql: (names) => statements(sessionsSql(names.sessions))
42
+ }, {
43
+ name: "members",
44
+ title: "The functions that keep realtime.members",
45
+ version: 3,
46
+ compatibleFrom: 3,
47
+ sql: () => statements(membersFunctionsSql)
48
+ }],
49
+ resourceReplaceables: [members],
50
+ read: readRelay(members)
51
+ };
52
+ //#endregion
53
+ export { relayModule };
@@ -0,0 +1,28 @@
1
+ import { relayModels } from "./relay-models.js";
2
+ import { listedTable } from "./realtime-members-sql.js";
3
+ //#region src/relay/relay-targets.ts
4
+ /**
5
+ * What the relay module takes from the app for its channels (`appRelayChannels`): the sessions table of the auth store
6
+ * `store` names, as SQL names it, and each channel resource whose records list their members, by its table. Refuses an
7
+ * app without its own sign-in, whose sessions the relay's tickets name and end.
8
+ */
9
+ const relayInput = (channels, models, store) => {
10
+ if (!store) throw new Error("relay needs the app's own sign-in (auth), whose sessions its tickets name and end");
11
+ const listed = [...relayModels({ channels }, models).channels.values()].flatMap((channel) => channel.listed ? [channel.listed] : []);
12
+ const members = Object.fromEntries(listed.map((one) => {
13
+ const { table } = one.model;
14
+ return [one.model.name, {
15
+ table: {
16
+ ...table.schema !== void 0 && { schema: table.schema },
17
+ name: table.name
18
+ },
19
+ input: listedTable(one)
20
+ }];
21
+ }));
22
+ return {
23
+ names: { sessions: String(store.names.table("session")) },
24
+ resources: { members }
25
+ };
26
+ };
27
+ //#endregion
28
+ export { relayInput };
@@ -9,7 +9,5 @@ const sessionEnd = (auth) => async (id) => {
9
9
  const expires = rows[0]?.expires;
10
10
  return expires === void 0 ? void 0 : new Date(expires);
11
11
  };
12
- /** The sign-in sessions table, as SQL names it. */
13
- const sessionsTable = (auth) => String(auth.names.table("session"));
14
12
  //#endregion
15
- export { sessionEnd, sessionsTable };
13
+ export { sessionEnd };
@@ -1,5 +1,4 @@
1
1
  import { jobsSchema } from "../jobs/schema.js";
2
- import { migrationCommand } from "../changes/capture-schema.js";
3
2
  import { sql } from "kysely";
4
3
  //#region src/relay/sessions-sql.ts
5
4
  /** The schema of the relay's database objects. */
@@ -31,35 +30,13 @@ $$`,
31
30
  `drop trigger if exists ${triggerName} on ${sessions}`,
32
31
  `create trigger ${triggerName} after delete on ${sessions} referencing old table as ended for each statement execute function ${relaySchema}.session_ended()`
33
32
  ];
34
- /**
35
- * Refuses a sessions table the app's database does not have, nor gets from the migration that writes the trigger
36
- * after the auth store's steps (`written`): the trigger queues its job in the app's jobs queue, so the relay needs the
37
- * auth store in the app's own database, as the `auth` config puts it.
38
- */
39
- const refuseSessionsElsewhere = (table, written, sessions) => {
40
- if (table || written) return;
41
- throw new Error(`The relay needs the sign-in sessions table ${sessions} in the app's own database, whose jobs queue closes ended sessions' sockets, but the app's database has no such table: keep the auth store in the app's database to use the relay.`);
42
- };
43
- const current = async (db, sessions, written) => {
44
- const { rows } = await sql`select to_regclass(${sessions}) is not null as here, exists (
33
+ /** Whether the trigger stands on the sessions table, with the function of this version. Only reads the catalogue. */
34
+ const sessionsCurrent = async (db, sessions) => {
35
+ const { rows } = await sql`select exists (
45
36
  select from pg_trigger t join pg_proc p on p.oid = t.tgfoid
46
37
  where t.tgrelid = to_regclass(${sessions}) and t.tgname = ${triggerName} and obj_description(p.oid, 'pg_proc') = ${sessionsComment}
47
38
  ) as found`.execute(db);
48
- refuseSessionsElsewhere(rows[0].here, written, sessions);
49
39
  return rows[0].found;
50
40
  };
51
- /**
52
- * The SQL that ends deleted sessions' sockets on the relay (`sessionsSql`); `undefined` when it is current. `written`:
53
- * the same migration writes the auth store's steps before it, which create the sessions table the database lacks yet.
54
- */
55
- const relaySessionsMigration = async (db, sessions, written) => {
56
- if (await current(db, sessions, written)) return;
57
- return `${sessionsSql(sessions).join(";\n\n")};`;
58
- };
59
- /** Refuses to start while deleted sessions would keep their sockets on the relay. */
60
- const checkRelaySessions = async (db, sessions) => {
61
- if (await current(db, sessions, false)) return;
62
- throw new Error(`The trigger that closes a deleted session's sockets on the relay is missing or out of date on ${sessions}. Write it as a migration with \`${migrationCommand}\`, apply it with the project's migrations, then start again.`);
63
- };
64
41
  //#endregion
65
- export { checkRelaySessions, closeSessionsJob, refuseSessionsElsewhere, relaySchema, relaySessionsMigration, sessionsSql };
42
+ export { closeSessionsJob, relaySchema, sessionsCurrent, sessionsSql };
@@ -13,11 +13,18 @@ import { deleteRows, insertRows, updateRows } from "./write-rows.js";
13
13
  import { Hono } from "hono";
14
14
  import { sql } from "kysely";
15
15
  //#region src/rest/table-routes.ts
16
+ /** Whether `entry` is over a table of the auth store: one the store's names give, or any in a schema it guards. */
17
+ const overStore = (deps, entry) => {
18
+ if (!deps.storeTables) return false;
19
+ const { name, schema = "public" } = entry.model.table;
20
+ return deps.storeTables.schemas.includes(schema) || deps.storeTables.tables.some((table) => table.name === name && (table.schema ?? "public") === schema);
21
+ };
16
22
  /**
17
23
  * The resource whose table `name` is, in the `public` schema or the search path's: `/api/sb/rest/v1` serves the tables the app
18
- * declares as resources, as PostgREST serves those of its exposed schema. Any other is 404 (PGRST205).
24
+ * declares as resources, as PostgREST serves those of its exposed schema, but never one of the auth store's, even under a
25
+ * resource. Any other is 404 (PGRST205).
19
26
  */
20
- const servedEntry = (deps, name) => deps.registry.entries.find((one) => one.model.table.name === name && (one.model.table.schema ?? "public") === "public");
27
+ const servedEntry = (deps, name) => deps.registry.entries.find((one) => one.model.table.name === name && (one.model.table.schema ?? "public") === "public" && !overStore(deps, one));
21
28
  const tableEntry = (deps, name) => {
22
29
  const entry = servedEntry(deps, name);
23
30
  if (!entry) throw restError(404, "PGRST205", `Could not find the table 'public.${name}' in the schema cache`);
@@ -0,0 +1,40 @@
1
+ import { RealtimeRelaySettings } from "../relay/relay-options.js";
2
+ import { EventsOptions } from "../changes/events-options.js";
3
+ import { WebhooksOptions } from "../webhooks/table/options.js";
4
+ import { WorkflowsOptions } from "../workflows/options.js";
5
+ import { NotificationsOptions } from "../notifications/options.js";
6
+ import { StatusOptions } from "../status/options.js";
7
+ import { StoreNamesSource } from "../better-auth/store/store-names.js";
8
+ import { ResourceModel } from "@protobase/schema";
9
+ //#region src/schema-modules/feature-schemas-sql.d.ts
10
+ /** The webhooks schema, and its delivery log with `deliveryLog`. */
11
+ export declare const webhooksSchemaSql: (options?: WebhooksOptions, events?: EventsOptions) => string;
12
+ /** The workflows schema, and its storage configurations and custom steps while they are on. */
13
+ export declare const workflowsSchemaSql: (options?: Pick<WorkflowsOptions, "schema" | "storage" | "customSteps">) => string;
14
+ /** The devices and history tables, but for one a store of the app's own replaces; with `auth`, pointing at its users. */
15
+ export declare const notificationsSchemaSql: (options?: NotificationsOptions, auth?: StoreNamesSource) => string;
16
+ /** The statuses table, pointing at the users of `auth`. */
17
+ export declare const statusSchemaSql: (options: StatusOptions, auth: StoreNamesSource) => string;
18
+ /** The calls schema, and the notices table rings reach open pages through. */
19
+ export declare const callsSchemaSql: (events?: EventsOptions) => string;
20
+ /** The relay's trigger on the sessions table of `auth`, and `realtime.members` with the triggers of `channels`. */
21
+ export declare const relaySchemaSql: (auth: StoreNamesSource, channels: RealtimeRelaySettings["channels"], models: readonly ResourceModel[]) => string;
22
+ /** The `realtime_relay` schema of several processes. */
23
+ export declare const realtimeRelaySchemaSql: () => string;
24
+ /** The `channels` schema of SQL's broadcasts to the record channels. */
25
+ export declare const channelSendsSchemaSql: () => string;
26
+ /** The rollups and formulas of `models`, with their values set and their time rollups filled. */
27
+ export declare const computedSchemaSql: (models: readonly ResourceModel[]) => string;
28
+ /** The derived schema, with each derived value of `models` declared and its fill enqueued into the jobs schema. */
29
+ export declare const derivedSchemaSql: (models: readonly ResourceModel[]) => string;
30
+ /** The schemas of user tables and of their definitions, and pg_trgm. */
31
+ export declare const userTablesSchemaSql: () => string;
32
+ /** The comments table of the resources with `.comments()`. */
33
+ export declare const commentsSchemaSql: () => string;
34
+ /** The table of the bounces and complaints of the mail the app sends. */
35
+ export declare const mailFeedbackSchemaSql: () => string;
36
+ /** The table of who unsubscribed from which topic of the bulk mail. */
37
+ export declare const unsubscribesSchemaSql: () => string;
38
+ /** The role of the built-in assistant's read-only queries and its runner, with select on the tables of `models` but those of `auth`'s store. */
39
+ export declare const querySchemaSql: (models: readonly ResourceModel[], auth?: StoreNamesSource) => string;
40
+ //#endregion
@@ -0,0 +1,51 @@
1
+ import { storeNames } from "../better-auth/store/store-names.js";
2
+ import { changesSchema } from "../changes/events-options.js";
3
+ import { computedInput } from "../computed/computed-input.js";
4
+ import { derivedInput } from "../derived/derived-module.js";
5
+ import { noInput } from "./schema-module.js";
6
+ import { queryInput } from "../assistant/query-runner/query-input.js";
7
+ import { notificationsTargets } from "../notifications/notifications-targets.js";
8
+ import { relayInput } from "../relay/relay-targets.js";
9
+ import { statusTargets } from "../status/status-targets.js";
10
+ import { webhooksTargets } from "../webhooks/table/webhooks-targets.js";
11
+ import { workflowsTargets } from "../workflows/workflows-targets.js";
12
+ import { modulesSql } from "./modules-sql.js";
13
+ //#region src/schema-modules/feature-schemas-sql.ts
14
+ /** The webhooks schema, and its delivery log with `deliveryLog`. */
15
+ const webhooksSchemaSql = (options = {}, events) => modulesSql(webhooksTargets(options, events));
16
+ /** The workflows schema, and its storage configurations and custom steps while they are on. */
17
+ const workflowsSchemaSql = (options = {}) => modulesSql(workflowsTargets(options));
18
+ /** The devices and history tables, but for one a store of the app's own replaces; with `auth`, pointing at its users. */
19
+ const notificationsSchemaSql = (options = {}, auth) => modulesSql(notificationsTargets(options, auth && storeNames(auth)));
20
+ /** The statuses table, pointing at the users of `auth`. */
21
+ const statusSchemaSql = (options, auth) => modulesSql(statusTargets(options, storeNames(auth)));
22
+ /** The calls schema, and the notices table rings reach open pages through. */
23
+ const callsSchemaSql = (events) => modulesSql({
24
+ notices: {
25
+ names: { schema: changesSchema(events) },
26
+ resources: {}
27
+ },
28
+ calls: noInput
29
+ });
30
+ /** The relay's trigger on the sessions table of `auth`, and `realtime.members` with the triggers of `channels`. */
31
+ const relaySchemaSql = (auth, channels, models) => modulesSql({ relay: relayInput(channels, models, storeNames(auth)) });
32
+ /** The `realtime_relay` schema of several processes. */
33
+ const realtimeRelaySchemaSql = () => modulesSql({ realtime_relay: noInput });
34
+ /** The `channels` schema of SQL's broadcasts to the record channels. */
35
+ const channelSendsSchemaSql = () => modulesSql({ channel_sends: noInput });
36
+ /** The rollups and formulas of `models`, with their values set and their time rollups filled. */
37
+ const computedSchemaSql = (models) => modulesSql({ computed: computedInput(models) });
38
+ /** The derived schema, with each derived value of `models` declared and its fill enqueued into the jobs schema. */
39
+ const derivedSchemaSql = (models) => modulesSql({ derived: derivedInput(models) });
40
+ /** The schemas of user tables and of their definitions, and pg_trgm. */
41
+ const userTablesSchemaSql = () => modulesSql({ user_tables: noInput });
42
+ /** The comments table of the resources with `.comments()`. */
43
+ const commentsSchemaSql = () => modulesSql({ comments: noInput });
44
+ /** The table of the bounces and complaints of the mail the app sends. */
45
+ const mailFeedbackSchemaSql = () => modulesSql({ mail_feedback: noInput });
46
+ /** The table of who unsubscribed from which topic of the bulk mail. */
47
+ const unsubscribesSchemaSql = () => modulesSql({ unsubscribes: noInput });
48
+ /** The role of the built-in assistant's read-only queries and its runner, with select on the tables of `models` but those of `auth`'s store. */
49
+ const querySchemaSql = (models, auth) => modulesSql({ query: queryInput(models, auth && storeNames(auth)) });
50
+ //#endregion
51
+ export { callsSchemaSql, channelSendsSchemaSql, commentsSchemaSql, computedSchemaSql, derivedSchemaSql, mailFeedbackSchemaSql, notificationsSchemaSql, querySchemaSql, realtimeRelaySchemaSql, relaySchemaSql, statusSchemaSql, unsubscribesSchemaSql, userTablesSchemaSql, webhooksSchemaSql, workflowsSchemaSql };
@@ -0,0 +1,31 @@
1
+ import { ModuleInput, ModuleModel, SchemaModule } from "./schema-module.js";
2
+ //#region src/schema-modules/module-difference.d.ts
3
+ /**
4
+ * What brings `module` from `from`, `undefined` when the database has none of it, to its latest model for `input`: the
5
+ * steps after its last, then each replaceable at another version, then the removals of each resource's objects, the
6
+ * last resource replaceable's first, then each resource's objects that differ (`resourceDifference`), each under its
7
+ * title; `sql` undefined when nothing differs. `after`: what those objects rebuild once the transaction commits, the
8
+ * migration's `.after.sql`; undefined when they rebuild nothing. `breaks` says what of it the code before does not run
9
+ * on, one line each: a step that does not only add, and a replaceable replaced from a version older than its
10
+ * `compatibleFrom`; none when it is additive. `model` is the module once it is applied, which keeps the objects of
11
+ * resources kept for older code. Refuses a name of `input` that differs from the one `from` has.
12
+ */
13
+ export declare const moduleDifference: (module: SchemaModule, input: ModuleInput, from: ModuleModel | undefined) => {
14
+ sql: string | undefined;
15
+ after: string | undefined;
16
+ breaks: string[];
17
+ model: ModuleModel;
18
+ };
19
+ /**
20
+ * What becomes of `module` once the config no longer needs it: its tables and replaceables stay as the migrations
21
+ * left them, and each resource's objects go, as no resource needs them, which breaks older code (`breaks`), but for
22
+ * those a replaceable without `drop` leaves; `sql` undefined when it has none. It leaves nothing for after the
23
+ * transaction.
24
+ */
25
+ export declare const retiredDifference: (module: SchemaModule, from: ModuleModel) => {
26
+ sql: string | undefined;
27
+ after: undefined;
28
+ breaks: string[];
29
+ model: ModuleModel;
30
+ };
31
+ //#endregion
@@ -0,0 +1,98 @@
1
+ import { resourceDifference, resourceEntry, wantedResources } from "./resource-difference.js";
2
+ import { noInput } from "./schema-module.js";
3
+ //#region src/schema-modules/module-difference.ts
4
+ const withParts = (model, names, resources) => {
5
+ const used = Object.fromEntries(Object.entries(resources).filter(([, entries]) => Object.keys(entries).length > 0));
6
+ return {
7
+ ...model,
8
+ ...Object.keys(names).length > 0 && { names },
9
+ ...Object.keys(used).length > 0 && { resources: used }
10
+ };
11
+ };
12
+ /**
13
+ * The model of `module` with every step and replaceable this version of Protobase ships, and the objects of each
14
+ * resource of `input` that needs them: what the runtime reads.
15
+ */
16
+ const latestModel = (module, input = noInput) => {
17
+ const resources = Object.fromEntries(module.resourceReplaceables.map((replaceable) => [replaceable.name, Object.fromEntries(wantedResources(input.resources[replaceable.name] ?? {}).map(([key, given]) => [key, resourceEntry(replaceable, input.names, given)]))]));
18
+ const model = {
19
+ lastStep: module.steps.at(-1).step,
20
+ replaceables: Object.fromEntries(module.replaceables.map(({ name, version }) => [name, version]))
21
+ };
22
+ return withParts(model, input.names, resources);
23
+ };
24
+ /**
25
+ * How the names `after` changes the ones `before`, from `source`, has, one line each: a module's names cannot change
26
+ * once its statements are written. A name `before` lacks is new rather than changed.
27
+ */
28
+ const moduleNameChanges = (module, before, after, source) => Object.entries(before ?? {}).flatMap(([role, name]) => after[role] === name ? [] : [`${module.label}'s ${role} is "${name}" in ${source} and "${after[role] ?? ""}" now`]);
29
+ const newerThanShipped = (module, from) => {
30
+ const latest = latestModel(module);
31
+ const resources = module.resourceReplaceables.some(({ name, version }) => Object.values(from.resources?.[name] ?? {}).some((entry) => entry.version > version));
32
+ return from.lastStep > latest.lastStep || Object.entries(from.replaceables).some(([name, version]) => version > (latest.replaceables[name] ?? 0)) || resources;
33
+ };
34
+ /**
35
+ * What brings `module` from `from`, `undefined` when the database has none of it, to its latest model for `input`: the
36
+ * steps after its last, then each replaceable at another version, then the removals of each resource's objects, the
37
+ * last resource replaceable's first, then each resource's objects that differ (`resourceDifference`), each under its
38
+ * title; `sql` undefined when nothing differs. `after`: what those objects rebuild once the transaction commits, the
39
+ * migration's `.after.sql`; undefined when they rebuild nothing. `breaks` says what of it the code before does not run
40
+ * on, one line each: a step that does not only add, and a replaceable replaced from a version older than its
41
+ * `compatibleFrom`; none when it is additive. `model` is the module once it is applied, which keeps the objects of
42
+ * resources kept for older code. Refuses a name of `input` that differs from the one `from` has.
43
+ */
44
+ const moduleDifference = (module, input, from) => {
45
+ if (from && newerThanShipped(module, from)) throw new Error(`${module.label} is newer than this version of Protobase writes: upgrade Protobase`);
46
+ const renamed = moduleNameChanges(module, from?.names, input.names, "the snapshot");
47
+ if (renamed.length > 0) throw new Error(`Not implemented: renaming what a migration made: ${renamed.join("; ")}. Name it as before in the config`);
48
+ const steps = module.steps.filter(({ step }) => step > (from?.lastStep ?? 0));
49
+ const replaced = module.replaceables.filter(({ name, version }) => from?.replaceables[name] !== version);
50
+ const perResource = module.resourceReplaceables.map((replaceable) => ({
51
+ name: replaceable.name,
52
+ ...resourceDifference(replaceable, input.names, input.resources[replaceable.name] ?? {}, from?.resources?.[replaceable.name] ?? {})
53
+ }));
54
+ const drops = perResource.toReversed().flatMap(({ drops }) => drops);
55
+ const parts = [
56
+ ...[...steps, ...replaced].map(({ title, sql }) => `-- ${title}.\n${sql(input.names).trim()}`),
57
+ ...drops,
58
+ ...perResource.flatMap(({ writes }) => writes)
59
+ ];
60
+ const after = perResource.flatMap(({ after }) => after);
61
+ const breakingSteps = steps.filter(({ additive }) => !additive).map(({ step, name }) => `its step ${step} (${name}) does not only add`);
62
+ const olderReplaced = replaced.flatMap(({ name, version, compatibleFrom }) => {
63
+ const had = from?.replaceables[name];
64
+ return had !== void 0 && had < compatibleFrom ? [`it replaces its ${name} at version ${had} with version ${version}, which the code before does not run on`] : [];
65
+ });
66
+ const model = withParts(latestModel(module), input.names, Object.fromEntries(perResource.map(({ name, entries }) => [name, entries])));
67
+ return {
68
+ sql: parts.length > 0 ? parts.join("\n\n") : void 0,
69
+ after: after.length > 0 ? after.join("\n\n") : void 0,
70
+ breaks: [
71
+ ...breakingSteps,
72
+ ...olderReplaced,
73
+ ...perResource.flatMap(({ breaks }) => breaks)
74
+ ],
75
+ model
76
+ };
77
+ };
78
+ /**
79
+ * What becomes of `module` once the config no longer needs it: its tables and replaceables stay as the migrations
80
+ * left them, and each resource's objects go, as no resource needs them, which breaks older code (`breaks`), but for
81
+ * those a replaceable without `drop` leaves; `sql` undefined when it has none. It leaves nothing for after the
82
+ * transaction.
83
+ */
84
+ const retiredDifference = (module, from) => {
85
+ const names = from.names ?? {};
86
+ const dropped = module.resourceReplaceables.map((replaceable) => resourceDifference(replaceable, names, {}, from.resources?.[replaceable.name] ?? {}));
87
+ const parts = dropped.toReversed().flatMap(({ drops }) => drops);
88
+ const { resources: _, names: __, ...rest } = from;
89
+ const model = withParts(rest, names, Object.fromEntries(module.resourceReplaceables.map(({ name }, index) => [name, dropped[index].entries])));
90
+ return {
91
+ sql: parts.length > 0 ? parts.join("\n\n") : void 0,
92
+ after: void 0,
93
+ breaks: dropped.flatMap(({ breaks }) => breaks),
94
+ model
95
+ };
96
+ };
97
+ //#endregion
98
+ export { latestModel, moduleDifference, moduleNameChanges, retiredDifference };
@@ -0,0 +1,13 @@
1
+ import { moduleDifference } from "./module-difference.js";
2
+ import { schemaModules, targetIds } from "./modules.js";
3
+ //#region src/schema-modules/modules-sql.ts
4
+ /**
5
+ * The schema modules of `targets`, in their order, as a migration writes them on a database without them, with what
6
+ * they leave for after the transaction run in it.
7
+ */
8
+ const modulesSql = (targets) => targetIds(targets).flatMap((id) => {
9
+ const { sql, after } = moduleDifference(schemaModules[id], targets[id], void 0);
10
+ return [sql, ...after === void 0 ? [] : [after]];
11
+ }).join("\n\n");
12
+ //#endregion
13
+ export { modulesSql };
@@ -0,0 +1,37 @@
1
+ import { ModuleInput, ModuleModel, SchemaModule } from "./schema-module.js";
2
+ //#region src/schema-modules/modules.d.ts
3
+ export declare const schemaModules: {
4
+ jobs: SchemaModule;
5
+ mail_feedback: SchemaModule;
6
+ computed: SchemaModule;
7
+ changes: SchemaModule;
8
+ history: SchemaModule;
9
+ derived: SchemaModule;
10
+ notices: SchemaModule;
11
+ comments: SchemaModule;
12
+ webhooks: SchemaModule;
13
+ webhook_delivery_log: SchemaModule;
14
+ workflows: SchemaModule;
15
+ workflow_storage: SchemaModule;
16
+ workflow_custom_steps: SchemaModule;
17
+ notification_devices: SchemaModule;
18
+ notification_history: SchemaModule;
19
+ status: SchemaModule;
20
+ calls: SchemaModule;
21
+ relay: SchemaModule;
22
+ realtime_relay: SchemaModule;
23
+ channel_sends: SchemaModule;
24
+ user_tables: SchemaModule;
25
+ unsubscribes: SchemaModule;
26
+ query: SchemaModule;
27
+ };
28
+ export type SchemaModuleId = keyof typeof schemaModules;
29
+ /** The schema modules' part of a model, by module: in the snapshot, the record and the runtime alike. */
30
+ export type ModulesModel = Partial<Record<SchemaModuleId, ModuleModel>>;
31
+ /** The schema modules an app needs, each with what it gives the module (`neededModules`), in their order. */
32
+ export type ModuleTargets = Partial<Record<SchemaModuleId, ModuleInput>>;
33
+ /** The modules of `targets`, in their order. */
34
+ export declare const targetIds: (targets: ModuleTargets) => ("status" | "query" | "user_tables" | "history" | "comments" | "derived" | "jobs" | "changes" | "mail_feedback" | "calls" | "webhooks" | "webhook_delivery_log" | "workflows" | "workflow_custom_steps" | "computed" | "notices" | "workflow_storage" | "notification_devices" | "notification_history" | "relay" | "realtime_relay" | "channel_sends" | "unsubscribes")[];
35
+ /** The model of each module of `targets` this version of Protobase reads. */
36
+ export declare const latestModules: (targets: ModuleTargets) => ModulesModel;
37
+ //#endregion
@@ -0,0 +1,56 @@
1
+ import { callsModule } from "../calls/calls-module.js";
2
+ import { channelSendsModule } from "../channels/sends-module.js";
3
+ import { computedModule } from "../computed/computed-module.js";
4
+ import { derivedModule } from "../derived/derived-module.js";
5
+ import { changesModule } from "../changes/changes-module.js";
6
+ import { commentsModule } from "../comments/comments-module.js";
7
+ import { historyModule } from "../history/history-module.js";
8
+ import { latestModel } from "./module-difference.js";
9
+ import { jobsModule } from "../jobs/jobs-module.js";
10
+ import { noticesModule } from "../live/notices-module.js";
11
+ import { mailFeedbackModule } from "../mail/feedback/mail-feedback-module.js";
12
+ import { unsubscribesModule } from "../mail/unsubscribe/unsubscribes-module.js";
13
+ import { notificationDevicesModule } from "../notifications/devices-module.js";
14
+ import { notificationHistoryModule } from "../notifications/history-module.js";
15
+ import { realtimeRelayModule } from "../realtime/relay/realtime-relay-module.js";
16
+ import { relayModule } from "../relay/relay-module.js";
17
+ import { statusModule } from "../status/status-module.js";
18
+ import { webhookDeliveryLogModule } from "../webhooks/table/delivery-log-module.js";
19
+ import { webhooksSchemaModule } from "../webhooks/table/webhooks-schema-module.js";
20
+ import { workflowCustomStepsModule } from "../workflows/custom-steps/custom-steps-module.js";
21
+ import { workflowStorageModule } from "../workflows/storage/storage-module.js";
22
+ import { workflowsSchemaModule } from "../workflows/workflows-schema-module.js";
23
+ import { userTablesModule } from "../user-tables/user-tables-module.js";
24
+ import { queryModule } from "../assistant/query-runner/query-module.js";
25
+ //#region src/schema-modules/modules.ts
26
+ const schemaModules = {
27
+ jobs: jobsModule,
28
+ mail_feedback: mailFeedbackModule,
29
+ computed: computedModule,
30
+ changes: changesModule,
31
+ history: historyModule,
32
+ derived: derivedModule,
33
+ notices: noticesModule,
34
+ comments: commentsModule,
35
+ webhooks: webhooksSchemaModule,
36
+ webhook_delivery_log: webhookDeliveryLogModule,
37
+ workflows: workflowsSchemaModule,
38
+ workflow_storage: workflowStorageModule,
39
+ workflow_custom_steps: workflowCustomStepsModule,
40
+ notification_devices: notificationDevicesModule,
41
+ notification_history: notificationHistoryModule,
42
+ status: statusModule,
43
+ calls: callsModule,
44
+ relay: relayModule,
45
+ realtime_relay: realtimeRelayModule,
46
+ channel_sends: channelSendsModule,
47
+ user_tables: userTablesModule,
48
+ unsubscribes: unsubscribesModule,
49
+ query: queryModule
50
+ };
51
+ /** The modules of `targets`, in their order. */
52
+ const targetIds = (targets) => Object.keys(schemaModules).filter((id) => targets[id]);
53
+ /** The model of each module of `targets` this version of Protobase reads. */
54
+ const latestModules = (targets) => Object.fromEntries(targetIds(targets).map((id) => [id, latestModel(schemaModules[id], targets[id])]));
55
+ //#endregion
56
+ export { latestModules, schemaModules, targetIds };
@@ -0,0 +1,41 @@
1
+ import { RealtimeOptions } from "../realtime/relay/realtime-options.js";
2
+ import { EventsOptions } from "../changes/events-options.js";
3
+ import { WebhooksOptions } from "../webhooks/table/options.js";
4
+ import { WorkflowsOptions } from "../workflows/options.js";
5
+ import { NotificationsOptions } from "../notifications/options.js";
6
+ import { StatusOptions } from "../status/options.js";
7
+ import { StoreNames } from "../better-auth/store/store-names.js";
8
+ import { ModuleTargets } from "./modules.js";
9
+ import { RelayApp } from "../relay/realtime-channels.js";
10
+ import { ResourceModel } from "@protobase/schema";
11
+ //#region src/schema-modules/needed-modules.d.ts
12
+ /** What of an app decides which schema modules it needs. */
13
+ export type ModuleApp = {
14
+ models: readonly ResourceModel[];
15
+ views: RelayApp['views'];
16
+ /** The app's own jobs. */
17
+ jobs: readonly unknown[];
18
+ signIn: boolean;
19
+ /** The auth store's names with sign-in on, whose users the notifications and statuses tables point at and whose sessions the relay's trigger is on. */
20
+ authStore?: StoreNames;
21
+ mailFeedback?: boolean;
22
+ unsubscribe?: boolean;
23
+ webhooks?: WebhooksOptions;
24
+ workflows?: Pick<WorkflowsOptions, 'schema' | 'storage' | 'customSteps'>;
25
+ notifications?: NotificationsOptions;
26
+ status?: StatusOptions;
27
+ calls?: object;
28
+ realtime?: RealtimeOptions;
29
+ /** Where captured changes live (`events` in `protobase.config.ts`). */
30
+ events?: EventsOptions;
31
+ /** Whether the tables users add serve (`userTables`). */
32
+ userTables?: boolean;
33
+ /** Whether the built-in assistant's read-only queries run, as a role of their own that must not read the auth store. */
34
+ readOnlyQueries?: boolean;
35
+ };
36
+ /**
37
+ * The schema modules `app` needs, in their order, each with what it takes from the app: the same for `protobase
38
+ * migration`, which writes them, and the server, which reads the record of migrations for them.
39
+ */
40
+ export declare const neededModules: (app: ModuleApp) => ModuleTargets;
41
+ //#endregion
@@ -0,0 +1,61 @@
1
+ import { capturedBy } from "../changes/capture-sql.js";
2
+ import { relayed, sqlSends } from "../realtime/relay/realtime-options.js";
3
+ import { derivedNames } from "../derived/derived-sql.js";
4
+ import { changesSchema } from "../changes/events-options.js";
5
+ import { hasBuckets } from "../computed/buckets-targets.js";
6
+ import { appRelayChannels } from "../relay/realtime-channels.js";
7
+ import { mailFeedbackResource } from "../mail/feedback/mail-feedback-resource.js";
8
+ import { computedInput, computedOn } from "../computed/computed-input.js";
9
+ import { derivedInput } from "../derived/derived-module.js";
10
+ import { noInput } from "./schema-module.js";
11
+ import { queryInput } from "../assistant/query-runner/query-input.js";
12
+ import { changesInput } from "../changes/changes-input.js";
13
+ import { notificationsTargets } from "../notifications/notifications-targets.js";
14
+ import { relayInput } from "../relay/relay-targets.js";
15
+ import { statusTargets } from "../status/status-targets.js";
16
+ import { webhooksTargets } from "../webhooks/table/webhooks-targets.js";
17
+ import { workflowsTargets } from "../workflows/workflows-targets.js";
18
+ //#region src/schema-modules/needed-modules.ts
19
+ /**
20
+ * The schema modules `app` needs, in their order, each with what it takes from the app: the same for `protobase
21
+ * migration`, which writes them, and the server, which reads the record of migrations for them.
22
+ */
23
+ const neededModules = (app) => {
24
+ const captured = app.mailFeedback ? [...app.models, mailFeedbackResource.toModel()] : app.models;
25
+ const comments = app.models.some((model) => model.comments);
26
+ const aiFields = app.models.some((model) => Object.values(model.fields).some((field) => field.ai));
27
+ const relayChannels = appRelayChannels(app.realtime?.relay, {
28
+ views: app.views,
29
+ models: app.models,
30
+ signIn: app.signIn
31
+ });
32
+ const changes = captured.some(capturedBy(captured));
33
+ const jobs = app.jobs.length > 0 || changes || comments || aiFields || app.models.some(hasBuckets) || Boolean(app.webhooks || app.workflows || app.notifications || relayChannels);
34
+ const events = {
35
+ names: { schema: changesSchema(app.events) },
36
+ resources: {}
37
+ };
38
+ return {
39
+ ...jobs && { jobs: noInput },
40
+ ...app.mailFeedback && { mail_feedback: noInput },
41
+ ...app.models.some(computedOn) && { computed: computedInput(app.models) },
42
+ ...changes && { changes: changesInput(captured, app.events) },
43
+ ...app.models.some((model) => model.history) && { history: events },
44
+ ...derivedNames(app.models).length > 0 && { derived: derivedInput(app.models) },
45
+ ...(app.models.some((model) => model.live) || Boolean(app.calls)) && { notices: events },
46
+ ...comments && { comments: noInput },
47
+ ...app.webhooks && webhooksTargets(app.webhooks, app.events),
48
+ ...app.workflows && workflowsTargets(app.workflows),
49
+ ...app.notifications && notificationsTargets(app.notifications, app.authStore),
50
+ ...app.status && statusTargets(app.status, app.authStore),
51
+ ...app.calls && { calls: noInput },
52
+ ...relayChannels && { relay: relayInput(relayChannels, app.models, app.authStore) },
53
+ ...relayed(app.realtime) && { realtime_relay: noInput },
54
+ ...sqlSends(app.realtime) && { channel_sends: noInput },
55
+ ...app.userTables && { user_tables: noInput },
56
+ ...app.unsubscribe && { unsubscribes: noInput },
57
+ ...app.readOnlyQueries && { query: queryInput(app.models, app.authStore) }
58
+ };
59
+ };
60
+ //#endregion
61
+ export { neededModules };