Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include filter hasMany but exclude if relation empty #4995

Closed
fikarisasi opened this issue Mar 30, 2020 · 7 comments
Closed

Include filter hasMany but exclude if relation empty #4995

fikarisasi opened this issue Mar 30, 2020 · 7 comments
Labels
feature Repository Issues related to @loopback/repository package stale

Comments

@fikarisasi
Copy link

I have a /customers controller and get their data using this filter:

{
	"offset": 0,
	"limit": 10,
	"include": [{
		"relation": "demands",
		"scope": {
			"include": [{
				"relation": "item"
			}]
		}
	}]
}

the result is:

  [{
  	"name": "John",
  	"demands": [{
  		"qty": 10,
  		"item_id": 1,
  		"customer_id": 1,
  		"item": {
  			"id": 1,
  			"name": "Pasta"
  		}
  	}]
  }, {
  	"name": "Rachel"
  }]

I want the result that excluding Rachel because she doesn't have demands, I tried this but the result is still the same:

{
	"offset": 0,
	"limit": 10,
	"where": {
		"demands": {
			"neq": null
		}
	},
	"include": [{
		"relation": "demands",
		"scope": {
			"include": [{
				"relation": "item"
			}]
		}
	}]
}

any idea how can I achieve this?

  [{
  	"name": "John",
  	"demands": [{
  		"qty": 10,
  		"item_id": 1,
  		"customer_id": 1,
  		"item": {
  			"id": 1,
  			"name": "Pasta"
  		}
  	}]
  }]
@fikarisasi fikarisasi added the bug label Mar 30, 2020
@emonddr
Copy link
Contributor

emonddr commented Mar 30, 2020

@fikarisasi , at the moment we don't support the filter you have specified above. Please read https://loopback.io/doc/en/lb4/HasMany-relation.html#querying-related-models to view the queries we do support.

In the meantime, as a workaround, in the controller method, you can always analyze the result returned from the repository, strip out the data you don't want (result objects without demands property, and return this modified result set from the controller method.

@achrinza
Copy link
Member

Loosely-related #4299

@achrinza achrinza added feature Repository Issues related to @loopback/repository package and removed bug labels Mar 31, 2020
@fikarisasi
Copy link
Author

fikarisasi commented Mar 31, 2020

@fikarisasi , at the moment we don't support the filter you have specified above. Please read https://loopback.io/doc/en/lb4/HasMany-relation.html#querying-related-models to view the queries we do support.

In the meantime, as a workaround, in the controller method, you can always analyze the result returned from the repository, strip out the data you don't want (result objects without demands property, and return this modified result set from the controller method.

@emonddr any idea how to strip out the data with the correct limit filter? I already tried to strip out the data but it doesn't return the correct limit filter. For example, if I limit the data to 10, and result objects with demands property are only 2, it only shows 2 result objects instead of showing 10 data. This is wrong since I use skip and limit filter for pagination in the client-side.

@RuslanUshakov
Copy link

Any updates?

@RuslanUshakov
Copy link

RuslanUshakov commented Jun 15, 2020

@emonddr If you try to filter data by related models and use limit it does not work properly.
For instance here is the filter:
{ "limit": 5, "offset": 0, "where": {}, "order": [ "id asc" ], "include": [ { "relation": "site", "scope": { "where": { "name": { "like": "%mysite%" } } } } ] }

mysite is the 7th item. But i cannot get it since i have limit 5. I just receive first 5 items without site relation.

@stale
Copy link

stale bot commented Dec 25, 2020

This issue has been marked stale because it has not seen activity within six months. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository. This issue will be closed within 30 days of being stale.

@stale stale bot added the stale label Dec 25, 2020
@stale
Copy link

stale bot commented Jul 14, 2021

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

@stale stale bot closed this as completed Jul 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Repository Issues related to @loopback/repository package stale
Projects
None yet
Development

No branches or pull requests

4 participants