You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the example above, since we selected all columns from a single table the query will return the `User` struct that was generated for the table. If additional columns are selected a new structure will be generated to match the output. In the following example we will join in the `post` table to get a users post count.
189
+
In the example above, since we selected all columns from a single table the query will return the `User` struct that was generated for the table. If additional columns are selected a new structure will be generated to match the selected columns. In the following example we will join in the `post` table to get a users post count.
188
190
```sql
189
191
DEFINE QUERY fetchUsers AS
190
192
SELECT user.*, COUNT(post.*) AS numberOfPosts
191
193
OUTER JOIN post ONpost.userId=user.id
192
194
GROUP BYuser.id;
193
195
```
194
196
195
-
The following `struct` would automatically be generated for the query. Since we used the syntax `user.*` it will embed the `User` struct instead of replicating it's columns. Any embeded table struct will also get a `@dynamicMemberLookup` method generated so it can be accessed directly like the other column values.
197
+
The following `struct` would automatically be generated for the query. Since we used the syntax `user.*` it will embed the `User` struct instead of replicating it's columns. Any embeded table struct will also get a `@dynamicMemberLookup` method generated so it can be accessed directly like the other column values. This allows extensions on the table struct to work across many queries.
196
198
```swift
197
199
@dynamicMemberLookup
198
200
FetchUsersOutput {
@@ -219,7 +221,11 @@ struct UserPostsInput {
219
221
let dateUpper: Date
220
222
}
221
223
224
+
// Using the extension
222
225
let posts =tryawait database.userQueries.userPosts.execute(id: id, dateLower: lower, dateUpper: upper)
226
+
227
+
// Or using the input type directly
228
+
let posts =tryawait database.userQueries.userPosts.execute(with: UserPostInput(...))
SQLite is a unique SQL database engine in that it is fairly lawless when it comes to typing. SQLite will allow you create a column with an `INTEGER` and gladly insert a `TEXT` into it. It will even let you make up your own type names and will take them. Otter will not allow this and tends to operate more strictly like the table option `STRICT`. Only the core types that SQLite recognizes are usable for the column type.
238
+
SQLite is a unique SQL database engine in that it is fairly lawless when it comes to typing. SQLite will allow you create a column with an `INTEGER` and gladly insert a `TEXT` into it. It will even let you make up your own type names and it will take them. Otter will not allow this and tends to operate more strictly like the table option `STRICT`. Only the core types that SQLite recognizes are usable for the column type.
233
239
| SQLite | Swift |
234
240
|---------|--------|
235
241
| INTEGER | Int |
@@ -238,7 +244,7 @@ SQLite is a unique SQL database engine in that it is fairly lawless when it come
238
244
| BLOB | Data |
239
245
| ANY | SQLAny |
240
246
241
-
####Custom Types
247
+
### Custom Types
242
248
While your column only can be one of the core SQLite types, what type that ends up as in Swift can be different. Using the `AS` keyword you can specify the Swift type to decode it to. Think of the column type as the storage type while the type in the `AS` will be the type actually in the interface.
243
249
244
250
Using the `AS` keyword you can specify the type to use in `Swift`
@@ -265,7 +271,7 @@ func then<Next>(
265
271
```
266
272
267
273
## Dependency Injection
268
-
> TLDR; Avoid the repository pattern, inject queries.
274
+
> TL;DR Avoid the repository pattern, inject queries.
269
275
270
276
Otter was written with application development in mind. One of the common walls when talking to a database is dependecy injection.
271
277
Normally this would mean wrapping your database calls in a repository or some other layer to keep the model layer testable without needing a database connection.
0 commit comments