Git Product home page Git Product logo

Comments (2)

gijo-varghese avatar gijo-varghese commented on July 28, 2024

any updates on this? This would be a great feature

from loopback-counts-mixin.

arjamizo avatar arjamizo commented on July 28, 2024

@gijo-varghese @enceladus unfortunately this cannot be done as loopback-counts-mixin only iterates over existing relations (starting with has) and injects virtual field with it, there isn't even a straightforward way of conditional counting as resource[relation].count() does not have any placeholder for an argument.

The easiest way of achieving counting models would be doing this in 2 steps: first adding include filter field which fetches related models (note that they are provided as relations, in lb2 retrievable via getAsync() and in lb3 with .findAll(), and then, for every relation injected with include you have to call .count(filter) method with filter representing your conditions, consider

List all users from Poland having more than 10 books with title containing Tadeusz Chciuk

    const usersFromPoland = app.models.User.find({where: {"email": {like: "%.pl"}}, include: {"books": }}).then(users => Promise.all(users.map(u => u.books.count({where: {"title": {like: "%Tadeusz Chciuk%"}}}).then(books => {return {user: u, count: books.length}}))).then(usersWithBooks => userWithBooks.filter(uwb => uwb.count > 10))

assuming that there's another model Book and there's relation User<->Book

Another way could be introducing this plugin to relations interface or even Inclusion interface.

Have fun!

from loopback-counts-mixin.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.