Skip to content

lovetodream/postgres-nio-macros

Repository files navigation

PostgresNIOMacros

codecov

Macros for PostgresNIO.

@Statement(_:)1

Creates a PostgresPreparedStatement from a decorated query string.

import PostgresNIO
import PostgresNIOMacros

@Statement("SELECT \("id", UUID.self), \("name", String.self), \("age", Int.self) FROM users WHERE \(bind: "age", Int.self) < age")
struct UsersStatement {}

let connection: PostgresConnection = ...
let stream = try await connection.execute(UsersStatement(age: 18), logger: ...)
for try await user in stream {
    print(user.id, user.name, user.age)
}
Expanded source code.
struct UsersStatement {
    struct Row {
        var id: UUID
        var name: String
        var age: Int
    }

    static let sql = "SELECT id, name, age FROM users WHERE $1 < age"
    
    var age: Int

    func makeBindings() throws -> PostgresBindings {
        var bindings = PostgresBindings(capacity: 1)
        bindings.append(age)
        return bindings
    }

    func decodeRow(_ row: PostgresRow) throws -> Row {
        let (id, name, age) = try row.decode((UUID, String, Int).self)
        return Row(id: id, name: name, age: age)
    }
}
extension UsersStatement: PostgresPreparedStatement {}

Footnotes

  1. This macro was initially pitched to me by @fabianfett.

About

Macros for PostgresNIO to make your live easier.

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published