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

feat: refactor filters into standalone package #6182

Merged
merged 1 commit into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
# - Primary owner(s): @agnes512
# - Standby owner(s): @hacksparrow
# Includes the following repos: loopback-datasource-juggler
/packages/filter @agnes512 @hacksparrow
/packages/repository @agnes512 @hacksparrow
/packages/repository-json-schema @agnes512 @hacksparrow
/packages/repository-tests @agnes512 @hacksparrow
Expand Down
Empty file added bin/update-contributors.js
Empty file.
1 change: 1 addition & 0 deletions docs/site/MONOREPO.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ one in the monorepo: `npm run update-monorepo-file`
| [packages/core](https://github.com/strongloop/loopback-next/tree/master/packages/core) | @loopback/core | Define and implement core constructs such as Application and Component |
| [packages/eslint-config](https://github.com/strongloop/loopback-next/tree/master/packages/eslint-config) | @loopback/eslint-config | ESLint configuration for LoopBack projects |
| [packages/express](https://github.com/strongloop/loopback-next/tree/master/packages/express) | @loopback/express | Integrate with Express and expose middleware infrastructure for sequence and interceptors |
| [packages/filter](https://github.com/strongloop/loopback-next/tree/master/packages/filter) | @loopback/filter | Utility typings and filters for LoopBack filters. |
| [packages/http-caching-proxy](https://github.com/strongloop/loopback-next/tree/master/packages/http-caching-proxy) | @loopback/http-caching-proxy | A caching HTTP proxy for integration tests. NOT SUITABLE FOR PRODUCTION USE! |
| [packages/http-server](https://github.com/strongloop/loopback-next/tree/master/packages/http-server) | @loopback/http-server | A wrapper for creating HTTP/HTTPS servers |
| [packages/metadata](https://github.com/strongloop/loopback-next/tree/master/packages/metadata) | @loopback/metadata | Utilities to help developers implement TypeScript decorators, define/merge metadata, and inspect metadata |
Expand Down
2 changes: 2 additions & 0 deletions packages/filter/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package-lock=true
scripts-prepend-node-path=true
27 changes: 27 additions & 0 deletions packages/filter/LICENSE
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.
135 changes: 135 additions & 0 deletions packages/filter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# @loopback/filter
Copy link
Contributor

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

Copy link
Member Author

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 with query- or repository- may create confusion that the filter only applies when working with either one of those concepts.

Copy link
Member

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.


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
25 changes: 25 additions & 0 deletions packages/filter/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions packages/filter/package.json
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"
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// Copyright IBM Corp. 2019,2020. All Rights Reserved.
// Node module: @loopback/repository
// 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

import {expect} from '@loopback/testlab';
import {
AnyObject,
FilterBuilder,
filterTemplate,
isFilter,
KeyOf,
Where,
WhereBuilder,
} from '../../../';
} from '../..';
import {AnyObject} from '../../types';

describe('WhereBuilder', () => {
it('builds where object', () => {
Expand Down
20 changes: 20 additions & 0 deletions packages/filter/src/index.ts
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';
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright IBM Corp. 2017,2020. All Rights Reserved.
// Node module: @loopback/repository
// 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

import assert from 'assert';
import {AnyObject} from './common-types';
import {AnyObject} from './types';

/* eslint-disable @typescript-eslint/no-explicit-any */

Expand Down Expand Up @@ -513,7 +513,7 @@ export class WhereBuilder<MT extends object = AnyObject> {
* ```ts
* const filterBuilder = new FilterBuilder();
* const filter = filterBuilder
* .fields('id', a', 'b')
* .fields('id', 'a', 'b')
* .limit(10)
* .offset(0)
* .order(['a ASC', 'b DESC'])
Expand Down
10 changes: 10 additions & 0 deletions packages/filter/src/types.ts
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>;
20 changes: 20 additions & 0 deletions packages/filter/tsconfig.json
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"
}
]
}
1 change: 1 addition & 0 deletions packages/repository/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"dependencies": {
"@loopback/core": "^2.9.5",
"@loopback/filter": "^1.0.0",
"@types/debug": "^4.1.5",
"debug": "^4.1.1",
"lodash": "^4.17.20",
Expand Down
6 changes: 3 additions & 3 deletions packages/repository/src/connectors/crud.connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Connector} from './connector';
import {Filter, Where} from '@loopback/filter';
import {Class, Count, Options} from '../common-types';
import {Entity, EntityData} from '../model';
import {Filter, Where} from '../query';
import {Class, Options, Count} from '../common-types';
import {Connector} from './connector';

/**
* CRUD operations for connector implementations
Expand Down
6 changes: 3 additions & 3 deletions packages/repository/src/connectors/kv.connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Connector} from './connector';
import {Entity, EntityData} from '../model';
import {Filter} from '@loopback/filter';
import {Class, Options} from '../common-types';
import {Filter} from '../query';
import {Entity, EntityData} from '../model';
import {Connector} from './connector';

/**
* Key/Value operations for connector implementations
Expand Down
Loading