From 04efff3b49c9a65ada065dfe65d5f06814da8f36 Mon Sep 17 00:00:00 2001 From: Andrey Sobolev Date: Wed, 2 Feb 2022 16:04:13 +0700 Subject: [PATCH] Fix mixin sorting (#905) Signed-off-by: Andrey Sobolev --- server/mongo/src/storage.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/server/mongo/src/storage.ts b/server/mongo/src/storage.ts index fee1a959d5..eaff28a6fb 100644 --- a/server/mongo/src/storage.ts +++ b/server/mongo/src/storage.ts @@ -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(key, clazz) } sort[key] = options.sort[_key] === SortingOrder.Ascending ? 1 : -1 } @@ -242,6 +244,21 @@ abstract class MongoAdapterBase extends TxProcessor { return result } + private checkMixinKey(key: string, clazz: Ref>): 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( _class: Ref>, query: DocumentQuery, @@ -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(key, _class) const order = options.sort[key] === SortingOrder.Ascending ? 1 : -1 - sort[key] = order + sort[ckey] = order } cursor = cursor.sort(sort) }