@protobase/server 0.132.3 → 0.133.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.
@@ -279,8 +279,8 @@ const organizationsPage = async (auth, scope, { after, size = 100 }) => {
279
279
  };
280
280
  /**
281
281
  * Makes `userId` the owner, with the creator's app roles, of every organization without members, in one statement: for
282
- * the first user of an app whose organizations were made before anyone could own them, such as by its seed. Returns
283
- * how many.
282
+ * the first user of an app whose organizations were made before anyone could own them, such as by its seed. The
283
+ * default organization is left out: the migration made it to stand in while organizations are off. Returns how many.
284
284
  */
285
285
  const ownOrganizationsWithoutMembers = (auth, userId) => acrossOrganizations("the first user owns every organization without members", () => ownEmptyOrganizations(auth, userId));
286
286
  const ownEmptyOrganizations = async (auth, userId) => {
@@ -288,9 +288,11 @@ const ownEmptyOrganizations = async (auth, userId) => {
288
288
  const store = storeSqlOf(await contextOf(auth));
289
289
  const member = store.table("member");
290
290
  const m = member.columns;
291
+ const defaultId = settings.options.default?.id;
292
+ const notDefault = defaultId === void 0 ? sql`` : sql`and cast(o.id as text) <> ${defaultId}`;
291
293
  const { rows } = await sql`insert into ${member} (id, ${m.organization_id}, ${m.user_id}, ${m.role}, ${m.app_roles}, ${m.created_at})
292
294
  select ${store.newId}, o.id, ${userId}, 'owner', ${JSON.stringify(settings.creatorAppRoles)}::jsonb, ${/* @__PURE__ */ new Date()} from ${store.table("organization")} o
293
- where not exists (select from ${member} m where m.${m.organization_id} = o.id)
295
+ where not exists (select from ${member} m where m.${m.organization_id} = o.id) ${notDefault}
294
296
  returning id`.execute(store.db);
295
297
  return rows.length;
296
298
  };
@@ -1,5 +1,5 @@
1
1
  import { allModules } from "../names/modules-on.js";
2
- import { resolveNames } from "../names/resolve-names.js";
2
+ import { resolveNames, tableNameOf } from "../names/resolve-names.js";
3
3
  import { ddl } from "./ddl.js";
4
4
  import { migrationNames } from "./steps/step.js";
5
5
  import { moduleSteps } from "./steps/index.js";
@@ -51,14 +51,21 @@ const madeColumns = async (db, names) => {
51
51
  };
52
52
  return run().finally(() => tx.rollback().execute());
53
53
  };
54
- /** Whether the store has any table of the steps besides the user table, which may be another system's. */
54
+ /**
55
+ * The names of the steps' tables, those of the modules that are off included, besides the user table, which may be
56
+ * another system's. Each is named on its own, so a table of a module that is off never clashes with one that is on,
57
+ * such as a user table named organization; the store's names already refused a name two tables that are on share.
58
+ */
59
+ const stepTableNames = ({ names }) => {
60
+ const tables = names.input.tables ?? {};
61
+ const user = names.table("user").name;
62
+ const named = Object.values(moduleSteps).flatMap((steps) => steps.flatMap((step) => step.tables.map((table) => tableNameOf(tables, table))));
63
+ return new Set(named.filter((name) => name !== user));
64
+ };
65
+ /** Whether the store has any table of the steps besides the user table (`stepTableNames`). */
55
66
  const storeHasTables = async (db, names) => {
56
- const tables = new Set((await columnsIn(db, names.schema)).map((column) => column.table));
57
- const every = resolveNames({
58
- ...names.names.input,
59
- modules: allModules
60
- });
61
- return Object.values(moduleSteps).some((steps) => steps.some((step) => step.tables.some((table) => tables.has(every.tables[table].name))));
67
+ const candidates = stepTableNames(names);
68
+ return (await columnsIn(db, names.schema)).some((column) => candidates.has(column.table));
62
69
  };
63
70
  /** Which versions the store matches and which it still needs; a version it matches only partly is an error. */
64
71
  const planAdoption = async (db, names) => {
@@ -109,4 +116,4 @@ $adopt$`;
109
116
  return `-- The auth store an earlier Protobase made, without a record of its migrations, adopted: ${adopted.map(({ migration }) => `${migration.version} ${migration.name}`).join(", ")}\n-- recorded as applied, once every table and column they make is there.\n${statementsSql([check, recordSteps(names, stepsOfVersions(adopted.map(({ migration }) => migration.version)))])}`;
110
117
  };
111
118
  //#endregion
112
- export { adoptionSql, planAdoption, storeHasTables };
119
+ export { adoptionSql, planAdoption, stepTableNames, storeHasTables };
@@ -16,7 +16,7 @@ const watchTable = ddl`create table if not exists ${retiredSchema}.${"writes"} (
16
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
17
  begin
18
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)
19
+ values (format('%I.%I', tg_table_schema, tg_table_name), tg_argv[0], date_trunc('hour', clock_timestamp()), 1)
20
20
  on conflict (table_name, column_name, hour) do update set writes = counted.writes + 1;
21
21
  return null;
22
22
  end
@@ -35,7 +35,9 @@ export declare const hasUsers: (auth: AdminAuth) => Promise<boolean>;
35
35
  * the first user is always an `admin`, later ones get `role` (default `user`). The address counts as verified, so the
36
36
  * user can also sign in with an emailed code. `accounts` link the user to sign-in providers up front, so a provider
37
37
  * signs them in by its own id whatever address it reports; one that signs in another user already is refused. With
38
- * organizations, the first user also owns every organization without members, such as those an app's seed made.
38
+ * organizations, the first user also owns every organization without members, such as those an app's seed made,
39
+ * but not the default organization, which the migration made to stand in while organizations are off: on a fresh store
40
+ * they start in no organization and name their first one, where the app's setup for a new organization runs.
39
41
  */
40
42
  export declare const createUser: (auth: AdminAuth, input: NewUser) => Promise<{
41
43
  id: string;
@@ -34,7 +34,9 @@ const checkAccounts = async (auth, accounts) => {
34
34
  * the first user is always an `admin`, later ones get `role` (default `user`). The address counts as verified, so the
35
35
  * user can also sign in with an emailed code. `accounts` link the user to sign-in providers up front, so a provider
36
36
  * signs them in by its own id whatever address it reports; one that signs in another user already is refused. With
37
- * organizations, the first user also owns every organization without members, such as those an app's seed made.
37
+ * organizations, the first user also owns every organization without members, such as those an app's seed made,
38
+ * but not the default organization, which the migration made to stand in while organizations are off: on a fresh store
39
+ * they start in no organization and name their first one, where the app's setup for a new organization runs.
38
40
  */
39
41
  const createUser = async (auth, input) => {
40
42
  const first = !await hasUsers(auth);
package/dist/package.js CHANGED
@@ -1,4 +1,4 @@
1
1
  //#region package.json
2
- var version = "0.132.3";
2
+ var version = "0.133.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.132.3",
3
+ "version": "0.133.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.132.3",
48
- "@protobase/query": "0.132.3",
49
- "@protobase/schema": "0.132.3",
50
- "@protobase/workflow-plugins": "0.132.3",
51
- "@protobase/workflows": "0.132.3",
47
+ "@protobase/layout": "0.133.1",
48
+ "@protobase/query": "0.133.1",
49
+ "@protobase/schema": "0.133.1",
50
+ "@protobase/workflow-plugins": "0.133.1",
51
+ "@protobase/workflows": "0.133.1",
52
52
  "@react-email/render": "^2.1.0",
53
53
  "@react-pdf/primitives": "^4.4.0",
54
54
  "@react-pdf/renderer": "^4.9.1",