MemDB sort and query fixes (#1765)

Signed-off-by: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com>
This commit is contained in:
Denis Bykhov 2022-05-16 21:39:44 +06:00 committed by GitHub
parent a1c298ebca
commit 8977fd2cee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 13 deletions

View File

@ -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<T>[]

View File

@ -33,11 +33,16 @@ function isArrayValueCheck<T, P> (val: T, value: P): boolean {
/**
* @public
*/
export function resultSort<T extends Doc> (result: T[], sortOptions: SortingQuery<T>): void {
export function resultSort<T extends Doc> (
result: T[],
sortOptions: SortingQuery<T>,
_class: Ref<Class<T>>,
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<T extends Doc> (key: string, obj: any, _class: Ref<Class<T>>, 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<T extends Doc> (
docs: Doc[],
query: DocumentQuery<T>,
clazz: Ref<Class<T>>,
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)

View File

@ -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<Doc>, tx: TxUpdateDoc<Doc>): boolean {