File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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.
You can’t perform that action at this time.
0 commit comments