Skip to content

Commit 04395b1

Browse files
authored
Update README.md
1 parent fa0494d commit 04395b1

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,39 @@ class ViewModel {
8686
let query: any LatestExpensesQuery
8787
}
8888
```
89+
90+
## Swift Macros
91+
> TLDR; Don't use for larger projects ⚠️
92+
93+
Otter can even run within a Swift macro by adding the `@Database` macro to a `struct`. As of now it is not recommended for larger projects.
94+
There are quite a few limitations that won't scale well beyond a fairly simple schema and a handfull of queries.
95+
96+
```swift
97+
@Database
98+
struct DB {
99+
@Query("SELECT * FROM foo")
100+
var selectFooQuery: SelectFooDatabaseQuery
101+
102+
@Query("INSERT INTO foo (bar, baz) VALUES (?, ?)", inputName: "FooInput")
103+
var insertFooQuery: InsertFooDatabaseQuery
104+
105+
static var migrations: [String] {
106+
return [
107+
"CREATE TABLE foo (bar INTEGER, baz TEXT);"
108+
]
109+
}
110+
}
111+
112+
func main() async throws {
113+
let database = try DB.inMemory()
114+
try await database.insertFooQuery.execute(with: .init(bar: 1, baz: "Baz"))
115+
let foos = try await database.selectFooQuery.execute()
116+
print(foos)
117+
}
118+
```
119+
120+
### Current Limitations
121+
* Since macros operate purely on the syntax, all queries must be within the `@Database` itself so the schema can be inferred properly.
122+
* All generated types will be nested under the `@Database` struct.
123+
* All `@Query` definitions must define their type as the generated `typealias` by the `@Database` macro.
124+
* Any diagnostics will be on the entire string rather than the part that actually failed.

0 commit comments

Comments
 (0)