Skip to content

Commit 9e3eda5

Browse files
committed
Clarify docs on custom representations for database functions.
1 parent 841db84 commit 9e3eda5

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

Sources/StructuredQueriesSQLiteCore/Documentation.docc/Articles/CustomFunctions.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,35 @@ configuration.prepareDatabase { db in
5050
5151
### Custom representations
5252
53-
To define a type that works with a custom representation, like JSON, you can use the `as` parameter
54-
of the macro:
53+
To define a type that works with a custom representation, i.e. anytime you use `@Column(as:)` in
54+
your data type, you can use the `as` parameter of the macro to specify those types. For example,
55+
if your model holds onto a date and you want to store that date as a
56+
[unix timestamp](<doc:Foundation/Date/UnixTimeRepresentation-struct>) (i.e. double),
57+
then you can do so like this:
58+
59+
```swift
60+
@Table
61+
struct Reminder {
62+
let id: UUID
63+
var title = ""
64+
@Column(as: Date.UnixTimeRepresentation.self)
65+
var dueDate: Date
66+
}
67+
```
68+
69+
And if you wanted to pass this `dueDate` to a Swift database function, you can do so like this:
70+
71+
```swift
72+
@DatabaseFunction(
73+
as: ((Date.UnixTimeRepresentation.self) -> Bool).self
74+
)
75+
func isPastDue(_ date: Date) -> Bool {
76+
date < Date()
77+
}
78+
```
79+
80+
As another example, if you wanted to pass an array of strings from SQL to your Swift database
81+
function, then you can shuffle the data through using json:
5582

5683
```swift
5784
@DatabaseFunction(

0 commit comments

Comments
 (0)