@somewhere-cloud/cli 0.38.94 → 0.38.96

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.
@@ -6925,6 +6925,9 @@ function resolveParentChains(tables, tableByName, privatePolicy, errors) {
6925
6925
  const parentKey = parentKeys.length === 1 ? parentKeys[0] : void 0;
6926
6926
  let inherited = null;
6927
6927
  let edges = 1;
6928
+ if (parentTable && parentTable.scope.kind === "policy" && parentTable.scope.policy.kind === "parent" && parentTable.scope.policy.author) {
6929
+ return fail(`Table "${t.name}" uses parent({ via: '${pending.via}' }), but "${parentTable.name}" declares parent({ author }); a table cannot inherit from a table with authors yet.`);
6930
+ }
6928
6931
  if (parentTable && parentTable.scope.kind === "policy" && parentTable.scope.policy.kind === "parent") {
6929
6932
  const above = resolveParent(parentTable);
6930
6933
  if (above === null) {
@@ -6947,7 +6950,7 @@ function resolveParentChains(tables, tableByName, privatePolicy, errors) {
6947
6950
  if (!compatible) {
6948
6951
  return fail(`Table "${t.name}" uses parent({ via: '${pending.via}' }), but that foreign key does not have the same scalar type as "${parentTable.name}"."${parentKey.name}".`);
6949
6952
  }
6950
- t.scope.policy = { kind: "parent", via: pending.via, table: parentTable.name, parentKey: parentKey.name, policy: inherited };
6953
+ t.scope.policy = { kind: "parent", via: pending.via, table: parentTable.name, parentKey: parentKey.name, policy: inherited, ...pending.author ? { author: pending.author } : {} };
6951
6954
  if (t.client?.create && !t.client.create.includes(pending.via)) {
6952
6955
  errors.push(`Table "${t.name}" uses parent({ via: '${pending.via}' }) and client.create must include "${pending.via}" so every new row explicitly selects an authorized parent.`);
6953
6956
  }
@@ -7748,6 +7751,133 @@ function resolveAppRoleScopes(tables, appRoles, errors) {
7748
7751
  }
7749
7752
 
7750
7753
 
7754
+ var roleLines = new WeakMap();
7755
+ var columnLines = new WeakMap();
7756
+ function readColumnRules(r, tableName, line, fail) {
7757
+ const where = `columns on table "${tableName}"`;
7758
+ r.expectPunct("{", `opening ${where}, which takes { column: { write: ['role'] } } or { column: { memberOf: 'team_column' } }`);
7759
+ const rules = {};
7760
+ const memberOf = {};
7761
+ const seen = new Set();
7762
+ const lines2 = [];
7763
+ const at2 = {};
7764
+ while (!r.tryPunct("}")) {
7765
+ const column = r.readKey(`for a column in ${where}`);
7766
+ const name = column.name.toLowerCase();
7767
+ if (seen.has(name)) fail(column.line, `${where} lists "${column.name}" twice.`);
7768
+ seen.add(name);
7769
+ r.expectPunct(":", `after "${column.name}" in ${where}`);
7770
+ r.expectPunct("{", `opening the rule for "${column.name}" in ${where}, which takes { write: ['role'] } or { memberOf: 'team_column' }`);
7771
+ let roles = null;
7772
+ let team = null;
7773
+ while (!r.tryPunct("}")) {
7774
+ const key = r.readKey(`for an option of column "${column.name}" in ${where}`);
7775
+ if (key.name === "memberOf") {
7776
+ if (team) fail(key.line, `column "${column.name}" in ${where} declares memberOf twice.`);
7777
+ r.expectPunct(":", `after "memberOf" for column "${column.name}"`);
7778
+ const t = r.next();
7779
+ if (t.kind !== "string" || !/^[A-Za-z_][A-Za-z0-9_]{0,63}$/.test(t.value)) fail(t.line, `columns.${column.name}.memberOf on table "${tableName}" must be the quoted name of the table's team column, such as 'team_id'.`);
7780
+ team = t.value.toLowerCase();
7781
+ if (!r.tryPunct(",")) {
7782
+ r.expectPunct("}", `closing the rule for "${column.name}"`);
7783
+ break;
7784
+ }
7785
+ continue;
7786
+ }
7787
+ if (key.name !== "write") fail(key.line, `column "${column.name}" in ${where} has an unknown option "${key.name}". Allowed: write, memberOf.`);
7788
+ if (roles) fail(key.line, `column "${column.name}" in ${where} declares write twice.`);
7789
+ r.expectPunct(":", `after "write" for column "${column.name}"`);
7790
+ const open = r.next();
7791
+ if (open.kind !== "punct" || open.value !== "[") fail(open.line, `columns.${column.name}.write on table "${tableName}" must be a list of quoted app role names, such as ['staff'].`);
7792
+ const list = [];
7793
+ while (!r.tryPunct("]")) {
7794
+ const t = r.next();
7795
+ if (t.kind !== "string") fail(t.line, `columns.${column.name}.write on table "${tableName}" must list quoted app role names; values must be literal strings.`);
7796
+ if (!/^[a-z][a-z0-9_]{0,63}$/.test(t.value)) fail(t.line, `columns.${column.name}.write on table "${tableName}" names "${t.value}", which is not an app role name (lowercase letters, digits and _, starting with a letter).`);
7797
+ if (list.includes(t.value)) fail(t.line, `columns.${column.name}.write on table "${tableName}" lists "${t.value}" twice.`);
7798
+ list.push(t.value);
7799
+ lines2.push({ column: name, role: t.value, line: t.line });
7800
+ if (!r.tryPunct(",")) {
7801
+ r.expectPunct("]", `closing columns.${column.name}.write`);
7802
+ break;
7803
+ }
7804
+ }
7805
+ if (list.length === 0) fail(open.line, `columns.${column.name}.write on table "${tableName}" is empty, so no caller could ever write "${column.name}". List the app roles that may, or leave the column out of columns.`);
7806
+ roles = [...list].sort();
7807
+ if (!r.tryPunct(",")) {
7808
+ r.expectPunct("}", `closing the rule for "${column.name}"`);
7809
+ break;
7810
+ }
7811
+ }
7812
+ if (!roles && !team) fail(column.line, `column "${column.name}" in ${where} needs write: ['role'] or memberOf: 'team_column'.`);
7813
+ if (roles) rules[name] = roles;
7814
+ if (team) memberOf[name] = team;
7815
+ at2[name] = column.line;
7816
+ if (!r.tryPunct(",")) {
7817
+ r.expectPunct("}", `closing ${where}`);
7818
+ break;
7819
+ }
7820
+ }
7821
+ if (seen.size === 0) fail(line, `${where} is empty; leave it out, or list the columns that take a rule.`);
7822
+ const out = {};
7823
+ if (Object.keys(rules).length) {
7824
+ const sorted2 = {};
7825
+ for (const name of Object.keys(rules).sort()) sorted2[name] = rules[name];
7826
+ roleLines.set(sorted2, lines2);
7827
+ columnLines.set(sorted2, at2);
7828
+ out.write = sorted2;
7829
+ }
7830
+ if (Object.keys(memberOf).length) {
7831
+ const sorted2 = {};
7832
+ for (const name of Object.keys(memberOf).sort()) sorted2[name] = memberOf[name];
7833
+ columnLines.set(sorted2, at2);
7834
+ out.memberOf = sorted2;
7835
+ }
7836
+ return out;
7837
+ }
7838
+ function validateColumnMemberOf(memberOf, input, fail) {
7839
+ const at2 = columnLines.get(memberOf) ?? {};
7840
+ for (const [column, team] of Object.entries(memberOf)) {
7841
+ const line = at2[column] ?? input.nameLine;
7842
+ const label = `columns.${column}.memberOf on table "${input.tableName}"`;
7843
+ const declared = input.columns.find((c) => c.name === column);
7844
+ if (column === input.ownerColumn) fail(line, `${label} names the owner column, which the platform writes; it takes no memberOf rule.`);
7845
+ if (!declared) fail(line, `${label}: "${column}" is not one of its declared columns.`);
7846
+ if (declared.helper !== "text") fail(line, `${label}: "${column}" must be a text() column holding a user id.`);
7847
+ if (declared.hasDefault && declared.default !== null) fail(line, `${label}: "${column}" declares a default user id, which a create that leaves the column out would store without the team check. Remove the default (a nullable column may default to null).`);
7848
+ if (input.accessColumns.includes(column)) fail(line, `${label}: "${column}" is read by the table's access rule, so it takes no memberOf rule.`);
7849
+ if (column === input.roomSave) fail(line, `${label}: "${column}" is the room's save column.`);
7850
+ if (!input.groupColumns.includes(team)) {
7851
+ fail(line, `${label} names "${team}", which is not this table's team column. memberOf names the table's own group() column (group(), anyOf(owner(), group()) or an allOf() group() gate); a team inherited through parent() is not supported yet.`);
7852
+ }
7853
+ }
7854
+ }
7855
+ function validateColumnRules(rules, input, fail) {
7856
+ const at2 = columnLines.get(rules) ?? {};
7857
+ if (input.serverOnly) fail(input.nameLine, `table "${input.tableName}" is serverOnly(), so no caller writes it; columns write rules apply to caller writes and are not accepted on it.`);
7858
+ for (const column of Object.keys(rules)) {
7859
+ const line = at2[column] ?? input.nameLine;
7860
+ const label = `columns.${column} on table "${input.tableName}"`;
7861
+ if (column === input.ownerColumn) fail(line, `${label} names the owner column, which the platform writes; no caller writes it, so it takes no write rule.`);
7862
+ if (!input.declaredColumns.has(column)) fail(line, `${label} names "${column}", which is not one of its declared columns.`);
7863
+ if (input.accessColumns.includes(column)) fail(line, `${label} names "${column}", which the table's access rule reads; who may set it is decided by that rule, so it takes no column write rule.`);
7864
+ if (column === input.roomSave) fail(line, `${label} names "${column}", the column this table's room saves its state to. The room writes it, not a caller, so a write rule on it would not be enforced on room saves; it is not accepted.`);
7865
+ }
7866
+ }
7867
+ function resolveColumnRuleRoles(tables, appRoles, groups, errors) {
7868
+ const vocabulary = new Set((appRoles ?? []).map((role) => role.name));
7869
+ const teamRoles = new Set(["owner", "admin", "member", ...groups?.roles ?? []]);
7870
+ for (const t of tables) {
7871
+ if (!t.columnRules) continue;
7872
+ for (const { column, role, line } of roleLines.get(t.columnRules) ?? []) {
7873
+ if (vocabulary.has(role)) continue;
7874
+ errors.push(teamRoles.has(role) ? `line ${line}: columns.${column}.write on table "${t.name}" names "${role}", a team role. Column write rules take app roles only for now (declared in the schema's appRoles option); a team role cannot restrict a column yet.` : `line ${line}: columns.${column}.write on table "${t.name}" names "${role}", which is not declared in the schema's appRoles option. Column write rules accept only app roles declared there.`);
7875
+ }
7876
+ roleLines.delete(t.columnRules);
7877
+ }
7878
+ }
7879
+
7880
+
7751
7881
  var ROLE_NAME_PATTERN = /^[a-z][a-z0-9_]{0,63}$/;
7752
7882
  var EMAIL_DOMAIN_PATTERN = /^(?=.{4,253}$)(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z][a-z0-9-]{0,61}[a-z0-9]$/;
7753
7883
  var ROLE_DOMAIN_MAP_JS = `
@@ -8157,9 +8287,17 @@ function declaredScopeIdentityColumns(scope) {
8157
8287
  if (scope.kind === "owner") return [scope.column];
8158
8288
  if (scope.kind === "member") return [...scope.group];
8159
8289
  if (scope.kind !== "policy") return [];
8160
- const collect = (policy) => policy.kind === "owner" ? [policy.column] : policy.kind === "member" ? [...policy.group] : policy.kind === "any_of" ? policy.policies.flatMap(collect) : policy.kind === "all_of" ? [...collect(policy.gate), ...collect(policy.who)] : policy.kind === "group" ? [policy.column] : policy.kind === "app_role" ? [] : [policy.via];
8290
+ const collect = (policy) => policy.kind === "owner" ? [policy.column] : policy.kind === "member" ? [...policy.group] : policy.kind === "any_of" ? policy.policies.flatMap(collect) : policy.kind === "all_of" ? [...collect(policy.gate), ...collect(policy.who)] : policy.kind === "group" ? [policy.column] : policy.kind === "app_role" ? [] : [policy.via, ...policy.author ? [policy.author.column] : []];
8161
8291
  return [...new Set(collect(scope.policy))];
8162
8292
  }
8293
+ function declaredTeamColumns(scope) {
8294
+ if (scope.kind !== "policy") return [];
8295
+ const p = scope.policy;
8296
+ if (p.kind === "group") return [p.column];
8297
+ if (p.kind === "any_of") return p.policies.flatMap((x) => x.kind === "group" ? [x.column] : []);
8298
+ if (p.kind === "all_of") return p.gate.kind === "group" ? [p.gate.column] : [];
8299
+ return [];
8300
+ }
8163
8301
  var ROOM_REFEREE_PATH = /^\/api\/[A-Za-z0-9_\-]+(?:\/[A-Za-z0-9_\-.]+)*$/;
8164
8302
  function readRoomsOption(r, rawName, line) {
8165
8303
  r.expectPunct("{", `opening the rooms option of table "${rawName}", which takes { save?: 'column', referee?: '/api/path' }`);
@@ -8911,17 +9049,56 @@ function readAllOfScope(r, tableName, line) {
8911
9049
  }
8912
9050
  function readParentScope(r, tableName, line) {
8913
9051
  r.expectPunct("(", 'after "parent"');
8914
- const opts = r.readLiteralObject(`in parent() options for table "${tableName}"`);
9052
+ const where = `parent() options for table "${tableName}"`;
9053
+ r.expectPunct("{", `in ${where}`);
9054
+ let via = null;
9055
+ let authorColumn = null;
9056
+ const actions = {};
9057
+ const seen = new Set();
9058
+ while (!r.tryPunct("}")) {
9059
+ const key = r.readKey(`for an option key in ${where}`);
9060
+ if (seen.has(key.name)) throw new SchemaTsError(`line ${key.line}: option "${key.name}" appears twice in ${where}.`);
9061
+ seen.add(key.name);
9062
+ r.expectPunct(":", `after "${key.name}" in ${where}`);
9063
+ if (key.name === "via" || key.name === "author") {
9064
+ const t = r.next();
9065
+ if (t.kind !== "string" || !SAFE_IDENT.test(t.value) || t.value.length > MAX_NAME_LENGTH) {
9066
+ throw new SchemaTsError(key.name === "via" ? `line ${t.line}: parent({ via }) on table "${tableName}" must name a valid declared foreign-key column.` : `line ${t.line}: parent({ author }) on table "${tableName}" must name a declared nullable text() column the platform stamps with the row's creator.`);
9067
+ }
9068
+ if (key.name === "via") via = { value: t.value.toLowerCase(), line: t.line };
9069
+ else authorColumn = { value: t.value.toLowerCase(), line: t.line };
9070
+ } else if (key.name === "update" || key.name === "delete") {
9071
+ const open = r.next();
9072
+ if (open.kind !== "punct" || open.value !== "[") throw new SchemaTsError(`line ${open.line}: parent({ ${key.name} }) on table "${tableName}" lists who may ${key.name} a row: 'author' and/or 'parent', such as ['author', 'parent'].`);
9073
+ const list = [];
9074
+ while (!r.tryPunct("]")) {
9075
+ const t = r.next();
9076
+ if (t.kind !== "string" || t.value !== "author" && t.value !== "parent") throw new SchemaTsError(`line ${t.line}: parent({ ${key.name} }) on table "${tableName}" accepts 'author' (the row's author) and 'parent' (whoever may ${key.name} the parent row).`);
9077
+ if (list.includes(t.value)) throw new SchemaTsError(`line ${t.line}: parent({ ${key.name} }) on table "${tableName}" lists '${t.value}' twice.`);
9078
+ list.push(t.value);
9079
+ if (!r.tryPunct(",")) {
9080
+ r.expectPunct("]", `closing parent({ ${key.name} })`);
9081
+ break;
9082
+ }
9083
+ }
9084
+ if (list.length === 0) throw new SchemaTsError(`line ${open.line}: parent({ ${key.name} }) on table "${tableName}" is empty, so nobody could ${key.name} a row. List 'author' and/or 'parent', or leave it out for today's rule ('parent').`);
9085
+ actions[key.name] = ["author", "parent"].filter((a) => list.includes(a));
9086
+ } else {
9087
+ throw new SchemaTsError(`line ${key.line}: parent() on table "${tableName}" has an unknown option "${key.name}". Allowed: via, author, update, delete.`);
9088
+ }
9089
+ if (!r.tryPunct(",")) {
9090
+ r.expectPunct("}", `closing ${where}`);
9091
+ break;
9092
+ }
9093
+ }
8915
9094
  r.tryPunct(",");
8916
9095
  r.expectPunct(")", "closing parent()");
8917
- if (opts.size !== 1 || !opts.has("via")) {
8918
- throw new SchemaTsError(`line ${line}: parent() on table "${tableName}" needs exactly { via: 'foreign_key' }.`);
8919
- }
8920
- const value = opts.get("via").value;
8921
- if (typeof value !== "string" || !SAFE_IDENT.test(value) || value.length > MAX_NAME_LENGTH) {
8922
- throw new SchemaTsError(`line ${opts.get("via").line}: parent({ via }) on table "${tableName}" must name a valid declared foreign-key column.`);
8923
- }
8924
- const sentinel = { kind: "parent", via: value.toLowerCase(), table: "", parentKey: "", policy: { kind: "owner", column: "" } };
9096
+ if (!via) throw new SchemaTsError(`line ${line}: parent() on table "${tableName}" needs { via: 'foreign_key' }.`);
9097
+ const usesAuthor = [actions.update, actions.delete].some((list) => list?.includes("author"));
9098
+ if (usesAuthor && !authorColumn) throw new SchemaTsError(`line ${line}: parent() on table "${tableName}" lets 'author' act but declares no author column; add author: 'author_id' (a nullable text() column the platform stamps).`);
9099
+ if (!authorColumn && (actions.update || actions.delete)) throw new SchemaTsError(`line ${line}: parent() on table "${tableName}" lists update or delete without an author column; without author, every operation already follows the parent.`);
9100
+ const author = authorColumn ? { column: authorColumn.value, ...actions.update ? { update: actions.update } : {}, ...actions.delete ? { delete: actions.delete } : {} } : void 0;
9101
+ const sentinel = { kind: "parent", via: via.value, table: "", parentKey: "", policy: { kind: "owner", column: "" }, ...author ? { author } : {} };
8925
9102
  rememberParentLine(sentinel, line);
8926
9103
  return { kind: "policy", policy: sentinel };
8927
9104
  }
@@ -9125,6 +9302,7 @@ function readTable(r, rawName, nameLine) {
9125
9302
  let relationsDeclared = false;
9126
9303
  let clientInput;
9127
9304
  let roomsInput;
9305
+ let columnOptions;
9128
9306
  if (r.tryPunct(",")) {
9129
9307
  r.expectPunct("{", `opening the options of table "${rawName}"`);
9130
9308
  while (!r.tryPunct("}")) {
@@ -9152,9 +9330,14 @@ function readTable(r, rawName, nameLine) {
9152
9330
  } else if (key.name === "rooms") {
9153
9331
  if (roomsInput) throw new SchemaTsError(`line ${key.line}: table "${rawName}" declares "rooms" twice.`);
9154
9332
  roomsInput = readRoomsOption(r, rawName, key.line);
9333
+ } else if (key.name === "columns") {
9334
+ if (columnOptions) throw new SchemaTsError(`line ${key.line}: table "${rawName}" declares "columns" twice.`);
9335
+ columnOptions = readColumnRules(r, rawName, key.line, (at2, message) => {
9336
+ throw new SchemaTsError(`line ${at2}: ${message}`);
9337
+ });
9155
9338
  } else {
9156
9339
  throw new SchemaTsError(
9157
- `line ${key.line}: table "${rawName}" has an unknown option "${key.name}". Allowed: scope, indexes, unique, relations, client, rooms.`
9340
+ `line ${key.line}: table "${rawName}" has an unknown option "${key.name}". Allowed: scope, indexes, unique, relations, client, rooms, columns.`
9158
9341
  );
9159
9342
  }
9160
9343
  if (!r.tryPunct(",")) {
@@ -9282,7 +9465,46 @@ function readTable(r, rawName, nameLine) {
9282
9465
  client = parsed.permissions;
9283
9466
  }
9284
9467
  const rooms = roomsInput ? validateRoomsOption(roomsInput, rawName, nameLine, columns, scope, client, relations) : void 0;
9285
- return { kind: "table", table: { name, scope, relations, columns, indexes, uniques, removedColumns: [...removedColumns].sort(), ...client ? { client } : {}, ...rooms ? { rooms } : {} } };
9468
+ if (scope.kind === "policy" && scope.policy.kind === "parent" && scope.policy.author) {
9469
+ const authorColumn = scope.policy.author.column;
9470
+ const declared = columns.find((c) => c.name === authorColumn);
9471
+ if (!declared || declared.helper !== "text" || !declared.nullable || declared.hasDefault || declared.renamedFrom) {
9472
+ throw new SchemaTsError(`line ${nameLine}: parent({ author: '${authorColumn}' }) on table "${rawName}" must name a declared text({ nullable: true }) column without a default or rename: the platform stamps it with the row's creator, and a row without one grants its author nothing.`);
9473
+ }
9474
+ if (authorColumn === scope.policy.via) throw new SchemaTsError(`line ${nameLine}: parent({ author }) on table "${rawName}" cannot be the parent link "${authorColumn}".`);
9475
+ if (client && ((client.create ?? []).includes(authorColumn) || (client.update ?? []).includes(authorColumn))) {
9476
+ throw new SchemaTsError(`line ${nameLine}: table "${rawName}": "${authorColumn}" is the author the platform stamps, so client.create and client.update cannot list it.`);
9477
+ }
9478
+ }
9479
+ const columnRules = columnOptions?.write;
9480
+ const memberOf = columnOptions?.memberOf;
9481
+ if (memberOf) {
9482
+ validateColumnMemberOf(memberOf, {
9483
+ tableName: rawName,
9484
+ nameLine,
9485
+ columns,
9486
+ ownerColumn: managedOwner?.column ?? null,
9487
+ accessColumns: declaredScopeIdentityColumns(scope),
9488
+ roomSave: rooms?.save ?? null,
9489
+ groupColumns: declaredTeamColumns(scope)
9490
+ }, (at2, message) => {
9491
+ throw new SchemaTsError(`line ${at2}: ${message}`);
9492
+ });
9493
+ }
9494
+ if (columnRules) {
9495
+ validateColumnRules(columnRules, {
9496
+ tableName: rawName,
9497
+ nameLine,
9498
+ declaredColumns: new Set(columns.map((c) => c.name)),
9499
+ ownerColumn: managedOwner?.column ?? null,
9500
+ accessColumns: declaredScopeIdentityColumns(scope),
9501
+ roomSave: rooms?.save ?? null,
9502
+ serverOnly: scope.kind === "server_only"
9503
+ }, (at2, message) => {
9504
+ throw new SchemaTsError(`line ${at2}: ${message}`);
9505
+ });
9506
+ }
9507
+ return { kind: "table", table: { name, scope, relations, columns, indexes, uniques, removedColumns: [...removedColumns].sort(), ...client ? { client } : {}, ...rooms ? { rooms } : {}, ...columnRules ? { columnRules } : {}, ...memberOf ? { memberOf } : {} } };
9286
9508
  }
9287
9509
  function extractSchemaTs(source, options = {}) {
9288
9510
  if (typeof source !== "string" || source.trim().length === 0) {
@@ -9386,6 +9608,7 @@ function extractSchemaTs(source, options = {}) {
9386
9608
  return null;
9387
9609
  };
9388
9610
  resolveAppRoleScopes(tables, appRoles, errors);
9611
+ resolveColumnRuleRoles(tables, appRoles, groupRoles, errors);
9389
9612
  resolveGroupsAndParents(tables, tableByName, privatePolicy, groupRoles, errors);
9390
9613
  validateDeclaredMemberships(tables, tableByName, knownColumns, errors);
9391
9614
  validateDeclaredRelations(tables, tableByName, knownColumns, (table) => removedTables.includes(table) ? "removedTable()" : exportedTables.includes(table) ? "exported()" : null, errors);