avram icon indicating copy to clipboard operation
avram copied to clipboard

Clean up crystal-db proxy methods with correct usage

Open jwoertink opened this issue 3 years ago • 1 comments

https://github.com/luckyframework/avram/blob/519213e5c5f6a7a61766d16e815a2f6220500cae/src/avram/database.cr#L87-L123

Using these low-level crystal-db methods give a false sense of unified code in how they work. For example, it looks like you can do this, but you can't...

# assume this returns a single SpecialModel
AppDatabase.query(sql, args: [1], as: SpecialModel)

# assume this returns an Array(SpecialModel)
AppDatabase.query_all(sql, args: [1], as: SpecialModel)

The issue here is that query actually returns a ResultSet which could either be a single record or an array of records... There's no as: named arg for query.

Ref: https://github.com/will/crystal-pg/issues/202

The reason we map these though is so we can properly log them through Avram (and Dexter). I'm sure there's other wrong uses in here, but they should all be cleaned up.

jwoertink avatar Jan 25 '22 19:01 jwoertink

Important thing to point out is that this code also allows us to maintain database connections and transactions.

matthewmcgarvey avatar Jan 25 '22 20:01 matthewmcgarvey