Comments (2)
any updates on this? This would be a great feature
from loopback-counts-mixin.
@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)
- This is working with relationships like HasAndBelongsToMany HOT 1
- add support for filter in headers
- HasManyThrough relation - Error: Invalid relation type: undefined HOT 1
- how can i get two models counts ? HOT 2
- Returns 500 instead of 404 when parent object does not exist
- Counts Not working in Related Query HOT 6
- Can I use it in nodejs query?
- It will returns 500, if the main entity is not find.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from loopback-counts-mixin.