Open lukehutch opened 1 year ago
An alternative syntax could be (await session.db.query('SELECT * FROM profiles WHERE ...')).deserialize([typeColumn1, typeColumn2, ...],serializationManager)
.
I'd allow selecting only some columns, using GROUP BY
and joins.
With dart3 it might even be possible to further enhance it to List<(ColumnType1, ColumnType2)> result = (await session.db.query('SELECT * FROM profiles WHERE ...')).deserialize<(ColumnType1, ColumnType2)>(serializationManager)
.
Untested implementation using an extension:
/// Licensed by "The Unlicense" (https://choosealicense.com/licenses/unlicense/)
extension on List<List<dynamic>> {
List<List<dynamic>> deserialize(
List<Type> types, SerializationManager serializationManager) {
return map(
(row) => List.generate(
types.length,
(index) => serializationManager.deserialize(row[index], types[index]),
),
).toList();
}
}
edit: Use something like this to acquire nullable types.
Type getType<T>() => T;
var type = getType<int?>();
Is your feature request related to a problem? Please describe.
Currently raw queries in Serverpod take the form
and return type
List<List<dynamic>>
of rows and columns.I need to execute a query that is a bit too exotic for the expression builder syntax (specifically I need to use
SELECT * FROM profiles WHERE name NOT IN (SELECT name FROM blockednames)
).However, I want to automatically deserialize the results to type
Profile
(the type serialized to theprofiles
table), rather than be given a simple row/column result set.As far as I can see, there's no way to do this right now. Could a mechanism be added for this? e.g.