steebchen / prisma-client-go

Prisma Client Go is an auto-generated and fully type-safe database client
https://goprisma.org
Apache License 2.0
2.13k stars 95 forks source link

Is there a Count() function? #229

Open Newcore-mobile opened 4 years ago

Newcore-mobile commented 4 years ago

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.

steebchen commented 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.

Newcore-mobile commented 4 years ago

All right, thank you 💯

Southclaws commented 2 years ago

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
steebchen commented 2 years ago

That's a good point. I'll bump this internally.

BertoldVinczeIMC commented 4 months ago

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
}