Mixin projection fix (#2527)

Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
Denis Bykhov 2023-01-20 21:48:05 +06:00 committed by GitHub
parent 42e354f0ff
commit a7da534e06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 5 deletions

View File

@ -105,6 +105,13 @@ export interface ReverseLookup {
*/ */
export type Lookup<T extends Doc> = Refs<T> | ReverseLookups | (Refs<T> & ReverseLookups) export type Lookup<T extends Doc> = Refs<T> | ReverseLookups | (Refs<T> & ReverseLookups)
/**
* @public
*/
export type Projection<T extends Doc> = {
[P in keyof T]?: 0 | 1
}
/** /**
* @public * @public
*/ */
@ -113,9 +120,7 @@ export type FindOptions<T extends Doc> = {
limit?: number limit?: number
sort?: SortingQuery<T> sort?: SortingQuery<T>
lookup?: Lookup<T> lookup?: Lookup<T>
projection?: { projection?: Projection<T>
[P in keyof T]?: 0 | 1
}
} }
/** /**

View File

@ -29,6 +29,7 @@ import core, {
Lookup, Lookup,
Mixin, Mixin,
ModelDb, ModelDb,
Projection,
QueryUpdate, QueryUpdate,
Ref, Ref,
ReverseLookups, ReverseLookups,
@ -349,7 +350,12 @@ abstract class MongoAdapterBase extends TxProcessor {
} }
} }
if (options?.projection !== undefined) { if (options?.projection !== undefined) {
resultPipeline.push({ $project: options.projection }) const projection: Projection<T> = {}
for (const key in options.projection) {
const ckey = this.checkMixinKey<T>(key, clazz) as keyof T
projection[ckey] = options.projection[key]
}
resultPipeline.push({ $project: projection })
} }
pipeline.push({ pipeline.push({
$facet: { $facet: {
@ -438,7 +444,12 @@ abstract class MongoAdapterBase extends TxProcessor {
let cursor = coll.find<T>(this.translateQuery(_class, query)) let cursor = coll.find<T>(this.translateQuery(_class, query))
if (options?.projection !== undefined) { if (options?.projection !== undefined) {
cursor = cursor.project(options.projection) const projection: Projection<T> = {}
for (const key in options.projection) {
const ckey = this.checkMixinKey<T>(key, _class) as keyof T
projection[ckey] = options.projection[key]
}
cursor = cursor.project(projection)
} }
let total: number | undefined let total: number | undefined
if (options !== null && options !== undefined) { if (options !== null && options !== undefined) {