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
Is there a reason codegen returns private structs over exported or interfaces? If these methods could return an interface and or in some instances a generic interface, like the one below, it could open up some cool opportunities.
// prisma-client-go // GENERATED FILE// generic transaction interfacetypeTx[Tany] interface {
ExtractQuery() builder.QueryIsTx()
Result() T
}
// example of a model returning the Tx interfacefunc (ractivityCreateOne) Tx() Tx[*ActivityModel] {
v:=NewactivityUniqueTxResult()
v.query=r.queryv.query.TxResult=make(chan []byte, 1)
returnv
}
By returning an interface specifically on Tx() we can create our own wrapping interface that enables "query builder" methods.
funcDoSomethingTheNeedsTransactions(ctx context.Context, db*db.DB, inputInput) (*prisma.ActivityModel, error) {
act, _:=CreateActivityTx(ctx, db, input)
group:=buildGroupQuery(db, "some name").Tx()
iferr:=db.Prisma.Transaction(act, group).Exec(ctx); err!=nil {
returnnil, err
}
returnact.Result(), nil
}
// these same query builders can then be used in a mocks expectationmock.Activity.Expect(
BuildActivityQuery(client, Input{}),
).Returns(expected)
The text was updated successfully, but these errors were encountered:
That's an interesting suggestion. Do you actually have the need in your app to use this?
The reason most of the methods are private is that most users don't need it, and I wanted to really think about the naming and so forth before making these public.
I prepared one small PR to make the transaction results public and it also renames the transaction interface to a better name: #1180.
When I have some more spare time, I might take another look and work on a bigger PR to make this more unified, maybe with generics where it works or makes sense. Thanks for the suggestion and the example code, that's really helpful!
Is there a reason codegen returns private structs over exported or interfaces? If these methods could return an interface and or in some instances a generic interface, like the one below, it could open up some cool opportunities.
By returning an interface specifically on
Tx()
we can create our own wrapping interface that enables "query builder" methods.The text was updated successfully, but these errors were encountered: