zero-swift 0.4.7 → 0.5.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 (3) hide show
  1. package/README.md +2 -1
  2. package/dist/bin.js +83 -3
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -15,7 +15,8 @@ needs the app's own `@rocicorp/zero`, 1.11 or later.
15
15
  ## `zero-swift codegen`
16
16
 
17
17
  Generates Swift from the TypeScript schema. For each table it writes a row
18
- struct, a `ZeroSchema`, and a `zql` builder with a `Query`.
18
+ struct with its `Update` and `Key`, a `ZeroSchema`, a `zql` builder with a
19
+ `Query`, and a typed `tx.mutate.<table>` for mutators.
19
20
 
20
21
  ```sh
21
22
  npx zero-swift codegen # reads zero-swift.json
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.4.7",
12
+ version: "0.5.0",
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: {
@@ -127,8 +127,12 @@ var rowMembers = [
127
127
  "hashValue",
128
128
  "CodingKeys",
129
129
  // The parameter of `init(entry:)`, which assigns the columns.
130
- "entry"
130
+ "entry",
131
+ // What `tx.mutate.<table>.update` and `.delete` take.
132
+ "Update",
133
+ "Key"
131
134
  ];
135
+ var mutateMembers = ["tx", "table", "insert", "upsert"];
132
136
  var objectMembers = [
133
137
  "zeroValue",
134
138
  "valueForNull",
@@ -463,7 +467,7 @@ function generate(schema, customTypes, config, source) {
463
467
  p.property = ref(ids[i]);
464
468
  }
465
469
  const identifiable = table.primaryKey.length === 1 && table.primaryKey[0] === "id" && columns.some((c) => c.name === "id" && c.id === "id" && !c.optional);
466
- const conformances = ["ZeroRow", "Codable", "Hashable"];
470
+ const conformances = ["ZeroTable", "Codable", "Hashable"];
467
471
  if (identifiable) {
468
472
  conformances.push("Identifiable");
469
473
  }
@@ -535,6 +539,25 @@ function generate(schema, customTypes, config, source) {
535
539
  lines.push(" ]");
536
540
  lines.push(" }");
537
541
  lines.push(...codingKeys([...columns, ...relationships]));
542
+ const keyColumns = table.primaryKey.map(
543
+ (k) => columns.find((c) => c.name === k)
544
+ );
545
+ lines.push(
546
+ "",
547
+ ...columnsDecl(
548
+ pub,
549
+ "Update",
550
+ `What \`tx.mutate.${t}.update\` takes: the primary key, and the other columns to set. A column that is nil, or \`.unchanged\`, keeps its value.`,
551
+ columns.map((c) => ({ ...c, key: keyColumns.includes(c) }))
552
+ ),
553
+ "",
554
+ ...columnsDecl(
555
+ pub,
556
+ "Key",
557
+ `What \`tx.mutate.${t}.delete\` takes: the primary key.`,
558
+ keyColumns.map((c) => ({ ...c, key: true }))
559
+ )
560
+ );
538
561
  lines.push("}");
539
562
  typeDecls.push(lines.join("\n"));
540
563
  }
@@ -568,8 +591,65 @@ function generate(schema, customTypes, config, source) {
568
591
  out.push("");
569
592
  out.push(`${pub}let ${identifier(zqlName)} = ${zqlType}()`);
570
593
  out.push("");
594
+ out.push(
595
+ `/// The typed CRUD operations in a mutator, one per table: \`tx.mutate.${tableNames[0] ?? "table"}.insert(\u2026)\`.`
596
+ );
597
+ out.push("extension TransactionMutate {");
598
+ const mutateIds = new Members(mutateMembers).claimAll(
599
+ tableNames.map((t) => ({ name: t, id: sanitize(t) }))
600
+ );
601
+ for (const [i, t] of tableNames.entries()) {
602
+ const row = rowNames.get(t);
603
+ out.push(
604
+ ` ${pub}var ${ref(mutateIds[i])}: TypedTableMutator<${row}> { TypedTableMutator(self) }`
605
+ );
606
+ }
607
+ out.push("}");
608
+ out.push("");
571
609
  return out.join("\n");
572
610
  }
611
+ function columnsDecl(pub, name, doc, columns) {
612
+ const nullable = (c) => c.swiftType.endsWith("?");
613
+ const type = (c) => c.key ? c.swiftType : nullable(c) ? `ColumnUpdate<${c.swiftType.slice(0, -1)}>` : `${c.swiftType}?`;
614
+ const fallback = (c) => c.key ? "" : nullable(c) ? " = .unchanged" : " = nil";
615
+ const lines = [
616
+ ` /// ${doc}`,
617
+ ` ${pub}struct ${name}: ZeroColumnValues, Hashable {`
618
+ ];
619
+ for (const c of columns) {
620
+ lines.push(` ${pub}var ${c.property}: ${type(c)}`);
621
+ }
622
+ lines.push("");
623
+ lines.push(` ${pub}init(`);
624
+ lines.push(
625
+ columns.map((c) => ` ${c.property}: ${type(c)}${fallback(c)}`).join(",\n")
626
+ );
627
+ lines.push(" ) {");
628
+ for (const c of columns) {
629
+ lines.push(` self.${c.property} = ${c.property}`);
630
+ }
631
+ lines.push(" }");
632
+ lines.push("");
633
+ lines.push(` ${pub}var columnValues: Row {`);
634
+ lines.push(" var row = Row()");
635
+ for (const c of columns) {
636
+ if (c.key) {
637
+ lines.push(` row[${str(c.name)}] = self.${c.property}.zeroValue`);
638
+ } else if (nullable(c)) {
639
+ lines.push(
640
+ ` if let value = self.${c.property}.zeroValue { row[${str(c.name)}] = value }`
641
+ );
642
+ } else {
643
+ lines.push(
644
+ ` if let value = self.${c.property} { row[${str(c.name)}] = value.zeroValue }`
645
+ );
646
+ }
647
+ }
648
+ lines.push(" return row");
649
+ lines.push(" }");
650
+ lines.push(" }");
651
+ return lines;
652
+ }
573
653
  function codingKeys(props) {
574
654
  if (props.every((p) => p.id === p.name)) {
575
655
  return [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zero-swift",
3
- "version": "0.4.7",
3
+ "version": "0.5.0",
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": {
@@ -26,8 +26,8 @@
26
26
  "vitest": "^5.0.5",
27
27
  "@rocicorp/zero": "1.12.0",
28
28
  "zero-schema": "0.0.0",
29
- "zero-types": "0.0.0",
30
- "zql": "0.0.0"
29
+ "zql": "0.0.0",
30
+ "zero-types": "0.0.0"
31
31
  },
32
32
  "peerDependencies": {
33
33
  "@rocicorp/zero": ">=1.11.0"