Skip to content

Need examples #513

Open
Open
@NikKovIos

Description

@NikKovIos

Hello, thanks for the great framework!

When trying to use it there are a lot of questions and I thought that it would be great to make some examples of use. I watched https://github.com/vapor/postgres-nio/blob/main/Snippets/Birthdays.swift and it helps a lot, but there are still questions. Like:

  • how to map to swift struct models and back. How to use PostgresDecodable.
  • can I use code like next instead withTaskGroup:
        let runned = Task {
            await client.run()
        }
        // do sql actions
        runned.cancel()

For now I'm using

static func query<T: Codable>(_ closure: @escaping ((PostgresClient) async throws -> T)) async throws -> T {
        return try await withThrowingTaskGroup(of: Void.self) { taskGroup in
            defer {
                taskGroup.cancelAll()
            }
            taskGroup.addTask {
                await client.run()
            }

            let result: T = try await closure(client)

            return result
        }
    }
try await Database.query { client in
                let rows = try await client.query("""
                                                  SELECT * FROM "\(Constants.reviewersTableName)"
                                                  """
                )
}

And get an error on the second call
PostgresNIO/PostgresClient.swift:420: Precondition failed: PostgresClient.run() should just be called once!
What is wrong? I thought block in defer is closing connection.

It would be useful to have a guide.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions