@loopback/repository 2.11.1 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +66 -0
- package/dist/common-types.d.ts +5 -3
- package/dist/common-types.js +9 -3
- package/dist/common-types.js.map +1 -1
- package/dist/connectors/crud.connector.d.ts +3 -3
- package/dist/connectors/kv.connector.d.ts +3 -3
- package/dist/decorators/model.decorator.js +7 -0
- package/dist/decorators/model.decorator.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +9 -1
- package/dist/index.js.map +1 -1
- package/dist/mixins/repository.mixin.d.ts +9 -3
- package/dist/model.d.ts +29 -6
- package/dist/model.js.map +1 -1
- package/dist/relations/belongs-to/belongs-to.decorator.js +10 -1
- package/dist/relations/belongs-to/belongs-to.decorator.js.map +1 -1
- package/dist/relations/has-many/has-many-through-repository.factory.d.ts +8 -2
- package/dist/relations/has-many/has-many-through-repository.factory.js +6 -0
- package/dist/relations/has-many/has-many-through-repository.factory.js.map +1 -1
- package/dist/relations/has-many/has-many-through.inclusion.resolver.d.ts +16 -0
- package/dist/relations/has-many/has-many-through.inclusion.resolver.js +74 -0
- package/dist/relations/has-many/has-many-through.inclusion.resolver.js.map +1 -0
- package/dist/relations/has-many/has-many.inclusion-resolver.js.map +1 -1
- package/dist/relations/has-many/has-many.repository.d.ts +1 -1
- package/dist/relations/has-one/has-one.repository.d.ts +1 -1
- package/dist/relations/has-one/has-one.repository.js.map +1 -1
- package/dist/relations/relation.helpers.d.ts +2 -2
- package/dist/relations/relation.helpers.js +1 -1
- package/dist/relations/relation.helpers.js.map +1 -1
- package/dist/relations/relation.types.d.ts +1 -1
- package/dist/relations/relation.types.js +1 -1
- package/dist/repositories/constraint-utils.d.ts +1 -1
- package/dist/repositories/constraint-utils.js +4 -4
- package/dist/repositories/constraint-utils.js.map +1 -1
- package/dist/repositories/legacy-juggler-bridge.d.ts +1 -1
- package/dist/repositories/legacy-juggler-bridge.js +1 -1
- package/dist/repositories/legacy-juggler-bridge.js.map +1 -1
- package/dist/repositories/repository.d.ts +1 -1
- package/dist/repositories/repository.js.map +1 -1
- package/package.json +16 -12
- package/src/common-types.ts +10 -4
- package/src/connectors/crud.connector.ts +3 -3
- package/src/connectors/kv.connector.ts +3 -3
- package/src/decorators/model.decorator.ts +9 -0
- package/src/index.ts +1 -1
- package/src/model.ts +32 -2
- package/src/relations/belongs-to/belongs-to.decorator.ts +21 -5
- package/src/relations/belongs-to/belongs-to.inclusion-resolver.ts +1 -1
- package/src/relations/has-many/has-many-through-repository.factory.ts +20 -3
- package/src/relations/has-many/has-many-through.inclusion.resolver.ts +135 -0
- package/src/relations/has-many/has-many.inclusion-resolver.ts +1 -1
- package/src/relations/has-many/has-many.repository.ts +1 -1
- package/src/relations/has-one/has-one.inclusion-resolver.ts +1 -1
- package/src/relations/has-one/has-one.repository.ts +1 -1
- package/src/relations/relation.helpers.ts +2 -5
- package/src/relations/relation.types.ts +1 -1
- package/src/repositories/constraint-utils.ts +1 -1
- package/src/repositories/legacy-juggler-bridge.ts +8 -7
- package/src/repositories/repository.ts +3 -2
- package/dist/query.d.ts +0 -382
- package/dist/query.js +0 -487
- package/dist/query.js.map +0 -1
- package/src/query.ts +0 -729
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// Copyright IBM Corp. 2020. All Rights Reserved.
|
|
2
|
+
// Node module: @loopback/repository
|
|
3
|
+
// This file is licensed under the MIT License.
|
|
4
|
+
// License text available at https://opensource.org/licenses/MIT
|
|
5
|
+
|
|
6
|
+
import {Filter, Inclusion} from '@loopback/filter';
|
|
7
|
+
import debugFactory from 'debug';
|
|
8
|
+
import {AnyObject, Options} from '../../common-types';
|
|
9
|
+
import {Entity} from '../../model';
|
|
10
|
+
import {EntityCrudRepository} from '../../repositories/repository';
|
|
11
|
+
import {
|
|
12
|
+
findByForeignKeys,
|
|
13
|
+
flattenTargetsOfOneToManyRelation,
|
|
14
|
+
StringKeyOf,
|
|
15
|
+
} from '../relation.helpers';
|
|
16
|
+
import {Getter, HasManyDefinition, InclusionResolver} from '../relation.types';
|
|
17
|
+
import {resolveHasManyMetadata} from './has-many.helpers';
|
|
18
|
+
|
|
19
|
+
const debug = debugFactory(
|
|
20
|
+
'loopback:repository:has-many-through-inclusion-resolver',
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Creates InclusionResolver for HasManyThrough relation.
|
|
25
|
+
* Notice that this function only generates the inclusionResolver.
|
|
26
|
+
* It doesn't register it for the source repository.
|
|
27
|
+
*
|
|
28
|
+
*
|
|
29
|
+
* @param meta - metadata of the hasMany relation (including through)
|
|
30
|
+
* @param getThroughRepo - through repository getter i.e. where through
|
|
31
|
+
* instances are
|
|
32
|
+
* @param getTargetRepo - target repository getter i.e where target instances
|
|
33
|
+
* are
|
|
34
|
+
*/
|
|
35
|
+
export function createHasManyThroughInclusionResolver<
|
|
36
|
+
Through extends Entity,
|
|
37
|
+
ThroughID,
|
|
38
|
+
ThroughRelations extends object,
|
|
39
|
+
Target extends Entity,
|
|
40
|
+
TargetID,
|
|
41
|
+
TargetRelations extends object
|
|
42
|
+
>(
|
|
43
|
+
meta: HasManyDefinition,
|
|
44
|
+
getThroughRepo: Getter<
|
|
45
|
+
EntityCrudRepository<Through, ThroughID, ThroughRelations>
|
|
46
|
+
>,
|
|
47
|
+
getTargetRepo: Getter<
|
|
48
|
+
EntityCrudRepository<Target, TargetID, TargetRelations>
|
|
49
|
+
>,
|
|
50
|
+
): InclusionResolver<Entity, Target> {
|
|
51
|
+
const relationMeta = resolveHasManyMetadata(meta);
|
|
52
|
+
|
|
53
|
+
return async function fetchHasManyThroughModels(
|
|
54
|
+
entities: Entity[],
|
|
55
|
+
inclusion: Inclusion,
|
|
56
|
+
options?: Options,
|
|
57
|
+
): Promise<((Target & TargetRelations)[] | undefined)[]> {
|
|
58
|
+
if (!entities.length) return [];
|
|
59
|
+
|
|
60
|
+
debug('Fetching target models for entities:', entities);
|
|
61
|
+
debug('Relation metadata:', relationMeta);
|
|
62
|
+
|
|
63
|
+
const sourceKey = relationMeta.keyFrom;
|
|
64
|
+
const sourceIds = entities.map(e => (e as AnyObject)[sourceKey]);
|
|
65
|
+
const targetKey = relationMeta.keyTo as StringKeyOf<Target>;
|
|
66
|
+
if (!relationMeta.through) {
|
|
67
|
+
throw new Error(
|
|
68
|
+
`relationMeta.through must be defined on ${relationMeta}`,
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
const throughKeyTo = relationMeta.through.keyTo as StringKeyOf<Through>;
|
|
72
|
+
const throughKeyFrom = relationMeta.through.keyFrom as StringKeyOf<Through>;
|
|
73
|
+
|
|
74
|
+
debug('Parameters:', {
|
|
75
|
+
sourceKey,
|
|
76
|
+
sourceIds,
|
|
77
|
+
targetKey,
|
|
78
|
+
throughKeyTo,
|
|
79
|
+
throughKeyFrom,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
debug(
|
|
83
|
+
'sourceId types',
|
|
84
|
+
sourceIds.map(i => typeof i),
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
const throughRepo = await getThroughRepo();
|
|
88
|
+
const targetRepo = await getTargetRepo();
|
|
89
|
+
|
|
90
|
+
// find through models
|
|
91
|
+
const throughFound = await findByForeignKeys(
|
|
92
|
+
throughRepo,
|
|
93
|
+
throughKeyFrom,
|
|
94
|
+
sourceIds,
|
|
95
|
+
{}, // scope will be applied at the target level
|
|
96
|
+
options,
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
const throughResult = flattenTargetsOfOneToManyRelation(
|
|
100
|
+
sourceIds,
|
|
101
|
+
throughFound,
|
|
102
|
+
throughKeyFrom,
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
const result = [];
|
|
106
|
+
|
|
107
|
+
// convert from through entities to the target entities
|
|
108
|
+
for (const entityList of throughResult) {
|
|
109
|
+
if (entityList) {
|
|
110
|
+
// get target ids from the through entities by foreign key
|
|
111
|
+
const targetIds = entityList.map(entity => entity[throughKeyTo]);
|
|
112
|
+
|
|
113
|
+
// the explicit types and casts are needed
|
|
114
|
+
const targetEntityList = await findByForeignKeys<
|
|
115
|
+
Target,
|
|
116
|
+
TargetRelations,
|
|
117
|
+
StringKeyOf<Target>
|
|
118
|
+
>(
|
|
119
|
+
targetRepo,
|
|
120
|
+
targetKey,
|
|
121
|
+
(targetIds as unknown) as [],
|
|
122
|
+
inclusion.scope as Filter<Target>,
|
|
123
|
+
options,
|
|
124
|
+
);
|
|
125
|
+
result.push(targetEntityList);
|
|
126
|
+
} else {
|
|
127
|
+
// no entities found, add undefined to results
|
|
128
|
+
result.push(entityList);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
debug('fetchHasManyThroughModels result', result);
|
|
133
|
+
return result;
|
|
134
|
+
};
|
|
135
|
+
}
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
5
|
|
|
6
|
+
import {Filter, Inclusion} from '@loopback/filter';
|
|
6
7
|
import debugFactory from 'debug';
|
|
7
8
|
import {AnyObject, Options} from '../../common-types';
|
|
8
9
|
import {Entity} from '../../model';
|
|
9
|
-
import {Filter, Inclusion} from '../../query';
|
|
10
10
|
import {EntityCrudRepository} from '../../repositories/repository';
|
|
11
11
|
import {
|
|
12
12
|
findByForeignKeys,
|
|
@@ -4,9 +4,9 @@
|
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
5
|
|
|
6
6
|
import {Getter} from '@loopback/core';
|
|
7
|
+
import {Filter, Where} from '@loopback/filter';
|
|
7
8
|
import {Count, DataObject, Options} from '../../common-types';
|
|
8
9
|
import {Entity} from '../../model';
|
|
9
|
-
import {Filter, Where} from '../../query';
|
|
10
10
|
import {
|
|
11
11
|
constrainDataObject,
|
|
12
12
|
constrainFilter,
|
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
5
|
|
|
6
|
+
import {Filter, Inclusion} from '@loopback/filter';
|
|
6
7
|
import {AnyObject, Options} from '../../common-types';
|
|
7
8
|
import {Entity} from '../../model';
|
|
8
|
-
import {Filter, Inclusion} from '../../query';
|
|
9
9
|
import {EntityCrudRepository} from '../../repositories/repository';
|
|
10
10
|
import {
|
|
11
11
|
findByForeignKeys,
|
|
@@ -4,10 +4,10 @@
|
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
5
|
|
|
6
6
|
import {Getter} from '@loopback/core';
|
|
7
|
+
import {Filter} from '@loopback/filter';
|
|
7
8
|
import {Count, DataObject, Options} from '../../common-types';
|
|
8
9
|
import {EntityNotFoundError} from '../../errors';
|
|
9
10
|
import {Entity} from '../../model';
|
|
10
|
-
import {Filter} from '../../query';
|
|
11
11
|
import {
|
|
12
12
|
constrainDataObject,
|
|
13
13
|
constrainFilter,
|
|
@@ -24,7 +24,7 @@ const debug = debugFactory('loopback:repository:relation-helpers');
|
|
|
24
24
|
* @param targetRepository - The target repository where the related model instances are found
|
|
25
25
|
* @param fkName - Name of the foreign key
|
|
26
26
|
* @param fkValues - One value or array of values of the foreign key to be included
|
|
27
|
-
* @param scope - Additional scope constraints
|
|
27
|
+
* @param scope - Additional scope constraints
|
|
28
28
|
* @param options - Options for the operations
|
|
29
29
|
*/
|
|
30
30
|
export async function findByForeignKeys<
|
|
@@ -151,10 +151,7 @@ function isInclusionAllowed<T extends Entity, Relations extends object = {}>(
|
|
|
151
151
|
* @param targetKey - name of the target key
|
|
152
152
|
*
|
|
153
153
|
*/
|
|
154
|
-
export function flattenTargetsOfOneToOneRelation<
|
|
155
|
-
SourceWithRelations extends Entity,
|
|
156
|
-
Target extends Entity
|
|
157
|
-
>(
|
|
154
|
+
export function flattenTargetsOfOneToOneRelation<Target extends Entity>(
|
|
158
155
|
sourceIds: unknown[],
|
|
159
156
|
targetEntities: Target[],
|
|
160
157
|
targetKey: StringKeyOf<Target>,
|
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
5
|
|
|
6
|
+
import {Inclusion} from '@loopback/filter';
|
|
6
7
|
import {Options} from '../common-types';
|
|
7
8
|
import {Entity} from '../model';
|
|
8
|
-
import {Inclusion} from '../query';
|
|
9
9
|
import {TypeResolver} from '../type-resolver';
|
|
10
10
|
|
|
11
11
|
export enum RelationType {
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
5
|
|
|
6
|
+
import {Filter, FilterBuilder, Where, WhereBuilder} from '@loopback/filter';
|
|
6
7
|
import {cloneDeep} from 'lodash';
|
|
7
8
|
import {AnyObject, DataObject} from '../common-types';
|
|
8
9
|
import {Entity} from '../model';
|
|
9
|
-
import {Filter, FilterBuilder, Where, WhereBuilder} from '../query';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* A utility function which takes a filter and enforces constraint(s)
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
5
|
|
|
6
6
|
import {Getter} from '@loopback/core';
|
|
7
|
+
import {Filter, FilterExcludingWhere, Inclusion, Where} from '@loopback/filter';
|
|
7
8
|
import assert from 'assert';
|
|
8
9
|
import legacy from 'loopback-datasource-juggler';
|
|
9
10
|
import {
|
|
@@ -23,7 +24,6 @@ import {
|
|
|
23
24
|
PropertyType,
|
|
24
25
|
rejectNavigationalPropertiesInData,
|
|
25
26
|
} from '../model';
|
|
26
|
-
import {Filter, FilterExcludingWhere, Inclusion, Where} from '../query';
|
|
27
27
|
import {
|
|
28
28
|
BelongsToAccessor,
|
|
29
29
|
BelongsToDefinition,
|
|
@@ -54,9 +54,9 @@ export namespace juggler {
|
|
|
54
54
|
export import PersistedModel = legacy.PersistedModel;
|
|
55
55
|
export import KeyValueModel = legacy.KeyValueModel;
|
|
56
56
|
export import PersistedModelClass = legacy.PersistedModelClass;
|
|
57
|
-
// eslint-disable-next-line no-shadow
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
58
58
|
export import Transaction = legacy.Transaction;
|
|
59
|
-
// eslint-disable-next-line no-shadow
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
60
60
|
export import IsolationLevel = legacy.IsolationLevel;
|
|
61
61
|
}
|
|
62
62
|
|
|
@@ -763,10 +763,11 @@ export class DefaultCrudRepository<
|
|
|
763
763
|
*/
|
|
764
764
|
|
|
765
765
|
export class DefaultTransactionalRepository<
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
>
|
|
766
|
+
T extends Entity,
|
|
767
|
+
ID,
|
|
768
|
+
Relations extends object = {}
|
|
769
|
+
>
|
|
770
|
+
extends DefaultCrudRepository<T, ID, Relations>
|
|
770
771
|
implements TransactionalEntityRepository<T, ID, Relations> {
|
|
771
772
|
async beginTransaction(
|
|
772
773
|
options?: IsolationLevel | Options,
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
5
|
|
|
6
|
+
import {Filter, FilterExcludingWhere, Where} from '@loopback/filter';
|
|
6
7
|
import {
|
|
7
8
|
AnyObject,
|
|
8
9
|
Command,
|
|
@@ -16,7 +17,6 @@ import {CrudConnector} from '../connectors';
|
|
|
16
17
|
import {DataSource} from '../datasource';
|
|
17
18
|
import {EntityNotFoundError} from '../errors';
|
|
18
19
|
import {Entity, Model, ValueObject} from '../model';
|
|
19
|
-
import {Filter, FilterExcludingWhere, Where} from '../query';
|
|
20
20
|
import {InclusionResolver} from '../relations/relation.types';
|
|
21
21
|
import {IsolationLevel, Transaction} from '../transaction';
|
|
22
22
|
|
|
@@ -138,7 +138,8 @@ export interface EntityCrudRepository<
|
|
|
138
138
|
T extends Entity,
|
|
139
139
|
ID,
|
|
140
140
|
Relations extends object = {}
|
|
141
|
-
> extends EntityRepository<T, ID>,
|
|
141
|
+
> extends EntityRepository<T, ID>,
|
|
142
|
+
CrudRepository<T, Relations> {
|
|
142
143
|
// entityClass should have type "typeof T", but that's not supported by TSC
|
|
143
144
|
entityClass: typeof Entity & {prototype: T};
|
|
144
145
|
inclusionResolvers: Map<string, InclusionResolver<T, Entity>>;
|
package/dist/query.d.ts
DELETED
|
@@ -1,382 +0,0 @@
|
|
|
1
|
-
import { AnyObject } from './common-types';
|
|
2
|
-
/**
|
|
3
|
-
* Operators for where clauses
|
|
4
|
-
*/
|
|
5
|
-
export declare type Operators = 'eq' | 'neq' | 'gt' | 'gte' | 'lt' | 'lte' | 'inq' | 'nin' | 'between' | 'exists' | 'and' | 'or' | 'like' | 'nlike' | 'ilike' | 'nilike' | 'regexp';
|
|
6
|
-
/**
|
|
7
|
-
* Matching predicate comparison
|
|
8
|
-
*/
|
|
9
|
-
export declare type PredicateComparison<PT> = {
|
|
10
|
-
eq?: PT;
|
|
11
|
-
neq?: PT;
|
|
12
|
-
gt?: PT;
|
|
13
|
-
gte?: PT;
|
|
14
|
-
lt?: PT;
|
|
15
|
-
lte?: PT;
|
|
16
|
-
inq?: PT[];
|
|
17
|
-
nin?: PT[];
|
|
18
|
-
between?: [PT, PT];
|
|
19
|
-
exists?: boolean;
|
|
20
|
-
like?: PT;
|
|
21
|
-
nlike?: PT;
|
|
22
|
-
ilike?: PT;
|
|
23
|
-
nilike?: PT;
|
|
24
|
-
regexp?: string | RegExp;
|
|
25
|
-
};
|
|
26
|
-
/**
|
|
27
|
-
* Value types for `{propertyName: value}`
|
|
28
|
-
*/
|
|
29
|
-
export declare type ShortHandEqualType = string | number | boolean | Date;
|
|
30
|
-
/**
|
|
31
|
-
* Key types of a given model, excluding operators
|
|
32
|
-
*/
|
|
33
|
-
export declare type KeyOf<MT extends object> = Exclude<Extract<keyof MT, string>, Operators>;
|
|
34
|
-
/**
|
|
35
|
-
* Condition clause
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```ts
|
|
39
|
-
* {
|
|
40
|
-
* name: {inq: ['John', 'Mary']},
|
|
41
|
-
* status: 'ACTIVE',
|
|
42
|
-
* age: {gte: 40}
|
|
43
|
-
* }
|
|
44
|
-
* ```
|
|
45
|
-
*/
|
|
46
|
-
export declare type Condition<MT extends object> = {
|
|
47
|
-
[P in KeyOf<MT>]?: PredicateComparison<MT[P]> | (MT[P] & ShortHandEqualType);
|
|
48
|
-
};
|
|
49
|
-
/**
|
|
50
|
-
* Where clause
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
|
-
* ```ts
|
|
54
|
-
* {
|
|
55
|
-
* name: {inq: ['John', 'Mary']},
|
|
56
|
-
* status: 'ACTIVE'
|
|
57
|
-
* and: [...],
|
|
58
|
-
* or: [...],
|
|
59
|
-
* }
|
|
60
|
-
* ```
|
|
61
|
-
*/
|
|
62
|
-
export declare type Where<MT extends object = AnyObject> = Condition<MT> | AndClause<MT> | OrClause<MT>;
|
|
63
|
-
/**
|
|
64
|
-
* And clause
|
|
65
|
-
*
|
|
66
|
-
* @example
|
|
67
|
-
* ```ts
|
|
68
|
-
* {
|
|
69
|
-
* and: [...],
|
|
70
|
-
* }
|
|
71
|
-
* ```
|
|
72
|
-
*/
|
|
73
|
-
export interface AndClause<MT extends object> {
|
|
74
|
-
and: Where<MT>[];
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Or clause
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
* ```ts
|
|
81
|
-
* {
|
|
82
|
-
* or: [...],
|
|
83
|
-
* }
|
|
84
|
-
* ```
|
|
85
|
-
*/
|
|
86
|
-
export interface OrClause<MT extends object> {
|
|
87
|
-
or: Where<MT>[];
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Order by direction
|
|
91
|
-
*/
|
|
92
|
-
export declare type Direction = 'ASC' | 'DESC';
|
|
93
|
-
/**
|
|
94
|
-
* Order by
|
|
95
|
-
*
|
|
96
|
-
* Example:
|
|
97
|
-
* `{afieldname: 'ASC'}`
|
|
98
|
-
*/
|
|
99
|
-
export declare type Order<MT = AnyObject> = {
|
|
100
|
-
[P in keyof MT]: Direction;
|
|
101
|
-
};
|
|
102
|
-
/**
|
|
103
|
-
* Selection of fields
|
|
104
|
-
*
|
|
105
|
-
* Example:
|
|
106
|
-
* `{afieldname: true}`
|
|
107
|
-
*/
|
|
108
|
-
export declare type Fields<MT = AnyObject> = {
|
|
109
|
-
[P in keyof MT]?: boolean;
|
|
110
|
-
};
|
|
111
|
-
/**
|
|
112
|
-
* Inclusion of related items
|
|
113
|
-
*
|
|
114
|
-
* Note: scope means filter on related items
|
|
115
|
-
*
|
|
116
|
-
* Example:
|
|
117
|
-
* `{relation: 'aRelationName', scope: {<AFilterObject>}}`
|
|
118
|
-
*/
|
|
119
|
-
export interface Inclusion {
|
|
120
|
-
relation: string;
|
|
121
|
-
scope?: Filter<AnyObject>;
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Query filter object
|
|
125
|
-
*/
|
|
126
|
-
export interface Filter<MT extends object = AnyObject> {
|
|
127
|
-
/**
|
|
128
|
-
* The matching criteria
|
|
129
|
-
*/
|
|
130
|
-
where?: Where<MT>;
|
|
131
|
-
/**
|
|
132
|
-
* To include/exclude fields
|
|
133
|
-
*/
|
|
134
|
-
fields?: Fields<MT>;
|
|
135
|
-
/**
|
|
136
|
-
* Sorting order for matched entities. Each item should be formatted as
|
|
137
|
-
* `fieldName ASC` or `fieldName DESC`.
|
|
138
|
-
* For example: `['f1 ASC', 'f2 DESC', 'f3 ASC']`.
|
|
139
|
-
*
|
|
140
|
-
* We might want to use `Order` in the future. Keep it as `string[]` for now
|
|
141
|
-
* for compatibility with LoopBack 3.x.
|
|
142
|
-
*/
|
|
143
|
-
order?: string[];
|
|
144
|
-
/**
|
|
145
|
-
* Maximum number of entities
|
|
146
|
-
*/
|
|
147
|
-
limit?: number;
|
|
148
|
-
/**
|
|
149
|
-
* Skip N number of entities
|
|
150
|
-
*/
|
|
151
|
-
skip?: number;
|
|
152
|
-
/**
|
|
153
|
-
* Offset N number of entities. An alias for `skip`
|
|
154
|
-
*/
|
|
155
|
-
offset?: number;
|
|
156
|
-
/**
|
|
157
|
-
* To include related objects
|
|
158
|
-
*/
|
|
159
|
-
include?: Inclusion[];
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Filter without `where` property
|
|
163
|
-
*/
|
|
164
|
-
export declare type FilterExcludingWhere<MT extends object = AnyObject> = Omit<Filter<MT>, 'where'>;
|
|
165
|
-
/**
|
|
166
|
-
* TypeGuard for Filter
|
|
167
|
-
* @param candidate
|
|
168
|
-
*/
|
|
169
|
-
export declare function isFilter<MT extends object>(candidate: any): candidate is Filter<MT>;
|
|
170
|
-
/**
|
|
171
|
-
* A builder for Where object. It provides fluent APIs to add clauses such as
|
|
172
|
-
* `and`, `or`, and other operators.
|
|
173
|
-
*
|
|
174
|
-
* @example
|
|
175
|
-
* ```ts
|
|
176
|
-
* const whereBuilder = new WhereBuilder();
|
|
177
|
-
* const where = whereBuilder
|
|
178
|
-
* .eq('a', 1)
|
|
179
|
-
* .and({x: 'x'}, {y: {gt: 1}})
|
|
180
|
-
* .and({b: 'b'}, {c: {lt: 1}})
|
|
181
|
-
* .or({d: 'd'}, {e: {neq: 1}})
|
|
182
|
-
* .build();
|
|
183
|
-
* ```
|
|
184
|
-
*/
|
|
185
|
-
export declare class WhereBuilder<MT extends object = AnyObject> {
|
|
186
|
-
where: Where<MT>;
|
|
187
|
-
constructor(w?: Where<MT>);
|
|
188
|
-
private add;
|
|
189
|
-
/**
|
|
190
|
-
* @deprecated
|
|
191
|
-
* Starting from TypeScript 3.2, we don't have to cast any more. This method
|
|
192
|
-
* should be considered as `deprecated`.
|
|
193
|
-
*
|
|
194
|
-
* Cast an `and`, `or`, or condition clause to Where
|
|
195
|
-
* @param clause - And/Or/Condition clause
|
|
196
|
-
*/
|
|
197
|
-
cast(clause: AndClause<MT> | OrClause<MT> | Condition<MT>): Where<MT>;
|
|
198
|
-
/**
|
|
199
|
-
* Add an `and` clause.
|
|
200
|
-
* @param w - One or more where objects
|
|
201
|
-
*/
|
|
202
|
-
and(...w: (Where<MT> | Where<MT>[])[]): this;
|
|
203
|
-
/**
|
|
204
|
-
* Add an `or` clause.
|
|
205
|
-
* @param w - One or more where objects
|
|
206
|
-
*/
|
|
207
|
-
or(...w: (Where<MT> | Where<MT>[])[]): this;
|
|
208
|
-
/**
|
|
209
|
-
* Add an `=` condition
|
|
210
|
-
* @param key - Property name
|
|
211
|
-
* @param val - Property value
|
|
212
|
-
*/
|
|
213
|
-
eq<K extends KeyOf<MT>>(key: K, val: MT[K]): this;
|
|
214
|
-
/**
|
|
215
|
-
* Add a `!=` condition
|
|
216
|
-
* @param key - Property name
|
|
217
|
-
* @param val - Property value
|
|
218
|
-
*/
|
|
219
|
-
neq<K extends KeyOf<MT>>(key: K, val: MT[K]): this;
|
|
220
|
-
/**
|
|
221
|
-
* Add a `>` condition
|
|
222
|
-
* @param key - Property name
|
|
223
|
-
* @param val - Property value
|
|
224
|
-
*/
|
|
225
|
-
gt<K extends KeyOf<MT>>(key: K, val: MT[K]): this;
|
|
226
|
-
/**
|
|
227
|
-
* Add a `>=` condition
|
|
228
|
-
* @param key - Property name
|
|
229
|
-
* @param val - Property value
|
|
230
|
-
*/
|
|
231
|
-
gte<K extends KeyOf<MT>>(key: K, val: MT[K]): this;
|
|
232
|
-
/**
|
|
233
|
-
* Add a `<` condition
|
|
234
|
-
* @param key - Property name
|
|
235
|
-
* @param val - Property value
|
|
236
|
-
*/
|
|
237
|
-
lt<K extends KeyOf<MT>>(key: K, val: MT[K]): this;
|
|
238
|
-
/**
|
|
239
|
-
* Add a `<=` condition
|
|
240
|
-
* @param key - Property name
|
|
241
|
-
* @param val - Property value
|
|
242
|
-
*/
|
|
243
|
-
lte<K extends KeyOf<MT>>(key: K, val: MT[K]): this;
|
|
244
|
-
/**
|
|
245
|
-
* Add a `inq` condition
|
|
246
|
-
* @param key - Property name
|
|
247
|
-
* @param val - An array of property values
|
|
248
|
-
*/
|
|
249
|
-
inq<K extends KeyOf<MT>>(key: K, val: MT[K][]): this;
|
|
250
|
-
/**
|
|
251
|
-
* Add a `nin` condition
|
|
252
|
-
* @param key - Property name
|
|
253
|
-
* @param val - An array of property values
|
|
254
|
-
*/
|
|
255
|
-
nin<K extends KeyOf<MT>>(key: K, val: MT[K][]): this;
|
|
256
|
-
/**
|
|
257
|
-
* Add a `between` condition
|
|
258
|
-
* @param key - Property name
|
|
259
|
-
* @param val1 - Property value lower bound
|
|
260
|
-
* @param val2 - Property value upper bound
|
|
261
|
-
*/
|
|
262
|
-
between<K extends KeyOf<MT>>(key: K, val1: MT[K], val2: MT[K]): this;
|
|
263
|
-
/**
|
|
264
|
-
* Add a `exists` condition
|
|
265
|
-
* @param key - Property name
|
|
266
|
-
* @param val - Exists or not
|
|
267
|
-
*/
|
|
268
|
-
exists<K extends KeyOf<MT>>(key: K, val?: boolean): this;
|
|
269
|
-
/**
|
|
270
|
-
* Add a where object. For conflicting keys with the existing where object,
|
|
271
|
-
* create an `and` clause.
|
|
272
|
-
* @param where - Where filter
|
|
273
|
-
*/
|
|
274
|
-
impose(where: Where<MT>): this;
|
|
275
|
-
/**
|
|
276
|
-
* Add a `like` condition
|
|
277
|
-
* @param key - Property name
|
|
278
|
-
* @param val - Regexp condition
|
|
279
|
-
*/
|
|
280
|
-
like<K extends KeyOf<MT>>(key: K, val: MT[K]): this;
|
|
281
|
-
/**
|
|
282
|
-
* Add a `nlike` condition
|
|
283
|
-
* @param key - Property name
|
|
284
|
-
* @param val - Regexp condition
|
|
285
|
-
*/
|
|
286
|
-
nlike<K extends KeyOf<MT>>(key: K, val: MT[K]): this;
|
|
287
|
-
/**
|
|
288
|
-
* Add a `ilike` condition
|
|
289
|
-
* @param key - Property name
|
|
290
|
-
* @param val - Regexp condition
|
|
291
|
-
*/
|
|
292
|
-
ilike<K extends KeyOf<MT>>(key: K, val: MT[K]): this;
|
|
293
|
-
/**
|
|
294
|
-
* Add a `nilike` condition
|
|
295
|
-
* @param key - Property name
|
|
296
|
-
* @param val - Regexp condition
|
|
297
|
-
*/
|
|
298
|
-
nilike<K extends KeyOf<MT>>(key: K, val: MT[K]): this;
|
|
299
|
-
/**
|
|
300
|
-
* Add a `regexp` condition
|
|
301
|
-
* @param key - Property name
|
|
302
|
-
* @param val - Regexp condition
|
|
303
|
-
*/
|
|
304
|
-
regexp<K extends KeyOf<MT>>(key: K, val: string | RegExp): this;
|
|
305
|
-
/**
|
|
306
|
-
* Get the where object
|
|
307
|
-
*/
|
|
308
|
-
build(): Where<MT>;
|
|
309
|
-
}
|
|
310
|
-
/**
|
|
311
|
-
* A builder for Filter. It provides fleunt APIs to add clauses such as
|
|
312
|
-
* `fields`, `order`, `where`, `limit`, `offset`, and `include`.
|
|
313
|
-
*
|
|
314
|
-
* @example
|
|
315
|
-
* ```ts
|
|
316
|
-
* const filterBuilder = new FilterBuilder();
|
|
317
|
-
* const filter = filterBuilder
|
|
318
|
-
* .fields('id', a', 'b')
|
|
319
|
-
* .limit(10)
|
|
320
|
-
* .offset(0)
|
|
321
|
-
* .order(['a ASC', 'b DESC'])
|
|
322
|
-
* .where({id: 1})
|
|
323
|
-
* .build();
|
|
324
|
-
* ```
|
|
325
|
-
*/
|
|
326
|
-
export declare class FilterBuilder<MT extends object = AnyObject> {
|
|
327
|
-
filter: Filter<MT>;
|
|
328
|
-
constructor(f?: Filter<MT>);
|
|
329
|
-
/**
|
|
330
|
-
* Set `limit`
|
|
331
|
-
* @param limit - Maximum number of records to be returned
|
|
332
|
-
*/
|
|
333
|
-
limit(limit: number): this;
|
|
334
|
-
/**
|
|
335
|
-
* Set `offset`
|
|
336
|
-
* @param offset - Offset of the number of records to be returned
|
|
337
|
-
*/
|
|
338
|
-
offset(offset: number): this;
|
|
339
|
-
/**
|
|
340
|
-
* Alias to `offset`
|
|
341
|
-
* @param skip
|
|
342
|
-
*/
|
|
343
|
-
skip(skip: number): this;
|
|
344
|
-
/**
|
|
345
|
-
* Describe what fields to be included/excluded
|
|
346
|
-
* @param f - A field name to be included, an array of field names to be
|
|
347
|
-
* included, or an Fields object for the inclusion/exclusion
|
|
348
|
-
*/
|
|
349
|
-
fields(...f: (Fields<MT> | (keyof MT)[] | keyof MT)[]): this;
|
|
350
|
-
private validateOrder;
|
|
351
|
-
/**
|
|
352
|
-
* Describe the sorting order
|
|
353
|
-
* @param o - A field name with optional direction, an array of field names,
|
|
354
|
-
* or an Order object for the field/direction pairs
|
|
355
|
-
*/
|
|
356
|
-
order(...o: (string | string[] | Order<MT>)[]): this;
|
|
357
|
-
/**
|
|
358
|
-
* Declare `include`
|
|
359
|
-
* @param i - A relation name, an array of relation names, or an `Inclusion`
|
|
360
|
-
* object for the relation/scope definitions
|
|
361
|
-
*/
|
|
362
|
-
include(...i: (string | string[] | Inclusion)[]): this;
|
|
363
|
-
/**
|
|
364
|
-
* Declare a where clause
|
|
365
|
-
* @param w - Where object
|
|
366
|
-
*/
|
|
367
|
-
where(w: Where<MT>): this;
|
|
368
|
-
/**
|
|
369
|
-
* Add a Filter or Where constraint object. If it is a filter object, create
|
|
370
|
-
* an `and` clause for conflicting keys with its where object. For any other
|
|
371
|
-
* properties, throw an error. If it's not a Filter, coerce it to a filter,
|
|
372
|
-
* and carry out the same logic.
|
|
373
|
-
*
|
|
374
|
-
* @param constraint - a constraint object to merge with own filter object
|
|
375
|
-
*/
|
|
376
|
-
impose(constraint: Filter<MT> | Where<MT>): this;
|
|
377
|
-
/**
|
|
378
|
-
* Return the filter object
|
|
379
|
-
*/
|
|
380
|
-
build(): Filter<MT>;
|
|
381
|
-
}
|
|
382
|
-
export declare function filterTemplate(strings: TemplateStringsArray, ...keys: any[]): (ctx: AnyObject) => any;
|