Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need examples #513

Open
NikKovIos opened this issue Oct 4, 2024 · 0 comments
Open

Need examples #513

NikKovIos opened this issue Oct 4, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@NikKovIos
Copy link

NikKovIos commented Oct 4, 2024

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.

@NikKovIos NikKovIos added the enhancement New feature or request label Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant