@ptc-org/nestjs-query-typegoose 2.0.0-alpha.1 → 2.0.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 +1 -1
- package/package.json +9 -10
- package/src/index.d.ts +2 -0
- package/src/index.js.map +1 -0
- package/src/module.d.ts +5 -0
- package/src/module.js +19 -0
- package/src/module.js.map +1 -0
- package/src/providers.d.ts +3 -0
- package/src/providers.js +41 -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/reference-query.service.d.ts +34 -0
- package/src/services/reference-query.service.js +237 -0
- package/src/services/reference-query.service.js.map +1 -0
- package/src/services/typegoose-query-service.d.ts +134 -0
- package/src/services/typegoose-query-service.js +246 -0
- package/src/services/typegoose-query-service.js.map +1 -0
- package/src/typegoose-interface.helpers.d.ts +14 -0
- package/src/typegoose-interface.helpers.js +3 -0
- package/src/typegoose-interface.helpers.js.map +1 -0
- package/src/typegoose-types.helper.d.ts +40 -0
- package/src/typegoose-types.helper.js +40 -0
- package/src/typegoose-types.helper.js.map +1 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WhereBuilder = void 0;
|
|
4
|
+
const comparison_builder_1 = require("./comparison.builder");
|
|
5
|
+
/**
|
|
6
|
+
* @internal
|
|
7
|
+
* Builds a WHERE clause from a Filter.
|
|
8
|
+
*/
|
|
9
|
+
class WhereBuilder {
|
|
10
|
+
constructor(Model, comparisonBuilder = new comparison_builder_1.ComparisonBuilder(Model)) {
|
|
11
|
+
this.Model = Model;
|
|
12
|
+
this.comparisonBuilder = comparisonBuilder;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Builds a WHERE clause from a Filter.
|
|
16
|
+
* @param filter - the filter to build the WHERE clause from.
|
|
17
|
+
*/
|
|
18
|
+
build(filter) {
|
|
19
|
+
const { and, or } = filter;
|
|
20
|
+
let ands = [];
|
|
21
|
+
let ors = [];
|
|
22
|
+
let filterQuery = {};
|
|
23
|
+
if (and && and.length) {
|
|
24
|
+
ands = and.map((f) => this.build(f));
|
|
25
|
+
}
|
|
26
|
+
if (or && or.length) {
|
|
27
|
+
ors = or.map((f) => this.build(f));
|
|
28
|
+
}
|
|
29
|
+
const filterAnds = this.filterFields(filter);
|
|
30
|
+
if (filterAnds) {
|
|
31
|
+
ands = [...ands, filterAnds];
|
|
32
|
+
}
|
|
33
|
+
if (ands.length) {
|
|
34
|
+
filterQuery = { ...filterQuery, $and: ands };
|
|
35
|
+
}
|
|
36
|
+
if (ors.length) {
|
|
37
|
+
filterQuery = { ...filterQuery, $or: ors };
|
|
38
|
+
}
|
|
39
|
+
return filterQuery;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Creates field comparisons from a filter. This method will ignore and/or properties.
|
|
43
|
+
* @param filter - the filter with fields to create comparisons for.
|
|
44
|
+
*/
|
|
45
|
+
filterFields(filter) {
|
|
46
|
+
const ands = Object.keys(filter)
|
|
47
|
+
.filter((f) => f !== 'and' && f !== 'or')
|
|
48
|
+
.map((field) => this.withFilterComparison(field, this.getField(filter, field)));
|
|
49
|
+
if (ands.length === 1) {
|
|
50
|
+
return ands[0];
|
|
51
|
+
}
|
|
52
|
+
if (ands.length) {
|
|
53
|
+
return { $and: ands };
|
|
54
|
+
}
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
getField(obj, field) {
|
|
58
|
+
return obj[field];
|
|
59
|
+
}
|
|
60
|
+
withFilterComparison(field, cmp) {
|
|
61
|
+
const opts = Object.keys(cmp);
|
|
62
|
+
if (opts.length === 1) {
|
|
63
|
+
const cmpType = opts[0];
|
|
64
|
+
return this.comparisonBuilder.build(field, cmpType, cmp[cmpType]);
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
$or: opts.map((cmpType) => this.comparisonBuilder.build(field, cmpType, cmp[cmpType]))
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.WhereBuilder = WhereBuilder;
|
|
72
|
+
//# sourceMappingURL=where.builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"where.builder.js","sourceRoot":"","sources":["../../../../../packages/query-typegoose/src/query/where.builder.ts"],"names":[],"mappings":";;;AAGA,6DAA+E;AAE/E;;;GAGG;AACH,MAAa,YAAY;IACvB,YACW,KAAwC,EACxC,oBAA+C,IAAI,sCAAiB,CAAS,KAAK,CAAC;QADnF,UAAK,GAAL,KAAK,CAAmC;QACxC,sBAAiB,GAAjB,iBAAiB,CAAkE;IAC3F,CAAC;IAEJ;;;OAGG;IACI,KAAK,CAAC,MAAsB;QACjC,MAAM,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,MAAM,CAAA;QAC1B,IAAI,IAAI,GAAmC,EAAE,CAAA;QAC7C,IAAI,GAAG,GAAmC,EAAE,CAAA;QAC5C,IAAI,WAAW,GAAiC,EAAE,CAAA;QAElD,IAAI,GAAG,IAAI,GAAG,CAAC,MAAM,EAAE;YACrB,IAAI,GAAG,GAAG,CAAC,GAAG,CAA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AAA;IAClB,CAAC;IAEO,QAAQ,CACd,GAA8B,EAC9B,KAAQ;QAER,OAAO,GAAG,CAAC,KAAK,CAAqC,CAAA;IACvD,CAAC;IAEO,oBAAoB,CAC1B,KAAQ,EACR,GAAqC;QAErC,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAA+C,CAAA;QAC3E,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;YACrB,MAAM,OAAO,GAAG,IAAI,CAAC,CAAC,CAAC,CAAA;YACvB,OAAO,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,CAAqC,CAAC,CAAA;SACtG;QACD,OAAO;YACL,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,CAAqC,CAAC,CAAC;SAC3F,CAAA;IACnC,CAAC;CACF;AA/ED,oCA+EC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './typegoose-query-service';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../packages/query-typegoose/src/services/index.ts"],"names":[],"mappings":";;;AAAA,oEAAyC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { AggregateQuery, AggregateResponse, Class, Filter, FindRelationOptions, GetByIdOptions, ModifyRelationOptions, Query } from '@ptc-org/nestjs-query-core';
|
|
2
|
+
import { DocumentType, ReturnModelType } from '@typegoose/typegoose';
|
|
3
|
+
import { Base } from '@typegoose/typegoose/lib/defaultClasses';
|
|
4
|
+
import { FilterQueryBuilder } from '../query';
|
|
5
|
+
export declare abstract class ReferenceQueryService<Entity extends Base> {
|
|
6
|
+
readonly Model: ReturnModelType<new () => Entity>;
|
|
7
|
+
abstract readonly filterQueryBuilder: FilterQueryBuilder<Entity>;
|
|
8
|
+
protected constructor(Model: ReturnModelType<new () => Entity>);
|
|
9
|
+
abstract getById(id: string | number, opts?: GetByIdOptions<Entity>): Promise<DocumentType<Entity>>;
|
|
10
|
+
aggregateRelations<Relation>(RelationClass: Class<Relation>, relationName: string, entities: DocumentType<Entity>[], filter: Filter<Relation>, aggregate: AggregateQuery<Relation>): Promise<Map<DocumentType<Entity>, AggregateResponse<DocumentType<Relation>>[]>>;
|
|
11
|
+
aggregateRelations<Relation>(RelationClass: Class<Relation>, relationName: string, dto: DocumentType<Entity>, filter: Filter<Relation>, aggregate: AggregateQuery<Relation>): Promise<AggregateResponse<DocumentType<Relation>>[]>;
|
|
12
|
+
countRelations<Relation>(RelationClass: Class<Relation>, relationName: string, entities: DocumentType<Entity>[], filter: Filter<Relation>): Promise<Map<DocumentType<Entity>, number>>;
|
|
13
|
+
countRelations<Relation>(RelationClass: Class<Relation>, relationName: string, dto: DocumentType<Entity>, filter: Filter<Relation>): Promise<number>;
|
|
14
|
+
findRelation<Relation>(RelationClass: Class<Relation>, relationName: string, dtos: DocumentType<Entity>[], opts?: FindRelationOptions<Relation>): Promise<Map<Entity, Relation | undefined>>;
|
|
15
|
+
findRelation<Relation>(RelationClass: Class<Relation>, relationName: string, dto: DocumentType<Entity>, opts?: FindRelationOptions<Relation>): Promise<DocumentType<Relation> | undefined>;
|
|
16
|
+
queryRelations<Relation>(RelationClass: Class<Relation>, relationName: string, entities: DocumentType<Entity>[], query: Query<Relation>): Promise<Map<DocumentType<Entity>, DocumentType<Relation>[]>>;
|
|
17
|
+
queryRelations<Relation>(RelationClass: Class<Relation>, relationName: string, dto: DocumentType<Entity>, query: Query<Relation>): Promise<DocumentType<Relation>[]>;
|
|
18
|
+
addRelations<Relation>(relationName: string, id: string, relationIds: (string | number)[], opts?: ModifyRelationOptions<Entity, Relation>): Promise<DocumentType<Entity>>;
|
|
19
|
+
setRelations<Relation>(relationName: string, id: string, relationIds: (string | number)[], opts?: ModifyRelationOptions<Entity, Relation>): Promise<DocumentType<Entity>>;
|
|
20
|
+
setRelation<Relation>(relationName: string, id: string | number, relationId: string | number, opts?: ModifyRelationOptions<Entity, Relation>): Promise<DocumentType<Entity>>;
|
|
21
|
+
removeRelation<Relation>(relationName: string, id: string | number, relationId: string | number, opts?: ModifyRelationOptions<Entity, Relation>): Promise<DocumentType<Entity>>;
|
|
22
|
+
removeRelations<Relation>(relationName: string, id: string | number, relationIds: string[] | number[], opts?: ModifyRelationOptions<Entity, Relation>): Promise<DocumentType<Entity>>;
|
|
23
|
+
private getReferenceEntity;
|
|
24
|
+
private isReferencePath;
|
|
25
|
+
private isVirtualPath;
|
|
26
|
+
private getReferenceFilter;
|
|
27
|
+
private getObjectIdReferenceFilter;
|
|
28
|
+
private getVirtualReferenceFilter;
|
|
29
|
+
private getReferenceModel;
|
|
30
|
+
private getRefCount;
|
|
31
|
+
private getReferenceQueryBuilder;
|
|
32
|
+
private checkForReference;
|
|
33
|
+
private findAndUpdate;
|
|
34
|
+
}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ReferenceQueryService = void 0;
|
|
4
|
+
/* eslint-disable no-underscore-dangle */
|
|
5
|
+
const common_1 = require("@nestjs/common");
|
|
6
|
+
const nestjs_query_core_1 = require("@ptc-org/nestjs-query-core");
|
|
7
|
+
const typegoose_1 = require("@typegoose/typegoose");
|
|
8
|
+
const query_1 = require("../query");
|
|
9
|
+
const typegoose_types_helper_1 = require("../typegoose-types.helper");
|
|
10
|
+
class ReferenceQueryService {
|
|
11
|
+
constructor(Model) {
|
|
12
|
+
this.Model = Model;
|
|
13
|
+
}
|
|
14
|
+
async aggregateRelations(RelationClass, relationName, dto, filter, aggregateQuery) {
|
|
15
|
+
this.checkForReference('AggregateRelations', relationName);
|
|
16
|
+
const relationModel = this.getReferenceModel(relationName);
|
|
17
|
+
const referenceQueryBuilder = this.getReferenceQueryBuilder(relationName);
|
|
18
|
+
if (Array.isArray(dto)) {
|
|
19
|
+
return dto.reduce(async (mapPromise, entity) => {
|
|
20
|
+
const map = await mapPromise;
|
|
21
|
+
const refs = await this.aggregateRelations(RelationClass, relationName, entity, filter, aggregateQuery);
|
|
22
|
+
return map.set(entity, refs);
|
|
23
|
+
}, Promise.resolve(new Map()));
|
|
24
|
+
}
|
|
25
|
+
const assembler = nestjs_query_core_1.AssemblerFactory.getAssembler(RelationClass, this.getReferenceEntity(relationName));
|
|
26
|
+
const refFilter = this.getReferenceFilter(relationName, dto, assembler.convertQuery({ filter }).filter);
|
|
27
|
+
if (!refFilter) {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
const { filterQuery, aggregate, options } = referenceQueryBuilder.buildAggregateQuery(assembler.convertAggregateQuery(aggregateQuery), refFilter);
|
|
31
|
+
const aggPipeline = [{ $match: filterQuery }, { $group: aggregate }];
|
|
32
|
+
if (options.sort) {
|
|
33
|
+
aggPipeline.push({ $sort: options.sort ?? {} });
|
|
34
|
+
}
|
|
35
|
+
const aggResult = await relationModel.aggregate(aggPipeline).exec();
|
|
36
|
+
return query_1.AggregateBuilder.convertToAggregateResponse(aggResult);
|
|
37
|
+
}
|
|
38
|
+
async countRelations(RelationClass, relationName, dto, filter) {
|
|
39
|
+
this.checkForReference('CountRelations', relationName);
|
|
40
|
+
if (Array.isArray(dto)) {
|
|
41
|
+
return dto.reduce(async (mapPromise, entity) => {
|
|
42
|
+
const map = await mapPromise;
|
|
43
|
+
const refs = await this.countRelations(RelationClass, relationName, entity, filter);
|
|
44
|
+
return map.set(entity, refs);
|
|
45
|
+
}, Promise.resolve(new Map()));
|
|
46
|
+
}
|
|
47
|
+
const assembler = nestjs_query_core_1.AssemblerFactory.getAssembler(RelationClass, this.getReferenceEntity(relationName));
|
|
48
|
+
const relationModel = this.getReferenceModel(relationName);
|
|
49
|
+
const referenceQueryBuilder = this.getReferenceQueryBuilder(relationName);
|
|
50
|
+
const refFilter = this.getReferenceFilter(relationName, dto, assembler.convertQuery({ filter }).filter);
|
|
51
|
+
if (!refFilter) {
|
|
52
|
+
return 0;
|
|
53
|
+
}
|
|
54
|
+
return relationModel.countDocuments(referenceQueryBuilder.buildFilterQuery(refFilter)).exec();
|
|
55
|
+
}
|
|
56
|
+
async findRelation(RelationClass, relationName, dto, opts) {
|
|
57
|
+
this.checkForReference('FindRelation', relationName);
|
|
58
|
+
const referenceQueryBuilder = this.getReferenceQueryBuilder(relationName);
|
|
59
|
+
if (Array.isArray(dto)) {
|
|
60
|
+
return dto.reduce(async (prev, curr) => {
|
|
61
|
+
const map = await prev;
|
|
62
|
+
const ref = await this.findRelation(RelationClass, relationName, curr, opts);
|
|
63
|
+
return map.set(curr, ref);
|
|
64
|
+
}, Promise.resolve(new Map()));
|
|
65
|
+
}
|
|
66
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
67
|
+
const foundEntity = await this.Model.findById(dto._id ?? dto.id);
|
|
68
|
+
if (!foundEntity) {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
const assembler = nestjs_query_core_1.AssemblerFactory.getAssembler(RelationClass, this.getReferenceEntity(relationName));
|
|
72
|
+
const filterQuery = referenceQueryBuilder.buildFilterQuery(assembler.convertQuery({ filter: opts?.filter }).filter);
|
|
73
|
+
const populated = await foundEntity.populate({ path: relationName, match: filterQuery });
|
|
74
|
+
const populatedRef = populated.get(relationName);
|
|
75
|
+
return populatedRef ? assembler.convertToDTO(populatedRef) : undefined;
|
|
76
|
+
}
|
|
77
|
+
async queryRelations(RelationClass, relationName, dto, query) {
|
|
78
|
+
this.checkForReference('QueryRelations', relationName);
|
|
79
|
+
const referenceQueryBuilder = this.getReferenceQueryBuilder(relationName);
|
|
80
|
+
if (Array.isArray(dto)) {
|
|
81
|
+
return dto.reduce(async (mapPromise, entity) => {
|
|
82
|
+
const map = await mapPromise;
|
|
83
|
+
const refs = await this.queryRelations(RelationClass, relationName, entity, query);
|
|
84
|
+
return map.set(entity, refs);
|
|
85
|
+
}, Promise.resolve(new Map()));
|
|
86
|
+
}
|
|
87
|
+
const foundEntity = await this.Model.findById(dto._id ?? dto.id);
|
|
88
|
+
if (!foundEntity) {
|
|
89
|
+
return [];
|
|
90
|
+
}
|
|
91
|
+
const assembler = nestjs_query_core_1.AssemblerFactory.getAssembler(RelationClass, this.getReferenceEntity(relationName));
|
|
92
|
+
const { filterQuery, options } = referenceQueryBuilder.buildQuery(assembler.convertQuery(query));
|
|
93
|
+
const populated = await foundEntity.populate({ path: relationName, match: filterQuery, options });
|
|
94
|
+
return assembler.convertToDTOs(populated.get(relationName));
|
|
95
|
+
}
|
|
96
|
+
async addRelations(relationName, id, relationIds, opts) {
|
|
97
|
+
this.checkForReference('AddRelations', relationName, false);
|
|
98
|
+
const refCount = await this.getRefCount(relationName, relationIds, opts?.relationFilter);
|
|
99
|
+
if (relationIds.length !== refCount) {
|
|
100
|
+
throw new Error(`Unable to find all ${relationName} to add to ${this.Model.modelName}`);
|
|
101
|
+
}
|
|
102
|
+
return this.findAndUpdate(id, opts?.filter, {
|
|
103
|
+
$push: { [relationName]: { $each: relationIds } }
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
async setRelations(relationName, id, relationIds, opts) {
|
|
107
|
+
this.checkForReference('AddRelations', relationName, false);
|
|
108
|
+
const refCount = await this.getRefCount(relationName, relationIds, opts?.relationFilter);
|
|
109
|
+
if (relationIds.length !== refCount) {
|
|
110
|
+
throw new Error(`Unable to find all ${relationName} to set on ${this.Model.modelName}`);
|
|
111
|
+
}
|
|
112
|
+
return this.findAndUpdate(id, opts?.filter, { [relationName]: relationIds });
|
|
113
|
+
}
|
|
114
|
+
async setRelation(relationName, id, relationId, opts) {
|
|
115
|
+
this.checkForReference('SetRelation', relationName, false);
|
|
116
|
+
const refCount = await this.getRefCount(relationName, [relationId], opts?.relationFilter);
|
|
117
|
+
if (refCount !== 1) {
|
|
118
|
+
throw new Error(`Unable to find ${relationName} to set on ${this.Model.modelName}`);
|
|
119
|
+
}
|
|
120
|
+
return this.findAndUpdate(id, opts?.filter, { [relationName]: relationId });
|
|
121
|
+
}
|
|
122
|
+
async removeRelation(relationName, id, relationId, opts) {
|
|
123
|
+
this.checkForReference('RemoveRelation', relationName, false);
|
|
124
|
+
const refCount = await this.getRefCount(relationName, [relationId], opts?.relationFilter);
|
|
125
|
+
if (refCount !== 1) {
|
|
126
|
+
throw new Error(`Unable to find ${relationName} to remove from ${this.Model.modelName}`);
|
|
127
|
+
}
|
|
128
|
+
await this.findAndUpdate(id, opts?.filter, { $unset: { [relationName]: relationId } });
|
|
129
|
+
// reload the document
|
|
130
|
+
return this.getById(id);
|
|
131
|
+
}
|
|
132
|
+
async removeRelations(relationName, id, relationIds, opts) {
|
|
133
|
+
this.checkForReference('RemoveRelations', relationName, false);
|
|
134
|
+
const refCount = await this.getRefCount(relationName, relationIds, opts?.relationFilter);
|
|
135
|
+
if (relationIds.length !== refCount) {
|
|
136
|
+
throw new Error(`Unable to find all ${relationName} to remove from ${this.Model.modelName}`);
|
|
137
|
+
}
|
|
138
|
+
if (this.isVirtualPath(relationName)) {
|
|
139
|
+
throw new Error(`RemoveRelations not supported for virtual relation ${relationName}`);
|
|
140
|
+
}
|
|
141
|
+
await this.findAndUpdate(id, opts?.filter, { $pullAll: { [relationName]: relationIds } });
|
|
142
|
+
// reload the document
|
|
143
|
+
return this.getById(id);
|
|
144
|
+
}
|
|
145
|
+
getReferenceEntity(relationName) {
|
|
146
|
+
const ReferenceModel = this.getReferenceModel(relationName);
|
|
147
|
+
return (0, typegoose_1.getClass)(ReferenceModel.modelName);
|
|
148
|
+
}
|
|
149
|
+
isReferencePath(refName) {
|
|
150
|
+
return !!this.Model.schema.path(refName);
|
|
151
|
+
}
|
|
152
|
+
isVirtualPath(refName) {
|
|
153
|
+
return !!this.Model.schema.virtualpath(refName);
|
|
154
|
+
}
|
|
155
|
+
getReferenceFilter(refName, entity, filter) {
|
|
156
|
+
if (this.isReferencePath(refName)) {
|
|
157
|
+
return this.getObjectIdReferenceFilter(refName, entity, filter);
|
|
158
|
+
}
|
|
159
|
+
if (this.isVirtualPath(refName)) {
|
|
160
|
+
const virtualType = this.Model.schema.virtualpath(refName);
|
|
161
|
+
if ((0, typegoose_types_helper_1.isVirtualTypeWithReferenceOptions)(virtualType)) {
|
|
162
|
+
return this.getVirtualReferenceFilter(virtualType, entity, filter);
|
|
163
|
+
}
|
|
164
|
+
throw new Error(`Unable to lookup reference type for ${refName}`);
|
|
165
|
+
}
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
getObjectIdReferenceFilter(refName, entity, filter) {
|
|
169
|
+
const referenceIds = entity[refName];
|
|
170
|
+
const refFilter = {
|
|
171
|
+
_id: { [Array.isArray(referenceIds) ? 'in' : 'eq']: referenceIds }
|
|
172
|
+
};
|
|
173
|
+
return (0, nestjs_query_core_1.mergeFilter)(filter ?? {}, refFilter);
|
|
174
|
+
}
|
|
175
|
+
getVirtualReferenceFilter(virtualType, entity, filter) {
|
|
176
|
+
const { foreignField, localField } = virtualType.options;
|
|
177
|
+
const refVal = entity[localField];
|
|
178
|
+
const isArray = Array.isArray(refVal);
|
|
179
|
+
const lookupFilter = {
|
|
180
|
+
[foreignField]: { [isArray ? 'in' : 'eq']: refVal }
|
|
181
|
+
};
|
|
182
|
+
return (0, nestjs_query_core_1.mergeFilter)(filter ?? {}, lookupFilter);
|
|
183
|
+
}
|
|
184
|
+
getReferenceModel(refName) {
|
|
185
|
+
let refModel;
|
|
186
|
+
if (this.isReferencePath(refName)) {
|
|
187
|
+
const schemaType = this.Model.schema.path(refName);
|
|
188
|
+
if ((0, typegoose_types_helper_1.isEmbeddedSchemaTypeOptions)(schemaType)) {
|
|
189
|
+
refModel = (0, typegoose_1.getModelWithString)(schemaType.$embeddedSchemaType.options.ref);
|
|
190
|
+
}
|
|
191
|
+
else if ((0, typegoose_types_helper_1.isSchemaTypeWithReferenceOptions)(schemaType)) {
|
|
192
|
+
refModel = (0, typegoose_1.getModelWithString)(schemaType.options.ref);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
else if (this.isVirtualPath(refName)) {
|
|
196
|
+
const schemaType = this.Model.schema.virtualpath(refName);
|
|
197
|
+
if ((0, typegoose_types_helper_1.isVirtualTypeWithReferenceOptions)(schemaType)) {
|
|
198
|
+
refModel = (0, typegoose_1.getModelWithString)(schemaType.options.ref);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if (!refModel) {
|
|
202
|
+
throw new Error(`Unable to lookup reference type for ${refName}`);
|
|
203
|
+
}
|
|
204
|
+
return refModel;
|
|
205
|
+
}
|
|
206
|
+
getRefCount(relationName, relationIds, filter) {
|
|
207
|
+
const referenceModel = this.getReferenceModel(relationName);
|
|
208
|
+
const referenceQueryBuilder = this.getReferenceQueryBuilder(relationName);
|
|
209
|
+
return referenceModel.countDocuments(referenceQueryBuilder.buildIdFilterQuery(relationIds, filter)).exec();
|
|
210
|
+
}
|
|
211
|
+
getReferenceQueryBuilder(refName) {
|
|
212
|
+
return new query_1.FilterQueryBuilder(this.getReferenceModel(refName));
|
|
213
|
+
}
|
|
214
|
+
checkForReference(operation, refName, allowVirtual = true) {
|
|
215
|
+
if (this.isReferencePath(refName)) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
if (this.isVirtualPath(refName)) {
|
|
219
|
+
if (allowVirtual) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
throw new Error(`${operation} not supported for virtual relation ${refName}`);
|
|
223
|
+
}
|
|
224
|
+
throw new Error(`Unable to find reference ${refName} on ${this.Model.modelName}`);
|
|
225
|
+
}
|
|
226
|
+
async findAndUpdate(id, filter, query) {
|
|
227
|
+
const entity = await this.Model.findOneAndUpdate(this.filterQueryBuilder.buildIdFilterQuery(id, filter), query, {
|
|
228
|
+
new: true
|
|
229
|
+
}).exec();
|
|
230
|
+
if (!entity) {
|
|
231
|
+
throw new common_1.NotFoundException(`Unable to find ${this.Model.modelName} with id: ${id}`);
|
|
232
|
+
}
|
|
233
|
+
return entity;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
exports.ReferenceQueryService = ReferenceQueryService;
|
|
237
|
+
//# sourceMappingURL=reference-query.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reference-query.service.js","sourceRoot":"","sources":["../../../../../packages/query-typegoose/src/services/reference-query.service.ts"],"names":[],"mappings":";;;AAAA,yCAAyC;AACzC,2CAAkD;AAClD,kEAWmC;AACnC,oDAA4G;AAI5G,oCAA+D;AAC/D,sEAKkC;AAElC,MAAsB,qBAAqB;IAGzC,YAA+B,KAAwC;QAAxC,UAAK,GAAL,KAAK,CAAmC;IAAG,CAAC;IAoBpE,KAAK,CAAC,kBAAkB,CAC7B,aAA8B,EAC9B,YAAoB,EACpB,GAAkD,EAClD,MAAwB,EACxB,cAAwC;QAIxC,IAAI,CAAC,iBAAiB,CAAC,oBAAoB,EAAE,YAAY,CAAC,CAAA;QAC1D,MAAM,aAAa,GAAG,IAAI,CAAC,iBAAiB,CAAC,YAAY,CAAC,CAAA;QAC1D,MAAM,qBAAqB,GAAG,IAAI,CAAC,wBAAwB,CAAC,YAAY,CAAC,CAAA;QACzE,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;YACtB,OAAO,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,EAAE;gBAC7C,MAAM,GAAG,GAAG,MAAM,UAAU,CAAA;gBAC5B,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,kBAAkB,CAAC,aAAa,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,cAAc,CAAC,CAAA;gBACvG,OAAO,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;YAC9B,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,IAAI,GAAG,EAAqE,CAAC,CAAC,CAAA;SAClG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AI,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC,CAAA;IACrE,CAAC;IAEO,iBAAiB,CAAC,SAAiB,EAAE,OAAe,EAAE,YAAY,GAAG,IAAI;QAC/E,IAAI,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,EAAE;YACjC,OAAM;SACP;QACD,IAAI,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE;YAC/B,IAAI,YAAY,EAAE;gBAChB,OAAM;aACP;YACD,MAAM,IAAI,KAAK,CAAC,GAAG,SAAS,uCAAuC,OAAO,EAAE,CAAC,CAAA;SAC9E;QACD,MAAM,IAAI,KAAK,CAAC,4BAA4B,OAAO,OAAO,IAAI,CAAC,KAAK,CAAC,SAAS,EAAE,CAAC,CAAA;IACnF,CAAC;IAEO,KAAK,CAAC,aAAa,CACzB,EAAmB,EACnB,MAAsB,EACtB,KAAiD;QAEjD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,gBAAgB,CAAC,IAAI,CAAC,kBAAkB,CAAC,kBAAkB,CAAC,EAAE,EAAE,MAAM,CAAC,EAAE,KAAK,EAAE;YAC9G,GAAG,EAAE,IAAI;SACV,CAAC,CAAC,IAAI,EAAE,CAAA;QACT,IAAI,CAAC,MAAM,EAAE;YACX,MAAM,IAAI,0BAAiB,CAAC,kBAAkB,IAAI,CAAC,KAAK,CAAC,SAAS,aAAa,EAAE,EAAE,CAAC,CAAA;SACrF;QAED,OAAO,MAAM,CAAA;IACf,CAAC;CACF;AA/XD,sDA+XC"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { AggregateQuery, AggregateResponse, DeepPartial, DeleteManyResponse, DeleteOneOptions, Filter, FindByIdOptions, GetByIdOptions, Query, QueryService, UpdateManyResponse, UpdateOneOptions } from '@ptc-org/nestjs-query-core';
|
|
2
|
+
import { DocumentType, mongoose, ReturnModelType } from '@typegoose/typegoose';
|
|
3
|
+
import { Base } from '@typegoose/typegoose/lib/defaultClasses';
|
|
4
|
+
import { FilterQueryBuilder } from '../query';
|
|
5
|
+
import { ReferenceQueryService } from './reference-query.service';
|
|
6
|
+
export interface TypegooseQueryServiceOpts {
|
|
7
|
+
toObjectOptions?: mongoose.ToObjectOptions;
|
|
8
|
+
}
|
|
9
|
+
export declare class TypegooseQueryService<Entity extends Base> extends ReferenceQueryService<Entity> implements QueryService<Entity> {
|
|
10
|
+
readonly Model: ReturnModelType<new () => Entity>;
|
|
11
|
+
readonly filterQueryBuilder: FilterQueryBuilder<Entity>;
|
|
12
|
+
constructor(Model: ReturnModelType<new () => Entity>, filterQueryBuilder?: FilterQueryBuilder<Entity>);
|
|
13
|
+
/**
|
|
14
|
+
* Query for multiple entities, using a Query from `@ptc-org/nestjs-query-core`.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const todoItems = await this.service.query({
|
|
19
|
+
* filter: { title: { eq: 'Foo' } },
|
|
20
|
+
* paging: { limit: 10 },
|
|
21
|
+
* sorting: [{ field: "create", direction: SortDirection.DESC }],
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
24
|
+
* @param query - The Query used to filter, page, and sort rows.
|
|
25
|
+
*/
|
|
26
|
+
query(query: Query<Entity>): Promise<DocumentType<Entity>[]>;
|
|
27
|
+
aggregate(filter: Filter<Entity>, aggregateQuery: AggregateQuery<Entity>): Promise<AggregateResponse<Entity>[]>;
|
|
28
|
+
count(filter: Filter<Entity>): Promise<number>;
|
|
29
|
+
/**
|
|
30
|
+
* Find an entity by it's `id`.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* const todoItem = await this.service.findById(1);
|
|
35
|
+
* ```
|
|
36
|
+
* @param id - The id of the record to find.
|
|
37
|
+
* @param opts - Additional options
|
|
38
|
+
*/
|
|
39
|
+
findById(id: string | number, opts?: FindByIdOptions<Entity>): Promise<DocumentType<Entity> | undefined>;
|
|
40
|
+
/**
|
|
41
|
+
* Gets an entity by it's `id`. If the entity is not found a rejected promise is returned.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* try {
|
|
46
|
+
* const todoItem = await this.service.getById(1);
|
|
47
|
+
* } catch(e) {
|
|
48
|
+
* console.error('Unable to find entity with id = 1');
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
* @param id - The id of the record to find.
|
|
52
|
+
* @param opts - Additional options
|
|
53
|
+
*/
|
|
54
|
+
getById(id: string, opts?: GetByIdOptions<Entity>): Promise<DocumentType<Entity>>;
|
|
55
|
+
/**
|
|
56
|
+
* Creates a single entity.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* const todoItem = await this.service.createOne({title: 'Todo Item', completed: false });
|
|
61
|
+
* ```
|
|
62
|
+
* @param record - The entity to create.
|
|
63
|
+
*/
|
|
64
|
+
createOne(record: DeepPartial<Entity>): Promise<DocumentType<Entity>>;
|
|
65
|
+
/**
|
|
66
|
+
* Create multiple entities.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* const todoItem = await this.service.createMany([
|
|
71
|
+
* {title: 'Todo Item 1', completed: false },
|
|
72
|
+
* {title: 'Todo Item 2', completed: true },
|
|
73
|
+
* ]);
|
|
74
|
+
* ```
|
|
75
|
+
* @param records - The entities to create.
|
|
76
|
+
*/
|
|
77
|
+
createMany(records: DeepPartial<Entity>[]): Promise<DocumentType<Entity>[]>;
|
|
78
|
+
/**
|
|
79
|
+
* Update an entity.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* const updatedEntity = await this.service.updateOne(1, { completed: true });
|
|
84
|
+
* ```
|
|
85
|
+
* @param id - The `id` of the record.
|
|
86
|
+
* @param update - A `Partial` of the entity with fields to update.
|
|
87
|
+
* @param opts - Additional options
|
|
88
|
+
*/
|
|
89
|
+
updateOne(id: string, update: DeepPartial<Entity>, opts?: UpdateOneOptions<Entity>): Promise<DocumentType<Entity>>;
|
|
90
|
+
/**
|
|
91
|
+
* Update multiple entities with a `@ptc-org/nestjs-query-core` Filter.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* const { updatedCount } = await this.service.updateMany(
|
|
96
|
+
* { completed: true }, // the update to apply
|
|
97
|
+
* { title: { eq: 'Foo Title' } } // Filter to find records to update
|
|
98
|
+
* );
|
|
99
|
+
* ```
|
|
100
|
+
* @param update - A `Partial` of entity with the fields to update
|
|
101
|
+
* @param filter - A Filter used to find the records to update
|
|
102
|
+
*/
|
|
103
|
+
updateMany(update: DeepPartial<Entity>, filter: Filter<Entity>): Promise<UpdateManyResponse>;
|
|
104
|
+
/**
|
|
105
|
+
* Delete an entity by `id`.
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
*
|
|
109
|
+
* ```ts
|
|
110
|
+
* const deletedTodo = await this.service.deleteOne(1);
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* @param id - The `id` of the entity to delete.
|
|
114
|
+
* @param opts - Additional filter to use when finding the entity to delete.
|
|
115
|
+
*/
|
|
116
|
+
deleteOne(id: string, opts?: DeleteOneOptions<Entity>): Promise<DocumentType<Entity>>;
|
|
117
|
+
/**
|
|
118
|
+
* Delete multiple records with a `@ptc-org/nestjs-query-core` `Filter`.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
*
|
|
122
|
+
* ```ts
|
|
123
|
+
* const { deletedCount } = this.service.deleteMany({
|
|
124
|
+
* created: { lte: new Date('2020-1-1') }
|
|
125
|
+
* });
|
|
126
|
+
* ```
|
|
127
|
+
*
|
|
128
|
+
* @param filter - A `Filter` to find records to delete.
|
|
129
|
+
*/
|
|
130
|
+
deleteMany(filter: Filter<Entity>): Promise<DeleteManyResponse>;
|
|
131
|
+
private ensureIdIsNotPresent;
|
|
132
|
+
private getUpdateQuery;
|
|
133
|
+
private buildArrayUpdateQuery;
|
|
134
|
+
}
|