Skip to content

Commit 0c6c9ef

Browse files
committed
wip
1 parent e4a0d9c commit 0c6c9ef

4 files changed

Lines changed: 72 additions & 18 deletions

File tree

Sources/SharingGRDBCore/Documentation.docc/Articles/ComparisonWithSwiftData.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SwiftUI views (including UIKit, `@Observable` models, _etc._). This article desc
1010
approaches compare in a variety of situations, such as setting up the data store, fetching data,
1111
associations, and more.
1212

13-
* [Designing your schema](#Designing-your-schema)
13+
* [Defining your schema](#Defining-your-schema)
1414
* [Setting up external storage](#Setting-up-external-storage)
1515
* [Fetching data for a view](#Fetching-data-for-a-view)
1616
* [Fetching data for an @Observable model](#Fetching-data-for-an-Observable-model)
@@ -22,7 +22,7 @@ associations, and more.
2222
* [Manual migrations](#Manual-migrations)
2323
* [Supported Apple platforms](#Supported-Apple-platforms)
2424

25-
### Designing your schema
25+
### Defining your schema
2626

2727
Both SharingGRDB and SwiftData come with tools to expose your data types' fields to the compiler
2828
so that type-safe and schema-safe queries can be written. SharingGRDB uses another library of ours
@@ -73,6 +73,11 @@ Some key differences:
7373
* The `@Model` version of `Item` does not need an `id` field because SwiftData provides a
7474
`persistentIdentifier` to each model.
7575

76+
See the [documentation][sq-defining-schema] from StructuredQueries for more information on how
77+
to define your schema.
78+
79+
[sq-defining-schema]: https://swiftpackageindex.com/pointfreeco/swift-structured-queries/main/documentation/structuredqueriescore/definingyourschema
80+
7681
### Setting up external storage
7782

7883
Both SharingGRDB and SwiftData require some work to be done at the entry point of the app in order

Sources/SharingGRDBCore/Documentation.docc/Articles/MigrationGuides/MigratingTo0.2.md

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,62 @@ simplify the library, and make it more powerful. As such, we often need to depre
99
in favor of newer ones. We recommend people update their code as quickly as possible to the newest
1010
APIs, and these guides contain tips to do so.
1111

12-
> Important: Before following any particular migration guide be sure you have followed all the
13-
> preceding migration guides.
14-
15-
* [FetchAll, FetchOne, Fetch](#)
12+
* [@FetchAll, @FetchOne, @Fetch](#)
1613
* [fetchAll, fetchOne, fetch: soft-deprecated](#)
1714
* [Avoiding the cost of macros](#)
1815

19-
<!--
20-
todo: finish
21-
-->
16+
## @FetchAll, @FetchOne, @Fetch
17+
18+
SharingGRDB 0.2.0 comes with 3 brand new property wrappers that largely replace the need for
19+
SwiftData and its `@Query` macro. In 0.1.0, one would perform queries as either a hard coded SQL
20+
string:
21+
22+
```swift
23+
@SharedReader(.fetchAll("SELECT * FROM reminders WHERE isCompleted ORDER BY title"))
24+
var completedReminders: [Reminder]
25+
```
26+
27+
Or by defining a ``FetchKeyRequest`` conformance to perform a query using GRDB's query builder:
28+
29+
```swift
30+
struct CompletedReminders: FetchKeyRequest {
31+
func fetch(_ db: Database) throws -> [Reminder] {
32+
Reminder.all()
33+
.where(Column("isCompleted"))
34+
.order(Column("title"))
35+
}
36+
}
37+
38+
@SharedReader(.fetch(CompletedReminders()))
39+
var completedReminders
40+
```
41+
42+
Each of these are cumbersome, and version 0.2.0 of SharingGRDB fixes it thanks to our newly
43+
released [StructuredQueries][] library. You can now describe the query for your data in a type-safe
44+
manner, and directly inline:
45+
46+
```swift
47+
@FetchAll(Reminde.where(\.isCompleted).order(by: \.title))
48+
var completedReminders: [Reminder]
49+
```
50+
51+
Read <doc:Fetching> for more information on how to use these new property wrappers.
52+
53+
[StructuredQueries]: http://github.com/pointfreeco/swift-structured-queries
54+
55+
## fetchAll, fetchOne, fetch: soft-deprecated
56+
57+
The [`.fetchAll`](<doc:Sharing/SharedReaderKey/fetchAll(sql:arguments:database:)>),
58+
[`.fetchOne`](<doc:Sharing/SharedReaderKey/fetchOne(sql:arguments:database:)>),
59+
and [`.fetch`](<doc:Sharing/SharedReaderKey/fetch(_:database:)>) APIs have been soft-deprecated
60+
in favor of the more modern tools described above and in <doc:Fetching>. They will be hard
61+
deprecated in a future release of SharingGRDB, and removed in 1.0.
62+
63+
## Avoiding the cost of macros
64+
65+
SharingGRDB introduces a macro in version 0.2.0 (in particular, the `@Table` macro), and
66+
unfortunately macros currently come with an unfortunate cost in that you have to compile SwiftSyntax
67+
from scratch, which can take time. If the cost of macros is too high for you, then you can depend
68+
on the SharingGRDBCore module instead of the full SharingGRDB module. This will give you access to
69+
only a subset of tools provided by SharingGRDB, but you will have access to all tools that were
70+
available in version 0.1.0 of the library.

Sources/SharingGRDBCore/Documentation.docc/SharingGRDBCore.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ in SwiftData:
105105
> Note: For more information on preparing a SQLite database, see <doc:PreparingDatabase>.
106106
107107
This `defaultDatabase` connection is used implicitly by SharingGRDB's strategies, like
108-
[`@FetchAll`](<doc:SharingGRDBCore/FetchAll):
108+
[`@FetchAll`](<doc:SharingGRDBCore/FetchAll>):
109109

110110
```swift
111111
@FetchAll var items: [Item]

Sources/SharingGRDBCore/FetchKey.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ extension SharedReaderKey {
4343
/// - Parameters:
4444
/// - request: A request describing the data to fetch.
4545
/// - database: The database to read from. A value of `nil` will use
46-
/// `@Dependency(\.defaultDatabase)``.
46+
/// `@Dependency(\.defaultDatabase)`.
4747
/// - Returns: A key that can be passed to the `@SharedReader` property wrapper.
4848
@available(iOS, deprecated: 9999, message: "Use the '@Fetch' property wrapper, instead")
4949
@available(macOS, deprecated: 9999, message: "Use the '@Fetch' property wrapper, instead")
@@ -70,7 +70,7 @@ extension SharedReaderKey {
7070
/// - Parameters:
7171
/// - request: A request describing the data to fetch.
7272
/// - database: The database to read from. A value of `nil` will use
73-
/// `@Dependency(\.defaultDatabase)``.
73+
/// `@Dependency(\.defaultDatabase)`.
7474
/// - Returns: A key that can be passed to the `@SharedReader` property wrapper.
7575
@available(iOS, deprecated: 9999, message: "Use the '@Fetch' property wrapper, instead")
7676
@available(macOS, deprecated: 9999, message: "Use the '@Fetch' property wrapper, instead")
@@ -99,7 +99,7 @@ extension SharedReaderKey {
9999
/// - sql: A raw SQL string describing the data to fetch.
100100
/// - arguments: Arguments to bind to the SQL statement.
101101
/// - database: The database to read from. A value of `nil` will use
102-
/// `@Dependency(\.defaultDatabase)``.
102+
/// `@Dependency(\.defaultDatabase)`.
103103
/// - Returns: A key that can be passed to the `@SharedReader` property wrapper.
104104
@available(iOS, deprecated: 9999, message: "Use '@FetchAll' and '#sql', instead")
105105
@available(macOS, deprecated: 9999, message: "Use '@FetchAll' and '#sql', instead")
@@ -132,7 +132,7 @@ extension SharedReaderKey {
132132
/// - sql: A raw SQL string describing the data to fetch.
133133
/// - arguments: Arguments to bind to the SQL statement.
134134
/// - database: The database to read from. A value of `nil` will use
135-
/// `@Dependency(\.defaultDatabase)``.
135+
/// `@Dependency(\.defaultDatabase)`.
136136
/// - Returns: A key that can be passed to the `@SharedReader` property wrapper.
137137
@available(iOS, deprecated: 9999, message: "Use '@FetchOne' and '#sql', instead")
138138
@available(macOS, deprecated: 9999, message: "Use '@FetchOne' and '#sql', instead")
@@ -158,7 +158,7 @@ extension SharedReaderKey {
158158
/// - Parameters:
159159
/// - request: A request describing the data to fetch.
160160
/// - database: The database to read from. A value of `nil` will use
161-
/// `@Dependency(\.defaultDatabase)``.
161+
/// `@Dependency(\.defaultDatabase)`.
162162
/// - scheduler: The scheduler to observe from. By default, database observation is performed
163163
/// asynchronously on the main queue.
164164
/// - Returns: A key that can be passed to the `@SharedReader` property wrapper.
@@ -184,7 +184,7 @@ extension SharedReaderKey {
184184
/// - Parameters:
185185
/// - request: A request describing the data to fetch.
186186
/// - database: The database to read from. A value of `nil` will use
187-
/// `@Dependency(\.defaultDatabase)``.
187+
/// `@Dependency(\.defaultDatabase)`.
188188
/// - scheduler: The scheduler to observe from. By default, database observation is performed
189189
/// asynchronously on the main queue.
190190
/// - Returns: A key that can be passed to the `@SharedReader` property wrapper.
@@ -211,7 +211,7 @@ extension SharedReaderKey {
211211
/// - sql: A raw SQL string describing the data to fetch.
212212
/// - arguments: Arguments to bind to the SQL statement.
213213
/// - database: The database to read from. A value of `nil` will use
214-
/// `@Dependency(\.defaultDatabase)``.
214+
/// `@Dependency(\.defaultDatabase)`.
215215
/// - scheduler: The scheduler to observe from. By default, database observation is performed
216216
/// asynchronously on the main queue.
217217
/// - Returns: A key that can be passed to the `@SharedReader` property wrapper.
@@ -244,7 +244,7 @@ extension SharedReaderKey {
244244
/// - sql: A raw SQL string describing the data to fetch.
245245
/// - arguments: Arguments to bind to the SQL statement.
246246
/// - database: The database to read from. A value of `nil` will use
247-
/// `@Dependency(\.defaultDatabase)``.
247+
/// `@Dependency(\.defaultDatabase)`.
248248
/// - scheduler: The scheduler to observe from. By default, database observation is performed
249249
/// asynchronously on the main queue.
250250
/// - Returns: A key that can be passed to the `@SharedReader` property wrapper.

0 commit comments

Comments
 (0)