Skip to content

Commit

Permalink
Update documentatiion for #577
Browse files Browse the repository at this point in the history
  • Loading branch information
groue committed Jul 27, 2019
1 parent 2996a24 commit 23678ab
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
18 changes: 9 additions & 9 deletions Documentation/AssociationsBasics.md
Original file line number Diff line number Diff line change
Expand Up @@ -1791,30 +1791,30 @@ The default name is built from the aggregating method, the **[association key](#

Those default names are lost whenever an aggregate is modified (negated, added, multiplied, whatever).

You can name or rename aggregates with the `aliased` method:
You can name or rename aggregates with the `forKey` method:

```swift
struct AuthorInfo: Decodable, FetchableRecord {
var author: Author
var numberOfBooks: Int
}
let numberOfBooks = Author.books.count.aliased("numberOfBooks") // <--
let numberOfBooks = Author.books.count.forKey("numberOfBooks") // <--
let request = Author.annotated(with: numberOfBooks)
let authorInfos: [AuthorInfo] = try AuthorInfo.fetchAll(db, request)

struct AuthorInfo: Decodable, FetchableRecord {
var author: Author
var hasBooks: Bool
}
let hasBooks = (Author.books.isEmpty == false).aliased("hasBooks") // <--
let hasBooks = (Author.books.isEmpty == false).forKey("hasBooks") // <--
let request = Author.annotated(with: hasBooks)
let authorInfos: [AuthorInfo] = try AuthorInfo.fetchAll(db, request)

struct AuthorInfo: Decodable, FetchableRecord {
var author: Author
var workCount: Int
}
let workCount = (Author.books.count + Author.paintings.count).aliased("workCount") // <--
let workCount = (Author.books.count + Author.paintings.count).forKey("workCount") // <--
let request = Author.annotated(with: workCount)
let authorInfos: [AuthorInfo] = try AuthorInfo.fetchAll(db, request)
```
Expand All @@ -1827,7 +1827,7 @@ struct AuthorInfo: Decodable, FetchableRecord {
var numberOfBooks: Int

static func all() -> QueryInterfaceRequest<AuthorInfo> {
let numberOfBooks = Author.books.count.aliased(CodingKey.numberOfBooks) // <--
let numberOfBooks = Author.books.count.forKey(CodingKey.numberOfBooks) // <--
return Author
.annotated(with: numberOfBooks)
.asRequest(of: AuthorInfo.self)
Expand Down Expand Up @@ -2041,7 +2041,7 @@ Aggregates can be modified and combined with Swift operators:

```swift
let workCount = Author.books.count + Author.paintings.count)
let request = Author.annotated(with: workCount.aliased("workCount"))
let request = Author.annotated(with: workCount.forKey("workCount"))
```

- IFNULL operator `??`
Expand Down Expand Up @@ -2133,7 +2133,7 @@ In this other example, the `Author.books` and `Author.paintings` have the distin

let aggregate = Author.books.count + // association key "books"
Author.paintings.count // association key "paintings"
let request = Author.annotated(with: aggregate.aliased("workCount"))
let request = Author.annotated(with: aggregate.forKey("workCount"))
let authorInfos: [AuthorInfo] = try AuthorInfo.fetchAll(db, request)
```

Expand Down Expand Up @@ -2201,11 +2201,11 @@ But in the following example, we use the same association `Author.books` twice,
let novelCount = Author.books // association key "books"
.filter(Column("kind") == "novel")
.count
.aliased("novelCount")
.forKey("novelCount")
let theatrePlayCount = Author.books // association key "books"
.filter(Column("kind") == "theatrePlay")
.count
.aliased("theatrePlayCount")
.forKey("theatrePlayCount")
let request = Author.annotated(with: novelCount, theatrePlayCount)
let authorInfos: [AuthorInfo] = try AuthorInfo.fetchAll(db, request)
```
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4275,7 +4275,7 @@ Feed [requests](#requests) with SQL expressions built from your Swift code:

```swift
// SELECT ((temperature * 1.8) + 32) AS farenheit FROM planet
Planet.select((temperatureColumn * 1.8 + 32).aliased("farenheit"))
Planet.select((temperatureColumn * 1.8 + 32).forKey("farenheit"))
```

> :point_up: **Note**: an expression like `nameColumn + "rrr"` will be interpreted by SQLite as a numerical addition (with funny results), not as a string concatenation.
Expand Down

0 comments on commit 23678ab

Please sign in to comment.