vapor / fluent-postgres-driver

🐘 PostgreSQL driver for Fluent.
MIT License
149 stars 53 forks source link

Select SQL query for the fluent-postgres-driver with bindings #214

Open nikitakuzmin2017 opened 1 year ago

nikitakuzmin2017 commented 1 year ago

Hello everyone! I do not understand how to put bindings properly in SQL query. Always I just get 500 error. I add them as 'PostgresData', then put after query SQL. It looks like this:

// PostgreSQL
        var returnValue: [Song] = []

        guard let postgres = req.db as? PostgresDatabase else {
            throw Abort(.internalServerError)
        }

        let id = PostgresData(int32: 289) // add binding

        do {
            let results = try await postgres.query("SELECT songs FROM bangs WHERE songDuration = $1", [id]).get() // add binding here as an array, '$1' inside the array

            for result in results {
                returnValue.append(try result.sql().decode(model: Song.self))
            }
        } catch {
            throw Abort(.accepted) // error here!
        }
        return returnValue

Could you please help me, I am struggling with this for one week looking for any documentation.