fluent-postgres-driver icon indicating copy to clipboard operation
fluent-postgres-driver copied to clipboard

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

Open nikitakuzmin2017 opened this issue 2 years ago • 0 comments

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.

nikitakuzmin2017 avatar Jun 05 '23 20:06 nikitakuzmin2017