@protobase/server 0.47.1 → 0.49.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 (105) hide show
  1. package/dist/admin-options.d.ts +2 -2
  2. package/dist/app-identity.d.ts +2 -2
  3. package/dist/app-name.d.ts +2 -0
  4. package/dist/auth-store.d.ts +3 -0
  5. package/dist/auth-store.js +3 -0
  6. package/dist/better-auth/auth-schema.d.ts +7 -8
  7. package/dist/better-auth/auth-schema.js +40 -42
  8. package/dist/better-auth/auth-settings.d.ts +8754 -0
  9. package/dist/better-auth/auth-settings.js +43 -0
  10. package/dist/better-auth/auth-tables.d.ts +1 -1
  11. package/dist/better-auth/auth-tables.js +11 -9
  12. package/dist/better-auth/authenticate-settings.d.ts +15 -0
  13. package/dist/better-auth/authenticate-settings.js +12 -0
  14. package/dist/better-auth/authenticator.d.ts +1 -1
  15. package/dist/better-auth/create-auth.d.ts +34 -14
  16. package/dist/better-auth/create-auth.js +27 -10
  17. package/dist/better-auth/mcp/mcp-auth-plugins.d.ts +2719 -0
  18. package/dist/better-auth/mcp/mcp-auth-plugins.js +68 -0
  19. package/dist/better-auth/mcp/mcp-plugins.d.ts +7 -2
  20. package/dist/better-auth/mcp/mcp-plugins.js +3 -68
  21. package/dist/better-auth/mcp/mcp-token-claims.js +1 -1
  22. package/dist/better-auth/organizations/store.d.ts +1 -0
  23. package/dist/better-auth/saml-plugin.d.ts +993 -0
  24. package/dist/better-auth/saml-plugin.js +36 -0
  25. package/dist/better-auth/saml-sign-in.d.ts +27 -1
  26. package/dist/better-auth/saml-sign-in.js +1 -33
  27. package/dist/better-auth/store/adoption.js +87 -0
  28. package/dist/better-auth/store/ddl.js +18 -0
  29. package/dist/better-auth/store/drift.d.ts +9 -0
  30. package/dist/better-auth/store/drift.js +18 -0
  31. package/dist/better-auth/store/migration-sql.js +15 -0
  32. package/dist/better-auth/store/migrations/001-core.js +80 -0
  33. package/dist/better-auth/store/migrations/002-two-factor.js +25 -0
  34. package/dist/better-auth/store/migrations/003-passkeys.js +29 -0
  35. package/dist/better-auth/store/migrations/004-sign-in-policy.js +22 -0
  36. package/dist/better-auth/store/migrations/005-staff-sign-in.js +25 -0
  37. package/dist/better-auth/store/migrations/006-organizations.js +50 -0
  38. package/dist/better-auth/store/migrations/007-mcp.js +150 -0
  39. package/dist/better-auth/store/migrations/index.js +22 -0
  40. package/dist/better-auth/store/migrations/migration.d.ts +3 -0
  41. package/dist/better-auth/store/migrations/migration.js +14 -0
  42. package/dist/better-auth/store/migrations-table.d.ts +7 -0
  43. package/dist/better-auth/store/migrations-table.js +23 -0
  44. package/dist/better-auth/store/resources-outside.d.ts +7 -0
  45. package/dist/better-auth/store/resources-outside.js +20 -0
  46. package/dist/better-auth/store/store-db.js +13 -0
  47. package/dist/better-auth/store/store-names.d.ts +2 -0
  48. package/dist/better-auth/store/store-names.js +14 -0
  49. package/dist/better-auth/store/table-names.d.ts +7 -0
  50. package/dist/better-auth/store/table-names.js +10 -0
  51. package/dist/better-auth/user-table.js +1 -1
  52. package/dist/better-auth/users.d.ts +2 -2
  53. package/dist/comments/comment-users.d.ts +1 -1
  54. package/dist/config-exports.d.ts +2 -0
  55. package/dist/config-exports.js +2 -0
  56. package/dist/create-admin.d.ts +6 -4
  57. package/dist/create-admin.js +3 -1
  58. package/dist/encryption/keyring.js +1 -1
  59. package/dist/files/options.d.ts +5 -8
  60. package/dist/files/provider-settings.d.ts +16 -0
  61. package/dist/files/provider-settings.js +17 -0
  62. package/dist/files/runtime.js +4 -4
  63. package/dist/forms/runtime.js +3 -3
  64. package/dist/index.d.ts +22 -19
  65. package/dist/index.js +11 -11
  66. package/dist/internal.d.ts +13 -0
  67. package/dist/internal.js +13 -0
  68. package/dist/jobs/declared-job.d.ts +24 -0
  69. package/dist/jobs/declared-job.js +26 -0
  70. package/dist/jobs/job-types.d.ts +2 -2
  71. package/dist/mcp/mcp-authenticator.js +3 -3
  72. package/dist/notifications/device-store-of.d.ts +2 -2
  73. package/dist/notifications/device-store-of.js +1 -1
  74. package/dist/notifications/device-store.d.ts +1 -1
  75. package/dist/notifications/device-store.js +1 -1
  76. package/dist/notifications/history-store-of.d.ts +2 -2
  77. package/dist/notifications/history-store-of.js +1 -1
  78. package/dist/notifications/history-store.d.ts +1 -1
  79. package/dist/notifications/history-store.js +1 -1
  80. package/dist/notifications/options.d.ts +3 -1
  81. package/dist/notifications/options.js +1 -1
  82. package/dist/notifications/schema.d.ts +3 -3
  83. package/dist/notifications/schema.js +8 -8
  84. package/dist/notifications/send-notification.js +1 -1
  85. package/dist/notifications/sender.d.ts +1 -1
  86. package/dist/notifications/sender.js +2 -2
  87. package/dist/notifications/senders-of.d.ts +1 -1
  88. package/dist/notifications/senders-of.js +1 -1
  89. package/dist/package.js +1 -1
  90. package/dist/webhooks/subscription.d.ts +1 -0
  91. package/dist/webhooks/table/options.d.ts +1 -1
  92. package/dist/webhooks/table/schema.d.ts +2 -2
  93. package/dist/webhooks/table/schema.js +2 -2
  94. package/dist/webhooks/table/webhook-table.js +2 -2
  95. package/dist/webhooks/table/webhooks-module.d.ts +108 -0
  96. package/dist/webhooks/table/webhooks-module.js +11 -0
  97. package/package.json +8 -5
  98. package/dist/better-auth/mcp/mcp-tables.js +0 -32
  99. package/dist/better-auth/snake-case-migration.js +0 -122
  100. package/dist/notifications/plugin.d.ts +0 -13
  101. package/dist/notifications/plugin.js +0 -16
  102. package/dist/project-config.d.ts +0 -54
  103. package/dist/project-config.js +0 -182
  104. package/dist/webhooks/table/plugin.d.ts +0 -10
  105. package/dist/webhooks/table/plugin.js +0 -18
@@ -67,7 +67,7 @@ export type AdminOptions = {
67
67
  /** The assistant in the admin app's dock: a backend elsewhere or the built-in one (see `AssistantOptions`); `false` turns it off. */
68
68
  assistant?: AssistantOptions | false;
69
69
  /**
70
- * The MCP endpoint, on with `createAuth({ mcp })`. `sql: true` also offers admins and the `ai` role
70
+ * The MCP endpoint, on with `auth.mcp`. `sql: true` also offers admins and the `ai` role
71
71
  * `run_read_only_query`, which reads the whole database as the app's database role, outside the access rules.
72
72
  * `writes: true` offers `create_record` and `update_record`, through the REST API's access rules, `validate` and write
73
73
  * hooks; the person approves each write in their client, and a client that cannot ask is refused.
@@ -84,7 +84,7 @@ export type AdminOptions = {
84
84
  } | false;
85
85
  /**
86
86
  * The websites whose browser code may call the API (`/api/*` but `/api/auth/*`) besides the app's own origins (see
87
- * `createAuth`'s `origins`): a list of origins, or `*` for any. Defaults to the one the platform passes in
87
+ * `auth.origins`): a list of origins, or `*` for any. Defaults to the one the platform passes in
88
88
  * `PROTOBASE_CORS_ORIGINS` (`*` or comma separated), when set. A `mobile` section adds the app's Capacitor origins
89
89
  * (`capacitor://localhost`, `https://localhost`). With none of these nor other origins, the API sends no CORS headers.
90
90
  */
@@ -1,7 +1,7 @@
1
1
  //#region src/app-identity.d.ts
2
2
  /**
3
- * The `app` section of `protobase.config.ts`: the colours the installed app shows, beside its name (`createAuth`'s
4
- * `appName`) and its icons (`protobase.favicon.svg` and the PNGs beside it). Any CSS colour, such as `#0d9488`.
3
+ * The `app` section of `protobase.config.ts`: the colours the installed app shows, beside its name (`auth.appName`)
4
+ * and its icons (`protobase.favicon.svg` and the PNGs beside it). Any CSS colour, such as `#0d9488`.
5
5
  */
6
6
  export type AppIdentity = {
7
7
  /** The colour of the installed app's title bar and of the browser's toolbar around it. */
@@ -1,6 +1,8 @@
1
1
  //#region src/app-name.d.ts
2
2
  /** The app's name when neither the app nor the platform names it. */
3
3
  export declare const defaultAppName = "Protobase";
4
+ /** The name the app goes by: the platform's `PROTOBASE_APP_NAME`, which deployments rename it with, over its own `own`. */
5
+ export declare const appNameOver: (env: Record<string, string | undefined>, own: string | undefined) => string | undefined;
4
6
  /** The name the app shows, in its upper left and its tabs, and mails as: its auth's, else the platform's, else the default. */
5
7
  export declare const shownAppName: (auth: {
6
8
  appName?: string;
@@ -0,0 +1,3 @@
1
+ import { storeTableNames } from "./better-auth/store/table-names.js";
2
+ import { migrationsTableName } from "./better-auth/store/migrations-table.js";
3
+ export { migrationsTableName, storeTableNames };
@@ -0,0 +1,3 @@
1
+ import { migrationsTableName } from "./better-auth/store/migrations-table.js";
2
+ import { storeTableNames } from "./better-auth/store/table-names.js";
3
+ export { migrationsTableName, storeTableNames };
@@ -1,17 +1,16 @@
1
1
  import { AdminAuth } from "./create-auth.js";
2
2
  //#region src/better-auth/auth-schema.d.ts
3
3
  /**
4
- * The SQL that brings the admin store up to what this version of Protobase and the project's Better Auth plugins need:
5
- * for a store with the camelCase names of Protobase 0.6, their rename to snake_case (`snakeCaseMigration`); otherwise
6
- * new tables, columns and indexes, each skipped where it exists, and never a drop but of MCP's tables once MCP is off
7
- * (`dropMcpTables`). `undefined` when the store is current. A change Better Auth cannot make safely, such as a required
8
- * column without a default on a table with rows, is an error.
4
+ * The SQL that brings the admin store to this version of Protobase: the migrations it has not applied, each followed by
5
+ * its row in `auth_migrations`; for a new store, that table first. A store an earlier Protobase made without the record
6
+ * is adopted: the migrations it matches are recorded as applied (`planAdoption`), the others follow. `undefined` when
7
+ * the store is current. The SQL never drops anything.
9
8
  */
10
9
  export declare const authSchemaMigration: (auth: AdminAuth) => Promise<string | undefined>;
11
10
  /**
12
- * Refuses to go on while the admin store lacks a table or column Better Auth needs, still has the camelCase names of
13
- * Protobase 0.6, since every sign-in would fail, or still has MCP's tables with MCP off, since their grants would come
14
- * back with MCP. The server never changes the schema itself: the project's migrations do, before the new version serves.
11
+ * Refuses to go on while the admin store has not applied every migration of this version of Protobase, from its
12
+ * `auth_migrations` alone, since every sign-in would fail. The server never changes the schema itself: the project's
13
+ * migrations do, before the new version serves.
15
14
  */
16
15
  export declare const checkAuthSchema: (auth: AdminAuth) => Promise<void>;
17
16
  //#endregion
@@ -1,54 +1,52 @@
1
- import { storeSchema } from "./auth-tables.js";
2
- import { dropMcpTables, leftoverMcpTables, mcpTableNames, withMcpTables } from "./mcp/mcp-tables.js";
3
- import { snakeCaseMigration } from "./snake-case-migration.js";
4
- import { getMigrations } from "better-auth/db/migration";
1
+ import { storeNames } from "./store/store-names.js";
2
+ import { latestStoreVersion, storeMigrations } from "./store/migrations/index.js";
3
+ import { appliedVersions, migrationsTableName } from "./store/migrations-table.js";
4
+ import { migrationSql, migrationsTableSql } from "./store/migration-sql.js";
5
+ import { adoptionSql, planAdoption, storeHasTables } from "./store/adoption.js";
6
+ import { storeDb } from "./store/store-db.js";
5
7
  //#region src/better-auth/auth-schema.ts
6
8
  /** The command that writes the pending changes as a migration file of the project. */
7
9
  const authMigrationCommand = "protobase auth migration";
10
+ const refuseNewer = (applied) => {
11
+ const newest = applied.at(-1);
12
+ if (newest !== void 0 && newest > latestStoreVersion) throw new Error(`The auth store is at migration ${newest}, newer than the ${latestStoreVersion} this version of Protobase knows; upgrade Protobase`);
13
+ };
8
14
  /**
9
- * Better Auth's statements, each made to skip what exists already, so a migration written from an empty database also
10
- * applies to one an earlier Protobase set up in place.
11
- */
12
- const skipExisting = (sql) => sql.split(";\n\n").map((statement) => statement.replace(/^create table (?!if not exists )/, "create table if not exists ").replace(/^create (unique )?index (?!if not exists )/, (_, unique) => `create ${unique ?? ""}index if not exists `).replace(/^(alter table .+? )add column (?!if not exists )/, "$1add column if not exists ")).join(";\n\n");
13
- /**
14
- * Better Auth adds `email_verified` to a `user` table that exists only when the table still has the camelCase names
15
- * of Protobase 0.6, whose `emailVerified` it does not know: such a store is renamed, never given new columns beside
16
- * the old ones.
17
- */
18
- const hasCamelCaseNames = ({ toBeAdded }) => toBeAdded.some(({ table, fields }) => table === "user" && "email_verified" in fields);
19
- /**
20
- * The SQL that brings the admin store up to what this version of Protobase and the project's Better Auth plugins need:
21
- * for a store with the camelCase names of Protobase 0.6, their rename to snake_case (`snakeCaseMigration`); otherwise
22
- * new tables, columns and indexes, each skipped where it exists, and never a drop but of MCP's tables once MCP is off
23
- * (`dropMcpTables`). `undefined` when the store is current. A change Better Auth cannot make safely, such as a required
24
- * column without a default on a table with rows, is an error.
15
+ * The SQL that brings the admin store to this version of Protobase: the migrations it has not applied, each followed by
16
+ * its row in `auth_migrations`; for a new store, that table first. A store an earlier Protobase made without the record
17
+ * is adopted: the migrations it matches are recorded as applied (`planAdoption`), the others follow. `undefined` when
18
+ * the store is current. The SQL never drops anything.
25
19
  */
26
20
  const authSchemaMigration = async (auth) => {
27
- const found = await getMigrations(withMcpTables(auth), { throwOnUnsafe: false });
28
- if (hasCamelCaseNames(found)) return snakeCaseMigration(storeSchema(auth));
29
- const drop = leftoverMcpTables(auth, found).length > 0 ? [dropMcpTables(storeSchema(auth))] : [];
30
- const { toBeCreated, toBeAdded, toBeAddedIndexes, compileMigrations } = await getMigrations(auth.options);
31
- const statements = [...toBeCreated.length === 0 && toBeAdded.length === 0 && toBeAddedIndexes.length === 0 ? [] : [skipExisting(await compileMigrations())], ...drop];
32
- return statements.length === 0 ? void 0 : statements.join("\n\n");
21
+ const names = storeNames(auth);
22
+ const db = storeDb(auth);
23
+ const applied = await appliedVersions(db, names);
24
+ if (applied !== void 0) {
25
+ refuseNewer(applied);
26
+ const pending = storeMigrations.filter((migration) => !applied.includes(migration.version));
27
+ return pending.length === 0 ? void 0 : pending.map((migration) => migrationSql(names, migration)).join("\n\n");
28
+ }
29
+ if (!await storeHasTables(db, names)) return [migrationsTableSql(names), ...storeMigrations.map((migration) => migrationSql(names, migration))].join("\n\n");
30
+ const { adopted, pending } = await planAdoption(db, names);
31
+ return [
32
+ migrationsTableSql(names),
33
+ adoptionSql(names, adopted),
34
+ ...pending.map((migration) => migrationSql(names, migration))
35
+ ].join("\n\n");
33
36
  };
34
37
  /**
35
- * The tables and `table.column`s the admin store lacks; an index missing is not a reason to refuse requests. MCP's
36
- * tables, named with MCP off only to find them left over, are never missing then.
37
- */
38
- const missingTablesAndColumns = (auth, { toBeCreated, toBeAdded }) => [...toBeCreated.map(({ table }) => table).filter((table) => auth.mcp || !mcpTableNames.includes(table)), ...toBeAdded.flatMap(({ table, fields }) => Object.keys(fields).map((field) => `${table}.${field}`))];
39
- /**
40
- * Refuses to go on while the admin store lacks a table or column Better Auth needs, still has the camelCase names of
41
- * Protobase 0.6, since every sign-in would fail, or still has MCP's tables with MCP off, since their grants would come
42
- * back with MCP. The server never changes the schema itself: the project's migrations do, before the new version serves.
38
+ * Refuses to go on while the admin store has not applied every migration of this version of Protobase, from its
39
+ * `auth_migrations` alone, since every sign-in would fail. The server never changes the schema itself: the project's
40
+ * migrations do, before the new version serves.
43
41
  */
44
42
  const checkAuthSchema = async (auth) => {
45
- const plan = await getMigrations(withMcpTables(auth), { throwOnUnsafe: false });
46
- if (hasCamelCaseNames(plan)) throw new Error(`The auth schema still has the camelCase names of Protobase 0.6, which this version names in snake_case. Write their rename as a migration with \`${authMigrationCommand}\`, apply it with the project's migrations, then start again.`);
47
- const missing = missingTablesAndColumns(auth, plan);
48
- if (missing.length > 0) throw new Error(`The auth schema is behind this version of Protobase (missing ${missing.join(", ")}). Write the change as a migration with \`${authMigrationCommand}\`, apply it with the project's migrations, then start again.`);
49
- const leftovers = leftoverMcpTables(auth, plan);
50
- if (leftovers.length === 0) return;
51
- throw new Error(`The auth schema still has the tables of the OAuth provider for MCP (${leftovers.join(", ")}), which is off. Write their removal as a migration with \`${authMigrationCommand}\`, apply it with the project's migrations, then start again; every MCP client then has to be allowed again.`);
43
+ const applied = await appliedVersions(storeDb(auth), storeNames(auth));
44
+ const write = `Write it as a migration with \`${authMigrationCommand}\`, apply it with the project's migrations, then start again.`;
45
+ if (applied === void 0) throw new Error(`The auth store has no ${migrationsTableName} table: it is new, or an earlier Protobase made it. ${write}`);
46
+ refuseNewer(applied);
47
+ const missing = storeMigrations.filter((migration) => !applied.includes(migration.version));
48
+ if (missing.length === 0) return;
49
+ throw new Error(`The auth schema is behind this version of Protobase (missing migrations ${missing.map(({ version, name }) => `${version} ${name}`).join(", ")}). ${write}`);
52
50
  };
53
51
  //#endregion
54
- export { authMigrationCommand, authSchemaMigration, checkAuthSchema, skipExisting };
52
+ export { authMigrationCommand, authSchemaMigration, checkAuthSchema };