@protobase/server 0.129.1 → 0.130.1
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.
- package/dist/assistant/query-runner/grants-sql.js +32 -0
- package/dist/assistant/query-runner/query-input.js +33 -0
- package/dist/assistant/query-runner/query-module.js +44 -0
- package/dist/assistant/query-runner/query-runner.d.ts +8 -11
- package/dist/assistant/query-runner/query-runner.js +15 -57
- package/dist/assistant/query-runner/read-query.js +54 -0
- package/dist/assistant/query-runner/runner-sql.js +4 -20
- package/dist/assistant/query-runner/steps/001-role.js +19 -0
- package/dist/auth-store.d.ts +2 -2
- package/dist/auth-store.js +3 -3
- package/dist/better-auth/auth-schema.d.ts +13 -4
- package/dist/better-auth/auth-schema.js +40 -32
- package/dist/better-auth/auth-settings.d.ts +59 -11
- package/dist/better-auth/auth-settings.js +57 -13
- package/dist/better-auth/auth-tables.d.ts +6 -3
- package/dist/better-auth/auth-tables.js +12 -7
- package/dist/better-auth/authenticator.js +10 -2
- package/dist/better-auth/create-auth.d.ts +62 -7
- package/dist/better-auth/create-auth.js +55 -14
- package/dist/better-auth/device-sign-in-check.js +13 -0
- package/dist/better-auth/names/declaration.d.ts +18 -5
- package/dist/better-auth/names/declaration.js +3 -1
- package/dist/better-auth/names/modules/auth.js +9 -0
- package/dist/better-auth/names/modules/index.d.ts +34 -4
- package/dist/better-auth/names/modules/organizations.js +10 -3
- package/dist/better-auth/names/modules/personal-access-tokens.js +6 -3
- package/dist/better-auth/names/modules-on.js +25 -2
- package/dist/better-auth/names/resolve-names.d.ts +9 -1
- package/dist/better-auth/names/resolve-names.js +15 -9
- package/dist/better-auth/organizations/accept-invitation.js +5 -4
- package/dist/better-auth/organizations/add-every-user.js +3 -2
- package/dist/better-auth/organizations/host-scope.d.ts +10 -0
- package/dist/better-auth/organizations/host-scope.js +15 -0
- package/dist/better-auth/organizations/host.d.ts +12 -6
- package/dist/better-auth/organizations/host.js +99 -61
- package/dist/better-auth/organizations/kept-organizations.d.ts +21 -0
- package/dist/better-auth/organizations/kept-organizations.js +62 -0
- package/dist/better-auth/organizations/member-endpoints.js +4 -3
- package/dist/better-auth/organizations/options.d.ts +15 -0
- package/dist/better-auth/organizations/options.js +19 -1
- package/dist/better-auth/organizations/people-endpoint.js +3 -2
- package/dist/better-auth/organizations/pinned-membership.js +81 -0
- package/dist/better-auth/organizations/pinned-sessions.js +46 -0
- package/dist/better-auth/organizations/session-organization.js +3 -2
- package/dist/better-auth/organizations/store.d.ts +1 -1
- package/dist/better-auth/organizations/store.js +14 -9
- package/dist/better-auth/personal-access-tokens/pinned-tokens.js +23 -0
- package/dist/better-auth/personal-access-tokens/token-plugin.js +75 -54
- package/dist/better-auth/personal-access-tokens/token-sessions.js +6 -3
- package/dist/better-auth/personal-access-tokens/token-store.js +15 -11
- package/dist/better-auth/store/adoption.js +25 -10
- package/dist/better-auth/store/drift.js +5 -4
- package/dist/better-auth/store/legacy-record.d.ts +10 -0
- package/dist/better-auth/store/legacy-record.js +56 -0
- package/dist/better-auth/store/migration-sql.js +31 -26
- package/dist/better-auth/store/offline/archive-revival.d.ts +11 -0
- package/dist/better-auth/store/offline/archive-revival.js +25 -0
- package/dist/better-auth/store/offline/baseline-store.d.ts +11 -7
- package/dist/better-auth/store/offline/baseline-store.js +15 -12
- package/dist/better-auth/store/offline/contract-gate.d.ts +18 -0
- package/dist/better-auth/store/offline/contract-gate.js +35 -0
- package/dist/better-auth/store/offline/module-archive.d.ts +41 -0
- package/dist/better-auth/store/offline/module-archive.js +95 -0
- package/dist/better-auth/store/offline/module-delete.d.ts +22 -0
- package/dist/better-auth/store/offline/module-delete.js +99 -0
- package/dist/better-auth/store/offline/module-switches.d.ts +32 -0
- package/dist/better-auth/store/offline/module-switches.js +113 -0
- package/dist/better-auth/store/offline/name-changes.d.ts +4 -1
- package/dist/better-auth/store/offline/name-changes.js +5 -2
- package/dist/better-auth/store/offline/offline-steps.d.ts +21 -7
- package/dist/better-auth/store/offline/offline-steps.js +29 -20
- package/dist/better-auth/store/offline/retired-watch.d.ts +5 -0
- package/dist/better-auth/store/offline/retired-watch.js +43 -0
- package/dist/better-auth/store/offline/settings-names.d.ts +3 -2
- package/dist/better-auth/store/offline/settings-names.js +10 -3
- package/dist/better-auth/store/offline/store-model.d.ts +27 -1
- package/dist/better-auth/store/offline/store-model.js +22 -5
- package/dist/better-auth/store/resources-outside.js +1 -1
- package/dist/better-auth/store/step-record.d.ts +13 -0
- package/dist/better-auth/store/step-record.js +59 -0
- package/dist/better-auth/store/steps/index.js +6 -1
- package/dist/better-auth/store/steps/legacy-versions.d.ts +7 -0
- package/dist/better-auth/store/steps/legacy-versions.js +4 -0
- package/dist/better-auth/store/steps/organizations/003-default-organization.js +113 -0
- package/dist/better-auth/store/steps/render.d.ts +3 -0
- package/dist/better-auth/store/steps/render.js +48 -25
- package/dist/better-auth/store/steps/step-lint.d.ts +14 -0
- package/dist/better-auth/store/steps/step-lint.js +31 -10
- package/dist/better-auth/store/steps/step.d.ts +18 -0
- package/dist/better-auth/store/steps/step.js +7 -1
- package/dist/better-auth/store/store-db.d.ts +6 -0
- package/dist/better-auth/store/store-db.js +11 -5
- package/dist/better-auth/store/store-names.d.ts +4 -0
- package/dist/better-auth/store/store-sql.d.ts +1 -1
- package/dist/better-auth/token-standing.js +12 -7
- package/dist/better-auth/users.d.ts +7 -2
- package/dist/better-auth/users.js +25 -3
- package/dist/built-in-resources.js +1 -1
- package/dist/calls/calls-module.js +42 -0
- package/dist/calls/calls-sql.js +4 -30
- package/dist/changes/capture-names.d.ts +3 -1
- package/dist/changes/capture-names.js +5 -2
- package/dist/changes/capture-schema-sql.d.ts +10 -0
- package/dist/changes/capture-schema-sql.js +21 -0
- package/dist/changes/capture-schema.d.ts +0 -18
- package/dist/changes/capture-schema.js +1 -74
- package/dist/changes/capture-sql.js +30 -24
- package/dist/changes/changes-input.js +36 -0
- package/dist/changes/changes-module.js +44 -0
- package/dist/changes/read-capture.js +67 -0
- package/dist/changes/steps/001-outbox.js +23 -0
- package/dist/channels/sends-module.js +37 -0
- package/dist/channels/sends-schema-sql.js +5 -5
- package/dist/comments/comments-module.js +30 -0
- package/dist/comments/comments-sql.js +1 -22
- package/dist/computed/buckets-sql.d.ts +5 -1
- package/dist/computed/buckets-sql.js +39 -46
- package/dist/computed/{buckets-schema.d.ts → buckets-targets.d.ts} +2 -3
- package/dist/computed/buckets-targets.js +50 -0
- package/dist/computed/computed-input.js +65 -0
- package/dist/computed/computed-module.js +102 -0
- package/dist/computed/formula-sql.js +14 -1
- package/dist/computed/read-computed.js +100 -0
- package/dist/computed/rollup-sql.js +13 -10
- package/dist/config-exports.d.ts +2 -2
- package/dist/create-admin.js +21 -5
- package/dist/derived/derived-module.js +89 -0
- package/dist/derived/derived-sql.js +1 -47
- package/dist/functions/define-function.d.ts +10 -4
- package/dist/functions/routes.js +5 -3
- package/dist/functions/supabase-routes.js +7 -5
- package/dist/history/history-module.js +34 -0
- package/dist/index.d.ts +19 -28
- package/dist/index.js +11 -21
- package/dist/internal.d.ts +30 -10
- package/dist/internal.js +26 -8
- package/dist/jobs/functions-sql.js +7 -11
- package/dist/jobs/job-data.js +36 -0
- package/dist/jobs/job-types.d.ts +6 -3
- package/dist/jobs/jobs-module.d.ts +5 -0
- package/dist/jobs/jobs-module.js +84 -0
- package/dist/jobs/run-job.d.ts +1 -1
- package/dist/jobs/run-job.js +7 -5
- package/dist/jobs/runtime.d.ts +9 -4
- package/dist/jobs/runtime.js +11 -6
- package/dist/jobs/schema.d.ts +0 -11
- package/dist/jobs/schema.js +1 -59
- package/dist/jobs/steps/001-pg-boss-45.js +483 -0
- package/dist/live/notices-module.js +30 -0
- package/dist/live/notices-sql.js +2 -1
- package/dist/mail/feedback/feedback-schema.js +3 -30
- package/dist/mail/feedback/mail-feedback-module.js +37 -0
- package/dist/mail/unsubscribe/unsubscribe-schema.js +3 -21
- package/dist/mail/unsubscribe/unsubscribes-module.js +31 -0
- package/dist/migration-record/check-record.d.ts +9 -7
- package/dist/migration-record/check-record.js +12 -11
- package/dist/migration-record/module-needs.js +30 -0
- package/dist/migration-record/read-record.js +27 -15
- package/dist/migration-record/record-model.d.ts +7 -2
- package/dist/migration-record/record-model.js +14 -6
- package/dist/migration-record/startup-decision.js +18 -7
- package/dist/migration-sql/additive-statements.d.ts +4 -0
- package/dist/migration-sql/additive-statements.js +105 -0
- package/dist/notifications/devices-module.js +61 -0
- package/dist/notifications/history-module.js +50 -0
- package/dist/notifications/notifications-targets.js +23 -0
- package/dist/notifications/read-table.js +16 -0
- package/dist/notifications/schema-sql.js +1 -22
- package/dist/notifications/schema.js +1 -101
- package/dist/notifications/table-names.js +40 -0
- package/dist/organization-scope/app-scope.js +38 -0
- package/dist/organization-scope/auth-request-organization.js +71 -0
- package/dist/organization-scope/organization-tables.js +20 -0
- package/dist/organization-scope/sign-in-organization.js +21 -0
- package/dist/package.js +1 -1
- package/dist/personal-mail/shared-sender-store.js +4 -2
- package/dist/realtime/relay/realtime-relay-module.js +37 -0
- package/dist/relation-found.js +6 -0
- package/dist/relay/read-relay.js +85 -0
- package/dist/relay/realtime-members-sql.js +51 -28
- package/dist/relay/relay-module.js +53 -0
- package/dist/relay/relay-targets.js +28 -0
- package/dist/relay/session-end.js +1 -3
- package/dist/relay/sessions-sql.js +4 -27
- package/dist/rest/table-routes.js +9 -2
- package/dist/schema-modules/feature-schemas-sql.d.ts +40 -0
- package/dist/schema-modules/feature-schemas-sql.js +51 -0
- package/dist/schema-modules/module-difference.d.ts +31 -0
- package/dist/schema-modules/module-difference.js +98 -0
- package/dist/schema-modules/modules-sql.js +13 -0
- package/dist/schema-modules/modules.d.ts +37 -0
- package/dist/schema-modules/modules.js +56 -0
- package/dist/schema-modules/needed-modules.d.ts +41 -0
- package/dist/schema-modules/needed-modules.js +61 -0
- package/dist/schema-modules/resource-difference.js +83 -0
- package/dist/schema-modules/schema-module.d.ts +112 -0
- package/dist/schema-modules/schema-module.js +8 -0
- package/dist/status/status-module.js +63 -0
- package/dist/status/status-targets.js +21 -0
- package/dist/types.d.ts +6 -1
- package/dist/user-tables/reloading-admin.d.ts +1 -0
- package/dist/user-tables/reloading-admin.js +1 -0
- package/dist/user-tables/schema-sql.js +1 -14
- package/dist/user-tables/schema.js +4 -27
- package/dist/user-tables/user-tables-module.js +38 -0
- package/dist/webhooks/table/delivery-log-module.js +36 -0
- package/dist/webhooks/table/owner-roles.js +3 -2
- package/dist/webhooks/table/schema-sql.js +27 -26
- package/dist/webhooks/table/webhooks-schema-module.js +63 -0
- package/dist/webhooks/table/webhooks-targets.js +26 -0
- package/dist/webhooks/webhooks.js +1 -1
- package/dist/workflows/custom-steps/custom-steps-module.js +51 -0
- package/dist/workflows/custom-steps/custom-steps-sql.js +10 -7
- package/dist/workflows/custom-steps/runners-sql.js +10 -7
- package/dist/workflows/read-catalogue.js +23 -0
- package/dist/workflows/schema-sql.js +12 -9
- package/dist/workflows/status-sql.js +12 -6
- package/dist/workflows/storage/storage-module.js +39 -0
- package/dist/workflows/storage/storage-sql.js +8 -5
- package/dist/workflows/workflows-schema-module.js +49 -0
- package/dist/workflows/workflows-tables-sql.js +32 -0
- package/dist/workflows/workflows-targets.js +16 -0
- package/package.json +6 -6
- package/dist/assistant/query-runner/runner-plan.js +0 -30
- package/dist/better-auth/store/migrations-table.js +0 -27
- package/dist/better-auth/store/modules-off.js +0 -17
- package/dist/calls/calls-sql.d.ts +0 -11
- package/dist/changes/capture-plan.js +0 -79
- package/dist/channels/sends-schema.d.ts +0 -7
- package/dist/channels/sends-schema.js +0 -18
- package/dist/comments/comments-sql.d.ts +0 -14
- package/dist/computed/buckets-plan.d.ts +0 -2
- package/dist/computed/buckets-plan.js +0 -88
- package/dist/computed/buckets-schema.js +0 -39
- package/dist/computed/computed-plan.js +0 -81
- package/dist/computed/computed-schema.d.ts +0 -16
- package/dist/computed/computed-schema.js +0 -63
- package/dist/derived/derived-sql.d.ts +0 -12
- package/dist/mail/feedback/feedback-schema.d.ts +0 -7
- package/dist/mail/unsubscribe/unsubscribe-schema.d.ts +0 -10
- package/dist/notifications/schema.d.ts +0 -14
- package/dist/realtime/relay/relay-schema.d.ts +0 -7
- package/dist/realtime/relay/relay-schema.js +0 -20
- package/dist/relay/realtime-members-schema.js +0 -67
- package/dist/relay/relay-schema.d.ts +0 -14
- package/dist/relay/relay-schema.js +0 -28
- package/dist/status/schema.d.ts +0 -12
- package/dist/status/schema.js +0 -33
- package/dist/user-tables/schema.d.ts +0 -11
- package/dist/webhooks/table/schema.d.ts +0 -16
- package/dist/webhooks/table/schema.js +0 -56
- package/dist/workflows/schema.d.ts +0 -13
- package/dist/workflows/schema.js +0 -88
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { legacyVersions } from "../steps/legacy-versions.js";
|
|
2
|
-
import { guardedStepsSql, migrationsTableSql, statementsSql } from "../migration-sql.js";
|
|
3
1
|
import { moduleSteps } from "../steps/index.js";
|
|
2
|
+
import { afterStepsSql, guardedStepsSql, statementsSql, stepRecordSql } from "../migration-sql.js";
|
|
4
3
|
import { renderSteps } from "../steps/render.js";
|
|
4
|
+
import { stepRecordMigration } from "../legacy-record.js";
|
|
5
5
|
import { migrationNames } from "../steps/step.js";
|
|
6
|
-
import { refuseModulesOff } from "../modules-off.js";
|
|
7
6
|
//#region src/better-auth/store/offline/offline-steps.ts
|
|
8
7
|
const refuseNewer = (lastSteps) => {
|
|
9
8
|
for (const [module, last] of Object.entries(lastSteps)) {
|
|
@@ -12,26 +11,36 @@ const refuseNewer = (lastSteps) => {
|
|
|
12
11
|
}
|
|
13
12
|
};
|
|
14
13
|
/**
|
|
15
|
-
* The
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* (`lastSteps` undefined), that table first. `checks`: the checks of the steps on tables the store has already, which
|
|
19
|
-
* refuse when its rows break what a step assumes. `undefined` when the snapshot has every step. `externalUser`: the
|
|
20
|
-
* snapshot records the user table as a relation Protobase did not make, whose statements are left out.
|
|
14
|
+
* The parts the store has: those of each module's steps up to its last, and those a step adds to another module's
|
|
15
|
+
* tables while both are on. Those it added to a module that was off since, or before, are written again, and their
|
|
16
|
+
* guard skips them where the database has them.
|
|
21
17
|
*/
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
18
|
+
const snapshotHad = ({ lastSteps, on }) => (module, step, target) => step <= (lastSteps[module] ?? 0) && (target === module || on.includes(module) && on.includes(target));
|
|
19
|
+
/**
|
|
20
|
+
* The parts of the modules that are on that the snapshot's store lacks, each followed by its row in `auth_steps`, each
|
|
21
|
+
* run only while that table lacks it, so a database that has it already skips it; for a store the snapshot has none of
|
|
22
|
+
* (`store` undefined), that table first, and for one without it, that table, filled from `auth_migrations`. `checks`:
|
|
23
|
+
* the checks of the parts on tables the store has already, which refuse when its rows break what a step assumes;
|
|
24
|
+
* `after`, what the parts leave for the migration's `.after.sql`, when they leave anything. `undefined` when the
|
|
25
|
+
* snapshot's store has every part. `externalUser`: the snapshot records the user table as a relation Protobase did
|
|
26
|
+
* not make, whose statements are left out. `organizations`: the config's default organization and the role of the
|
|
27
|
+
* users there already (`renderSteps`).
|
|
28
|
+
*/
|
|
29
|
+
const offlineStepsMigration = (names, store, organizations, externalUser = false) => {
|
|
30
|
+
if (store) refuseNewer(store.lastSteps);
|
|
31
|
+
const had = store ? snapshotHad(store) : () => false;
|
|
26
32
|
const migration = migrationNames(names, externalUser || names.users.keyTable !== void 0);
|
|
27
|
-
const rendered = renderSteps(migration, names.names.modules, had);
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const steps =
|
|
31
|
-
const
|
|
33
|
+
const rendered = renderSteps(migration, names.names.modules, had, organizations);
|
|
34
|
+
const record = !store ? [stepRecordSql(names)] : store.stepRecord ? [] : [stepRecordMigration(names)];
|
|
35
|
+
if (rendered.length === 0 && record.length === 0) return;
|
|
36
|
+
const steps = rendered.length === 0 ? [] : [guardedStepsSql(names, rendered)];
|
|
37
|
+
const onTablesThere = ({ module, on = module }) => had(on, moduleSteps[on][0].step, on);
|
|
38
|
+
const checks = rendered.filter((part) => part.check && onTablesThere(part)).map((part) => statementsSql([part.check]));
|
|
39
|
+
const after = afterStepsSql(rendered);
|
|
32
40
|
return {
|
|
33
|
-
sql:
|
|
34
|
-
checks
|
|
41
|
+
sql: [...record, ...steps].join("\n\n"),
|
|
42
|
+
checks,
|
|
43
|
+
...after && { after }
|
|
35
44
|
};
|
|
36
45
|
};
|
|
37
46
|
//#endregion
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { generatedName } from "../../names/identifier-length.js";
|
|
2
|
+
import { ddl } from "../ddl.js";
|
|
3
|
+
import { sql } from "kysely";
|
|
4
|
+
//#region src/better-auth/store/offline/retired-watch.ts
|
|
5
|
+
/** The schema of the watch, made by the first migration that retires a column. */
|
|
6
|
+
const retiredSchema = "retired";
|
|
7
|
+
/** The trigger on a column's table that counts its writes; a name past 63 bytes is shortened as Postgres would not. */
|
|
8
|
+
const watchTriggerName = (column) => generatedName(`${column}_retired_writes`);
|
|
9
|
+
const watchTable = ddl`create table if not exists ${retiredSchema}.${"writes"} (
|
|
10
|
+
table_name text not null,
|
|
11
|
+
column_name text not null,
|
|
12
|
+
hour timestamptz not null,
|
|
13
|
+
writes bigint not null,
|
|
14
|
+
primary key (table_name, column_name, hour)
|
|
15
|
+
)`;
|
|
16
|
+
const countFunction = ddl`create or replace function ${retiredSchema}.${"count_write"}() returns trigger language plpgsql security definer set search_path = pg_catalog, pg_temp as $retired$
|
|
17
|
+
begin
|
|
18
|
+
insert into retired.writes as counted (table_name, column_name, hour, writes)
|
|
19
|
+
values (format('%I.%I', tg_table_schema, tg_table_name), tg_argv[0], date_trunc('hour', now()), 1)
|
|
20
|
+
on conflict (table_name, column_name, hour) do update set writes = counted.writes + 1;
|
|
21
|
+
return null;
|
|
22
|
+
end
|
|
23
|
+
$retired$`;
|
|
24
|
+
/** The statements that make the watch's schema, table and function, each only when missing. */
|
|
25
|
+
const watchBaseStatements = () => [
|
|
26
|
+
ddl`create schema if not exists ${retiredSchema}`,
|
|
27
|
+
watchTable,
|
|
28
|
+
countFunction
|
|
29
|
+
];
|
|
30
|
+
/**
|
|
31
|
+
* The trigger that counts the writes of `column` of `table` that set it, under `name`: the column itself, or one that
|
|
32
|
+
* holds its values since, such as its archive, which then counts as `column`.
|
|
33
|
+
*/
|
|
34
|
+
const watchTrigger = (table, column, name = column) => ddl`create trigger ${watchTriggerName(name.name)} after insert or update of ${name} on ${table} for each row when (new.${name} is not null) execute function ${retiredSchema}.${"count_write"}(${sql.lit(column.name)})`;
|
|
35
|
+
/** The statements that start the watch on `columns`: its schema, table and function, then a trigger on each column. */
|
|
36
|
+
const watchStatements = (columns) => {
|
|
37
|
+
if (columns.length === 0) return [];
|
|
38
|
+
return [...watchBaseStatements(), ...columns.map(({ table, column }) => watchTrigger(table, column))];
|
|
39
|
+
};
|
|
40
|
+
/** The statements that stop the watch on `columns`, once their module is on again; a column retired before the watch existed has none. */
|
|
41
|
+
const unwatchStatements = (columns) => columns.map(({ table, column }) => ddl`drop trigger if exists ${watchTriggerName(column.name)} on ${table}`);
|
|
42
|
+
//#endregion
|
|
43
|
+
export { retiredSchema, unwatchStatements, watchBaseStatements, watchStatements, watchTrigger, watchTriggerName };
|
|
@@ -2,9 +2,10 @@ import { AuthSettings } from "../../auth-settings.js";
|
|
|
2
2
|
import { StoreNames } from "../store-names.js";
|
|
3
3
|
//#region src/better-auth/store/offline/settings-names.d.ts
|
|
4
4
|
/**
|
|
5
|
-
* The store's names from the app's `auth` settings alone, as the app's Better Auth
|
|
5
|
+
* The store's names from the app's `auth` settings alone, the modules its features turn on, as the app's Better Auth
|
|
6
|
+
* resolves them, without building it:
|
|
6
7
|
* no database, URL or secret. `ownUserIds`: the app's hooks give new users their ids (`generateUserId` in
|
|
7
8
|
* auth/hooks.ts), which the hooks then decide over `generateUserId: 'uuid'`.
|
|
8
9
|
*/
|
|
9
|
-
export declare const settingsStoreNames: (settings: Pick<AuthSettings, "schema" | "tables" | "users" | "generateUserId">, ownUserIds?: boolean) => StoreNames;
|
|
10
|
+
export declare const settingsStoreNames: (settings: Pick<AuthSettings, "schema" | "tables" | "users" | "generateUserId" | "organizations" | "mcp" | "openIdProvider" | "personalAccessTokens" | "personalMail" | "signInProviders" | "signInDevices">, ownUserIds?: boolean) => StoreNames;
|
|
10
11
|
//#endregion
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { modulesOf } from "../../names/modules-on.js";
|
|
2
2
|
import { resolveNames } from "../../names/resolve-names.js";
|
|
3
|
+
import { organizationsEnabled } from "../../organizations/options.js";
|
|
4
|
+
import { userTable } from "../../user-table.js";
|
|
3
5
|
//#region src/better-auth/store/offline/settings-names.ts
|
|
4
6
|
/**
|
|
5
|
-
* The store's names from the app's `auth` settings alone, as the app's Better Auth
|
|
7
|
+
* The store's names from the app's `auth` settings alone, the modules its features turn on, as the app's Better Auth
|
|
8
|
+
* resolves them, without building it:
|
|
6
9
|
* no database, URL or secret. `ownUserIds`: the app's hooks give new users their ids (`generateUserId` in
|
|
7
10
|
* auth/hooks.ts), which the hooks then decide over `generateUserId: 'uuid'`.
|
|
8
11
|
*/
|
|
@@ -12,7 +15,11 @@ const settingsStoreNames = (settings, ownUserIds = false) => {
|
|
|
12
15
|
schema,
|
|
13
16
|
names: resolveNames({
|
|
14
17
|
schema,
|
|
15
|
-
tables: settings.tables
|
|
18
|
+
tables: settings.tables,
|
|
19
|
+
modules: modulesOf({
|
|
20
|
+
...settings,
|
|
21
|
+
organizations: organizationsEnabled(settings.organizations)
|
|
22
|
+
})
|
|
16
23
|
}),
|
|
17
24
|
users: userTable(settings.users),
|
|
18
25
|
uuidIds: !ownUserIds && settings.generateUserId === "uuid"
|
|
@@ -17,7 +17,7 @@ export type StoreModel = {
|
|
|
17
17
|
userKeyTable: string | null;
|
|
18
18
|
/** The user table's JSON metadata column (`users.metadata`). */
|
|
19
19
|
userMetadata: string | null;
|
|
20
|
-
/** Whether every table whose rows belong to an organization has the column that names it. */
|
|
20
|
+
/** Whether every table whose rows belong to an organization has the column that names it while organizations are on. */
|
|
21
21
|
scopedTablesCarryOrganization: boolean;
|
|
22
22
|
/** Each module that is on: its last step applied and its tables by internal name. */
|
|
23
23
|
modules: Partial<Record<ModuleId, {
|
|
@@ -33,4 +33,30 @@ export declare const latestSteps: (modules: readonly ModuleId[]) => LastSteps;
|
|
|
33
33
|
export declare const storeModel: ({ schema, names, users, uuidIds }: StoreNames, lastSteps: LastSteps) => StoreModel;
|
|
34
34
|
/** Every name the store has in the database: its schema, each table, `table.column` and index, sorted. */
|
|
35
35
|
export declare const storeNamesList: (model: StoreModel) => string[];
|
|
36
|
+
/** A column a module turned off retired from a table still on, by their names. */
|
|
37
|
+
export type RetiredName = {
|
|
38
|
+
table: string;
|
|
39
|
+
column: string;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* What awaits the sign-off of a module turned off with `--off archive` or `--off delete`: the choice, the id of the
|
|
43
|
+
* migration that turned it off, and the columns it retired then.
|
|
44
|
+
*/
|
|
45
|
+
export type DeferredOff = {
|
|
46
|
+
choice: 'archive' | 'delete';
|
|
47
|
+
file: string;
|
|
48
|
+
columns: RetiredName[];
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* A module turned off whose tables and rows the store keeps: the last step it had, and its tables' names then; `off`
|
|
52
|
+
* once it awaits the sign-off that archives it.
|
|
53
|
+
*/
|
|
54
|
+
export type KeptModule = {
|
|
55
|
+
module: ModuleId;
|
|
56
|
+
step: number;
|
|
57
|
+
tables: Record<string, ModelTable>;
|
|
58
|
+
off?: DeferredOff;
|
|
59
|
+
};
|
|
60
|
+
/** `module` kept at `step`, its tables named as `names` would name them with it on. */
|
|
61
|
+
export declare const keptModule: ({ schema, names, users, uuidIds }: StoreNames, module: ModuleId, step: number) => KeptModule;
|
|
36
62
|
//#endregion
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { allModules } from "../../names/modules-on.js";
|
|
2
|
+
import { resolveNames } from "../../names/resolve-names.js";
|
|
2
3
|
import { moduleSteps } from "../steps/index.js";
|
|
3
4
|
//#region src/better-auth/store/offline/store-model.ts
|
|
4
5
|
/** The last step of each module of `modules`, as this version of Protobase ships them. */
|
|
5
6
|
const latestSteps = (modules) => Object.fromEntries(modules.map((module) => [module, moduleSteps[module].at(-1).step]));
|
|
6
|
-
const declaredIndexes = (internal) => builtInModules.flatMap((module) => module.tables[internal]?.indexes ?? []);
|
|
7
7
|
/** The model of a store with `names` at `lastSteps`: a module `lastSteps` leaves out is not in it. */
|
|
8
8
|
const storeModel = ({ schema, names, users, uuidIds }, lastSteps) => {
|
|
9
9
|
const tables = Object.values(names.tables);
|
|
@@ -13,14 +13,14 @@ const storeModel = ({ schema, names, users, uuidIds }, lastSteps) => {
|
|
|
13
13
|
ids: uuidIds ? "uuid" : "text",
|
|
14
14
|
userKeyTable: users.keyTable ? `${users.keyTable.schema}.${users.keyTable.table}` : null,
|
|
15
15
|
userMetadata: users.metadata ?? null,
|
|
16
|
-
scopedTablesCarryOrganization: tables.filter((table) => table.scope === "organization").every((table) => "organization_id" in table.columns),
|
|
16
|
+
scopedTablesCarryOrganization: !names.modules.includes("organizations") || tables.filter((table) => table.scope === "organization").every((table) => "organization_id" in table.columns),
|
|
17
17
|
modules: Object.fromEntries(modules.map((module) => [module, {
|
|
18
18
|
lastStep: lastSteps[module],
|
|
19
19
|
tables: Object.fromEntries(tables.filter((table) => table.module === module).map((table) => [table.internal, {
|
|
20
20
|
name: table.name,
|
|
21
21
|
scope: table.scope,
|
|
22
22
|
columns: Object.fromEntries(Object.values(table.columns).map((column) => [column.internal, column.name])),
|
|
23
|
-
indexes: Object.fromEntries(
|
|
23
|
+
indexes: Object.fromEntries(Object.keys(table.indexes).map((index) => [index, table.index(index).name]))
|
|
24
24
|
}]))
|
|
25
25
|
}]))
|
|
26
26
|
};
|
|
@@ -36,5 +36,22 @@ const storeNamesList = (model) => {
|
|
|
36
36
|
...Object.values(table.indexes).map((index) => `${prefix}${index}`)
|
|
37
37
|
])].sort();
|
|
38
38
|
};
|
|
39
|
+
/** `module` kept at `step`, its tables named as `names` would name them with it on. */
|
|
40
|
+
const keptModule = ({ schema, names, users, uuidIds }, module, step) => {
|
|
41
|
+
const withIt = resolveNames({
|
|
42
|
+
...names.input,
|
|
43
|
+
modules: allModules.filter((other) => other === module || names.modules.includes(other))
|
|
44
|
+
});
|
|
45
|
+
return {
|
|
46
|
+
module,
|
|
47
|
+
step,
|
|
48
|
+
tables: storeModel({
|
|
49
|
+
schema,
|
|
50
|
+
names: withIt,
|
|
51
|
+
users,
|
|
52
|
+
uuidIds
|
|
53
|
+
}, { [module]: step }).modules[module].tables
|
|
54
|
+
};
|
|
55
|
+
};
|
|
39
56
|
//#endregion
|
|
40
|
-
export { latestSteps, storeModel, storeNamesList };
|
|
57
|
+
export { keptModule, latestSteps, storeModel, storeNamesList };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { plainStoreDb } from "./store-db.js";
|
|
2
1
|
import { authStoreTables } from "../auth-tables.js";
|
|
2
|
+
import { plainStoreDb } from "./store-db.js";
|
|
3
3
|
import { sql } from "kysely";
|
|
4
4
|
//#region src/better-auth/store/resources-outside.ts
|
|
5
5
|
const quoted = (name) => `"${name.replaceAll("\"", "\"\"")}"`;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ModuleId } from "../names/modules/index.js";
|
|
2
|
+
import "../names/resolve-names.js";
|
|
3
|
+
import "./store-names.js";
|
|
4
|
+
import "./steps/render.js";
|
|
5
|
+
import { Kysely } from "kysely";
|
|
6
|
+
//#region src/better-auth/store/step-record.d.ts
|
|
7
|
+
/** A part of a step the store applied. */
|
|
8
|
+
export type AppliedStep = {
|
|
9
|
+
module: ModuleId;
|
|
10
|
+
step: number;
|
|
11
|
+
target: ModuleId;
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { builtInModules } from "../names/modules/index.js";
|
|
2
|
+
import { ddl } from "./ddl.js";
|
|
3
|
+
import { moduleSteps } from "./steps/index.js";
|
|
4
|
+
import { sql } from "kysely";
|
|
5
|
+
//#region src/better-auth/store/step-record.ts
|
|
6
|
+
/** `Had` over a list of applied parts. */
|
|
7
|
+
const hadOf = (applied) => (module, step, target) => applied.some((part) => part.module === module && part.step === step && part.target === target);
|
|
8
|
+
/** The table, in the store's schema, as the store's names resolve `auth_steps`. */
|
|
9
|
+
const stepRecordTable = ({ names }) => names.table("auth_steps");
|
|
10
|
+
/** The statement that makes the table when it is missing. */
|
|
11
|
+
const createStepRecord = (names) => {
|
|
12
|
+
const table = stepRecordTable(names);
|
|
13
|
+
const { module, step, target, applied_at } = table.columns;
|
|
14
|
+
return ddl`create table if not exists ${table} (
|
|
15
|
+
${module} text not null,
|
|
16
|
+
${step} integer not null,
|
|
17
|
+
${target} text not null,
|
|
18
|
+
${applied_at} timestamptz,
|
|
19
|
+
primary key (${module}, ${step}, ${target})
|
|
20
|
+
)`;
|
|
21
|
+
};
|
|
22
|
+
/** The part a rendered step is. */
|
|
23
|
+
const partOf = (part) => ({
|
|
24
|
+
module: part.module,
|
|
25
|
+
step: part.step.step,
|
|
26
|
+
target: part.on ?? part.module
|
|
27
|
+
});
|
|
28
|
+
/** The statement that records `parts` as applied, now. */
|
|
29
|
+
const recordSteps = (names, parts) => {
|
|
30
|
+
const table = stepRecordTable(names);
|
|
31
|
+
const { module, step, target, applied_at } = table.columns;
|
|
32
|
+
const rows = parts.map((part) => ddl`(${sql.lit(part.module)}, ${sql.lit(part.step)}, ${sql.lit(part.target)}, now())`);
|
|
33
|
+
return ddl`insert into ${table} (${module}, ${step}, ${target}, ${applied_at}) values ${sql.join(rows, sql`, `)}`;
|
|
34
|
+
};
|
|
35
|
+
/** The condition that holds once `part` is recorded. */
|
|
36
|
+
const stepRecorded = (names, part) => {
|
|
37
|
+
const table = stepRecordTable(names);
|
|
38
|
+
const { module, step, target } = table.columns;
|
|
39
|
+
const key = partOf(part);
|
|
40
|
+
return ddl`exists (select from ${table} where ${module} = ${sql.lit(key.module)} and ${step} = ${sql.lit(key.step)} and ${target} = ${sql.lit(key.target)})`;
|
|
41
|
+
};
|
|
42
|
+
const known = (part) => builtInModules.some(({ id }) => id === part.module) && builtInModules.some(({ id }) => id === part.target) && moduleSteps[part.module].some(({ step }) => step === part.step);
|
|
43
|
+
/** Refuses a recorded part of a step this version does not ship, such as one a newer Protobase applied. */
|
|
44
|
+
const refuseUnshipped = (rows) => {
|
|
45
|
+
const unknown = rows.filter((row) => !known(row));
|
|
46
|
+
if (unknown.length > 0) throw new Error(`The auth store records ${unknown.map((row) => `step ${row.step} of ${row.module}`).join(", ")}, which this version of Protobase does not ship; upgrade Protobase`);
|
|
47
|
+
};
|
|
48
|
+
/** The parts the store recorded; `undefined` when it has no `auth_steps`. Refuses a step this version does not ship. */
|
|
49
|
+
const appliedSteps = async (db, names) => {
|
|
50
|
+
const table = stepRecordTable(names);
|
|
51
|
+
const [found] = (await sql`select to_regclass(${String(table)}) is not null as found`.execute(db)).rows;
|
|
52
|
+
if (!found?.found) return;
|
|
53
|
+
const { module, step, target } = table.columns;
|
|
54
|
+
const { rows } = await sql`select ${module} as module, ${step} as step, ${target} as target from ${table} order by 1, 2, 3`.execute(db);
|
|
55
|
+
refuseUnshipped(rows);
|
|
56
|
+
return rows;
|
|
57
|
+
};
|
|
58
|
+
//#endregion
|
|
59
|
+
export { appliedSteps, createStepRecord, hadOf, partOf, recordSteps, refuseUnshipped, stepRecordTable, stepRecorded };
|
|
@@ -11,6 +11,7 @@ import { senderSharing } from "./mail-senders/005-sender-sharing.js";
|
|
|
11
11
|
import { mcp } from "./mcp/001-mcp.js";
|
|
12
12
|
import { organizations } from "./organizations/001-organizations.js";
|
|
13
13
|
import { organizationIndex } from "./organizations/002-organization-index.js";
|
|
14
|
+
import { defaultOrganization } from "./organizations/003-default-organization.js";
|
|
14
15
|
import { passkeys } from "./passkeys/001-passkeys.js";
|
|
15
16
|
import { rpIdIndex } from "./passkeys/002-rp-id-index.js";
|
|
16
17
|
import { personalAccessTokens } from "./personal-access-tokens/001-personal-access-tokens.js";
|
|
@@ -33,7 +34,11 @@ const moduleSteps = {
|
|
|
33
34
|
passkeys: [passkeys, rpIdIndex],
|
|
34
35
|
sign_in_policy: [signInPolicy],
|
|
35
36
|
staff_sign_in: [staffSignIn, staffSignInIndexes],
|
|
36
|
-
organizations: [
|
|
37
|
+
organizations: [
|
|
38
|
+
organizations,
|
|
39
|
+
organizationIndex,
|
|
40
|
+
defaultOrganization
|
|
41
|
+
],
|
|
37
42
|
mcp: [mcp],
|
|
38
43
|
personal_access_tokens: [personalAccessTokens],
|
|
39
44
|
mail_senders: [
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { generatedName } from "../../../names/identifier-length.js";
|
|
2
|
+
import { ddl } from "../../ddl.js";
|
|
3
|
+
import { sql } from "kysely";
|
|
4
|
+
//#region src/better-auth/store/steps/organizations/003-default-organization.ts
|
|
5
|
+
const fillBatch = 1e4;
|
|
6
|
+
const settings = ({ organizations }) => {
|
|
7
|
+
if (!organizations) throw new Error("organizations.default is not set: give the config the organization that stands in while organizations are off, such as { id, name, slug }");
|
|
8
|
+
return organizations;
|
|
9
|
+
};
|
|
10
|
+
const defaultId = ({ idRef }, input) => ddl`cast(${sql.lit(settings(input).default.id)} as ${idRef})`;
|
|
11
|
+
const setCheckName = ({ table }) => generatedName(`${table("personal_access_token").name}_organization_id_set`);
|
|
12
|
+
/**
|
|
13
|
+
* The default organization, `organizations.default`, and every row of an organization in one. Its organization is
|
|
14
|
+
* made when the store lacks it, under its id. When organizations turn on for the first time, every user there already
|
|
15
|
+
* becomes its member with `organizations.existingUsersRole`, which a new store with its own user table, holding none,
|
|
16
|
+
* does without, and every personal access token works in it; when they
|
|
17
|
+
* were on before, a token without an organization has none to copy, so the step refuses, naming it, such as one made
|
|
18
|
+
* while they were off before tokens made then were given the default organization as they were made. The tokens'
|
|
19
|
+
* organization is then required: filled in batches, checked without a scan, and indexed concurrently, once the
|
|
20
|
+
* migration's transaction commits.
|
|
21
|
+
*/
|
|
22
|
+
const defaultOrganization = {
|
|
23
|
+
step: 3,
|
|
24
|
+
name: "default_organization",
|
|
25
|
+
version: 18,
|
|
26
|
+
title: "The default organization, its members, and the organization of every personal access token",
|
|
27
|
+
tables: [],
|
|
28
|
+
check: ({ table, idRef }, input) => {
|
|
29
|
+
const organization = table("organization");
|
|
30
|
+
const o = organization.columns;
|
|
31
|
+
const { id, slug } = settings(input).default;
|
|
32
|
+
return ddl`do $check$
|
|
33
|
+
begin
|
|
34
|
+
if exists (select from ${organization} where ${o.slug} = ${sql.lit(slug)} and id <> cast(${sql.lit(id)} as ${idRef})) then
|
|
35
|
+
raise exception 'The auth store has an organization with the slug % under another id than the default organization''s, %: give organizations.default the id of that organization, or another slug.', ${sql.lit(slug)}, ${sql.lit(id)};
|
|
36
|
+
end if;
|
|
37
|
+
end
|
|
38
|
+
$check$`;
|
|
39
|
+
},
|
|
40
|
+
statements: (names, input) => {
|
|
41
|
+
const { table, user, idRef } = names;
|
|
42
|
+
const organization = table("organization");
|
|
43
|
+
const o = organization.columns;
|
|
44
|
+
const member = table("member");
|
|
45
|
+
const m = member.columns;
|
|
46
|
+
const { default: { name, slug }, existingUsersRole } = settings(input);
|
|
47
|
+
const id = defaultId(names, input);
|
|
48
|
+
const made = ddl`insert into ${organization} (id, ${o.name}, ${o.slug}, ${o.created_at})
|
|
49
|
+
select ${id}, ${sql.lit(name)}, ${sql.lit(slug)}, now()
|
|
50
|
+
where not exists (select from ${organization} where id = ${id})`;
|
|
51
|
+
if (!input.firstOn || !input.usersThere && existingUsersRole === void 0) return [made];
|
|
52
|
+
if (existingUsersRole === void 0) throw new Error("organizations.existingUsersRole is not set: name the app role every user there already holds in the default organization as organizations turn on");
|
|
53
|
+
return [made, ddl`insert into ${member} (id, ${m.organization_id}, ${m.user_id}, ${m.role}, ${m.created_at}, ${m.app_roles})
|
|
54
|
+
select cast(pg_catalog.gen_random_uuid() as ${idRef}), ${id}, people.id, 'member', now(), jsonb_build_array(${sql.lit(existingUsersRole)})
|
|
55
|
+
from ${user} as people
|
|
56
|
+
where not exists (select from ${member} where ${m.organization_id} = ${id} and ${m.user_id} = people.id)`];
|
|
57
|
+
},
|
|
58
|
+
adds: { personal_access_tokens: (names) => {
|
|
59
|
+
const token = names.table("personal_access_token");
|
|
60
|
+
return [ddl`alter table ${token} add constraint ${sql.id(setCheckName(names))} check (${token.columns.organization_id} is not null) not valid`];
|
|
61
|
+
} },
|
|
62
|
+
addsChecks: { personal_access_tokens: ({ table }, input) => {
|
|
63
|
+
const token = table("personal_access_token");
|
|
64
|
+
const t = token.columns;
|
|
65
|
+
if (input.firstOn) return;
|
|
66
|
+
return ddl`do $check$
|
|
67
|
+
declare
|
|
68
|
+
found text;
|
|
69
|
+
total bigint;
|
|
70
|
+
begin
|
|
71
|
+
select count(*) into total from ${token} where ${t.organization_id} is null;
|
|
72
|
+
if total > 0 then
|
|
73
|
+
select string_agg(id::text, ', ' order by id) into found from (select id from ${token} where ${t.organization_id} is null order by id limit 20) as listed;
|
|
74
|
+
raise exception 'The auth store has % personal access tokens without an organization, which organizations were on for: %. Delete them, or set the organization each works in, in a migration of the project''s own written before this one, then apply again.', total, found;
|
|
75
|
+
end if;
|
|
76
|
+
end
|
|
77
|
+
$check$`;
|
|
78
|
+
} },
|
|
79
|
+
addsAfter: { personal_access_tokens: (names, input, concurrent) => {
|
|
80
|
+
const token = names.table("personal_access_token");
|
|
81
|
+
const t = token.columns;
|
|
82
|
+
const check = sql.id(setCheckName(names));
|
|
83
|
+
const id = defaultId(names, input);
|
|
84
|
+
const pending = ddl`select id from ${token} where ${t.organization_id} is null limit ${sql.lit(fillBatch)}`;
|
|
85
|
+
const fill = concurrent ? ddl`do $fill$
|
|
86
|
+
declare
|
|
87
|
+
filled bigint;
|
|
88
|
+
begin
|
|
89
|
+
loop
|
|
90
|
+
update ${token} set ${t.organization_id} = ${id} where id in (${pending});
|
|
91
|
+
get diagnostics filled = row_count;
|
|
92
|
+
exit when filled = 0;
|
|
93
|
+
commit;
|
|
94
|
+
end loop;
|
|
95
|
+
end
|
|
96
|
+
$fill$` : ddl`update ${token} set ${t.organization_id} = ${id} where ${t.organization_id} is null`;
|
|
97
|
+
return [
|
|
98
|
+
...input.firstOn ? [fill] : [],
|
|
99
|
+
ddl`do $required$
|
|
100
|
+
begin
|
|
101
|
+
if exists (select from pg_catalog.pg_constraint where conrelid = ${sql.lit(String(token))}::regclass and conname = ${sql.lit(setCheckName(names))}) then
|
|
102
|
+
alter table ${token} validate constraint ${check};
|
|
103
|
+
alter table ${token} alter column ${t.organization_id} set not null;
|
|
104
|
+
alter table ${token} drop constraint ${check};
|
|
105
|
+
end if;
|
|
106
|
+
end
|
|
107
|
+
$required$`,
|
|
108
|
+
ddl`create index ${sql.raw(concurrent ? "concurrently " : "")}if not exists ${token.index("personal_access_token_organization_id_user_id_idx")} on ${token} (${t.organization_id}, ${t.user_id})`
|
|
109
|
+
];
|
|
110
|
+
} }
|
|
111
|
+
};
|
|
112
|
+
//#endregion
|
|
113
|
+
export { defaultOrganization };
|
|
@@ -1,41 +1,64 @@
|
|
|
1
1
|
import { builtInModules } from "../../names/modules/index.js";
|
|
2
2
|
import { moduleSteps } from "./index.js";
|
|
3
3
|
//#region src/better-auth/store/steps/render.ts
|
|
4
|
-
const addsOn = (names, module, step, on) => ({
|
|
5
|
-
module,
|
|
6
|
-
step,
|
|
7
|
-
on,
|
|
8
|
-
version: Math.max(step.version, moduleSteps[on][0].version),
|
|
9
|
-
statements: step.adds[on](names)
|
|
10
|
-
});
|
|
11
4
|
/**
|
|
12
|
-
* The
|
|
13
|
-
* the
|
|
14
|
-
*
|
|
15
|
-
* that is off gets nothing, and no other module adds to its tables.
|
|
5
|
+
* The version of the former single chain that made the part of `step` changing the tables of `target`: the step's
|
|
6
|
+
* own, or for the statements it adds to another module's tables, the later of its and that of the step that made
|
|
7
|
+
* them, as the former chain, with every module on, made them.
|
|
16
8
|
*/
|
|
17
|
-
const
|
|
9
|
+
const partVersion = (module, step, target) => target === module ? step.version : Math.max(step.version, moduleSteps[target][0].version);
|
|
10
|
+
const addsOn = (names, input, module, step, on) => {
|
|
11
|
+
const check = step.addsChecks?.[on]?.(names, input);
|
|
12
|
+
const after = step.addsAfter?.[on];
|
|
13
|
+
return {
|
|
14
|
+
module,
|
|
15
|
+
step,
|
|
16
|
+
on,
|
|
17
|
+
version: partVersion(module, step, on),
|
|
18
|
+
...check && { check },
|
|
19
|
+
statements: [...check ? [check] : [], ...step.adds[on](names, input)],
|
|
20
|
+
after: (concurrent) => after?.(names, input, concurrent) ?? []
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
const ownPart = (names, input, module, step) => {
|
|
24
|
+
const check = step.check?.(names, input);
|
|
25
|
+
return {
|
|
26
|
+
module,
|
|
27
|
+
step,
|
|
28
|
+
version: step.version,
|
|
29
|
+
...check && { check },
|
|
30
|
+
statements: [...check ? [check] : [], ...step.statements(names, input)],
|
|
31
|
+
after: () => []
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* The parts of the modules that are on that the store lacks, in order: each module's steps in its chain's order, the
|
|
36
|
+
* modules in the order they require one another, and the statements a step adds to another module's tables once that
|
|
37
|
+
* module's are there. A module the store has none of, such as one just turned on, gets its whole chain, and the
|
|
38
|
+
* statements other modules' steps add to its tables, those the store has applied included; a module that is off gets
|
|
39
|
+
* nothing, and no other module adds to its tables. `organizations`: the config's default organization and the role of
|
|
40
|
+
* the users there already, which the organizations' steps write rows from; organizations turn on for the first time
|
|
41
|
+
* when the store lacks their first step, and the users are there when it has sign-in's or another system made them.
|
|
42
|
+
*/
|
|
43
|
+
const renderSteps = (names, modules, had, organizations) => {
|
|
44
|
+
const input = {
|
|
45
|
+
organizations,
|
|
46
|
+
firstOn: !had("organizations", 1, "organizations"),
|
|
47
|
+
usersThere: names.externalUser || had("auth", 1, "auth")
|
|
48
|
+
};
|
|
18
49
|
const on = builtInModules.map(({ id }) => id).filter((module) => modules.includes(module));
|
|
19
50
|
const rendered = [];
|
|
20
51
|
const waiting = [];
|
|
21
|
-
const made = new Set(on.filter((module) => had(module, moduleSteps[module][0])));
|
|
22
|
-
const turnedOn = on.filter((module) => !made.has(module));
|
|
52
|
+
const made = new Set(on.filter((module) => had(module, moduleSteps[module][0].step, module)));
|
|
23
53
|
for (const module of on) {
|
|
24
|
-
for (const step of moduleSteps[module]
|
|
25
|
-
rendered.push(
|
|
26
|
-
|
|
27
|
-
step,
|
|
28
|
-
version: step.version,
|
|
29
|
-
statements: [...step.check ? [step.check(names)] : [], ...step.statements(names)]
|
|
30
|
-
});
|
|
31
|
-
for (const target of on.filter((target) => step.adds?.[target])) (made.has(target) ? rendered : waiting).push(addsOn(names, module, step, target));
|
|
54
|
+
for (const step of moduleSteps[module]) {
|
|
55
|
+
if (!had(module, step.step, module)) rendered.push(ownPart(names, input, module, step));
|
|
56
|
+
for (const target of on.filter((target) => step.adds?.[target] && !had(module, step.step, target))) (made.has(target) ? rendered : waiting).push(addsOn(names, input, module, step, target));
|
|
32
57
|
}
|
|
33
58
|
made.add(module);
|
|
34
59
|
rendered.push(...waiting.filter((adds) => adds.on === module));
|
|
35
|
-
if (!turnedOn.includes(module)) continue;
|
|
36
|
-
for (const other of on.filter((other) => other !== module)) for (const step of moduleSteps[other].filter((step) => step.adds?.[module] && had(other, step))) rendered.push(addsOn(names, other, step, module));
|
|
37
60
|
}
|
|
38
61
|
return rendered;
|
|
39
62
|
};
|
|
40
63
|
//#endregion
|
|
41
|
-
export { renderSteps };
|
|
64
|
+
export { partVersion, renderSteps };
|
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
//#region src/better-auth/store/steps/step-lint.d.ts
|
|
2
|
+
/** The Squawk rules a statement that takes from older code what it reads, writes or relies on breaks. */
|
|
3
|
+
export declare const contractingRules: string[];
|
|
4
|
+
/**
|
|
5
|
+
* The Squawk rules of a statement that holds a lock blocking reads for as long as it scans or rewrites a table. Its
|
|
6
|
+
* rules about locks that only block writes, such as an index built without `concurrently`, do not apply: the
|
|
7
|
+
* migrations run in one transaction while nothing else writes.
|
|
8
|
+
*/
|
|
9
|
+
export declare const lockingRules: string[];
|
|
2
10
|
/** A rendered step: `made`, the tables it makes as its SQL names them; `checked`, whether it starts with a check. */
|
|
3
11
|
export type LintedStep = {
|
|
4
12
|
label: string;
|
|
@@ -11,6 +19,12 @@ export type StepFinding = {
|
|
|
11
19
|
rule: string;
|
|
12
20
|
message: string;
|
|
13
21
|
};
|
|
22
|
+
/** What Squawk finds in a step, its line counted from 0. */
|
|
23
|
+
export type SquawkFinding = StepFinding & {
|
|
24
|
+
line: number;
|
|
25
|
+
};
|
|
26
|
+
/** Every finding of Squawk's in `steps`, each read as a file of its own, in one run. */
|
|
27
|
+
export declare const squawkReport: (steps: readonly Pick<LintedStep, "label" | "sql">[], squawk?: string) => SquawkFinding[];
|
|
14
28
|
/**
|
|
15
29
|
* What makes each step unsafe to apply unattended; none when every one is safe. `squawk`: the path of squawk-cli's
|
|
16
30
|
* `js/bin/squawk`, as the package that runs the lint resolves it.
|