Skip to content

Commit 7934e49

Browse files
committed
docs: update documentation for KeyPath
1 parent 4530948 commit 7934e49

File tree

2 files changed

+41
-58
lines changed

2 files changed

+41
-58
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,18 @@
55
### Breaking
66

77
* Drops support for Swift 3 and Swift 4. Swift 5 or newer must be used.
8+
9+
### Enhancements
10+
11+
* Added support for ordering by Swift KeyPath, for example:
12+
13+
```swift
14+
queryset.orderBy(\.createdAt, ascending: true)
15+
```
16+
17+
* Added support for filtering and excluding by Swift KeyPath, for example:
18+
19+
```swift
20+
queryset.exclude(\.name == "Kyle")
21+
queryset.filter(\.createdAt > Date())
22+
```

README.md

Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,10 @@ QueryKit, a simple type-safe Core Data query language.
66

77
## Usage
88

9-
To get the most out of QueryKit, and to get full type-safe queries, you may
10-
add extensions for your Core Data models providing properties which describe
11-
your models. You may use [querykit-cli](https://github.com/QueryKit/querykit-cli)
12-
to generate these automatically.
13-
14-
An extension for our a `Person` model might look as follows:
15-
16-
```swift
17-
extension User {
18-
static var name:Attribute<String> { return Attribute("name") }
19-
static var age:Attribute<Int> { return Attribute("age") }
20-
}
21-
```
22-
23-
This provides static properties on our User model which represent each property
24-
on our Core Data model, these may be used to construct predicates and sort
25-
descriptors with compile time safety, without stringly typing them
26-
into your application.
27-
289
```swift
29-
let namePredicate = Person.name == "Kyle"
30-
let agePredicate = Person.age > 25
31-
let ageSortDescriptor = Person.age.descending()
10+
QuerySet<Person>(context, "Person")
11+
.orderedBy(.name, ascending: true)
12+
.filter(\.age >= 18)
3213
```
3314

3415
### QuerySet
@@ -40,20 +21,19 @@ results based on the given parameters.
4021
#### Retrieving all objects
4122

4223
```swift
43-
let queryset = Person.queryset(context)
24+
let queryset = QuerySet<Person>(context, "Person")
4425
```
4526

4627
#### Retrieving specific objects with filters
4728

4829
You may filter a QuerySet using the `filter` and `exclude` methods, which
49-
accept a closure passing the model type allowing you to access the
50-
type-safe attributes.
30+
accept a predicate which can be constructed using KeyPath extensions.
5131

52-
The `filter` and `exclude` methods return brand new QuerySet's including your filter.
32+
The `filter` and `exclude` methods return new QuerySet's including your filter.
5333

5434
```swift
55-
queryset.filter { $0.name == "Kyle" }
56-
queryset.exclude { $0.age > 25 }
35+
queryset.filter(\.name == "Kyle")
36+
queryset.exclude(\.age > 25)
5737
```
5838

5939
You may also use standard `NSPredicate` if you want to construct complicated
@@ -70,14 +50,13 @@ The result of refining a QuerySet is itself a QuerySet, so it’s possible
7050
to chain refinements together. For example:
7151

7252
```swift
73-
queryset.filter { $0.name == "Kyle" }
74-
.exclude { $0.age < 25 }
75-
.filter { $0.isEmployed }
53+
queryset.filter(\.name == "Kyle")
54+
.exclude(\.age < 25)
7655
```
7756

78-
Each time you refine a QuerySet, you get a brand-new QuerySet that is in
79-
no way bound to the previous QuerySet. Each refinement creates a separate
80-
and distinct QuerySet that may be stored, used and reused.
57+
Each time you refine a QuerySet, you get a new QuerySet instance that is in no
58+
way bound to the previous QuerySet. Each refinement creates a separate and
59+
distinct QuerySet that may be stored, used and reused.
8160

8261
#### QuerySets are lazy
8362

@@ -88,17 +67,15 @@ QuerySet is *evaluated*.
8867
#### Ordering
8968

9069
You may order a QuerySet's results by using the `orderBy` function which
91-
accepts a closure passing the model type, and expects a sort descriptor in
92-
return.
70+
accepts a KeyPath.
9371

9472
```swift
95-
queryset.orderBy { $0.name.ascending() }
73+
queryset.orderBy(\.name, ascending: true)
9674
```
9775

9876
You may also pass in an `NSSortDescriptor` if you would rather.
9977

10078
```swift
101-
queryset.orderBy(Person.name.ascending())
10279
queryset.orderBy(NSSortDescriptor(key: "name", ascending: true))
10380
```
10481

@@ -108,7 +85,7 @@ Using slicing, a QuerySet's results may be limited to a specified range. For
10885
example, to get the first 5 items in our QuerySet:
10986

11087
```swift
111-
queryset[0..5]
88+
queryset[0...5]
11289
```
11390

11491
**NOTE**: *Remember, QuerySets are lazily evaluated. Slicing doesn’t evaluate the query.*
@@ -158,35 +135,26 @@ count or an error if the operation failed.
158135
let deleted = try? queryset.delete()
159136
```
160137

161-
#### Attribute
162-
163-
The `Attribute` is a generic structure for creating predicates in a
164-
type-safe manner as shown at the start of the README.
165-
166-
```swift
167-
let name = Attribute<String>("name")
168-
let age = Attribute<Int>("age")
169-
```
170-
171138
##### Operators
172139

173-
QueryKit provides custom operator functions allowing you to create predicates.
140+
QueryKit provides KeyPath extensions providing operator functions allowing you
141+
to create predicates.
174142

175143
```swift
176144
// Name is equal to Kyle
177-
name == "Kyle"
145+
\Person.name == "Kyle"
178146

179147
// Name is either equal to Kyle or Katie
180-
name << ["Kyle", "Katie"]
148+
\.Person.name << ["Kyle", "Katie"]
181149

182150
// Age is equal to 27
183-
age == 27
151+
\.Person.age == 27
184152

185153
// Age is more than or equal to 25
186-
age >= 25
154+
\Person.age >= 25
187155

188156
// Age is within the range 22 to 30.
189-
age << (22...30)
157+
\Person.age << (22...30)
190158
```
191159

192160
The following types of comparisons are supported using Attribute:
@@ -210,13 +178,13 @@ QueryKit provides the `!`, `&&` and `||` operators for joining multiple predicat
210178

211179
```swift
212180
// Persons name is Kyle or Katie
213-
Person.name == "Kyle" || Person.name == "Katie"
181+
\Person.name == "Kyle" || \Person.name == "Katie"
214182

215183
// Persons age is more than 25 and their name is Kyle
216-
Person.age >= 25 && Person.name == "Kyle"
184+
\Person.age >= 25 && \Person.name == "Kyle"
217185

218186
// Persons name is not Kyle
219-
!(Person.name == "Kyle")
187+
!(\Person.name == "Kyle")
220188
```
221189

222190
## Installation

0 commit comments

Comments
 (0)