-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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: refactor filters into standalone package #6182
Merged
achrinza
merged 1 commit into
loopbackio:master
from
achrinzafork:feat/filter-standalone
Sep 4, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Empty file.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
package-lock=true | ||
scripts-prepend-node-path=true |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Copyright (c) IBM Corp. 2020. | ||
Node module: @loopback/filter | ||
This project is licensed under the MIT License, full text below. | ||
|
||
-------- | ||
|
||
MIT License | ||
|
||
MIT License Copyright (c) IBM Corp. 2020 | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice (including the next | ||
paragraph) shall be included in all copies or substantial portions of the | ||
Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
# @loopback/filter | ||
|
||
A set of utility typings and filter builders to aid in constructing LoopBack | ||
filters using the | ||
[builder pattern](https://en.wikipedia.org/wiki/Builder_pattern). | ||
|
||
## Overview | ||
|
||
This lightweight module provides strongly-typed typings and filter builders to | ||
intuitively construct LoopBack filters to be used against querying a LoopBack | ||
filter-compatible server over the network or within the server itself. | ||
|
||
## Installation | ||
|
||
{% include note.html content="Already installed `@loopback/repository`? Import from there instead. This package is meant for standalone use without `@loopback/repository`." %} | ||
|
||
```sh | ||
npm install --save @loopback/filter | ||
``` | ||
|
||
## Basic use | ||
|
||
### WhereBuilder | ||
|
||
The `WhereBuilder` is a builder specifically meant to construct the `where` | ||
portion of the filter. | ||
|
||
```ts | ||
import {WhereBuilder} from '@loopback/filter'; | ||
|
||
const whereBuilder = new WhereBuilder(); | ||
const where = whereBuilder | ||
.between('price', 99, 299) | ||
.and({brand: 'LoopBack'}, {discount: {lt: 20}}) | ||
.or({instock: true}) | ||
.build(); | ||
``` | ||
|
||
### FilterBuilder | ||
|
||
The `FilterBuilder` is a builder to construct the filter as a whole. | ||
|
||
```ts | ||
import {FilterBuilder} from '@loopback/filter'; | ||
|
||
const filterBuilder = new FilterBuilder(); | ||
const filter = filterBuilder | ||
.fields('id', 'a', 'b') | ||
.limit(10) | ||
.offset(0) | ||
.order(['a ASC', 'b DESC']) | ||
.where({id: 1}) | ||
.build(); | ||
``` | ||
|
||
`FilterBuilder.where()` also accepts an instance of `WhereBuilder`. | ||
|
||
```ts | ||
const filterBuilder = new FilterBuilder(); | ||
const filter = filterBuilder | ||
// ... | ||
.where(where) | ||
.build(); | ||
``` | ||
|
||
## Advanced use | ||
|
||
### Strong typings | ||
|
||
The `FilterBuilder` and `WhereBuilder` accept a model or any string-based key | ||
objects' typing for strong typings: | ||
|
||
```ts | ||
/** | ||
* Everything was imported from `@loopback/repository` as `@loopback/filter` | ||
* is re-exported in that package. | ||
**/ | ||
import { | ||
FilterBuilder, | ||
model, | ||
property, | ||
WhereBuilder, | ||
} from '@loopback/repository'; | ||
|
||
@model() | ||
class Todo extends Entity { | ||
@property({id: true}) | ||
id: number; | ||
|
||
@property() | ||
title: string; | ||
|
||
@property() | ||
description: string; | ||
|
||
@property() | ||
priority: number; | ||
} | ||
|
||
const whereBuilder = new WhereBuilder<Todo>(); | ||
const where = whereBuilder.between('priority', 1, 3).build(); | ||
|
||
const filterBuilder = new FilterBuilder<Todo>(); | ||
const filter = filterBuilder | ||
.fields('id', 'title') | ||
.order(['title DESC']) | ||
.where(where) | ||
.build(); | ||
``` | ||
|
||
## API docs | ||
|
||
See the API docs for | ||
[FilterBuilder](https://loopback.io/doc/en/lb4/apidocs.filter.filterbuilder.html) | ||
and | ||
[WhereBuilder](https://loopback.io/doc/en/lb4/apidocs.filter.wherebuilder.html) | ||
for more details on the full API. | ||
|
||
## Contributions | ||
|
||
- [Guidelines](https://github.com/strongloop/loopback-next/blob/master/docs/CONTRIBUTING.md) | ||
- [Join the team](https://github.com/strongloop/loopback-next/issues/110) | ||
|
||
## Tests | ||
|
||
Run `npm test` from the root folder. | ||
|
||
## Contributors | ||
|
||
See | ||
[all contributors](https://github.com/strongloop/loopback-next/graphs/contributors). | ||
|
||
## License | ||
|
||
MIT |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"name": "@loopback/filter", | ||
"version": "1.0.0", | ||
"description": "Utility typings and filters for LoopBack filters.", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"engines": { | ||
"node": ">=10.16" | ||
}, | ||
"scripts": { | ||
"build": "lb-tsc", | ||
"build:watch": "lb-tsc --watch", | ||
"pretest": "npm run clean && npm run build", | ||
"test": "lb-mocha \"dist/__tests__/**/*.js\"", | ||
"clean": "lb-clean dist *.tsbuildinfo .eslintcache" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/strongloop/loopback-next.git", | ||
"directory": "packages/filter" | ||
}, | ||
"author": "IBM Corp.", | ||
"license": "MIT", | ||
"files": [ | ||
"README.md", | ||
"dist", | ||
"src", | ||
"!*/__tests__" | ||
], | ||
"dependencies": { | ||
"tslib": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"@loopback/build": "^6.2.1", | ||
"@loopback/testlab": "^3.2.3", | ||
"@types/node": "^10.17.28", | ||
"typescript": "~4.0.2" | ||
}, | ||
"copyright.owner": "IBM Corp.", | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
8 changes: 4 additions & 4 deletions
8
..._tests__/unit/query/query-builder.unit.ts → ...__/acceptance/query-builder.acceptance.ts
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright IBM Corp. 2020. All Rights Reserved. | ||
// Node module: @loopback/filter | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
/** | ||
* A set of utility typings and filter builders to aid in constructing LoopBack | ||
* filters using the | ||
* {@link https://en.wikipedia.org/wiki/Builder_pattern | builder pattern}. | ||
* | ||
* @remarks | ||
* This lightweight module provides strongly-typed typings and filter builders | ||
* to intuitively construct LoopBack filters to be used against querying a | ||
* LoopBack filter-compatible server over the network or within the server | ||
* itself. | ||
* | ||
* @packageDocumentation | ||
*/ | ||
|
||
export * from './query'; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright IBM Corp. 2020. All Rights Reserved. | ||
// Node module: @loopback/filter | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
/** | ||
* Objects with open properties | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type AnyObject = Record<string, any>; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/tsconfig", | ||
"extends": "@loopback/build/config/tsconfig.common.json", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"rootDir": "src", | ||
"composite": true | ||
}, | ||
"include": [ | ||
"src" | ||
], | ||
"references": [ | ||
{ | ||
"path": "../core/tsconfig.json" | ||
}, | ||
{ | ||
"path": "../testlab/tsconfig.json" | ||
} | ||
] | ||
} |
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which name is better?
@loopback/filter
@loopback/query-filter
@loopback/repository-filter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer
@loopback/filter
as the filter is use both when working with queries and repositories. Prefixing it withquery-
orrepository-
may create confusion that the filter only applies when working with either one of those concepts.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good discussion!
I vote for
@loopback/filter
.