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
feat(core): add support for filters/scopes #663
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Allows to define filters that will be automatically applied to the conditions. Filter can be defined at the entity level, dynamically via EM or in the ORM Configuration. ```typescript @entity() @filter({ name: 'expensive', cond: { price: { $gt: 1000 } } }) @filter({ name: 'long', cond: { 'length(text)': { $gt: 10000 } } }) @filter({ name: 'hasAuthor', cond: { author: { $ne: null } }, default: true }) @filter({ name: 'writtenBy', cond: args => ({ author: { name: args.name } }) }) export class Book { ... } const books1 = await orm.em.find(Book, {}, { filters: ['long', 'expensive'], }); const books2 = await orm.em.find(Book, {}, { filters: { hasAuthor: false, long: true, writtenBy: { name: 'God' } }, }); ``` EM filter API: ```typescript // bound to entity, enabled by default em.addFilter('writtenBy', args => ({ author: args.id }), Book); // global, enabled by default, for all entities em.addFilter('tenant', args => { ... }); // global, enabled by default, for only specified entities em.addFilter('tenant', args => { ... }, [Author, Book]); ... // set params (probably in some middleware) em.setFilterParams('tenant', { tenantId: 123 }); // setting global filter params ... // usage em.find(Book, {}); // same as `{ tenantId: 123 }` em.find(Book, {}, { filters: ['writtenBy'] }); // same as `{ author: 321, tenantId: 123 }` em.find(Book, {}, { filters: { tenant: false } }); // disabled tenant filter, so truly `{}` em.find(Book, {}, { filters: false }); // disabled all filters, so truly `{}` ``` Closes #385
B4nan
force-pushed
the
filters
branch
2 times, most recently
from
July 18, 2020 17:28
767a6fb
to
18de3ea
Compare
B4nan
added a commit
that referenced
this pull request
Aug 2, 2020
Allows to define filters that will be automatically applied to the conditions. Filter can be defined at the entity level, dynamically via EM or in the ORM configuration. ```typescript @entity() @filter({ name: 'expensive', cond: { price: { $gt: 1000 } } }) @filter({ name: 'long', cond: { 'length(text)': { $gt: 10000 } } }) @filter({ name: 'hasAuthor', cond: { author: { $ne: null } }, default: true }) @filter({ name: 'writtenBy', cond: args => ({ author: { name: args.name } }) }) export class Book { ... } const books1 = await orm.em.find(Book, {}, { filters: ['long', 'expensive'], }); const books2 = await orm.em.find(Book, {}, { filters: { hasAuthor: false, long: true, writtenBy: { name: 'God' } }, }); ``` EM filter API: ```typescript // bound to entity, enabled by default em.addFilter('writtenBy', args => ({ author: args.id }), Book); // global, enabled by default, for all entities em.addFilter('tenant', args => { ... }); // global, enabled by default, for only specified entities em.addFilter('tenant', args => { ... }, [Author, Book]); ... // set params (probably in some middleware) em.setFilterParams('tenant', { tenantId: 123 }); em.setFilterParams('writtenBy', { id: 321 }); ... // usage em.find(Book, {}); // same as `{ tenantId: 123 }` em.find(Book, {}, { filters: ['writtenBy'] }); // same as `{ author: 321, tenantId: 123 }` em.find(Book, {}, { filters: { tenant: false } }); // disabled tenant filter, so truly `{}` em.find(Book, {}, { filters: false }); // disabled all filters, so truly `{}` ``` Closes #385
B4nan
added a commit
that referenced
this pull request
Aug 9, 2020
Allows to define filters that will be automatically applied to the conditions. Filter can be defined at the entity level, dynamically via EM or in the ORM configuration. ```typescript @entity() @filter({ name: 'expensive', cond: { price: { $gt: 1000 } } }) @filter({ name: 'long', cond: { 'length(text)': { $gt: 10000 } } }) @filter({ name: 'hasAuthor', cond: { author: { $ne: null } }, default: true }) @filter({ name: 'writtenBy', cond: args => ({ author: { name: args.name } }) }) export class Book { ... } const books1 = await orm.em.find(Book, {}, { filters: ['long', 'expensive'], }); const books2 = await orm.em.find(Book, {}, { filters: { hasAuthor: false, long: true, writtenBy: { name: 'God' } }, }); ``` EM filter API: ```typescript // bound to entity, enabled by default em.addFilter('writtenBy', args => ({ author: args.id }), Book); // global, enabled by default, for all entities em.addFilter('tenant', args => { ... }); // global, enabled by default, for only specified entities em.addFilter('tenant', args => { ... }, [Author, Book]); ... // set params (probably in some middleware) em.setFilterParams('tenant', { tenantId: 123 }); em.setFilterParams('writtenBy', { id: 321 }); ... // usage em.find(Book, {}); // same as `{ tenantId: 123 }` em.find(Book, {}, { filters: ['writtenBy'] }); // same as `{ author: 321, tenantId: 123 }` em.find(Book, {}, { filters: { tenant: false } }); // disabled tenant filter, so truly `{}` em.find(Book, {}, { filters: false }); // disabled all filters, so truly `{}` ``` Closes #385
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Allows to define filters that will be automatically applied to the conditions.
Filter can be defined at the entity level, dynamically via EM or in the ORM
Configuration.
EM filter API:
Closes #385