@protobase/server 0.129.0 → 0.129.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.
@@ -0,0 +1,31 @@
1
+ import { ModuleId } from "../../names/modules/index.js";
2
+ import { StoreNames } from "../store-names.js";
3
+ import { LastSteps } from "./store-model.js";
4
+ //#region src/better-auth/store/offline/baseline-store.d.ts
5
+ /** A relation in the store's schema: its `pg_class.relkind` and its columns. */
6
+ export type LiveRelation = {
7
+ kind: string;
8
+ columns: string[];
9
+ };
10
+ /** The store as the database has it: the versions `auth_migrations` records, and every relation of its schema by name. */
11
+ export type LiveStore = {
12
+ versions: number[];
13
+ relations: Record<string, LiveRelation>;
14
+ };
15
+ /** What the store is: each module's last step, the modules off that have tables, whether organizations were ever on, and the relations Protobase did not make. */
16
+ export type BaselineStore = {
17
+ lastSteps: LastSteps;
18
+ kept: ModuleId[];
19
+ organizationsEverOn: boolean;
20
+ external: string[];
21
+ };
22
+ /**
23
+ * The store at `live` under the config's `names`: each module that is on at the step the record holds, every module
24
+ * that is off but has a table as kept, organizations as ever on when a table whose rows belong to an organization has
25
+ * the column that names it, and the user table as external when the config reads it through a view over another
26
+ * system's (`users.keyTable`). Refuses a user table the database has as anything but a table without `users.keyTable`,
27
+ * since the steps' foreign keys would point at it, and a table a recorded step made that the schema lacks under the
28
+ * config's name, such as one renamed since, naming each.
29
+ */
30
+ export declare const baselineStore: (names: StoreNames, live: LiveStore) => BaselineStore;
31
+ //#endregion
@@ -0,0 +1,62 @@
1
+ import { allModules } from "../../names/modules-on.js";
2
+ import { resolveNames } from "../../names/resolve-names.js";
3
+ import { legacyVersions } from "../steps/legacy-versions.js";
4
+ import { moduleSteps } from "../steps/index.js";
5
+ //#region src/better-auth/store/offline/baseline-store.ts
6
+ const tableKinds = ["r", "p"];
7
+ const relationOf = (live, name) => Object.hasOwn(live.relations, name) ? live.relations[name] : void 0;
8
+ /** Each module's last step whose version, and every one before it in its chain, the store records. */
9
+ const recordedSteps = (versions) => {
10
+ const shipped = legacyVersions.at(-1).version;
11
+ const newer = versions.filter((version) => version > shipped);
12
+ if (newer.length > 0) throw new Error(`The auth store records version ${newer.join(", ")} in auth_migrations, which this version of Protobase does not ship; upgrade Protobase`);
13
+ const steps = {};
14
+ for (const module of allModules) {
15
+ const had = moduleSteps[module].findIndex((step) => !versions.includes(step.version));
16
+ const count = had === -1 ? moduleSteps[module].length : had;
17
+ if (count > 0) steps[module] = moduleSteps[module][count - 1].step;
18
+ }
19
+ return steps;
20
+ };
21
+ /**
22
+ * The store at `live` under the config's `names`: each module that is on at the step the record holds, every module
23
+ * that is off but has a table as kept, organizations as ever on when a table whose rows belong to an organization has
24
+ * the column that names it, and the user table as external when the config reads it through a view over another
25
+ * system's (`users.keyTable`). Refuses a user table the database has as anything but a table without `users.keyTable`,
26
+ * since the steps' foreign keys would point at it, and a table a recorded step made that the schema lacks under the
27
+ * config's name, such as one renamed since, naming each.
28
+ */
29
+ const baselineStore = (names, live) => {
30
+ const recorded = recordedSteps(live.versions);
31
+ const on = names.names.modules;
32
+ const every = resolveNames({
33
+ ...names.names.input,
34
+ modules: allModules
35
+ });
36
+ const has = (internal) => relationOf(live, every.tables[internal].name) !== void 0;
37
+ const user = names.names.table("user");
38
+ const externalUser = names.users.keyTable !== void 0;
39
+ const userRelation = relationOf(live, user.name);
40
+ if (!externalUser && userRelation && !tableKinds.includes(userRelation.kind)) throw new Error(`The auth store's ${user.name} is a view or another relation that is not a table: name the table that holds the user ids in users.keyTable, which the steps' foreign keys point at, and run the baseline again`);
41
+ const lastSteps = Object.fromEntries(on.filter((module) => recorded[module] !== void 0).map((module) => [module, recorded[module]]));
42
+ const made = Object.entries(lastSteps).flatMap(([module, last]) => moduleSteps[module].filter((step) => step.step <= last).flatMap((step) => step.tables));
43
+ const missing = [...lastSteps.auth !== void 0 && !externalUser ? ["user"] : [], ...made].filter((internal) => !has(internal));
44
+ if (missing.length > 0) {
45
+ const listed = missing.map((internal) => every.tables[internal].name).join(", ");
46
+ throw new Error(`The auth store's schema lacks ${listed}, which the versions in auth_migrations made: name each as the store has it in auth.tables, or apply the project's migrations first`);
47
+ }
48
+ const kept = allModules.filter((module) => !on.includes(module) && Object.values(every.tables).some((table) => table.module === module && has(table.internal)));
49
+ const organizationsEverOn = Object.values(every.tables).some((table) => {
50
+ const column = table.columns.organization_id;
51
+ return table.scope === "organization" && column !== void 0 && (relationOf(live, table.name)?.columns.includes(column.name) ?? false);
52
+ });
53
+ const prefix = names.schema === void 0 ? "" : `${names.schema}.`;
54
+ return {
55
+ lastSteps,
56
+ kept,
57
+ organizationsEverOn,
58
+ external: externalUser ? [`${prefix}${user.name}`] : []
59
+ };
60
+ };
61
+ //#endregion
62
+ export { baselineStore, recordedSteps };
@@ -6,9 +6,10 @@ import { LastSteps } from "./store-model.js";
6
6
  * followed by its row in `auth_migrations`, which the server's check reads, each run only while that table lacks its
7
7
  * version, so a database that has it already skips it; for a store the snapshot has none of
8
8
  * (`lastSteps` undefined), that table first. `checks`: the checks of the steps on tables the store has already, which
9
- * refuse when its rows break what a step assumes. `undefined` when the snapshot has every step.
9
+ * refuse when its rows break what a step assumes. `undefined` when the snapshot has every step. `externalUser`: the
10
+ * snapshot records the user table as a relation Protobase did not make, whose statements are left out.
10
11
  */
11
- export declare const offlineStepsMigration: (names: StoreNames, lastSteps: LastSteps | undefined) => {
12
+ export declare const offlineStepsMigration: (names: StoreNames, lastSteps: LastSteps | undefined, externalUser?: boolean) => {
12
13
  sql: string;
13
14
  checks: string[];
14
15
  } | undefined;
@@ -16,13 +16,14 @@ const refuseNewer = (lastSteps) => {
16
16
  * followed by its row in `auth_migrations`, which the server's check reads, each run only while that table lacks its
17
17
  * version, so a database that has it already skips it; for a store the snapshot has none of
18
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.
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.
20
21
  */
21
- const offlineStepsMigration = (names, lastSteps) => {
22
+ const offlineStepsMigration = (names, lastSteps, externalUser = false) => {
22
23
  refuseModulesOff(names);
23
24
  if (lastSteps) refuseNewer(lastSteps);
24
25
  const had = (module, step) => step.step <= (lastSteps?.[module] ?? 0);
25
- const migration = migrationNames(names);
26
+ const migration = migrationNames(names, externalUser || names.users.keyTable !== void 0);
26
27
  const rendered = renderSteps(migration, names.names.modules, had);
27
28
  if (rendered.length === 0) return void 0;
28
29
  const recorded = (version) => Object.entries(moduleSteps).every(([module, steps]) => steps.filter((step) => step.version === version).every((step) => had(module, step)));
@@ -1,11 +1,14 @@
1
1
  import { tableId } from "../../names/identifiers.js";
2
2
  import { sql } from "kysely";
3
3
  //#region src/better-auth/store/steps/step.ts
4
- /** The `ddl` parts of the store's names. */
5
- const migrationNames = ({ names, users, uuidIds }) => ({
4
+ /**
5
+ * The `ddl` parts of the store's names. `externalUser`: the user table is a relation Protobase did not make, which its
6
+ * statements leave alone, as under `users.keyTable` or as a project's snapshot records it.
7
+ */
8
+ const migrationNames = ({ names, users, uuidIds }, externalUser = users.keyTable !== void 0) => ({
6
9
  table: names.table,
7
10
  user: users.keyTable ? tableId(users.keyTable.schema, users.keyTable.table) : names.table("user"),
8
- userDdl: (statements) => users.keyTable ? [] : statements,
11
+ userDdl: (statements) => externalUser ? [] : statements,
9
12
  metadata: users.metadata,
10
13
  id: sql.raw(uuidIds ? "uuid default pg_catalog.gen_random_uuid()" : "text"),
11
14
  idRef: sql.raw(uuidIds ? "uuid" : "text")
@@ -27,7 +27,8 @@ import { settingsStoreNames } from "./better-auth/store/offline/settings-names.j
27
27
  import { LastSteps, StoreModel, latestSteps, storeModel, storeNamesList } from "./better-auth/store/offline/store-model.js";
28
28
  import { nameChanges } from "./better-auth/store/offline/name-changes.js";
29
29
  import { offlineStepsMigration } from "./better-auth/store/offline/offline-steps.js";
30
+ import { LiveStore, baselineStore } from "./better-auth/store/offline/baseline-store.js";
30
31
  import { LintedStep, lintSteps } from "./better-auth/store/steps/step-lint.js";
31
32
  import { RecordModel, modelHash, runtimeModel } from "./migration-record/record-model.js";
32
33
  import { AfterIndex, recordTable, recordTableSql } from "./migration-record/record-table.js";
33
- export { type AfterIndex, type AppAuthInput, type AuthPlugins, type LastSteps, type LintedStep, type RecordModel, type StoreModel, type StoreNames, appAuthOptions, appDialect, appPool, betterAuthAuthenticator, bucketsRefreshes, buildAppAuth, buildAuth, buildAuthenticator, captureNames, checkCallsOptions, checkNotificationsOptions, checkStatusOptions, createAuth, declaredJob, fileProviderOf, isOverSignIn, isSubscription, latestSteps, lintSteps, loadAuthPlugins, localFiles, longQueryPool, modelHash, nameChanges, offlineStepsMigration, readOrigins, readPasskeyDomain, recordTable, recordTableSql, recordsRuntimeModel, refreshBuckets, runtimeModel, s3Files, settingsStoreNames, storeModel, storeNames, storeNamesList, uncapturedMessage, webhooksModule, workflowPlugins, workflowsModule };
34
+ export { type AfterIndex, type AppAuthInput, type AuthPlugins, type LastSteps, type LintedStep, type LiveStore, type RecordModel, type StoreModel, type StoreNames, appAuthOptions, appDialect, appPool, baselineStore, betterAuthAuthenticator, bucketsRefreshes, buildAppAuth, buildAuth, buildAuthenticator, captureNames, checkCallsOptions, checkNotificationsOptions, checkStatusOptions, createAuth, declaredJob, fileProviderOf, isOverSignIn, isSubscription, latestSteps, lintSteps, loadAuthPlugins, localFiles, longQueryPool, modelHash, nameChanges, offlineStepsMigration, readOrigins, readPasskeyDomain, recordTable, recordTableSql, recordsRuntimeModel, refreshBuckets, runtimeModel, s3Files, settingsStoreNames, storeModel, storeNames, storeNamesList, uncapturedMessage, webhooksModule, workflowPlugins, workflowsModule };
package/dist/internal.js CHANGED
@@ -29,5 +29,6 @@ import { captureNames } from "./changes/capture-names.js";
29
29
  import { checkCallsOptions } from "./calls/calls-options.js";
30
30
  import { settingsStoreNames } from "./better-auth/store/offline/settings-names.js";
31
31
  import { offlineStepsMigration } from "./better-auth/store/offline/offline-steps.js";
32
+ import { baselineStore } from "./better-auth/store/offline/baseline-store.js";
32
33
  import { lintSteps } from "./better-auth/store/steps/step-lint.js";
33
- export { appAuthOptions, appDialect, appPool, betterAuthAuthenticator, bucketsRefreshes, buildAppAuth, buildAuth, buildAuthenticator, captureNames, checkCallsOptions, checkNotificationsOptions, checkStatusOptions, createAuth, declaredJob, fileProviderOf, isOverSignIn, isSubscription, latestSteps, lintSteps, loadAuthPlugins, localFiles, longQueryPool, modelHash, nameChanges, offlineStepsMigration, readOrigins, readPasskeyDomain, recordTable, recordTableSql, recordsRuntimeModel, refreshBuckets, runtimeModel, s3Files, settingsStoreNames, storeModel, storeNames, storeNamesList, uncapturedMessage, webhooksModule, workflowPlugins, workflowsModule };
34
+ export { appAuthOptions, appDialect, appPool, baselineStore, betterAuthAuthenticator, bucketsRefreshes, buildAppAuth, buildAuth, buildAuthenticator, captureNames, checkCallsOptions, checkNotificationsOptions, checkStatusOptions, createAuth, declaredJob, fileProviderOf, isOverSignIn, isSubscription, latestSteps, lintSteps, loadAuthPlugins, localFiles, longQueryPool, modelHash, nameChanges, offlineStepsMigration, readOrigins, readPasskeyDomain, recordTable, recordTableSql, recordsRuntimeModel, refreshBuckets, runtimeModel, s3Files, settingsStoreNames, storeModel, storeNames, storeNamesList, uncapturedMessage, webhooksModule, workflowPlugins, workflowsModule };
package/dist/package.js CHANGED
@@ -1,4 +1,4 @@
1
1
  //#region package.json
2
- var version = "0.129.0";
2
+ var version = "0.129.1";
3
3
  //#endregion
4
4
  export { version };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/server",
3
- "version": "0.129.0",
3
+ "version": "0.129.1",
4
4
  "description": "The Protobase REST API as a Hono app, with Better Auth sign-in.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -44,11 +44,11 @@
44
44
  "@better-auth/sso": "^1.7.12",
45
45
  "@livekit/protocol": "1.53.1",
46
46
  "@modelcontextprotocol/server": "^2.3.1",
47
- "@protobase/layout": "0.129.0",
48
- "@protobase/query": "0.129.0",
49
- "@protobase/schema": "0.129.0",
50
- "@protobase/workflow-plugins": "0.129.0",
51
- "@protobase/workflows": "0.129.0",
47
+ "@protobase/layout": "0.129.1",
48
+ "@protobase/query": "0.129.1",
49
+ "@protobase/schema": "0.129.1",
50
+ "@protobase/workflow-plugins": "0.129.1",
51
+ "@protobase/workflows": "0.129.1",
52
52
  "@react-email/render": "^2.1.0",
53
53
  "@react-pdf/primitives": "^4.4.0",
54
54
  "@react-pdf/renderer": "^4.9.0",