Fix mixin sorting (#905)

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
Andrey Sobolev 2022-02-02 16:04:13 +07:00 committed by GitHub
parent 5b644aac5c
commit 04efff3b49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -224,6 +224,8 @@ abstract class MongoAdapterBase extends TxProcessor {
key += '.'
}
}
// Check if key is belong to mixin class, we need to add prefix.
key = this.checkMixinKey<T>(key, clazz)
}
sort[key] = options.sort[_key] === SortingOrder.Ascending ? 1 : -1
}
@ -242,6 +244,21 @@ abstract class MongoAdapterBase extends TxProcessor {
return result
}
private checkMixinKey<T extends Doc>(key: string, clazz: Ref<Class<T>>): string {
if (!key.includes('.')) {
try {
const attr = this.hierarchy.getAttribute(clazz, key)
if (this.hierarchy.isMixin(attr.attributeOf)) {
// It is mixin
key = attr.attributeOf + '.' + key
}
} catch (err: any) {
// ignore, if
}
}
return key
}
async findAll<T extends Doc>(
_class: Ref<Class<T>>,
query: DocumentQuery<T>,
@ -260,8 +277,9 @@ abstract class MongoAdapterBase extends TxProcessor {
if (options.sort !== undefined) {
const sort: Sort = {}
for (const key in options.sort) {
const ckey = this.checkMixinKey<T>(key, _class)
const order = options.sort[key] === SortingOrder.Ascending ? 1 : -1
sort[key] = order
sort[ckey] = order
}
cursor = cursor.sort(sort)
}