Return wanted type from transaction
jonasborggren opened this issue · 0 comments
jonasborggren commented
The ability to get the object you return from the queryBlock
function would be nice. For my REST API I want to return a object using the id of a insert query. I want to use a transaction to make sure all the queries work correctly, however the transaction
function lacks any return type, and it returns the error only if there is one.
Existing code:
mysql1_dart/lib/src/single_connection.dart
Lines 209 to 221 in 8c65ca5
Suggested code:
Future<T?> transaction<T>(
Future<T> Function(TransactionContext) queryBlock, {
Function(Object)? onError,
}) async {
await query('start transaction');
try {
// Run the query block
final result = await queryBlock(TransactionContext._(this));
// Neat, we made it here! Commit the changes!
await query('commit');
// Return the result of the query block
return result;
} catch (e) {
// Run rollback!
await query('rollback');
if (e is! _RollbackError) {
rethrow;
}
// Tell the client about the error
onError?.call(e);
return null;
}
}