@rocicorp/zero 0.13.2025020200 → 0.13.2025020300

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 (68) hide show
  1. package/out/{chunk-5S3K5CU2.js → chunk-2WSNDAJE.js} +335 -169
  2. package/out/{chunk-5S3K5CU2.js.map → chunk-2WSNDAJE.js.map} +4 -4
  3. package/out/otel/src/log-options.d.ts +1 -1
  4. package/out/react.js +1 -1
  5. package/out/react.js.map +2 -2
  6. package/out/replicache/src/persist/clients.d.ts +2 -50
  7. package/out/replicache/src/persist/clients.d.ts.map +1 -1
  8. package/out/solid.js +1 -1
  9. package/out/zero/src/change-protocol/v0.d.ts +2 -0
  10. package/out/zero/src/change-protocol/v0.d.ts.map +1 -0
  11. package/out/zero/src/change-protocol/v0.js +3 -0
  12. package/out/zero/src/change-protocol/v0.js.map +1 -0
  13. package/out/zero-cache/src/config/zero-config.d.ts +10 -2
  14. package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
  15. package/out/zero-cache/src/config/zero-config.js +8 -1
  16. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  17. package/out/zero-cache/src/server/multi/config.d.ts +5 -1
  18. package/out/zero-cache/src/server/multi/config.d.ts.map +1 -1
  19. package/out/zero-cache/src/services/change-source/custom/change-source.d.ts +2 -3
  20. package/out/zero-cache/src/services/change-source/custom/change-source.d.ts.map +1 -1
  21. package/out/zero-cache/src/services/change-source/custom/change-source.js +2 -3
  22. package/out/zero-cache/src/services/change-source/custom/change-source.js.map +1 -1
  23. package/out/zero-cache/src/services/change-source/protocol/current/path.d.ts +20 -0
  24. package/out/zero-cache/src/services/change-source/protocol/current/path.d.ts.map +1 -0
  25. package/out/zero-cache/src/services/change-source/protocol/current/path.js +20 -0
  26. package/out/zero-cache/src/services/change-source/protocol/current/path.js.map +1 -0
  27. package/out/zero-cache/src/services/change-source/protocol/current.d.ts +7 -0
  28. package/out/zero-cache/src/services/change-source/protocol/current.d.ts.map +1 -0
  29. package/out/zero-cache/src/services/change-source/protocol/current.js +7 -0
  30. package/out/zero-cache/src/services/change-source/protocol/current.js.map +1 -0
  31. package/out/zero-cache/src/services/change-source/protocol/mod.d.ts +2 -0
  32. package/out/zero-cache/src/services/change-source/protocol/mod.d.ts.map +1 -0
  33. package/out/zero-cache/src/services/change-source/protocol/mod.js +5 -0
  34. package/out/zero-cache/src/services/change-source/protocol/mod.js.map +1 -0
  35. package/out/zero-cache/src/services/litestream/commands.d.ts.map +1 -1
  36. package/out/zero-cache/src/services/litestream/commands.js +9 -1
  37. package/out/zero-cache/src/services/litestream/commands.js.map +1 -1
  38. package/out/zero-client/src/client/query-manager.d.ts +2 -1
  39. package/out/zero-client/src/client/query-manager.d.ts.map +1 -1
  40. package/out/zero-client/src/client/zero-poke-handler.d.ts +9 -1
  41. package/out/zero-client/src/client/zero-poke-handler.d.ts.map +1 -1
  42. package/out/zero-client/src/client/zero.d.ts +1 -1
  43. package/out/zero-client/src/client/zero.d.ts.map +1 -1
  44. package/out/zero-protocol/src/ast.d.ts +4 -0
  45. package/out/zero-protocol/src/ast.d.ts.map +1 -1
  46. package/out/zero-protocol/src/ast.js +138 -24
  47. package/out/zero-protocol/src/ast.js.map +1 -1
  48. package/out/zero-schema/src/builder/schema-builder.d.ts.map +1 -1
  49. package/out/zero-schema/src/builder/schema-builder.js +6 -0
  50. package/out/zero-schema/src/builder/schema-builder.js.map +1 -1
  51. package/out/zero-schema/src/builder/table-builder.d.ts +5 -1
  52. package/out/zero-schema/src/builder/table-builder.d.ts.map +1 -1
  53. package/out/zero-schema/src/builder/table-builder.js +20 -0
  54. package/out/zero-schema/src/builder/table-builder.js.map +1 -1
  55. package/out/zero-schema/src/permissions.d.ts.map +1 -1
  56. package/out/zero-schema/src/schema-config.d.ts +1 -0
  57. package/out/zero-schema/src/schema-config.d.ts.map +1 -1
  58. package/out/zero-schema/src/schema-config.js +2 -0
  59. package/out/zero-schema/src/schema-config.js.map +1 -1
  60. package/out/zero-schema/src/table-schema.d.ts +4 -0
  61. package/out/zero-schema/src/table-schema.d.ts.map +1 -1
  62. package/out/zero-schema/src/table-schema.js.map +1 -1
  63. package/out/zero.js +1 -1
  64. package/out/zqlite/src/table-source.d.ts +1 -0
  65. package/out/zqlite/src/table-source.d.ts.map +1 -1
  66. package/out/zqlite/src/table-source.js +1 -1
  67. package/out/zqlite/src/table-source.js.map +1 -1
  68. package/package.json +3 -3
@@ -5543,50 +5543,6 @@ function makeClientID() {
5543
5543
  }
5544
5544
 
5545
5545
  // ../replicache/src/persist/clients.ts
5546
- var clientV4Schema = readonlyObject({
5547
- /**
5548
- * A UNIX timestamp in milliseconds updated by the client once a minute
5549
- * while it is active and every time the client persists its state to
5550
- * the perdag.
5551
- * Should only be updated by the client represented by this structure.
5552
- */
5553
- heartbeatTimestampMs: valita_exports.number(),
5554
- /**
5555
- * The hash of the commit in the perdag this client last persisted.
5556
- * Should only be updated by the client represented by this structure.
5557
- */
5558
- headHash: hashSchema,
5559
- /**
5560
- * The mutationID of the commit at headHash (mutationID if it is a
5561
- * local commit, lastMutationID if it is an index change or snapshot commit).
5562
- * Should only be updated by the client represented by this structure.
5563
- * Read by other clients to determine if there are unacknowledged pending
5564
- * mutations for them to push on behalf of the client represented by this
5565
- * structure.
5566
- * This is redundant with information in the commit graph at headHash,
5567
- * but allows other clients to determine if there are unacknowledged pending
5568
- * mutations without having to load the commit graph at headHash.
5569
- */
5570
- mutationID: valita_exports.number(),
5571
- /**
5572
- * The highest lastMutationID received from the server for this client.
5573
- *
5574
- * Should be updated by the client represented by this structure whenever
5575
- * it persists its state to the perdag.
5576
- * Read by other clients to determine if there are unacknowledged pending
5577
- * mutations for them to push on behalf of the client represented by this
5578
- * structure, and *updated* by other clients upon successfully pushing
5579
- * pending mutations to avoid redundant pushes of those mutations.
5580
- *
5581
- * Note: This will be the same as the lastMutationID of the base snapshot of
5582
- * the commit graph at headHash when written by the client represented by this
5583
- * structure. However, when written by another client pushing pending
5584
- * mutations on this client's behalf it will be different. This is because
5585
- * the other client does not update the commit graph (it is unsafe to update
5586
- * another client's commit graph).
5587
- */
5588
- lastServerAckdMutationID: valita_exports.number()
5589
- });
5590
5546
  var clientV5Schema = readonlyObject({
5591
5547
  heartbeatTimestampMs: valita_exports.number(),
5592
5548
  headHash: hashSchema,
@@ -5630,15 +5586,8 @@ var clientV6Schema = readonlyObject({
5630
5586
  function isClientV6(client) {
5631
5587
  return client.refreshHashes !== void 0;
5632
5588
  }
5633
- function isClientV5(client) {
5634
- return client.clientGroupID !== void 0;
5635
- }
5636
5589
  var CLIENTS_HEAD_NAME = "clients";
5637
- var clientSchema = valita_exports.union(
5638
- clientV4Schema,
5639
- clientV5Schema,
5640
- clientV6Schema
5641
- );
5590
+ var clientSchema = valita_exports.union(clientV5Schema, clientV6Schema);
5642
5591
  function assertClient(value) {
5643
5592
  assert2(value, clientSchema);
5644
5593
  }
@@ -5668,7 +5617,7 @@ function clientMapToChunkData(clients, dagWrite) {
5668
5617
  }
5669
5618
  } else {
5670
5619
  dagWrite.assertValidHash(client.headHash);
5671
- if (isClientV5(client) && client.tempRefreshHash) {
5620
+ if (client.tempRefreshHash) {
5672
5621
  dagWrite.assertValidHash(client.tempRefreshHash);
5673
5622
  }
5674
5623
  }
@@ -5882,7 +5831,7 @@ function getRefsForClients(clients) {
5882
5831
  }
5883
5832
  } else {
5884
5833
  refs.add(client.headHash);
5885
- if (isClientV5(client) && client.tempRefreshHash) {
5834
+ if (client.tempRefreshHash) {
5886
5835
  refs.add(client.tempRefreshHash);
5887
5836
  }
5888
5837
  }
@@ -5898,10 +5847,7 @@ async function getClientGroupForClient(clientID, read) {
5898
5847
  }
5899
5848
  async function getClientGroupIDForClient(clientID, read) {
5900
5849
  const client = await getClient(clientID, read);
5901
- if (!client || !isClientV5(client)) {
5902
- return void 0;
5903
- }
5904
- return client.clientGroupID;
5850
+ return client?.clientGroupID;
5905
5851
  }
5906
5852
  async function setClient(clientID, client, dagWrite) {
5907
5853
  const clients = await getClients(dagWrite);
@@ -8383,11 +8329,17 @@ function enumeration() {
8383
8329
  customType: null
8384
8330
  });
8385
8331
  }
8386
- var TableBuilder = class {
8332
+ var TableBuilder = class _TableBuilder {
8387
8333
  #schema;
8388
8334
  constructor(schema) {
8389
8335
  this.#schema = schema;
8390
8336
  }
8337
+ from(serverName) {
8338
+ return new _TableBuilder({
8339
+ ...this.#schema,
8340
+ serverName
8341
+ });
8342
+ }
8391
8343
  columns(columns) {
8392
8344
  const columnSchemas = Object.fromEntries(
8393
8345
  Object.entries(columns).map(([k, v2]) => [k, v2.schema])
@@ -8416,6 +8368,16 @@ var TableBuilderWithColumns = class _TableBuilderWithColumns {
8416
8368
  if (this.#schema.primaryKey.length === 0) {
8417
8369
  throw new Error(`Table "${this.#schema.name}" is missing a primary key`);
8418
8370
  }
8371
+ const names = /* @__PURE__ */ new Set();
8372
+ for (const [col, { serverName }] of Object.entries(this.#schema.columns)) {
8373
+ const name = serverName ?? col;
8374
+ if (names.has(name)) {
8375
+ throw new Error(
8376
+ `Table "${this.#schema.name}" has multiple columns referencing "${name}"`
8377
+ );
8378
+ }
8379
+ names.add(name);
8380
+ }
8419
8381
  return this.#schema;
8420
8382
  }
8421
8383
  };
@@ -8424,6 +8386,12 @@ var ColumnBuilder = class _ColumnBuilder {
8424
8386
  constructor(schema) {
8425
8387
  this.#schema = schema;
8426
8388
  }
8389
+ from(serverName) {
8390
+ return new _ColumnBuilder({
8391
+ ...this.#schema,
8392
+ serverName
8393
+ });
8394
+ }
8427
8395
  optional() {
8428
8396
  return new _ColumnBuilder({
8429
8397
  ...this.#schema,
@@ -8439,7 +8407,13 @@ var ColumnBuilder = class _ColumnBuilder {
8439
8407
  function createSchema(version3, options) {
8440
8408
  const retTables = {};
8441
8409
  const retRelationships = {};
8410
+ const serverNames = /* @__PURE__ */ new Set();
8442
8411
  options.tables.forEach((table2) => {
8412
+ const { serverName = table2.schema.name } = table2.schema;
8413
+ if (serverNames.has(serverName)) {
8414
+ throw new Error(`Multiple tables reference the name "${serverName}"`);
8415
+ }
8416
+ serverNames.add(serverName);
8443
8417
  if (retTables[table2.schema.name]) {
8444
8418
  throw new Error(
8445
8419
  `Table "${table2.schema.name}" is defined more than once in the schema`
@@ -8604,6 +8578,10 @@ var disjunctionSchema = readonlyObject({
8604
8578
  type: valita_exports.literal("or"),
8605
8579
  conditions: readonlyArray(conditionSchema)
8606
8580
  });
8581
+ function mustCompoundKey(field) {
8582
+ assert(Array.isArray(field) && field.length >= 1);
8583
+ return field;
8584
+ }
8607
8585
  var compoundKeySchema = readonly(
8608
8586
  valita_exports.tuple([valita_exports.string()]).concat(valita_exports.array(valita_exports.string()))
8609
8587
  );
@@ -8632,42 +8610,162 @@ var astSchema = readonlyObject({
8632
8610
  exclusive: valita_exports.boolean()
8633
8611
  }).optional()
8634
8612
  });
8635
- var normalizeCache = /* @__PURE__ */ new WeakMap();
8636
- function normalizeAST(ast) {
8637
- const cached = normalizeCache.get(ast);
8638
- if (cached) {
8639
- return cached;
8640
- }
8641
- const where = flattened(ast.where);
8642
- const normalized = {
8613
+ function transformAST(ast, transform) {
8614
+ const { tableName, columnName } = transform;
8615
+ const colName = (c) => columnName(ast.table, c);
8616
+ const key = (table2, k) => {
8617
+ const serverKey = k.map((col) => columnName(table2, col));
8618
+ return mustCompoundKey(serverKey);
8619
+ };
8620
+ const where = ast.where ? transform.where(ast.where) : void 0;
8621
+ const transformed = {
8643
8622
  schema: ast.schema,
8644
- table: ast.table,
8623
+ table: tableName(ast.table),
8645
8624
  alias: ast.alias,
8646
- where: where ? sortedWhere(where) : void 0,
8647
- related: ast.related ? sortedRelated(
8625
+ where: where ? transformWhere(where, ast.table, transform) : void 0,
8626
+ related: ast.related ? transform.related(
8648
8627
  ast.related.map(
8649
8628
  (r) => ({
8650
- correlation: r.correlation,
8629
+ correlation: {
8630
+ parentField: key(ast.table, r.correlation.parentField),
8631
+ childField: key(r.subquery.table, r.correlation.childField)
8632
+ },
8651
8633
  hidden: r.hidden,
8652
- subquery: normalizeAST(r.subquery),
8634
+ subquery: transformAST(r.subquery, transform),
8653
8635
  system: r.system
8654
8636
  })
8655
8637
  )
8656
8638
  ) : void 0,
8657
- start: ast.start,
8639
+ start: ast.start ? {
8640
+ ...ast.start,
8641
+ row: Object.fromEntries(
8642
+ Object.entries(ast.start.row).map(([col, val]) => [
8643
+ colName(col),
8644
+ val
8645
+ ])
8646
+ )
8647
+ } : void 0,
8658
8648
  limit: ast.limit,
8659
- orderBy: ast.orderBy
8649
+ orderBy: ast.orderBy?.map(([col, dir]) => [colName(col), dir])
8650
+ };
8651
+ return transformed;
8652
+ }
8653
+ function transformWhere(where, table2, transform) {
8654
+ const { columnName } = transform;
8655
+ const condValue = (c) => c.type !== "column" ? c : { ...c, name: columnName(table2, c.name) };
8656
+ const key = (table3, k) => {
8657
+ const serverKey = k.map((col) => columnName(table3, col));
8658
+ return mustCompoundKey(serverKey);
8659
+ };
8660
+ if (where.type === "simple") {
8661
+ return { ...where, left: condValue(where.left) };
8662
+ } else if (where.type === "correlatedSubquery") {
8663
+ const { correlation, subquery } = where.related;
8664
+ return {
8665
+ ...where,
8666
+ related: {
8667
+ ...where.related,
8668
+ correlation: {
8669
+ parentField: key(table2, correlation.parentField),
8670
+ childField: key(subquery.table, correlation.childField)
8671
+ },
8672
+ subquery: transformAST(subquery, transform)
8673
+ }
8674
+ };
8675
+ }
8676
+ return {
8677
+ type: where.type,
8678
+ conditions: transform.conditions(
8679
+ where.conditions.map((c) => transformWhere(c, table2, transform))
8680
+ )
8660
8681
  };
8661
- normalizeCache.set(ast, normalized);
8682
+ }
8683
+ var normalizeCache = /* @__PURE__ */ new WeakMap();
8684
+ var NORMALIZE_TRANSFORM = {
8685
+ tableName: (t2) => t2,
8686
+ columnName: (_, c) => c,
8687
+ related: sortedRelated,
8688
+ where: flattened,
8689
+ conditions: (c) => c.sort(cmpCondition)
8690
+ };
8691
+ function normalizeAST(ast) {
8692
+ let normalized = normalizeCache.get(ast);
8693
+ if (!normalized) {
8694
+ normalized = transformAST(ast, NORMALIZE_TRANSFORM);
8695
+ normalizeCache.set(ast, normalized);
8696
+ }
8662
8697
  return normalized;
8663
8698
  }
8664
- function sortedWhere(where) {
8665
- if (where.type === "simple" || where.type === "correlatedSubquery") {
8666
- return where;
8699
+ function toServerAST(ast, tables) {
8700
+ return transformAST(ast, clientToServer(tables));
8701
+ }
8702
+ function toServerCondition(cond, table2, tables) {
8703
+ return transformWhere(cond, table2, clientToServer(tables));
8704
+ }
8705
+ function clientToServer(tables) {
8706
+ const mustTable = (table2) => {
8707
+ const t2 = tables[table2];
8708
+ if (!table2) {
8709
+ throw new Error(`invalid table "${table2}"`);
8710
+ }
8711
+ return t2;
8712
+ };
8713
+ return {
8714
+ tableName: (table2) => mustTable(table2).serverName ?? table2,
8715
+ columnName: (table2, col) => {
8716
+ const c = mustTable(table2).columns[col];
8717
+ if (!c) {
8718
+ throw new Error(`invalid column "${col}" in table "${table2}"`);
8719
+ }
8720
+ return c.serverName ?? col;
8721
+ },
8722
+ related: (r) => r,
8723
+ where: (w) => w,
8724
+ conditions: (c) => c
8725
+ };
8726
+ }
8727
+ function toClientAST(ast, tables) {
8728
+ return transformAST(ast, serverToClient(tables));
8729
+ }
8730
+ function serverToClient(tables) {
8731
+ const tableNames = Object.fromEntries(
8732
+ Object.entries(tables).map(([clientName, { serverName }]) => [
8733
+ serverName ?? clientName,
8734
+ clientName
8735
+ ])
8736
+ );
8737
+ const columnNamesByTable = Object.fromEntries(
8738
+ Object.entries(tables).map(
8739
+ ([clientTable, { serverName: serverTable, columns }]) => {
8740
+ const columnNames = Object.fromEntries(
8741
+ Object.entries(columns).map(([clientName, { serverName }]) => [
8742
+ serverName ?? clientName,
8743
+ clientName
8744
+ ])
8745
+ );
8746
+ return [serverTable ?? clientTable, columnNames];
8747
+ }
8748
+ )
8749
+ );
8750
+ function mustTable(table2, tables2) {
8751
+ const t2 = tables2[table2];
8752
+ if (!table2) {
8753
+ throw new Error(`invalid table "${table2}"`);
8754
+ }
8755
+ return t2;
8667
8756
  }
8668
8757
  return {
8669
- type: where.type,
8670
- conditions: where.conditions.map((w) => sortedWhere(w)).sort(cmpCondition)
8758
+ tableName: (table2) => mustTable(table2, tableNames),
8759
+ columnName: (table2, col) => {
8760
+ const name = mustTable(table2, columnNamesByTable)[col];
8761
+ if (!name) {
8762
+ throw new Error(`invalid column "${col}" in table "${table2}"`);
8763
+ }
8764
+ return name;
8765
+ },
8766
+ related: (r) => r,
8767
+ where: (w) => w,
8768
+ conditions: (c) => c
8671
8769
  };
8672
8770
  }
8673
8771
  function sortedRelated(related) {
@@ -8725,9 +8823,6 @@ function cmpRelated(a, b) {
8725
8823
  return compareUTF83(must(a.subquery.alias), must(b.subquery.alias));
8726
8824
  }
8727
8825
  function flattened(cond) {
8728
- if (cond === void 0) {
8729
- return void 0;
8730
- }
8731
8826
  if (cond.type === "simple" || cond.type === "correlatedSubquery") {
8732
8827
  return cond;
8733
8828
  }
@@ -11323,69 +11418,92 @@ async function definePermissions(schema, definer) {
11323
11418
  expressionBuilders[name] = new AuthQuery(schema, name).expressionBuilder();
11324
11419
  }
11325
11420
  const config = await definer();
11326
- return compilePermissions(config, expressionBuilders);
11421
+ return compilePermissions(schema, config, expressionBuilders);
11327
11422
  }
11328
- function compilePermissions(authz, expressionBuilders) {
11423
+ function compilePermissions(schema, authz, expressionBuilders) {
11329
11424
  if (!authz) {
11330
11425
  return void 0;
11331
11426
  }
11332
11427
  const ret = {};
11333
11428
  for (const [tableName, tableConfig] of Object.entries(authz)) {
11334
- ret[tableName] = {
11335
- row: compileRowConfig(tableConfig.row, expressionBuilders[tableName]),
11336
- cell: compileCellConfig(tableConfig.cell, expressionBuilders[tableName])
11429
+ const serverName = schema.tables[tableName].serverName ?? tableName;
11430
+ ret[serverName] = {
11431
+ row: compileRowConfig(
11432
+ schema,
11433
+ tableName,
11434
+ tableConfig.row,
11435
+ expressionBuilders[tableName]
11436
+ ),
11437
+ cell: compileCellConfig(
11438
+ schema,
11439
+ tableName,
11440
+ tableConfig.cell,
11441
+ expressionBuilders[tableName]
11442
+ )
11337
11443
  };
11338
11444
  }
11339
11445
  return ret;
11340
11446
  }
11341
- function compileRowConfig(rowRules, expressionBuilder) {
11447
+ function compileRowConfig(schema, tableName, rowRules, expressionBuilder) {
11342
11448
  if (!rowRules) {
11343
11449
  return void 0;
11344
11450
  }
11345
11451
  return {
11346
- select: compileRules(rowRules.select, expressionBuilder),
11347
- insert: compileRules(rowRules.insert, expressionBuilder),
11452
+ select: compileRules(schema, tableName, rowRules.select, expressionBuilder),
11453
+ insert: compileRules(schema, tableName, rowRules.insert, expressionBuilder),
11348
11454
  update: {
11349
11455
  preMutation: compileRules(
11456
+ schema,
11457
+ tableName,
11350
11458
  rowRules.update?.preMutation,
11351
11459
  expressionBuilder
11352
11460
  ),
11353
11461
  postMutation: compileRules(
11462
+ schema,
11463
+ tableName,
11354
11464
  rowRules.update?.postMutation,
11355
11465
  expressionBuilder
11356
11466
  )
11357
11467
  },
11358
- delete: compileRules(rowRules.delete, expressionBuilder)
11468
+ delete: compileRules(schema, tableName, rowRules.delete, expressionBuilder)
11359
11469
  };
11360
11470
  }
11361
- function compileRules(rules, expressionBuilder) {
11471
+ function compileRules(schema, tableName, rules, expressionBuilder) {
11362
11472
  if (!rules) {
11363
11473
  return void 0;
11364
11474
  }
11365
- return rules.map(
11366
- (rule) => [
11475
+ return rules.map((rule) => {
11476
+ const cond = rule(authDataRef, expressionBuilder);
11477
+ return [
11367
11478
  "allow",
11368
- rule(authDataRef, expressionBuilder)
11369
- ]
11370
- );
11479
+ toServerCondition(cond, tableName, schema.tables)
11480
+ ];
11481
+ });
11371
11482
  }
11372
- function compileCellConfig(cellRules, expressionBuilder) {
11483
+ function compileCellConfig(schema, tableName, cellRules, expressionBuilder) {
11373
11484
  if (!cellRules) {
11374
11485
  return void 0;
11375
11486
  }
11376
11487
  const ret = {};
11377
11488
  for (const [columnName, rules] of Object.entries(cellRules)) {
11378
11489
  ret[columnName] = {
11379
- select: compileRules(rules.select, expressionBuilder),
11380
- insert: compileRules(rules.insert, expressionBuilder),
11490
+ select: compileRules(schema, tableName, rules.select, expressionBuilder),
11491
+ insert: compileRules(schema, tableName, rules.insert, expressionBuilder),
11381
11492
  update: {
11382
- preMutation: compileRules(rules.update?.preMutation, expressionBuilder),
11493
+ preMutation: compileRules(
11494
+ schema,
11495
+ tableName,
11496
+ rules.update?.preMutation,
11497
+ expressionBuilder
11498
+ ),
11383
11499
  postMutation: compileRules(
11500
+ schema,
11501
+ tableName,
11384
11502
  rules.update?.postMutation,
11385
11503
  expressionBuilder
11386
11504
  )
11387
11505
  },
11388
- delete: compileRules(rules.delete, expressionBuilder)
11506
+ delete: compileRules(schema, tableName, rules.delete, expressionBuilder)
11389
11507
  };
11390
11508
  }
11391
11509
  return ret;
@@ -13104,6 +13222,44 @@ async function deleteImpl(tx, arg, schema) {
13104
13222
  await tx.del(key);
13105
13223
  }
13106
13224
 
13225
+ // ../zero-client/src/client/custom.ts
13226
+ var TransactionImpl = class {
13227
+ constructor(repTx, schema) {
13228
+ must(repTx.reason === "initial" || repTx.reason === "rebase");
13229
+ this.clientID = repTx.clientID;
13230
+ this.mutationID = repTx.mutationID;
13231
+ this.reason = repTx.reason === "initial" ? "optimistic" : "rebase";
13232
+ this.mutate = makeSchemaCRUD(schema, repTx);
13233
+ }
13234
+ clientID;
13235
+ mutationID;
13236
+ reason;
13237
+ mutate;
13238
+ };
13239
+ function makeReplicacheMutator(mutator, schema) {
13240
+ return (repTx, args) => {
13241
+ const tx = new TransactionImpl(repTx, schema);
13242
+ return mutator(tx, args);
13243
+ };
13244
+ }
13245
+ function makeSchemaCRUD(schema, tx) {
13246
+ const mutate = {};
13247
+ for (const [name] of Object.entries(schema.tables)) {
13248
+ mutate[name] = makeTableCRUD(schema, name, tx);
13249
+ }
13250
+ return mutate;
13251
+ }
13252
+ function makeTableCRUD(schema, tableName, tx) {
13253
+ const table2 = must(schema.tables[tableName]);
13254
+ const { primaryKey } = table2;
13255
+ return {
13256
+ insert: (value) => insertImpl(tx, { op: "insert", tableName, primaryKey, value }, schema),
13257
+ upsert: (value) => upsertImpl(tx, { op: "upsert", tableName, primaryKey, value }, schema),
13258
+ update: (value) => updateImpl(tx, { op: "update", tableName, primaryKey, value }, schema),
13259
+ delete: (id) => deleteImpl(tx, { op: "delete", tableName, primaryKey, value: id }, schema)
13260
+ };
13261
+ }
13262
+
13107
13263
  // ../zero-client/src/client/enable-analytics.ts
13108
13264
  var IPV4_ADDRESS_REGEX = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
13109
13265
  var IPV6_ADDRESS_HOSTNAME_REGEX = /^\[[a-fA-F0-9:]*:[a-fA-F0-9:]*\]$/;
@@ -13352,7 +13508,7 @@ function makeMessage(message, context, logLevel) {
13352
13508
  }
13353
13509
 
13354
13510
  // ../zero-client/src/client/version.ts
13355
- var version2 = "0.13.2025020200";
13511
+ var version2 = "0.13.2025020300";
13356
13512
 
13357
13513
  // ../zero-client/src/client/log-options.ts
13358
13514
  var LevelFilterLogSink = class {
@@ -13653,13 +13809,15 @@ var Success = 1;
13653
13809
  // ../zero-client/src/client/query-manager.ts
13654
13810
  var QueryManager = class {
13655
13811
  #clientID;
13812
+ #tables;
13656
13813
  #send;
13657
13814
  #queries = /* @__PURE__ */ new Map();
13658
13815
  #recentQueriesMaxSize;
13659
13816
  #recentQueries = /* @__PURE__ */ new Set();
13660
13817
  #gotQueries = /* @__PURE__ */ new Set();
13661
- constructor(clientID, send2, experimentalWatch, recentQueriesMaxSize) {
13818
+ constructor(clientID, tables, send2, experimentalWatch, recentQueriesMaxSize) {
13662
13819
  this.#clientID = clientID;
13820
+ this.#tables = tables;
13663
13821
  this.#recentQueriesMaxSize = recentQueriesMaxSize;
13664
13822
  this.#send = send2;
13665
13823
  experimentalWatch(
@@ -13743,17 +13901,16 @@ var QueryManager = class {
13743
13901
  let entry = this.#queries.get(astHash);
13744
13902
  this.#recentQueries.delete(astHash);
13745
13903
  if (!entry) {
13904
+ const serverAST = toServerAST(normalized, this.#tables);
13746
13905
  entry = {
13747
- normalized,
13906
+ normalized: serverAST,
13748
13907
  count: 1,
13749
13908
  gotCallbacks: gotCallback === void 0 ? [] : [gotCallback]
13750
13909
  };
13751
13910
  this.#queries.set(astHash, entry);
13752
13911
  this.#send([
13753
13912
  "changeDesiredQueries",
13754
- {
13755
- desiredQueriesPatch: [{ op: "put", hash: astHash, ast: normalized }]
13756
- }
13913
+ { desiredQueriesPatch: [{ op: "put", hash: astHash, ast: serverAST }] }
13757
13914
  ]);
13758
13915
  } else {
13759
13916
  ++entry.count;
@@ -13981,6 +14138,26 @@ function getServer(server) {
13981
14138
 
13982
14139
  // ../zero-client/src/client/zero-poke-handler.ts
13983
14140
  import { Lock as Lock4 } from "@rocicorp/lock";
14141
+ function makeClientNames(schema) {
14142
+ return new Map(
14143
+ Object.entries(schema.tables).map(
14144
+ ([tableName, { serverName: serverTableName, columns }]) => {
14145
+ let allSame = true;
14146
+ const names = {};
14147
+ for (const [name, { serverName }] of Object.entries(columns)) {
14148
+ if (serverName && serverName !== name) {
14149
+ allSame = false;
14150
+ }
14151
+ names[serverName ?? name] = name;
14152
+ }
14153
+ return [
14154
+ serverTableName ?? tableName,
14155
+ { tableName, columns: allSame ? null : names }
14156
+ ];
14157
+ }
14158
+ )
14159
+ );
14160
+ }
13984
14161
  var PokeHandler = class {
13985
14162
  #replicachePoke;
13986
14163
  #onPokeError;
@@ -13994,12 +14171,14 @@ var PokeHandler = class {
13994
14171
  // order poke errors.
13995
14172
  #pokeLock = new Lock4();
13996
14173
  #schema;
14174
+ #clientNames;
13997
14175
  #raf = getBrowserGlobalMethod("requestAnimationFrame") ?? rafFallback;
13998
14176
  constructor(replicachePoke, onPokeError, clientID, schema, lc) {
13999
14177
  this.#replicachePoke = replicachePoke;
14000
14178
  this.#onPokeError = onPokeError;
14001
14179
  this.#clientID = clientID;
14002
14180
  this.#schema = schema;
14181
+ this.#clientNames = makeClientNames(schema);
14003
14182
  this.#lc = lc.withContext("PokeHandler");
14004
14183
  }
14005
14184
  handlePokeStart(pokeStart) {
@@ -14077,7 +14256,11 @@ var PokeHandler = class {
14077
14256
  lc.debug?.("got poke lock at", now);
14078
14257
  lc.debug?.("merging", this.#pokeBuffer.length);
14079
14258
  try {
14080
- const merged = mergePokes(this.#pokeBuffer, this.#schema);
14259
+ const merged = mergePokes(
14260
+ this.#pokeBuffer,
14261
+ this.#schema,
14262
+ this.#clientNames
14263
+ );
14081
14264
  this.#pokeBuffer.length = 0;
14082
14265
  if (merged === void 0) {
14083
14266
  lc.debug?.("frame is empty");
@@ -14106,7 +14289,7 @@ var PokeHandler = class {
14106
14289
  this.#pokeBuffer.length = 0;
14107
14290
  }
14108
14291
  };
14109
- function mergePokes(pokeBuffer, schema) {
14292
+ function mergePokes(pokeBuffer, schema, clientNames) {
14110
14293
  if (pokeBuffer.length === 0) {
14111
14294
  return void 0;
14112
14295
  }
@@ -14145,7 +14328,8 @@ function mergePokes(pokeBuffer, schema) {
14145
14328
  ...queriesPatch.map(
14146
14329
  (op) => queryPatchOpToReplicachePatchOp(
14147
14330
  op,
14148
- (hash2) => toDesiredQueriesKey(clientID, hash2)
14331
+ (hash2) => toDesiredQueriesKey(clientID, hash2),
14332
+ schema
14149
14333
  )
14150
14334
  )
14151
14335
  );
@@ -14154,14 +14338,14 @@ function mergePokes(pokeBuffer, schema) {
14154
14338
  if (pokePart.gotQueriesPatch) {
14155
14339
  mergedPatch.push(
14156
14340
  ...pokePart.gotQueriesPatch.map(
14157
- (op) => queryPatchOpToReplicachePatchOp(op, toGotQueriesKey)
14341
+ (op) => queryPatchOpToReplicachePatchOp(op, toGotQueriesKey, schema)
14158
14342
  )
14159
14343
  );
14160
14344
  }
14161
14345
  if (pokePart.rowsPatch) {
14162
14346
  mergedPatch.push(
14163
14347
  ...pokePart.rowsPatch.map(
14164
- (p) => rowsPatchOpToReplicachePatchOp(p, schema)
14348
+ (p) => rowsPatchOpToReplicachePatchOp(p, schema, clientNames)
14165
14349
  )
14166
14350
  );
14167
14351
  }
@@ -14194,7 +14378,7 @@ function clientsPatchOpToReplicachePatchOp(op) {
14194
14378
  };
14195
14379
  }
14196
14380
  }
14197
- function queryPatchOpToReplicachePatchOp(op, toKey) {
14381
+ function queryPatchOpToReplicachePatchOp(op, toKey, schema) {
14198
14382
  switch (op.op) {
14199
14383
  case "clear":
14200
14384
  return op;
@@ -14208,88 +14392,69 @@ function queryPatchOpToReplicachePatchOp(op, toKey) {
14208
14392
  return {
14209
14393
  op: "put",
14210
14394
  key: toKey(op.hash),
14211
- value: op.ast
14395
+ value: toClientAST(op.ast, schema.tables)
14212
14396
  };
14213
14397
  }
14214
14398
  }
14215
- function rowsPatchOpToReplicachePatchOp(op, schema) {
14399
+ function rowsPatchOpToReplicachePatchOp(op, schema, clientNames) {
14400
+ if (op.op === "clear") {
14401
+ return op;
14402
+ }
14403
+ const names = clientNames.get(op.tableName);
14404
+ if (!names) {
14405
+ throw new Error(`unknown table name in ${JSON.stringify(op)}`);
14406
+ }
14407
+ const { tableName, columns } = names;
14216
14408
  switch (op.op) {
14217
- case "clear":
14218
- return op;
14219
14409
  case "del":
14220
14410
  return {
14221
14411
  op: "del",
14222
14412
  key: toPrimaryKeyString(
14223
- op.tableName,
14224
- schema.tables[op.tableName].primaryKey,
14225
- op.id
14413
+ tableName,
14414
+ schema.tables[tableName].primaryKey,
14415
+ toClientRow(op.id, columns)
14226
14416
  )
14227
14417
  };
14228
14418
  case "put":
14229
14419
  return {
14230
14420
  op: "put",
14231
14421
  key: toPrimaryKeyString(
14232
- op.tableName,
14233
- schema.tables[op.tableName].primaryKey,
14234
- op.value
14422
+ tableName,
14423
+ schema.tables[tableName].primaryKey,
14424
+ toClientRow(op.value, columns)
14235
14425
  ),
14236
- value: op.value
14426
+ value: toClientRow(op.value, columns)
14237
14427
  };
14238
14428
  case "update":
14239
14429
  return {
14240
14430
  op: "update",
14241
14431
  key: toPrimaryKeyString(
14242
- op.tableName,
14243
- schema.tables[op.tableName].primaryKey,
14244
- op.id
14432
+ tableName,
14433
+ schema.tables[tableName].primaryKey,
14434
+ toClientRow(op.id, columns)
14245
14435
  ),
14246
- merge: op.merge,
14247
- constrain: op.constrain
14436
+ merge: op.merge ? toClientRow(op.merge, columns) : void 0,
14437
+ constrain: toClientColumns(op.constrain, columns)
14248
14438
  };
14249
14439
  default:
14250
14440
  throw new Error("to be implemented");
14251
14441
  }
14252
14442
  }
14253
- function rafFallback(callback) {
14254
- setTimeout(callback, 0);
14255
- }
14256
-
14257
- // ../zero-client/src/client/custom.ts
14258
- var TransactionImpl = class {
14259
- constructor(repTx, schema) {
14260
- must(repTx.reason === "initial" || repTx.reason === "rebase");
14261
- this.clientID = repTx.clientID;
14262
- this.mutationID = repTx.mutationID;
14263
- this.reason = repTx.reason === "initial" ? "optimistic" : "rebase";
14264
- this.mutate = makeSchemaCRUD(schema, repTx);
14443
+ function toClientRow(row, names) {
14444
+ if (names === null) {
14445
+ return row;
14265
14446
  }
14266
- clientID;
14267
- mutationID;
14268
- reason;
14269
- mutate;
14270
- };
14271
- function makeReplicacheMutator(mutator, schema) {
14272
- return (repTx, args) => {
14273
- const tx = new TransactionImpl(repTx, schema);
14274
- return mutator(tx, args);
14275
- };
14276
- }
14277
- function makeSchemaCRUD(schema, tx) {
14278
- const mutate = {};
14279
- for (const [name] of Object.entries(schema.tables)) {
14280
- mutate[name] = makeTableCRUD(schema, name, tx);
14447
+ const clientRow = {};
14448
+ for (const col in row) {
14449
+ clientRow[names[col] ?? col] = row[col];
14281
14450
  }
14282
- return mutate;
14451
+ return clientRow;
14283
14452
  }
14284
- function makeTableCRUD(schema, tableName, tx) {
14285
- const table2 = must(schema.tables[tableName]);
14286
- const { primaryKey } = table2;
14287
- return {
14288
- insert: (value) => insertImpl(tx, { op: "insert", tableName, primaryKey, value }, schema),
14289
- upsert: (value) => upsertImpl(tx, { op: "upsert", tableName, primaryKey, value }, schema),
14290
- update: (value) => updateImpl(tx, { op: "update", tableName, primaryKey, value }, schema),
14291
- delete: (id) => deleteImpl(tx, { op: "delete", tableName, primaryKey, value: id }, schema)
14292
- };
14453
+ function toClientColumns(columns, names) {
14454
+ return !names || !columns ? columns : columns.map((col) => names[col] ?? col);
14455
+ }
14456
+ function rafFallback(callback) {
14457
+ setTimeout(callback, 0);
14293
14458
  }
14294
14459
 
14295
14460
  // ../zero-client/src/client/zero.ts
@@ -14543,6 +14708,7 @@ var Zero = class {
14543
14708
  this.mutateBatch = mutateBatch;
14544
14709
  this.#queryManager = new QueryManager(
14545
14710
  rep.clientID,
14711
+ schema.tables,
14546
14712
  (msg) => this.#sendChangeDesiredQueries(msg),
14547
14713
  rep.experimentalWatch.bind(rep),
14548
14714
  maxRecentQueries
@@ -15483,4 +15649,4 @@ export {
15483
15649
  escapeLike,
15484
15650
  Zero
15485
15651
  };
15486
- //# sourceMappingURL=chunk-5S3K5CU2.js.map
15652
+ //# sourceMappingURL=chunk-2WSNDAJE.js.map