zero-swift 0.5.5 → 0.5.6

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.
package/dist/bin.js CHANGED
@@ -9,7 +9,7 @@ import { Cli, z } from "incur";
9
9
  // package.json
10
10
  var package_default = {
11
11
  name: "zero-swift",
12
- version: "0.5.5",
12
+ version: "0.5.6",
13
13
  description: "Generates the Swift row types and schema of a Zero app for the Zero Swift client.",
14
14
  license: "Apache-2.0",
15
15
  repository: {
@@ -1106,7 +1106,7 @@ cli.command(
1106
1106
  "SAMPLES_NOT_FOUND",
1107
1107
  `Samples file ${relative(cwd, samples)} doesn't exist. Pass --samples.`
1108
1108
  );
1109
- const { recordDriftFile } = await import("./drift-SSEQSMJX.js");
1109
+ const { recordDriftFile } = await import("./drift-WXDTDUO2.js");
1110
1110
  const vectors = await recordDriftFile(samples);
1111
1111
  return writeOutput(
1112
1112
  c.error,
@@ -1415,12 +1415,6 @@ var orderingElementSchema = readonly(
1415
1415
  tuple([selectorSchema, literalUnion("asc", "desc")])
1416
1416
  );
1417
1417
  var orderingSchema = readonlyArray(orderingElementSchema);
1418
- var primitiveSchema = union(
1419
- string(),
1420
- number(),
1421
- boolean(),
1422
- null_()
1423
- );
1424
1418
  var equalityOpsSchema = literalUnion("=", "!=", "IS", "IS NOT");
1425
1419
  var orderOpsSchema = literalUnion("<", ">", "<=", ">=");
1426
1420
  var likeOpsSchema = literalUnion(
@@ -1450,6 +1444,24 @@ var columnReferenceSchema = readonlyObject({
1450
1444
  type: literal("column"),
1451
1445
  name: string()
1452
1446
  });
1447
+ var MAX_JSON_PATH_INDEX = 2 ** 31 - 1;
1448
+ function isValidJsonPathIndex(index) {
1449
+ return Number.isInteger(index) && index >= 0 && index <= MAX_JSON_PATH_INDEX;
1450
+ }
1451
+ var jsonPathSchema = readonlyArray(
1452
+ union(
1453
+ string(),
1454
+ number().assert(
1455
+ isValidJsonPathIndex,
1456
+ "expected a non-negative integer array index"
1457
+ )
1458
+ )
1459
+ ).assert((path2) => path2.length > 0, "expected a non-empty JSON path");
1460
+ var jsonPathReferenceSchema = readonlyObject({
1461
+ type: literal("json"),
1462
+ value: columnReferenceSchema,
1463
+ path: jsonPathSchema
1464
+ });
1453
1465
  var parameterReferenceSchema = readonlyObject({
1454
1466
  type: literal("static"),
1455
1467
  // The "namespace" of the injected parameter.
@@ -1463,14 +1475,41 @@ var parameterReferenceSchema = readonlyObject({
1463
1475
  var conditionValueSchema = union(
1464
1476
  literalReferenceSchema,
1465
1477
  columnReferenceSchema,
1478
+ jsonPathReferenceSchema,
1466
1479
  parameterReferenceSchema
1467
1480
  );
1481
+ function hasJsonLeafLiteralShape(c) {
1482
+ if (c.left.type !== "json" || c.right.type !== "literal") {
1483
+ return true;
1484
+ }
1485
+ const isList = Array.isArray(c.right.value);
1486
+ return c.op === "IN" || c.op === "NOT IN" ? isList || c.right.value === null : !isList;
1487
+ }
1488
+ function hasHomogeneousJsonInList(c) {
1489
+ if (c.left.type !== "json" || c.op !== "IN" && c.op !== "NOT IN" || c.right.type !== "literal") {
1490
+ return true;
1491
+ }
1492
+ const { value } = c.right;
1493
+ if (!Array.isArray(value)) {
1494
+ return true;
1495
+ }
1496
+ return isHomogeneousList(value);
1497
+ }
1498
+ function isHomogeneousList(list) {
1499
+ return list.every((e) => typeof e === typeof list[0]);
1500
+ }
1468
1501
  var simpleConditionSchema = readonlyObject({
1469
1502
  type: literal("simple"),
1470
1503
  op: simpleOperatorSchema,
1471
1504
  left: conditionValueSchema,
1472
1505
  right: union(parameterReferenceSchema, literalReferenceSchema)
1473
- });
1506
+ }).assert(
1507
+ hasJsonLeafLiteralShape,
1508
+ "expected a list for IN/NOT IN against a JSON path and a scalar for other operators"
1509
+ ).assert(
1510
+ hasHomogeneousJsonInList,
1511
+ "expected a list of values of one type for IN/NOT IN against a JSON path"
1512
+ );
1474
1513
  var correlatedSubqueryConditionOperatorSchema = literalUnion("EXISTS", "NOT EXISTS");
1475
1514
  var correlatedSubqueryConditionSchema = readonlyObject({
1476
1515
  type: literal("correlatedSubquery"),
@@ -1501,10 +1540,23 @@ var correlationSchema = readonlyObject({
1501
1540
  parentField: compoundKeySchema,
1502
1541
  childField: compoundKeySchema
1503
1542
  });
1543
+ var aggregateSchema = readonlyObject({
1544
+ fn: literalUnion("count", "sum", "avg", "min", "max"),
1545
+ field: string().optional(),
1546
+ path: jsonPathSchema.optional(),
1547
+ distinct: readonlyArray(string()).optional()
1548
+ }).assert(
1549
+ (a) => a.path === void 0 || a.field !== void 0 && a.fn !== "count",
1550
+ "expected an aggregate JSON path to have a field and not be a count"
1551
+ ).assert(
1552
+ (a) => a.distinct === void 0 || a.fn === "count" && a.field === void 0 && a.distinct.length > 0,
1553
+ "expected an aggregate over distinct columns to be a count of at least one"
1554
+ );
1504
1555
  var correlatedSubquerySchemaOmitSubquery = readonlyObject({
1505
1556
  correlation: correlationSchema,
1506
1557
  hidden: boolean().optional(),
1507
- system: literalUnion("permissions", "client", "test").optional()
1558
+ system: literalUnion("permissions", "client", "test").optional(),
1559
+ aggregate: aggregateSchema.optional()
1508
1560
  });
1509
1561
  var correlatedSubquerySchema = correlatedSubquerySchemaOmitSubquery.extend({
1510
1562
  subquery: lazy(() => astSchema)
@@ -1520,7 +1572,9 @@ var astSchema = readonlyObject({
1520
1572
  start: object({
1521
1573
  row: rowSchema,
1522
1574
  exclusive: boolean()
1523
- }).optional()
1575
+ }).optional(),
1576
+ aggregate: aggregateSchema.optional(),
1577
+ groupBy: readonlyArray(string()).assert((cols) => cols.length > 0, "expected groupBy to name a column").optional()
1524
1578
  });
1525
1579
  var MAX_AST_DEPTH = 50;
1526
1580
  function exceedsASTDepth(ast, maxDepth = MAX_AST_DEPTH) {
@@ -1594,7 +1648,18 @@ function normalizeSubqueries(cond) {
1594
1648
  }
1595
1649
  }
1596
1650
  function normalizedAST(ast) {
1597
- const { schema, table, alias, where, related, start, limit, orderBy } = ast;
1651
+ const {
1652
+ schema,
1653
+ table,
1654
+ alias,
1655
+ where,
1656
+ related,
1657
+ start,
1658
+ limit,
1659
+ orderBy,
1660
+ aggregate,
1661
+ groupBy
1662
+ } = ast;
1598
1663
  return asNormalized({
1599
1664
  schema,
1600
1665
  table,
@@ -1603,7 +1668,9 @@ function normalizedAST(ast) {
1603
1668
  related: related === void 0 ? void 0 : normalizeRelated(related),
1604
1669
  start,
1605
1670
  limit,
1606
- orderBy
1671
+ orderBy,
1672
+ aggregate,
1673
+ groupBy
1607
1674
  });
1608
1675
  }
1609
1676
  var normalizedConditions = /* @__PURE__ */ new WeakSet();
@@ -1616,7 +1683,7 @@ function normalizeRelated(related) {
1616
1683
  return related;
1617
1684
  }
1618
1685
  function normalizedRelated(related) {
1619
- const { correlation, hidden, subquery, system } = related;
1686
+ const { correlation, hidden, subquery, system, aggregate } = related;
1620
1687
  return {
1621
1688
  correlation: {
1622
1689
  parentField: correlation.parentField,
@@ -1624,7 +1691,8 @@ function normalizedRelated(related) {
1624
1691
  },
1625
1692
  hidden,
1626
1693
  subquery,
1627
- system
1694
+ system,
1695
+ aggregate
1628
1696
  };
1629
1697
  }
1630
1698
  function normalizeValuePosition(v) {
@@ -1633,6 +1701,12 @@ function normalizeValuePosition(v) {
1633
1701
  return { type: "literal", value: v.value };
1634
1702
  case "column":
1635
1703
  return { type: "column", name: v.name };
1704
+ case "json":
1705
+ return {
1706
+ type: "json",
1707
+ value: normalizeValuePosition(v.value),
1708
+ path: v.path
1709
+ };
1636
1710
  default:
1637
1711
  return { type: "static", anchor: v.anchor, field: v.field };
1638
1712
  }
@@ -1721,6 +1795,11 @@ function compareValuePosition(a, b) {
1721
1795
  case "column":
1722
1796
  assert(b.type === "column", "Expected column type for comparison");
1723
1797
  return compareUTF8(a.name, b.name);
1798
+ case "json":
1799
+ assert(b.type === "json", "Expected json type for comparison");
1800
+ return compareValuePosition(a.value, b.value) || // A path is strings and numbers, which cmpLiteralValue() orders by
1801
+ // type, so `['0']` (an object key) and `[0]` (an array index) differ.
1802
+ cmpLiteralValue(a.path, b.path);
1724
1803
  case "static":
1725
1804
  assert(b.type === "static", "Expected static type for comparison");
1726
1805
  return compareUTF8(a.anchor, b.anchor) || cmpLiteralValue(a.field, b.field);
@@ -1852,6 +1931,10 @@ var TAG_OR = 8199;
1852
1931
  var TAG_SYSTEM_PERMISSIONS = 8202;
1853
1932
  var TAG_SYSTEM_CLIENT = 8203;
1854
1933
  var TAG_SYSTEM_TEST = 8204;
1934
+ var TAG_AGGREGATE = 8206;
1935
+ var TAG_JSON = 8207;
1936
+ var TAG_GROUP_BY = 8208;
1937
+ var TAG_DISTINCT = 8209;
1855
1938
  var STR_MARK = 1073741824;
1856
1939
  function reset() {
1857
1940
  h1 = PRIME32_5;
@@ -1974,6 +2057,11 @@ function visitValuePosition(v) {
1974
2057
  }
1975
2058
  return;
1976
2059
  }
2060
+ case "json":
2061
+ mix(TAG_JSON);
2062
+ visitValuePosition(v.value);
2063
+ visitValue(v.path);
2064
+ return;
1977
2065
  default:
1978
2066
  unreachable(v);
1979
2067
  }
@@ -2010,8 +2098,28 @@ function visitCorrelatedSubquery(csq) {
2010
2098
  visitCompoundKey(csq.correlation.childField);
2011
2099
  mix(csq.hidden === void 0 ? TAG_UNDEF : csq.hidden ? TAG_TRUE : TAG_FALSE);
2012
2100
  mixOptionalSystem(csq.system);
2101
+ visitAggregate(csq.aggregate);
2013
2102
  visitAST(csq.subquery);
2014
2103
  }
2104
+ function visitAggregate(aggregate) {
2105
+ if (aggregate === void 0) {
2106
+ return;
2107
+ }
2108
+ mix(TAG_AGGREGATE);
2109
+ mixString(aggregate.fn);
2110
+ visitOptionalString(aggregate.field);
2111
+ if (aggregate.path !== void 0) {
2112
+ mix(TAG_JSON);
2113
+ visitValue(aggregate.path);
2114
+ }
2115
+ if (aggregate.distinct !== void 0) {
2116
+ mix(TAG_DISTINCT);
2117
+ mix(aggregate.distinct.length);
2118
+ for (const col of aggregate.distinct) {
2119
+ mixString(col);
2120
+ }
2121
+ }
2122
+ }
2015
2123
  function visitAST(ast) {
2016
2124
  mix(TAG_OBJ);
2017
2125
  visitOptionalString(ast.schema);
@@ -2045,6 +2153,14 @@ function visitAST(ast) {
2045
2153
  mixNumber(ast.limit);
2046
2154
  }
2047
2155
  visitOrdering(ast.orderBy);
2156
+ visitAggregate(ast.aggregate);
2157
+ if (ast.groupBy !== void 0) {
2158
+ mix(TAG_GROUP_BY);
2159
+ mix(ast.groupBy.length);
2160
+ for (const col of ast.groupBy) {
2161
+ mixString(col);
2162
+ }
2163
+ }
2048
2164
  mix(TAG_END);
2049
2165
  }
2050
2166
  function hashAST(ast) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zero-swift",
3
- "version": "0.5.5",
3
+ "version": "0.5.6",
4
4
  "description": "Generates the Swift row types and schema of a Zero app for the Zero Swift client.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -24,10 +24,10 @@
24
24
  "esbuild": "0.28.2",
25
25
  "oxfmt": "^0.74.0",
26
26
  "vitest": "^5.0.5",
27
+ "zero-schema": "0.0.0",
27
28
  "@rocicorp/zero": "1.12.0",
28
29
  "zero-types": "0.0.0",
29
- "zql": "0.0.0",
30
- "zero-schema": "0.0.0"
30
+ "zql": "0.0.0"
31
31
  },
32
32
  "peerDependencies": {
33
33
  "@rocicorp/zero": ">=1.11.0"