You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First of all, thanks again so much for creating this amazing library!
I'm running into an issue with the client.$transaction(...) API which makes it tricky to build an abstraction layer around the raw PrismaClient itself. Consider a method that attempts to create a new user after validation.
classDbAbstraction {
DbAbstraction() : _client =PrismaClient(...);
finalPrismaClient _client;
Future<User?> createUser({requiredString email, ...}) async {
return _client.$transaction((transaction) async {
final user = transaction.user.findFirst(email: ...);
if (user !=null) {
// Cannot create user if email already existsreturnnull;
}
// more checks?return transaction.user.create(...);
});
}
}
Now consider surrounding code which attempts to use this.
import'package:dartz/dartz.dart';
classAuthService {
AuthService(DbAbstraction db) : _db = db;
finalDbAbstraction _db;
Future<Either<AuthError, User>> createUser({requiredString email, ....}) async {
finalUser? user =await _db.createUser(email: email, ...);
if (user ==null) {
// which check failed? what went wrong? we don't know!
}
returnRight(user);
}
}
--
The issue here stems from the fact that the PrismaClient.$transaction method accepts a callback which itself accepts another PrismaClient - meaning nothing one layer higher than the PrismaClient can be aware of transactions without also being aware of the implementation details of the layer below it - aka, the PrismaClient.
Alternatively, if the PrismaClient exposed startTransaction, commitTransaction, and rollbackTransaction, then all of the methods in DbAbstraction could be dead simple - one query each - and the abstraction layer above it (AuthService), could decide when to start a transaction, confirm a new user's email address is available, create them, and commit the transaction all on its own - without ever seeing implementation details of the PrismaClient.
First of all, thanks again so much for creating this amazing library!
I'm running into an issue with the
client.$transaction(...)
API which makes it tricky to build an abstraction layer around the rawPrismaClient
itself. Consider a method that attempts to create a new user after validation.Now consider surrounding code which attempts to use this.
--
The issue here stems from the fact that the
PrismaClient.$transaction
method accepts a callback which itself accepts anotherPrismaClient
- meaning nothing one layer higher than thePrismaClient
can be aware of transactions without also being aware of the implementation details of the layer below it - aka, thePrismaClient
.Alternatively, if the
PrismaClient
exposedstartTransaction
,commitTransaction
, androllbackTransaction
, then all of the methods inDbAbstraction
could be dead simple - one query each - and the abstraction layer above it (AuthService
), could decide when to start a transaction, confirm a new user's email address is available, create them, and commit the transaction all on its own - without ever seeing implementation details of thePrismaClient
.It could look something like this:
The text was updated successfully, but these errors were encountered: