Skip to content

Commit ae6eaab

Browse files
ampagenttmm
andcommitted
feat(zero-swift): drop the generic tx.mutate.insert and upsert
tx.mutate.<table> is now the one way to write, as in JS. Generic code uses TypedTableMutator<R>(tx.mutate); hand-written rows without a generated member use the untyped tx.mutate.<table> with columnValues. Amp-Thread-ID: https://ampcode.com/threads/T-01a17664-8fd0-76be-8fd5-75ac6e7aa0d8 Co-authored-by: tmm <tmm@tmm.dev>
1 parent cc0f799 commit ae6eaab

6 files changed

Lines changed: 11 additions & 18 deletions

File tree

‎.changeset/typed-table-mutators.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
To migrate, regenerate with `npx zero-swift codegen`, then replace dictionaries passed to `tx.mutate.<table>` with the typed values:
88

99
```diff
10+
- try tx.mutate.insert(Issue(id: id, title: title, ...))
11+
+ try tx.mutate.issue.insert(.init(id: id, title: title, ...))
12+
1013
- try tx.mutate.issue.insert(["id": .string(id), "title": .string(title), ...])
1114
+ try tx.mutate.issue.insert(.init(id: id, title: title, ...))
1215

@@ -20,4 +23,4 @@ To migrate, regenerate with `npx zero-swift codegen`, then replace dictionaries
2023
+ try tx.mutate.issue.delete(.init(id: id))
2124
```
2225

23-
`tx.mutate.insert(row)` and `tx.mutate.upsert(row)` still work. For untyped writes, `tx.mutate.table("issue")` takes dictionaries as before.
26+
`tx.mutate.insert(row)` and `tx.mutate.upsert(row)` are gone: `tx.mutate.<table>` is the one way to write. Generic code can use `TypedTableMutator<R>(tx.mutate)`, and `tx.mutate.table("issue")` takes dictionaries for untyped writes.

‎swift/Sources/Zero/Client/Transaction.swift‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,6 @@ public struct TransactionMutate {
131131
/// The untyped CRUD operations on `name`, with values keyed by client
132132
/// column name.
133133
public func table(_ name: String) -> TableMutator { TableMutator(tx: tx, table: name) }
134-
135-
/// Inserts a row unless one with its primary key exists.
136-
public func insert<R: ZeroRow>(_ row: R) throws {
137-
try tx.insert(R.tableName, row.columnValues)
138-
}
139-
140-
/// Inserts a row, or replaces the columns of the existing one.
141-
public func upsert<R: ZeroRow>(_ row: R) throws {
142-
try tx.upsert(R.tableName, row.columnValues)
143-
}
144134
}
145135

146136
/// The CRUD operations on one table (`TableCRUD` in

‎swift/Sources/ZeroBenchmarks/Fixtures.swift‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ struct UpdateUser: Codable, Sendable {
7878
enum Mutators {
7979
static let insertUsers = defineMutator("insertUsers") { (tx, args: InsertUsers) in
8080
for n in args.from..<args.from + args.count {
81-
try tx.mutate.insert(User(n))
81+
try tx.mutate.user.insert(User(n).columnValues)
8282
}
8383
}
8484
static let updateUser = defineMutator("updateUser") { (tx, args: UpdateUser) in
8585
var row = User(args.a)
8686
let v = Double(args.value)
8787
(row.b, row.c, row.d, row.e, row.f, row.g, row.h, row.i, row.j) = (v, v, v, v, v, v, v, v, v)
88-
try tx.mutate.upsert(row)
88+
try tx.mutate.user.upsert(row.columnValues)
8989
}
9090
static let all: [any Mutator<Void>] = [insertUsers, updateUser]
9191
}

‎swift/Tests/AppleTests/Notes.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ enum Notes {
4040
}
4141

4242
static let put = defineMutator("note.put") { (tx, note: Put) in
43-
try tx.mutate.upsert(Note(id: note.id, text: note.text))
43+
try tx.mutate.note.upsert(Note(id: note.id, text: note.text).columnValues)
4444
}
4545
static let remove = defineMutator("note.remove") { (tx, id: String) in
4646
try tx.mutate.note.delete(["id": .string(id)])

‎swift/Tests/E2ETests/ClientE2ETests.swift‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ enum E2EApp {
2020
}
2121

2222
static let create = defineMutator("issue.create") { (tx, args: CreateIssue) in
23-
try tx.mutate.insert(
24-
TestIssue(
23+
try tx.mutate.issue.insert(
24+
.init(
2525
id: args.id, title: args.title, description: "", closed: false, ownerId: args.ownerId,
2626
createdAt: 100))
2727
}

‎swift/Tests/ZeroTests/ClientAPITests.swift‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import ZeroTesting
2222
zql.issue.where(\.ownerId, auth.userID).orderBy(\.id, .asc)
2323
}
2424
static let create = defineMutator("issue.create") { (tx, args: CreateIssue, auth: Auth) in
25-
try tx.mutate.insert(
26-
Issue(
25+
try tx.mutate.issue.insert(
26+
.init(
2727
id: args.id, title: args.title, description: "", closed: false,
2828
ownerId: args.ownerId ?? auth.userID, createdAt: 1))
2929
}

0 commit comments

Comments
 (0)