Skip to content

Commit d3352fd

Browse files
committed
Add TaggedStructuredQueries trait
1 parent 887545d commit d3352fd

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

Sources/StructuredQueriesCore/Documentation.docc/Articles/DefiningYourSchema.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,51 @@ With that you can insert reminders with notes like so:
331331
}
332332
}
333333
334+
#### Tagged identifiers
335+
336+
The [Tagged](https://github.com/pointfreeco/swift-tagged) library provides lightweight syntax for
337+
introducing type-safe identifiers (and more) to your models. StructuredQueries ships support for
338+
Tagged with a `TaggedStructuredQueries` package trait, which is available starting from Swift 6.1.
339+
340+
To enable the trait, specify it in the Package.swift file that depends on StructuredQueries:
341+
342+
```diff
343+
.package(
344+
url: "https://github.com/pointfreeco/swift-structured-queries",
345+
from: "0.2.0",
346+
+ traits: ["TaggedStructuredQueries"]
347+
),
348+
```
349+
350+
This will allow you to introduce distinct `Tagged` identifiers throughout your schema:
351+
352+
```diff
353+
@Table
354+
struct RemindersList: Identifiable {
355+
- let id: Int
356+
+ typealias ID = Tagged<Self, Int>
357+
+ let id: ID
358+
// ...
359+
}
360+
@Table
361+
struct Reminder: Identifiable {
362+
- let id: Int
363+
+ typealias ID = Tagged<Self, Int>
364+
+ let id: ID
365+
// ...
366+
var remindersList: Reminder.ID
367+
}
368+
```
369+
370+
This adds a new layer of type-safety when constructing queries. Previously comparing a
371+
`RemindersList.ID` to a `Reminder.ID` would compile just fine, even though it is a nonsensical thing
372+
to do. But now, such a comparison is a compile time error:
373+
374+
```
375+
RemindersList.leftJoin(Reminder.all) {
376+
$0.id == $1.id // 🛑 Requires the types 'Reminder.ID' and 'RemindersList.ID' be equivalent
377+
}
378+
334379
### Primary keyed tables
335380
336381
It is possible to tell let the `@Table` macro know which property of your data type is the primary

Sources/StructuredQueriesCore/Traits/Tagged.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
}
1515

1616
extension Tagged: QueryRepresentable where RawValue: QueryRepresentable {
17-
public init(queryOutput: RawValue.QueryOutput) {
18-
self.init(RawValue(queryOutput: queryOutput))
17+
public typealias QueryOutput = Tagged<Tag, RawValue.QueryOutput>
18+
19+
public var queryOutput: QueryOutput {
20+
QueryOutput(rawValue: self.rawValue.queryOutput)
1921
}
2022

21-
public var queryOutput: RawValue.QueryOutput {
22-
rawValue.queryOutput
23+
public init(queryOutput: QueryOutput) {
24+
self.init(rawValue: RawValue(queryOutput: queryOutput.rawValue))
2325
}
2426
}
2527

Tests/StructuredQueriesTests/UpdateTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ extension SnapshotTests {
365365
WHERE ("reminders"."id" = 1)
366366
RETURNING "dueDate"
367367
"""
368-
}results: {
368+
} results: {
369369
"""
370370
┌─────┐
371371
│ nil │
@@ -383,7 +383,7 @@ extension SnapshotTests {
383383
WHERE ("reminders"."id" = 1)
384384
RETURNING "dueDate"
385385
"""
386-
}results: {
386+
} results: {
387387
"""
388388
┌────────────────────────────────┐
389389
│ Date(2018-01-29T00:08:00.000Z) │

0 commit comments

Comments
 (0)