Open Newcore-mobile opened 4 years ago
Unfortunately the go client does not support that yet, you'll have to use a raw query instead until we add this feature to the go client.
All right, thank you 💯
One thing I'd love to see is a count of relations.
posts, err := d.db.Post.
FindMany(filters...).
With(
db.Post.Author.Fetch(),
db.Post.Tags.Fetch(),
db.Post.Category.Fetch(),
db.Post.Comments.Fetch().Count(), // Outputs a count of items in the Posts[] relation
).
Take(max).
OrderBy(db.Post.CreatedAt.Order(db.Direction(sort))).
Exec(ctx)
// posts.CommentsCount or something would now contain a the number of comments linked with each post
That's a good point. I'll bump this internally.
This is really needed as its badly affecting DX that you are required to build up a raw query parallel to normal ones. I provide an example where you would have to do some shady query building by concating a chain of OR-s with the fields to be able to have a count for the full-text search result.
var params []db.UserWhereParam
// Apply full-text search
if search != nil {
params = append(params, db.User.ID.Contains(*search.Search))
params = append(params, db.User.StripeCustomerID.Contains(*search.Search))
params = append(params, db.User.Email.Contains(*search.Search))
params = append(params, db.User.Organization.Contains(*search.Search))
}
query := u.dbClient.User.FindMany(params...)
users, err := query.Exec(context.Background())
if err != nil {
nil, err
}
One would need to create a variable, and concat it with a lot of OR strings. Instead of this it would be lovely to have something simple like:
//...all the query building skipped
users, err := query.WithCount().Exec(context.Background())
if err != nil {
nil, err
}
I saw the official doc: https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/crud#count have
count()
definition. But not found in the prisma-client-go.