From a7da534e06670614ce2d5390a44145aa67868257 Mon Sep 17 00:00:00 2001 From: Denis Bykhov Date: Fri, 20 Jan 2023 21:48:05 +0600 Subject: [PATCH] Mixin projection fix (#2527) Signed-off-by: Denis Bykhov --- packages/core/src/storage.ts | 11 ++++++++--- server/mongo/src/storage.ts | 15 +++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/core/src/storage.ts b/packages/core/src/storage.ts index becca9f244..d44eaf6236 100644 --- a/packages/core/src/storage.ts +++ b/packages/core/src/storage.ts @@ -105,6 +105,13 @@ export interface ReverseLookup { */ export type Lookup = Refs | ReverseLookups | (Refs & ReverseLookups) +/** + * @public + */ +export type Projection = { + [P in keyof T]?: 0 | 1 +} + /** * @public */ @@ -113,9 +120,7 @@ export type FindOptions = { limit?: number sort?: SortingQuery lookup?: Lookup - projection?: { - [P in keyof T]?: 0 | 1 - } + projection?: Projection } /** diff --git a/server/mongo/src/storage.ts b/server/mongo/src/storage.ts index f32e4483e9..bf393521f8 100644 --- a/server/mongo/src/storage.ts +++ b/server/mongo/src/storage.ts @@ -29,6 +29,7 @@ import core, { Lookup, Mixin, ModelDb, + Projection, QueryUpdate, Ref, ReverseLookups, @@ -349,7 +350,12 @@ abstract class MongoAdapterBase extends TxProcessor { } } if (options?.projection !== undefined) { - resultPipeline.push({ $project: options.projection }) + const projection: Projection = {} + for (const key in options.projection) { + const ckey = this.checkMixinKey(key, clazz) as keyof T + projection[ckey] = options.projection[key] + } + resultPipeline.push({ $project: projection }) } pipeline.push({ $facet: { @@ -438,7 +444,12 @@ abstract class MongoAdapterBase extends TxProcessor { let cursor = coll.find(this.translateQuery(_class, query)) if (options?.projection !== undefined) { - cursor = cursor.project(options.projection) + const projection: Projection = {} + for (const key in options.projection) { + const ckey = this.checkMixinKey(key, _class) as keyof T + projection[ckey] = options.projection[key] + } + cursor = cursor.project(projection) } let total: number | undefined if (options !== null && options !== undefined) {