|
| 1 | +# zero-swift |
| 2 | + |
| 3 | +The command-line tool for [Zero for Swift](https://github.com/wevm/zero-swift). |
| 4 | +Your Zero app's TypeScript schema, queries, and mutators stay the source of |
| 5 | +truth, and this tool generates what the Swift client needs from them. |
| 6 | + |
| 7 | +```sh |
| 8 | +npm install --save-dev zero-swift |
| 9 | +``` |
| 10 | + |
| 11 | +It needs Node 22.18 or later, which loads TypeScript files directly. It also |
| 12 | +needs the app's own `@rocicorp/zero`, 1.11 or later. |
| 13 | + |
| 14 | +## `zero-swift codegen` |
| 15 | + |
| 16 | +Generates Swift from the TypeScript schema. For each table it writes a row |
| 17 | +struct, a `ZeroSchema`, and a `zql` builder with a `Query`. |
| 18 | + |
| 19 | +```sh |
| 20 | +npx zero-swift codegen # reads zero-swift.json |
| 21 | +npx zero-swift codegen --check # fails when the output is out of date |
| 22 | +``` |
| 23 | + |
| 24 | +```json |
| 25 | +{ |
| 26 | + "schema": "../web/shared/schema.ts", |
| 27 | + "output": "Sources/App/Schema.swift", |
| 28 | + "schemaName": "app", |
| 29 | + "tables": { |
| 30 | + "issue": {"columns": {"shortID": {"type": "int"}}} |
| 31 | + } |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +`number()` columns are `Double`. Set a column's type to `int` to make it an |
| 36 | +`Int`; it must then hold integers. For a `json<T>()` column whose `T` is a |
| 37 | +plain object or a literal union, the tool generates a `Codable` type. |
| 38 | + |
| 39 | +`--schema`, `--out`, and `--config` override the config file. |
| 40 | + |
| 41 | +## `zero-swift drift record` |
| 42 | + |
| 43 | +You write the Swift queries and mutators by hand, and the drift harness |
| 44 | +checks them against the TypeScript ones. This command runs the TypeScript |
| 45 | +definitions on samples and records what they did. `Drift.expectNoDrift` in |
| 46 | +the Swift `ZeroTesting` library then fails a test wherever the Swift ones |
| 47 | +differ. |
| 48 | + |
| 49 | +```sh |
| 50 | +npx zero-swift drift record --samples drift-samples.ts --out Tests/AppTests/drift-vectors.json |
| 51 | +``` |
| 52 | + |
| 53 | +The samples module's default export holds: |
| 54 | + |
| 55 | +- `schema` |
| 56 | +- the `queries` and `mutators` registries |
| 57 | +- `querySamples`, each a name with optional `args` and `context` |
| 58 | +- `mutatorSamples`, each also with optional `seed` rows |
| 59 | + |
| 60 | +Record again whenever either side changes. `--check` fails when the recorded |
| 61 | +file is out of date. |
0 commit comments