From 8977fd2cee8090a5e7ab17c1951c9acc51bd6793 Mon Sep 17 00:00:00 2001 From: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com> Date: Mon, 16 May 2022 21:39:44 +0600 Subject: [PATCH] MemDB sort and query fixes (#1765) Signed-off-by: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com> --- packages/core/src/memdb.ts | 4 ++-- packages/core/src/query.ts | 22 ++++++++++++++++------ packages/query/src/index.ts | 10 +++++----- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/packages/core/src/memdb.ts b/packages/core/src/memdb.ts index fca36f2259..540b4ee48e 100644 --- a/packages/core/src/memdb.ts +++ b/packages/core/src/memdb.ts @@ -144,7 +144,7 @@ export abstract class MemDb extends TxProcessor { result = this.getObjectsByClass(baseClass) } - result = matchQuery(result, query, _class, this.hierarchy) + result = matchQuery(result, query, _class, this.hierarchy, true) if (baseClass !== _class) { // We need to filter instances without mixin was set @@ -153,7 +153,7 @@ export abstract class MemDb extends TxProcessor { if (options?.lookup !== undefined) result = await this.lookup(result as T[], options.lookup) - if (options?.sort !== undefined) resultSort(result, options?.sort) + if (options?.sort !== undefined) resultSort(result, options?.sort, _class, this.hierarchy) const total = result.length result = result.slice(0, options?.limit) const tresult = this.hierarchy.clone(result) as WithLookup[] diff --git a/packages/core/src/query.ts b/packages/core/src/query.ts index 041c331c45..5c79470842 100644 --- a/packages/core/src/query.ts +++ b/packages/core/src/query.ts @@ -33,11 +33,16 @@ function isArrayValueCheck (val: T, value: P): boolean { /** * @public */ -export function resultSort (result: T[], sortOptions: SortingQuery): void { +export function resultSort ( + result: T[], + sortOptions: SortingQuery, + _class: Ref>, + hierarchy: Hierarchy +): void { const sortFunc = (a: any, b: any): number => { for (const key in sortOptions) { - const aValue = getValue(key, a) - const bValue = getValue(key, b) + const aValue = getValue(key, a, _class, hierarchy) + const bValue = getValue(key, b, _class, hierarchy) const result = getSortingResult(aValue, bValue, sortOptions[key]) if (result !== 0) return result } @@ -62,8 +67,9 @@ function getSortingResult (aValue: any, bValue: any, order: SortingOrder): numbe return res * order } -function getValue (key: string, obj: any): any { - let value = getObjectValue(key, obj) +function getValue (key: string, obj: any, _class: Ref>, hierarchy: Hierarchy): any { + const tkey = checkMixinKey(key, _class, hierarchy) + let value = getObjectValue(tkey, obj) if (typeof value === 'object' && !Array.isArray(value)) { value = JSON.stringify(value) } @@ -76,10 +82,14 @@ export function matchQuery ( docs: Doc[], query: DocumentQuery, clazz: Ref>, - hierarchy: Hierarchy + hierarchy: Hierarchy, + skipLookup: boolean = false ): Doc[] { let result = [...docs] for (const key in query) { + if (skipLookup && key.startsWith('$lookup.')) { + continue + } const value = (query as any)[key] const tkey = checkMixinKey(key, clazz, hierarchy) result = findProperty(result, tkey, value) diff --git a/packages/query/src/index.ts b/packages/query/src/index.ts index 60d25dacb1..de18486bc1 100644 --- a/packages/query/src/index.ts +++ b/packages/query/src/index.ts @@ -288,7 +288,7 @@ export class LiveQuery extends TxProcessor implements Client { if (needCallback) { if (q.options?.sort !== undefined) { - resultSort(q.result, q.options?.sort) + resultSort(q.result, q.options?.sort, q._class, this.getHierarchy()) } await this.callback(q) } @@ -446,7 +446,7 @@ export class LiveQuery extends TxProcessor implements Client { q.total++ if (q.options?.sort !== undefined) { - resultSort(q.result, q.options?.sort) + resultSort(q.result, q.options?.sort, q._class, this.getHierarchy()) } if (q.options?.limit !== undefined && q.result.length > q.options.limit) { @@ -480,7 +480,7 @@ export class LiveQuery extends TxProcessor implements Client { if (needCallback) { if (q.options?.sort !== undefined) { - resultSort(q.result, q.options?.sort) + resultSort(q.result, q.options?.sort, q._class, this.getHierarchy()) } await this.callback(q) } @@ -577,7 +577,7 @@ export class LiveQuery extends TxProcessor implements Client { } if (needCallback) { if (q.options?.sort !== undefined) { - resultSort(q.result, q.options?.sort) + resultSort(q.result, q.options?.sort, q._class, this.getHierarchy()) } await this.callback(q) } @@ -715,7 +715,7 @@ export class LiveQuery extends TxProcessor implements Client { let needSort = sort.modifiedBy !== undefined || sort.modifiedOn !== undefined if (!needSort) needSort = this.checkNeedSort(sort, tx) - if (needSort) resultSort(q.result as Doc[], sort) + if (needSort) resultSort(q.result as Doc[], sort, q._class, this.getHierarchy()) } private checkNeedSort (sort: SortingQuery, tx: TxUpdateDoc): boolean {