Skip to content

Commit 839ef65

Browse files
author
KoLiBer
committed
Merge branch 'release/0.3.0'
2 parents 1034200 + f10df45 commit 839ef65

File tree

5 files changed

+65
-70
lines changed

5 files changed

+65
-70
lines changed

.travis.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: node_js
2+
node_js:
3+
- node
4+
5+
before_install: cd sources/
6+
script:
7+
- npm run build
8+
- npm run test
9+
deploy:
10+
provider: npm
11+
12+
api_key: $NPM_TOKEN
13+
skip_cleanup: true
14+
on:
15+
tags: true

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
# Changelog
22

3+
## v0.3.0
4+
5+
- **Fix**: change `FilterContext` to `InvocationContext` using repository context
6+
- **Feat**: add CI file
7+
38
## v0.1.0 - Initial release

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# loopback-component-filter
22

3+
[![Build Status](https://travis-ci.com/loopback4/loopback-component-filter.svg?branch=master)](https://travis-ci.com/loopback4/loopback-component-filter)
4+
35
Using this simple extension you can filter models in repository level.
46

57
---

sources/package.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "loopback-component-filter",
3-
"version": "0.1.0",
3+
"version": "0.3.0",
44
"description": "Loopback component models filter",
55
"keywords": [
66
"loopback-extension",
@@ -33,15 +33,15 @@
3333
"dist"
3434
],
3535
"dependencies": {
36-
"@loopback/boot": "^2.1.2",
37-
"@loopback/context": "^3.6.0",
38-
"@loopback/core": "^2.4.2",
39-
"@loopback/repository": "^2.3.0",
36+
"@loopback/boot": "^2.2.0",
37+
"@loopback/context": "^3.7.0",
38+
"@loopback/core": "^2.5.0",
39+
"@loopback/repository": "^2.4.0",
4040
"tslib": "^1.10.0"
4141
},
4242
"devDependencies": {
43-
"@loopback/build": "^5.3.0",
44-
"@loopback/testlab": "^3.1.2",
43+
"@loopback/build": "^5.3.1",
44+
"@loopback/testlab": "^3.1.3",
4545
"@types/node": "^10.17.21",
4646
"nodemon": "^2.0.3",
4747
"source-map-support": "^0.5.19",

sources/src/repositories/filter.repository.ts

+36-63
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { InvocationArgs, InvocationContext } from "@loopback/context";
2-
import { Application, CoreBindings } from "@loopback/core";
1+
import { InvocationContext } from "@loopback/context";
32
import {
43
juggler,
54
Class,
@@ -16,32 +15,14 @@ import {
1615

1716
import { Ctor } from "../types";
1817

19-
/**
20-
* Repository Config
21-
*/
22-
export interface FilterContext<
23-
Model extends Entity,
24-
ModelID,
25-
ModelRelations extends object = {}
26-
> {
27-
target: DefaultCrudRepository<Model, ModelID, ModelRelations>;
28-
methodName: keyof DefaultCrudRepository<Model, ModelID, ModelRelations>;
29-
args: InvocationArgs;
30-
invocationContext: InvocationContext;
31-
}
32-
33-
export interface RepositoryConfig<
34-
Model extends Entity,
35-
ModelID,
36-
ModelRelations extends object = {}
37-
> {
18+
export interface RepositoryConfig<Model extends Entity> {
3819
id: keyof Model;
3920
where: (
40-
context: FilterContext<Model, ModelID, ModelRelations>,
21+
context: InvocationContext,
4122
where: Where<Model>
4223
) => Promise<Where<Model>>;
4324
fields: (
44-
context: FilterContext<Model, ModelID, ModelRelations>,
25+
context: InvocationContext,
4526
fields: Fields<Model>
4627
) => Promise<Fields<Model>>;
4728
}
@@ -62,7 +43,7 @@ export function FilterCrudRepositoryMixin<
6243
Model extends Entity,
6344
ModelID,
6445
ModelRelations extends object = {}
65-
>(config: RepositoryConfig<Model, ModelID, ModelRelations>) {
46+
>(config: RepositoryConfig<Model>) {
6647
/**
6748
* Return function with generic type of repository class, returns mixed in class
6849
*
@@ -84,41 +65,8 @@ export function FilterCrudRepositoryMixin<
8465

8566
class Repository extends parentClass
8667
implements FilterCrudRepository<Model, ModelID, ModelRelations> {
87-
private application: Application;
88-
89-
constructor(
90-
ctor: Ctor<Model>,
91-
dataSource: juggler.DataSource,
92-
application: Application
93-
) {
68+
constructor(ctor: Ctor<Model>, dataSource: juggler.DataSource) {
9469
super(ctor, dataSource);
95-
96-
this.application = application;
97-
}
98-
99-
/**
100-
* Get FilterContext method
101-
*/
102-
private getFilterContext(
103-
args: IArguments
104-
): FilterContext<Model, ModelID, ModelRelations> {
105-
return {
106-
target: this,
107-
methodName: args.callee.name as any,
108-
args: Array.from(args),
109-
invocationContext: new InvocationContext(
110-
this.application,
111-
this.application.getSync(
112-
CoreBindings.CONTROLLER_CURRENT
113-
) as any,
114-
this.application.getSync(
115-
CoreBindings.CONTROLLER_METHOD_NAME
116-
),
117-
this.application.getSync(
118-
CoreBindings.CONTROLLER_METHOD_META
119-
)
120-
),
121-
};
12270
}
12371

12472
/**
@@ -128,7 +76,12 @@ export function FilterCrudRepositoryMixin<
12876
filter?: Filter<Model>,
12977
options?: Options
13078
): Promise<(Model & ModelRelations)[]> {
131-
const filterContext = this.getFilterContext(arguments);
79+
const filterContext = new InvocationContext(
80+
undefined as any,
81+
this,
82+
"read",
83+
Array.from(arguments)
84+
);
13285

13386
return await super.find(
13487
{
@@ -150,7 +103,12 @@ export function FilterCrudRepositoryMixin<
150103
filter?: Filter<Model>,
151104
options?: Options
152105
): Promise<(Model & ModelRelations) | null> {
153-
const filterContext = this.getFilterContext(arguments);
106+
const filterContext = new InvocationContext(
107+
undefined as any,
108+
this,
109+
"read",
110+
Array.from(arguments)
111+
);
154112

155113
return await super.findOne(
156114
{
@@ -203,7 +161,12 @@ export function FilterCrudRepositoryMixin<
203161
where?: Where<Model>,
204162
options?: Options
205163
): Promise<Count> {
206-
const filterContext = this.getFilterContext(arguments);
164+
const filterContext = new InvocationContext(
165+
undefined as any,
166+
this,
167+
"count",
168+
Array.from(arguments)
169+
);
207170

208171
return await super.count(
209172
await config.where(filterContext, where || {}),
@@ -236,7 +199,12 @@ export function FilterCrudRepositoryMixin<
236199
where?: Where<Model>,
237200
options?: Options
238201
): Promise<Count> {
239-
const filterContext = this.getFilterContext(arguments);
202+
const filterContext = new InvocationContext(
203+
undefined as any,
204+
this,
205+
"update",
206+
Array.from(arguments)
207+
);
240208

241209
return await super.updateAll(
242210
data,
@@ -275,7 +243,12 @@ export function FilterCrudRepositoryMixin<
275243
where?: Where<Model>,
276244
options?: Options
277245
): Promise<Count> {
278-
const filterContext = this.getFilterContext(arguments);
246+
const filterContext = new InvocationContext(
247+
undefined as any,
248+
this,
249+
"delete",
250+
Array.from(arguments)
251+
);
279252

280253
return await super.deleteAll(
281254
await config.where(filterContext, where || {}),

0 commit comments

Comments
 (0)