@ptc-org/nestjs-query-mongoose 1.0.0-alpha.1 → 2.0.0-alpha.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/README.md +8 -7
- package/package.json +4 -4
- package/src/index.d.ts +2 -0
- package/src/index.js +3 -3
- package/src/index.js.map +1 -0
- package/src/module.d.ts +6 -0
- package/src/module.js +19 -0
- package/src/module.js.map +1 -0
- package/src/mongoose-types.helper.d.ts +26 -0
- package/src/mongoose-types.helper.js +42 -0
- package/src/mongoose-types.helper.js.map +1 -0
- package/src/providers.d.ts +8 -0
- package/src/providers.js +25 -0
- package/src/providers.js.map +1 -0
- package/src/query/aggregate.builder.d.ts +25 -0
- package/src/query/aggregate.builder.js +111 -0
- package/src/query/aggregate.builder.js.map +1 -0
- package/src/query/comparison.builder.d.ts +29 -0
- package/src/query/comparison.builder.js +101 -0
- package/src/query/comparison.builder.js.map +1 -0
- package/src/query/filter-query.builder.d.ts +44 -0
- package/src/query/filter-query.builder.js +85 -0
- package/src/query/filter-query.builder.js.map +1 -0
- package/src/query/helpers.d.ts +1 -0
- package/src/query/helpers.js +8 -0
- package/src/query/helpers.js.map +1 -0
- package/src/query/index.d.ts +4 -0
- package/src/query/index.js +8 -0
- package/src/query/index.js.map +1 -0
- package/src/query/where.builder.d.ts +24 -0
- package/src/query/where.builder.js +72 -0
- package/src/query/where.builder.js.map +1 -0
- package/src/services/index.d.ts +1 -0
- package/src/services/index.js +5 -0
- package/src/services/index.js.map +1 -0
- package/src/services/mongoose-query.service.d.ts +145 -0
- package/src/services/mongoose-query.service.js +222 -0
- package/src/services/mongoose-query.service.js.map +1 -0
- package/src/services/reference-query.service.d.ts +28 -0
- package/src/services/reference-query.service.js +233 -0
- package/src/services/reference-query.service.js.map +1 -0
- package/CHANGELOG.md +0 -159
package/README.md
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
<p align="center">
|
|
2
|
-
<a href="https://
|
|
2
|
+
<a href="https://tripss.github.io/nestjs-query" target="blank"><img src="https://tripss.github.io/nestjs-query/img/logo.svg" width="120" alt="Nestjs-query Logo" /></a>
|
|
3
3
|
</p>
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.org/package/@nestjs-query/query-mongoose)
|
|
6
6
|
[](https://github.com/tripss/nestjs-query/actions?query=workflow%3ATest+and+branch%3Amaster+)
|
|
7
|
-
[](https://coveralls.io/github/tripss/nestjs-query?branch=master)
|
|
8
|
+
[](https://snyk.io/test/github/tripss/nestjs-query?targetFile=packages/query-mongoose/package.json)
|
|
9
9
|
|
|
10
10
|
# `@nestjs-query/query-mongoose`
|
|
11
11
|
|
|
12
|
-
The `query-mongoose` package that provides an implementation of `@ptc-org/nestjs-query-core` `QueryService`, built on
|
|
12
|
+
The `query-mongoose` package that provides an implementation of `@ptc-org/nestjs-query-core` `QueryService`, built on
|
|
13
|
+
top of of [nestjs](https://nestjs.com/) and [mongoose](https://mongoosejs.com/).
|
|
13
14
|
|
|
14
15
|
## Installation
|
|
15
16
|
|
|
16
|
-
[Install Guide](https://
|
|
17
|
+
[Install Guide](https://tripss.github.io/nestjs-query/docs/introduction/install)
|
|
17
18
|
|
|
18
19
|
## Getting Started
|
|
19
20
|
|
|
20
|
-
The get started with the `@nestjs-query/query-mongoose` package checkout
|
|
21
|
-
|
|
21
|
+
The get started with the `@nestjs-query/query-mongoose` package checkout
|
|
22
|
+
the [Getting Started](https://tripss.github.io/nestjs-query/docs/persistence/mongoose/getting-started) docs.
|
|
22
23
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ptc-org/nestjs-query-mongoose",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-alpha.0",
|
|
4
4
|
"description": "Mongoose adapter for @ptc-org/nestjs-query-core",
|
|
5
5
|
"author": "doug-martin <doug@dougamartin.com>",
|
|
6
6
|
"homepage": "https://github.com/tripss/nestjs-query#readme",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"test": "__tests__"
|
|
13
13
|
},
|
|
14
14
|
"files": [
|
|
15
|
-
"
|
|
15
|
+
"src/**"
|
|
16
16
|
],
|
|
17
17
|
"publishConfig": {
|
|
18
18
|
"access": "public"
|
|
@@ -21,13 +21,13 @@
|
|
|
21
21
|
"camel-case": "^4.1.2",
|
|
22
22
|
"lodash.escaperegexp": "^4.1.2",
|
|
23
23
|
"lodash.merge": "^4.6.2",
|
|
24
|
-
"tslib": "^2.
|
|
24
|
+
"tslib": "^2.4.0"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
27
|
"@nestjs/common": "^8.0.4",
|
|
28
28
|
"@nestjs/mongoose": "^8.0.0 || ^9.0.0",
|
|
29
29
|
"mongoose": "^5.10.0",
|
|
30
|
-
"@ptc-org/nestjs-query-core": "
|
|
30
|
+
"@ptc-org/nestjs-query-core": "2.0.0-alpha.0",
|
|
31
31
|
"reflect-metadata": "^0.1.13"
|
|
32
32
|
},
|
|
33
33
|
"repository": {
|
package/src/index.d.ts
ADDED
package/src/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
var services_1 = require("./services");
|
|
5
|
-
Object.defineProperty(exports, "MongooseQueryService", { enumerable: true, get: function () { return services_1.MongooseQueryService; } });
|
|
3
|
+
exports.MongooseQueryService = exports.NestjsQueryMongooseModule = void 0;
|
|
6
4
|
var module_1 = require("./module");
|
|
7
5
|
Object.defineProperty(exports, "NestjsQueryMongooseModule", { enumerable: true, get: function () { return module_1.NestjsQueryMongooseModule; } });
|
|
6
|
+
var services_1 = require("./services");
|
|
7
|
+
Object.defineProperty(exports, "MongooseQueryService", { enumerable: true, get: function () { return services_1.MongooseQueryService; } });
|
|
8
8
|
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/query-mongoose/src/index.ts"],"names":[],"mappings":";;;AAAA,mCAAoD;AAA3C,mHAAA,yBAAyB,OAAA;AAClC,uCAAiD;AAAxC,gHAAA,oBAAoB,OAAA"}
|
package/src/module.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { Document } from 'mongoose';
|
|
3
|
+
import { NestjsQueryModelDefinition } from './providers';
|
|
4
|
+
export declare class NestjsQueryMongooseModule {
|
|
5
|
+
static forFeature(models: NestjsQueryModelDefinition<Document>[], connectionName?: string): DynamicModule;
|
|
6
|
+
}
|
package/src/module.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NestjsQueryMongooseModule = void 0;
|
|
4
|
+
const mongoose_1 = require("@nestjs/mongoose");
|
|
5
|
+
const providers_1 = require("./providers");
|
|
6
|
+
class NestjsQueryMongooseModule {
|
|
7
|
+
static forFeature(models, connectionName) {
|
|
8
|
+
const queryServiceProviders = (0, providers_1.createMongooseQueryServiceProviders)(models);
|
|
9
|
+
const mongooseModule = mongoose_1.MongooseModule.forFeature(models, connectionName);
|
|
10
|
+
return {
|
|
11
|
+
imports: [mongooseModule],
|
|
12
|
+
module: NestjsQueryMongooseModule,
|
|
13
|
+
providers: [...queryServiceProviders],
|
|
14
|
+
exports: [...queryServiceProviders, mongooseModule]
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.NestjsQueryMongooseModule = NestjsQueryMongooseModule;
|
|
19
|
+
//# sourceMappingURL=module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module.js","sourceRoot":"","sources":["../../../../packages/query-mongoose/src/module.ts"],"names":[],"mappings":";;;AACA,+CAAiD;AAGjD,2CAA6F;AAE7F,MAAa,yBAAyB;IACpC,MAAM,CAAC,UAAU,CAAC,MAA8C,EAAE,cAAuB;QACvF,MAAM,qBAAqB,GAAG,IAAA,+CAAmC,EAAC,MAAM,CAAC,CAAA;QACzE,MAAM,cAAc,GAAG,yBAAc,CAAC,UAAU,CAAC,MAAM,EAAE,cAAc,CAAC,CAAA;QACxE,OAAO;YACL,OAAO,EAAE,CAAC,cAAc,CAAC;YACzB,MAAM,EAAE,yBAAyB;YACjC,SAAS,EAAE,CAAC,GAAG,qBAAqB,CAAC;YACrC,OAAO,EAAE,CAAC,GAAG,qBAAqB,EAAE,cAAc,CAAC;SACpD,CAAA;IACH,CAAC;CACF;AAXD,8DAWC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { SchemaType } from 'mongoose';
|
|
2
|
+
export declare type ReferenceOptions = {
|
|
3
|
+
type: SchemaType;
|
|
4
|
+
ref: string;
|
|
5
|
+
};
|
|
6
|
+
export declare function isReferenceOptions(options: unknown): options is ReferenceOptions;
|
|
7
|
+
export declare type SchemaTypeWithReferenceOptions = {
|
|
8
|
+
options: ReferenceOptions;
|
|
9
|
+
};
|
|
10
|
+
export declare function isSchemaTypeWithReferenceOptions(type: unknown): type is SchemaTypeWithReferenceOptions;
|
|
11
|
+
export declare type EmbeddedSchemaTypeOptions = {
|
|
12
|
+
$embeddedSchemaType: {
|
|
13
|
+
options: ReferenceOptions;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
export declare function isEmbeddedSchemaTypeOptions(options: unknown): options is EmbeddedSchemaTypeOptions;
|
|
17
|
+
export declare type VirtualReferenceOptions = {
|
|
18
|
+
ref: string;
|
|
19
|
+
localField: string;
|
|
20
|
+
foreignField: string;
|
|
21
|
+
};
|
|
22
|
+
export declare function isVirtualReferenceOptions(options: unknown): options is VirtualReferenceOptions;
|
|
23
|
+
export declare type VirtualTypeWithOptions = {
|
|
24
|
+
options: VirtualReferenceOptions;
|
|
25
|
+
};
|
|
26
|
+
export declare function isVirtualTypeWithReferenceOptions(virtualType: unknown): virtualType is VirtualTypeWithOptions;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isVirtualTypeWithReferenceOptions = exports.isVirtualReferenceOptions = exports.isEmbeddedSchemaTypeOptions = exports.isSchemaTypeWithReferenceOptions = exports.isReferenceOptions = void 0;
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
|
+
function isReferenceOptions(options) {
|
|
6
|
+
return (typeof options === 'object' &&
|
|
7
|
+
options !== null &&
|
|
8
|
+
'type' in options &&
|
|
9
|
+
'ref' in options &&
|
|
10
|
+
typeof options.ref === 'string');
|
|
11
|
+
}
|
|
12
|
+
exports.isReferenceOptions = isReferenceOptions;
|
|
13
|
+
function isSchemaTypeWithReferenceOptions(type) {
|
|
14
|
+
if (type && typeof type === 'object' && 'options' in type) {
|
|
15
|
+
const { options } = type;
|
|
16
|
+
return isReferenceOptions(options);
|
|
17
|
+
}
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
exports.isSchemaTypeWithReferenceOptions = isSchemaTypeWithReferenceOptions;
|
|
21
|
+
function isEmbeddedSchemaTypeOptions(options) {
|
|
22
|
+
if (options && typeof options === 'object' && '$embeddedSchemaType' in options) {
|
|
23
|
+
const { $embeddedSchemaType } = options;
|
|
24
|
+
return isReferenceOptions($embeddedSchemaType.options);
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
exports.isEmbeddedSchemaTypeOptions = isEmbeddedSchemaTypeOptions;
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
30
|
+
function isVirtualReferenceOptions(options) {
|
|
31
|
+
return (typeof options === 'object' && options !== null && 'ref' in options && 'localField' in options && 'foreignField' in options);
|
|
32
|
+
}
|
|
33
|
+
exports.isVirtualReferenceOptions = isVirtualReferenceOptions;
|
|
34
|
+
function isVirtualTypeWithReferenceOptions(virtualType) {
|
|
35
|
+
if (virtualType && typeof virtualType === 'object' && 'options' in virtualType) {
|
|
36
|
+
const { options } = virtualType;
|
|
37
|
+
return isVirtualReferenceOptions(options);
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
exports.isVirtualTypeWithReferenceOptions = isVirtualTypeWithReferenceOptions;
|
|
42
|
+
//# sourceMappingURL=mongoose-types.helper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mongoose-types.helper.js","sourceRoot":"","sources":["../../../../packages/query-mongoose/src/mongoose-types.helper.ts"],"names":[],"mappings":";;;AAOA,8DAA8D;AAC9D,SAAgB,kBAAkB,CAAC,OAAgB;IACjD,OAAO,CACL,OAAO,OAAO,KAAK,QAAQ;QAC3B,OAAO,KAAK,IAAI;QAChB,MAAM,IAAI,OAAO;QACjB,KAAK,IAAI,OAAO;QAChB,OAAQ,OAA4B,CAAC,GAAG,KAAK,QAAQ,CACtD,CAAA;AACH,CAAC;AARD,gDAQC;AAMD,SAAgB,gCAAgC,CAAC,IAAa;IAC5D,IAAI,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,SAAS,IAAI,IAAI,EAAE;QACzD,MAAM,EAAE,OAAO,EAAE,GAAG,IAA4B,CAAA;QAChD,OAAO,kBAAkB,CAAC,OAAO,CAAC,CAAA;KACnC;IACD,OAAO,KAAK,CAAA;AACd,CAAC;AAND,4EAMC;AAMD,SAAgB,2BAA2B,CAAC,OAAgB;IAC1D,IAAI,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,qBAAqB,IAAI,OAAO,EAAE;QAC9E,MAAM,EAAE,mBAAmB,EAAE,GAAG,OAAwD,CAAA;QACxF,OAAO,kBAAkB,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAA;KACvD;IACD,OAAO,KAAK,CAAA;AACd,CAAC;AAND,kEAMC;AAQD,8DAA8D;AAC9D,SAAgB,yBAAyB,CAAC,OAAgB;IACxD,OAAO,CACL,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,KAAK,IAAI,IAAI,KAAK,IAAI,OAAO,IAAI,YAAY,IAAI,OAAO,IAAI,cAAc,IAAI,OAAO,CAC5H,CAAA;AACH,CAAC;AAJD,8DAIC;AAMD,SAAgB,iCAAiC,CAAC,WAAoB;IACpE,IAAI,WAAW,IAAI,OAAO,WAAW,KAAK,QAAQ,IAAI,SAAS,IAAI,WAAW,EAAE;QAC9E,MAAM,EAAE,OAAO,EAAE,GAAG,WAAmC,CAAA;QACvD,OAAO,yBAAyB,CAAC,OAAO,CAAC,CAAA;KAC1C;IACD,OAAO,KAAK,CAAA;AACd,CAAC;AAND,8EAMC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { FactoryProvider } from '@nestjs/common';
|
|
2
|
+
import { ModelDefinition } from '@nestjs/mongoose';
|
|
3
|
+
import { Class } from '@ptc-org/nestjs-query-core';
|
|
4
|
+
import { Document } from 'mongoose';
|
|
5
|
+
export declare type NestjsQueryModelDefinition<Entity extends Document> = {
|
|
6
|
+
document: Class<Entity>;
|
|
7
|
+
} & ModelDefinition;
|
|
8
|
+
export declare const createMongooseQueryServiceProviders: (models: NestjsQueryModelDefinition<Document>[]) => FactoryProvider[];
|
package/src/providers.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createMongooseQueryServiceProviders = void 0;
|
|
4
|
+
const mongoose_1 = require("@nestjs/mongoose");
|
|
5
|
+
const nestjs_query_core_1 = require("@ptc-org/nestjs-query-core");
|
|
6
|
+
const mongoose_2 = require("mongoose");
|
|
7
|
+
const services_1 = require("./services");
|
|
8
|
+
// initialize default serializer for documents, this is the type that mongoose returns from queries
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
10
|
+
(0, nestjs_query_core_1.AssemblerSerializer)((obj) => obj.toObject({ virtuals: true }))(mongoose_2.Document);
|
|
11
|
+
function createMongooseQueryServiceProvider(model) {
|
|
12
|
+
return {
|
|
13
|
+
provide: (0, nestjs_query_core_1.getQueryServiceToken)(model.document),
|
|
14
|
+
useFactory(ModelClass) {
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
16
|
+
(0, nestjs_query_core_1.AssemblerDeserializer)((obj) => new ModelClass(obj))(model.document);
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
18
|
+
return new services_1.MongooseQueryService(ModelClass);
|
|
19
|
+
},
|
|
20
|
+
inject: [(0, mongoose_1.getModelToken)(model.name)]
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
const createMongooseQueryServiceProviders = (models) => models.map((model) => createMongooseQueryServiceProvider(model));
|
|
24
|
+
exports.createMongooseQueryServiceProviders = createMongooseQueryServiceProviders;
|
|
25
|
+
//# sourceMappingURL=providers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"providers.js","sourceRoot":"","sources":["../../../../packages/query-mongoose/src/providers.ts"],"names":[],"mappings":";;;AACA,+CAAiE;AACjE,kEAAoH;AACpH,uCAA0C;AAE1C,yCAAiD;AAMjD,mGAAmG;AACnG,+DAA+D;AAC/D,IAAA,uCAAmB,EAAC,CAAC,GAAa,EAAE,EAAE,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,mBAAQ,CAAC,CAAA;AAElF,SAAS,kCAAkC,CAA0B,KAAyC;IAC5G,OAAO;QACL,OAAO,EAAE,IAAA,wCAAoB,EAAC,KAAK,CAAC,QAAQ,CAAC;QAC7C,UAAU,CAAC,UAAyB;YAClC,wDAAwD;YACxD,IAAA,yCAAqB,EAAS,CAAC,GAAW,EAAE,EAAE,CAAC,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAA;YACnF,wDAAwD;YACxD,OAAO,IAAI,+BAAoB,CAAS,UAAU,CAAC,CAAA;QACrD,CAAC;QACD,MAAM,EAAE,CAAC,IAAA,wBAAa,EAAC,KAAK,CAAC,IAAI,CAAC,CAAC;KACpC,CAAA;AACH,CAAC;AAEM,MAAM,mCAAmC,GAAG,CAAC,MAA8C,EAAqB,EAAE,CACvH,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,kCAAkC,CAAC,KAAK,CAAC,CAAC,CAAA;AADrD,QAAA,mCAAmC,uCACkB"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { AggregateQuery, AggregateResponse } from '@ptc-org/nestjs-query-core';
|
|
2
|
+
import { Document } from 'mongoose';
|
|
3
|
+
declare type Aggregate = Record<string, Record<string, unknown>>;
|
|
4
|
+
declare type Group = {
|
|
5
|
+
_id: Record<string, string> | null;
|
|
6
|
+
};
|
|
7
|
+
export declare type MongooseGroupAndAggregate = Aggregate & Group;
|
|
8
|
+
/**
|
|
9
|
+
* @internal
|
|
10
|
+
* Builds a WHERE clause from a Filter.
|
|
11
|
+
*/
|
|
12
|
+
export declare class AggregateBuilder<Entity extends Document> {
|
|
13
|
+
static convertToAggregateResponse<Entity>(aggregates: Record<string, unknown>[]): AggregateResponse<Entity>[];
|
|
14
|
+
private static extractResponse;
|
|
15
|
+
/**
|
|
16
|
+
* Builds a aggregate SELECT clause from a aggregate.
|
|
17
|
+
* @param aggregate - the aggregates to select.
|
|
18
|
+
*/
|
|
19
|
+
build(aggregate: AggregateQuery<Entity>): MongooseGroupAndAggregate;
|
|
20
|
+
private createAggSelect;
|
|
21
|
+
private createGroupBySelect;
|
|
22
|
+
getGroupBySelects(fields?: (keyof Entity)[]): string[] | undefined;
|
|
23
|
+
private getGroupByAlias;
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AggregateBuilder = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const camel_case_1 = require("camel-case");
|
|
6
|
+
const helpers_1 = require("./helpers");
|
|
7
|
+
var AggregateFuncs;
|
|
8
|
+
(function (AggregateFuncs) {
|
|
9
|
+
AggregateFuncs["AVG"] = "avg";
|
|
10
|
+
AggregateFuncs["SUM"] = "sum";
|
|
11
|
+
AggregateFuncs["COUNT"] = "count";
|
|
12
|
+
AggregateFuncs["MAX"] = "max";
|
|
13
|
+
AggregateFuncs["MIN"] = "min";
|
|
14
|
+
})(AggregateFuncs || (AggregateFuncs = {}));
|
|
15
|
+
const AGG_REGEXP = /(avg|sum|count|max|min|group_by)_(.*)/;
|
|
16
|
+
/**
|
|
17
|
+
* @internal
|
|
18
|
+
* Builds a WHERE clause from a Filter.
|
|
19
|
+
*/
|
|
20
|
+
class AggregateBuilder {
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
22
|
+
static convertToAggregateResponse(aggregates) {
|
|
23
|
+
return aggregates.map(({ _id, ...response }) => {
|
|
24
|
+
return { ...this.extractResponse(_id), ...this.extractResponse(response) };
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
28
|
+
static extractResponse(response) {
|
|
29
|
+
if (!response) {
|
|
30
|
+
return {};
|
|
31
|
+
}
|
|
32
|
+
return Object.keys(response).reduce((agg, resultField) => {
|
|
33
|
+
const matchResult = AGG_REGEXP.exec(resultField);
|
|
34
|
+
if (!matchResult) {
|
|
35
|
+
throw new Error('Unknown aggregate column encountered.');
|
|
36
|
+
}
|
|
37
|
+
const [matchedFunc, matchedFieldName] = matchResult.slice(1);
|
|
38
|
+
const aggFunc = (0, camel_case_1.camelCase)(matchedFunc.toLowerCase());
|
|
39
|
+
const fieldName = matchedFieldName;
|
|
40
|
+
const aggResult = agg[aggFunc] || {};
|
|
41
|
+
return {
|
|
42
|
+
...agg,
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
44
|
+
[aggFunc]: { ...aggResult, [fieldName]: response[resultField] }
|
|
45
|
+
};
|
|
46
|
+
}, {});
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Builds a aggregate SELECT clause from a aggregate.
|
|
50
|
+
* @param aggregate - the aggregates to select.
|
|
51
|
+
*/
|
|
52
|
+
build(aggregate) {
|
|
53
|
+
const aggSelect = {
|
|
54
|
+
...this.createAggSelect(AggregateFuncs.COUNT, aggregate.count),
|
|
55
|
+
...this.createAggSelect(AggregateFuncs.SUM, aggregate.sum),
|
|
56
|
+
...this.createAggSelect(AggregateFuncs.AVG, aggregate.avg),
|
|
57
|
+
...this.createAggSelect(AggregateFuncs.MAX, aggregate.max),
|
|
58
|
+
...this.createAggSelect(AggregateFuncs.MIN, aggregate.min)
|
|
59
|
+
};
|
|
60
|
+
if (!Object.keys(aggSelect).length) {
|
|
61
|
+
throw new common_1.BadRequestException('No aggregate fields found.');
|
|
62
|
+
}
|
|
63
|
+
return { ...aggSelect, _id: this.createGroupBySelect(aggregate.groupBy) };
|
|
64
|
+
}
|
|
65
|
+
createAggSelect(func, fields) {
|
|
66
|
+
if (!fields) {
|
|
67
|
+
return {};
|
|
68
|
+
}
|
|
69
|
+
return fields.reduce((agg, field) => {
|
|
70
|
+
const aggAlias = `${func}_${field}`;
|
|
71
|
+
const fieldAlias = `$${(0, helpers_1.getSchemaKey)(String(field))}`;
|
|
72
|
+
if (func === 'count') {
|
|
73
|
+
return {
|
|
74
|
+
...agg,
|
|
75
|
+
[aggAlias]: {
|
|
76
|
+
$sum: {
|
|
77
|
+
$cond: {
|
|
78
|
+
if: { $in: [{ $type: fieldAlias }, ['missing', 'null']] },
|
|
79
|
+
then: 0,
|
|
80
|
+
else: 1
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
return { ...agg, [aggAlias]: { [`$${func}`]: fieldAlias } };
|
|
87
|
+
}, {});
|
|
88
|
+
}
|
|
89
|
+
createGroupBySelect(fields) {
|
|
90
|
+
if (!fields) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
return fields.reduce((id, field) => {
|
|
94
|
+
const aggAlias = this.getGroupByAlias(field);
|
|
95
|
+
const fieldAlias = `$${(0, helpers_1.getSchemaKey)(String(field))}`;
|
|
96
|
+
return { ...id, [aggAlias]: fieldAlias };
|
|
97
|
+
}, {});
|
|
98
|
+
}
|
|
99
|
+
getGroupBySelects(fields) {
|
|
100
|
+
if (!fields) {
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
// append _id so it pulls the sort from the _id field
|
|
104
|
+
return (fields ?? []).map((f) => `_id.${this.getGroupByAlias(f)}`);
|
|
105
|
+
}
|
|
106
|
+
getGroupByAlias(field) {
|
|
107
|
+
return `group_by_${field}`;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
exports.AggregateBuilder = AggregateBuilder;
|
|
111
|
+
//# sourceMappingURL=aggregate.builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aggregate.builder.js","sourceRoot":"","sources":["../../../../../packages/query-mongoose/src/query/aggregate.builder.ts"],"names":[],"mappings":";;;AAAA,2CAAoD;AAEpD,2CAAsC;AAGtC,uCAAwC;AAExC,IAAK,cAMJ;AAND,WAAK,cAAc;IACjB,6BAAW,CAAA;IACX,6BAAW,CAAA;IACX,iCAAe,CAAA;IACf,6BAAW,CAAA;IACX,6BAAW,CAAA;AACb,CAAC,EANI,cAAc,KAAd,cAAc,QAMlB;AAMD,MAAM,UAAU,GAAG,uCAAuC,CAAA;AAE1D;;;GAGG;AACH,MAAa,gBAAgB;IAC3B,wDAAwD;IACxD,MAAM,CAAC,0BAA0B,CAAS,UAAqC;QAC7E,OAAO,UAAU,CAAC,GAAG,CAAC,CAAC,EAAE,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F;iBACF,CAAA;aACF;YACD,OAAO,EAAE,GAAG,GAAG,EAAE,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC,IAAI,IAAI,EAAE,CAAC,EAAE,UAAU,EAAE,EAAE,CAAA;QAC7D,CAAC,EAAE,EAAE,CAAC,CAAA;IACR,CAAC;IAEO,mBAAmB,CAAC,MAAyB;QACnD,IAAI,CAAC,MAAM,EAAE;YACX,OAAO,IAAI,CAAA;SACZ;QACD,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,EAA0B,EAAE,KAAK,EAAE,EAAE;YACzD,MAAM,QAAQ,GAAG,IAAI,CAAC,eAAe,CAAC,KAAK,CAAC,CAAA;YAC5C,MAAM,UAAU,GAAG,IAAI,IAAA,sBAAY,EAAC,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,CAAA;YACpD,OAAO,EAAE,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAC,EAAE,UAAU,EAAE,CAAA;QAC1C,CAAC,EAAE,EAAE,CAAC,CAAA;IACR,CAAC;IAED,iBAAiB,CAAC,MAAyB;QACzC,IAAI,CAAC,MAAM,EAAE;YACX,OAAO,SAAS,CAAA;SACjB;QACD,qDAAqD;QACrD,OAAO,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,IAAI,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;IACpE,CAAC;IAEO,eAAe,CAAC,KAAmB;QACzC,OAAO,YAAY,KAAe,EAAE,CAAA;IACtC,CAAC;CACF;AA/FD,4CA+FC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { CommonFieldComparisonBetweenType, FilterComparisonOperators } from '@ptc-org/nestjs-query-core';
|
|
2
|
+
import { Document, FilterQuery, Model as MongooseModel } from 'mongoose';
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export declare type EntityComparisonField<Entity, F extends keyof Entity> = Entity[F] | Entity[F][] | CommonFieldComparisonBetweenType<Entity[F]> | true | false | null;
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
* Builder to create SQL Comparisons. (=, !=, \>, etc...)
|
|
10
|
+
*/
|
|
11
|
+
export declare class ComparisonBuilder<Entity extends Document> {
|
|
12
|
+
readonly Model: MongooseModel<Entity>;
|
|
13
|
+
readonly comparisonMap: Record<string, string>;
|
|
14
|
+
static DEFAULT_COMPARISON_MAP: Record<string, string>;
|
|
15
|
+
constructor(Model: MongooseModel<Entity>, comparisonMap?: Record<string, string>);
|
|
16
|
+
/**
|
|
17
|
+
* Creates a valid SQL fragment with parameters.
|
|
18
|
+
*
|
|
19
|
+
* @param field - the property in Entity to create the comparison for.
|
|
20
|
+
* @param cmp - the FilterComparisonOperator (eq, neq, gt, etc...)
|
|
21
|
+
* @param val - the value to compare to.
|
|
22
|
+
*/
|
|
23
|
+
build<F extends keyof Entity>(field: F, cmp: FilterComparisonOperators<Entity[F]>, val: EntityComparisonField<Entity, F>): FilterQuery<Entity>;
|
|
24
|
+
private betweenComparison;
|
|
25
|
+
private isBetweenVal;
|
|
26
|
+
private likeComparison;
|
|
27
|
+
private convertQueryValue;
|
|
28
|
+
private convertToObjectId;
|
|
29
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ComparisonBuilder = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const common_1 = require("@nestjs/common");
|
|
6
|
+
const lodash_escaperegexp_1 = (0, tslib_1.__importDefault)(require("lodash.escaperegexp"));
|
|
7
|
+
const mongoose_1 = require("mongoose");
|
|
8
|
+
const helpers_1 = require("./helpers");
|
|
9
|
+
/**
|
|
10
|
+
* @internal
|
|
11
|
+
* Builder to create SQL Comparisons. (=, !=, \>, etc...)
|
|
12
|
+
*/
|
|
13
|
+
class ComparisonBuilder {
|
|
14
|
+
constructor(Model, comparisonMap = ComparisonBuilder.DEFAULT_COMPARISON_MAP) {
|
|
15
|
+
this.Model = Model;
|
|
16
|
+
this.comparisonMap = comparisonMap;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Creates a valid SQL fragment with parameters.
|
|
20
|
+
*
|
|
21
|
+
* @param field - the property in Entity to create the comparison for.
|
|
22
|
+
* @param cmp - the FilterComparisonOperator (eq, neq, gt, etc...)
|
|
23
|
+
* @param val - the value to compare to.
|
|
24
|
+
*/
|
|
25
|
+
build(field, cmp, val) {
|
|
26
|
+
const schemaKey = (0, helpers_1.getSchemaKey)(`${String(field)}`);
|
|
27
|
+
const normalizedCmp = cmp.toLowerCase();
|
|
28
|
+
let querySelector;
|
|
29
|
+
if (this.comparisonMap[normalizedCmp]) {
|
|
30
|
+
// comparison operator (e.b. =, !=, >, <)
|
|
31
|
+
querySelector = {
|
|
32
|
+
[this.comparisonMap[normalizedCmp]]: this.convertQueryValue(field, val)
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
if (normalizedCmp.includes('like')) {
|
|
36
|
+
querySelector = this.likeComparison(normalizedCmp, val);
|
|
37
|
+
}
|
|
38
|
+
if (normalizedCmp.includes('between')) {
|
|
39
|
+
querySelector = this.betweenComparison(normalizedCmp, field, val);
|
|
40
|
+
}
|
|
41
|
+
if (!querySelector) {
|
|
42
|
+
throw new common_1.BadRequestException(`unknown operator ${JSON.stringify(cmp)}`);
|
|
43
|
+
}
|
|
44
|
+
return { [schemaKey]: querySelector };
|
|
45
|
+
}
|
|
46
|
+
betweenComparison(cmp, field, val) {
|
|
47
|
+
if (!this.isBetweenVal(val)) {
|
|
48
|
+
throw new Error(`Invalid value for ${cmp} expected {lower: val, upper: val} got ${JSON.stringify(val)}`);
|
|
49
|
+
}
|
|
50
|
+
if (cmp === 'notbetween') {
|
|
51
|
+
return { $lt: this.convertQueryValue(field, val.lower), $gt: this.convertQueryValue(field, val.upper) };
|
|
52
|
+
}
|
|
53
|
+
return { $gte: this.convertQueryValue(field, val.lower), $lte: this.convertQueryValue(field, val.upper) };
|
|
54
|
+
}
|
|
55
|
+
isBetweenVal(val) {
|
|
56
|
+
return val !== null && typeof val === 'object' && 'lower' in val && 'upper' in val;
|
|
57
|
+
}
|
|
58
|
+
likeComparison(cmp, val) {
|
|
59
|
+
const regExpStr = (0, lodash_escaperegexp_1.default)(`${String(val)}`).replace(/%/g, '.*');
|
|
60
|
+
const regExp = new RegExp(regExpStr, cmp.includes('ilike') ? 'i' : undefined);
|
|
61
|
+
if (cmp.startsWith('not')) {
|
|
62
|
+
return { $not: { $regex: regExp } };
|
|
63
|
+
}
|
|
64
|
+
return { $regex: regExp };
|
|
65
|
+
}
|
|
66
|
+
convertQueryValue(field, val) {
|
|
67
|
+
const schemaType = this.Model.schema.path((0, helpers_1.getSchemaKey)(field));
|
|
68
|
+
if (!schemaType) {
|
|
69
|
+
throw new common_1.BadRequestException(`unknown comparison field ${String(field)}`);
|
|
70
|
+
}
|
|
71
|
+
if (schemaType instanceof mongoose_1.Schema.Types.ObjectId) {
|
|
72
|
+
return this.convertToObjectId(val);
|
|
73
|
+
}
|
|
74
|
+
return val;
|
|
75
|
+
}
|
|
76
|
+
convertToObjectId(val) {
|
|
77
|
+
if (Array.isArray(val)) {
|
|
78
|
+
return val.map((v) => this.convertToObjectId(v));
|
|
79
|
+
}
|
|
80
|
+
if (typeof val === 'string' || typeof val === 'number') {
|
|
81
|
+
if (mongoose_1.Types.ObjectId.isValid(val)) {
|
|
82
|
+
return new mongoose_1.Types.ObjectId(val);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return val;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.ComparisonBuilder = ComparisonBuilder;
|
|
89
|
+
ComparisonBuilder.DEFAULT_COMPARISON_MAP = {
|
|
90
|
+
eq: '$eq',
|
|
91
|
+
neq: '$ne',
|
|
92
|
+
gt: '$gt',
|
|
93
|
+
gte: '$gte',
|
|
94
|
+
lt: '$lt',
|
|
95
|
+
lte: '$lte',
|
|
96
|
+
in: '$in',
|
|
97
|
+
notin: '$nin',
|
|
98
|
+
is: '$eq',
|
|
99
|
+
isnot: '$ne'
|
|
100
|
+
};
|
|
101
|
+
//# sourceMappingURL=comparison.builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"comparison.builder.js","sourceRoot":"","sources":["../../../../../packages/query-mongoose/src/query/comparison.builder.ts"],"names":[],"mappings":";;;;AAAA,2CAAoD;AAEpD,2FAA8C;AAC9C,uCAAuF;AAEvF,uCAAwC;AAaxC;;;GAGG;AACH,MAAa,iBAAiB;IAc5B,YACW,KAA4B,EAC5B,gBAAwC,iBAAiB,CAAC,sBAAsB;QADhF,UAAK,GAAL,KAAK,CAAuB;QAC5B,kBAAa,GAAb,aAAa,CAAmE;IACxF,CAAC;IAEJ;;;;;;OAMG;IACH,KAAK,CACH,KAAQ,EACR,GAAyC,EACzC,GAAqC;QAErC,MAAM,SAAS,GAAG,IAAA,sBAAY,EAAC,GAAG,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;QAClD,MAAM,aAAa,GAAI,GAAc,CAAC,WAAW,EAAE,CAAA;QACnD,IAAI,aAAiD,CAAA;QACrD,IAAI,IAAI,CAAC,aAAa,CAAC,aAAa,CAAC,EAAE;YACrC,yCAAyC;YACzC,aAAa,GAAG;gBACd,CAAC,IAAI,CAAC,aAAa,CAAC,aAAa,CAAC,CAAC,EAAE,IAAI,CAAC,iBAAiB,CAAC,KAAK,EAAE,GAAgB,CAAC;aAC3D,CAAA;SAC5B;QAED,IAAI,aAAa,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE;YAClC,aAAa,GAAG,IAAI,CAAC,cAAc,CAAC,aAAa,EAAE,GAAG,CAAC,CAAA;SACxD;QAED,IAAI,aAAa,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE;YACrC,aAAa,GAAG,IAAI,CAAC,iBAAiB,C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AAC;;AA5GH,8CA6GC;AA5GQ,wCAAsB,GAA2B;IACtD,EAAE,EAAE,KAAK;IACT,GAAG,EAAE,KAAK;IACV,EAAE,EAAE,KAAK;IACT,GAAG,EAAE,MAAM;IACX,EAAE,EAAE,KAAK;IACT,GAAG,EAAE,MAAM;IACX,EAAE,EAAE,KAAK;IACT,KAAK,EAAE,MAAM;IACb,EAAE,EAAE,KAAK;IACT,KAAK,EAAE,KAAK;CACb,CAAA"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { AggregateQuery, Filter, Query, SortField } from '@ptc-org/nestjs-query-core';
|
|
2
|
+
import { Document, FilterQuery, Model as MongooseModel } from 'mongoose';
|
|
3
|
+
import { AggregateBuilder, MongooseGroupAndAggregate } from './aggregate.builder';
|
|
4
|
+
import { WhereBuilder } from './where.builder';
|
|
5
|
+
declare type MongooseSort = Record<string, 1 | -1>;
|
|
6
|
+
declare type MongooseQuery<Entity extends Document> = {
|
|
7
|
+
filterQuery: FilterQuery<Entity>;
|
|
8
|
+
options: {
|
|
9
|
+
limit?: number;
|
|
10
|
+
skip?: number;
|
|
11
|
+
sort?: MongooseSort;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
declare type MongooseAggregateQuery<Entity extends Document> = MongooseQuery<Entity> & {
|
|
15
|
+
aggregate: MongooseGroupAndAggregate;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* @internal
|
|
19
|
+
*
|
|
20
|
+
* Class that will convert a Query into a `typeorm` Query Builder.
|
|
21
|
+
*/
|
|
22
|
+
export declare class FilterQueryBuilder<Entity extends Document> {
|
|
23
|
+
readonly Model: MongooseModel<Entity>;
|
|
24
|
+
readonly whereBuilder: WhereBuilder<Entity>;
|
|
25
|
+
readonly aggregateBuilder: AggregateBuilder<Entity>;
|
|
26
|
+
constructor(Model: MongooseModel<Entity>, whereBuilder?: WhereBuilder<Entity>, aggregateBuilder?: AggregateBuilder<Entity>);
|
|
27
|
+
buildQuery({ filter, paging, sorting }: Query<Entity>): MongooseQuery<Entity>;
|
|
28
|
+
buildAggregateQuery(aggregate: AggregateQuery<Entity>, filter?: Filter<Entity>): MongooseAggregateQuery<Entity>;
|
|
29
|
+
buildIdAggregateQuery(id: unknown | unknown[], filter: Filter<Entity>, aggregate: AggregateQuery<Entity>): MongooseAggregateQuery<Entity>;
|
|
30
|
+
buildIdFilterQuery(id: unknown | unknown[], filter?: Filter<Entity>): FilterQuery<Entity>;
|
|
31
|
+
/**
|
|
32
|
+
* Applies the filter from a Query to a `typeorm` QueryBuilder.
|
|
33
|
+
*
|
|
34
|
+
* @param filter - the filter.
|
|
35
|
+
*/
|
|
36
|
+
buildFilterQuery(filter?: Filter<Entity>): FilterQuery<Entity>;
|
|
37
|
+
/**
|
|
38
|
+
* Applies the ORDER BY clause to a `typeorm` QueryBuilder.
|
|
39
|
+
* @param sorts - an array of SortFields to create the ORDER BY clause.
|
|
40
|
+
*/
|
|
41
|
+
buildSorting(sorts?: SortField<Entity>[]): MongooseSort | undefined;
|
|
42
|
+
private buildAggregateSorting;
|
|
43
|
+
}
|
|
44
|
+
export {};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FilterQueryBuilder = void 0;
|
|
4
|
+
const nestjs_query_core_1 = require("@ptc-org/nestjs-query-core");
|
|
5
|
+
const aggregate_builder_1 = require("./aggregate.builder");
|
|
6
|
+
const helpers_1 = require("./helpers");
|
|
7
|
+
const where_builder_1 = require("./where.builder");
|
|
8
|
+
const MONGOOSE_SORT_DIRECTION = {
|
|
9
|
+
[nestjs_query_core_1.SortDirection.ASC]: 1,
|
|
10
|
+
[nestjs_query_core_1.SortDirection.DESC]: -1
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* @internal
|
|
14
|
+
*
|
|
15
|
+
* Class that will convert a Query into a `typeorm` Query Builder.
|
|
16
|
+
*/
|
|
17
|
+
class FilterQueryBuilder {
|
|
18
|
+
constructor(Model, whereBuilder = new where_builder_1.WhereBuilder(Model), aggregateBuilder = new aggregate_builder_1.AggregateBuilder()) {
|
|
19
|
+
this.Model = Model;
|
|
20
|
+
this.whereBuilder = whereBuilder;
|
|
21
|
+
this.aggregateBuilder = aggregateBuilder;
|
|
22
|
+
}
|
|
23
|
+
buildQuery({ filter, paging, sorting }) {
|
|
24
|
+
return {
|
|
25
|
+
filterQuery: this.buildFilterQuery(filter),
|
|
26
|
+
options: { limit: paging?.limit, skip: paging?.offset, sort: this.buildSorting(sorting) }
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
buildAggregateQuery(aggregate, filter) {
|
|
30
|
+
return {
|
|
31
|
+
filterQuery: this.buildFilterQuery(filter),
|
|
32
|
+
aggregate: this.aggregateBuilder.build(aggregate),
|
|
33
|
+
options: { sort: this.buildAggregateSorting(aggregate) }
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
buildIdAggregateQuery(id, filter, aggregate) {
|
|
37
|
+
return {
|
|
38
|
+
filterQuery: this.buildIdFilterQuery(id, filter),
|
|
39
|
+
aggregate: this.aggregateBuilder.build(aggregate),
|
|
40
|
+
options: { sort: this.buildAggregateSorting(aggregate) }
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
buildIdFilterQuery(id, filter) {
|
|
44
|
+
return {
|
|
45
|
+
...this.buildFilterQuery(filter),
|
|
46
|
+
_id: Array.isArray(id) ? { $in: id } : id
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Applies the filter from a Query to a `typeorm` QueryBuilder.
|
|
51
|
+
*
|
|
52
|
+
* @param filter - the filter.
|
|
53
|
+
*/
|
|
54
|
+
buildFilterQuery(filter) {
|
|
55
|
+
if (!filter) {
|
|
56
|
+
return {};
|
|
57
|
+
}
|
|
58
|
+
return this.whereBuilder.build(filter);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Applies the ORDER BY clause to a `typeorm` QueryBuilder.
|
|
62
|
+
* @param sorts - an array of SortFields to create the ORDER BY clause.
|
|
63
|
+
*/
|
|
64
|
+
buildSorting(sorts) {
|
|
65
|
+
if (!sorts) {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
return sorts.reduce((sort, sortField) => {
|
|
69
|
+
const field = (0, helpers_1.getSchemaKey)(sortField.field.toString());
|
|
70
|
+
const direction = MONGOOSE_SORT_DIRECTION[sortField.direction];
|
|
71
|
+
return { ...sort, [field]: direction };
|
|
72
|
+
}, {});
|
|
73
|
+
}
|
|
74
|
+
buildAggregateSorting(aggregate) {
|
|
75
|
+
const aggregateGroupBy = this.aggregateBuilder.getGroupBySelects(aggregate.groupBy);
|
|
76
|
+
if (!aggregateGroupBy) {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
return aggregateGroupBy.reduce((sort, sortField) => {
|
|
80
|
+
return { ...sort, [(0, helpers_1.getSchemaKey)(sortField)]: MONGOOSE_SORT_DIRECTION[nestjs_query_core_1.SortDirection.ASC] };
|
|
81
|
+
}, {});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.FilterQueryBuilder = FilterQueryBuilder;
|
|
85
|
+
//# sourceMappingURL=filter-query.builder.js.map
|