From a5ccda96c485167193b31aef3dbcff8ba739069b Mon Sep 17 00:00:00 2001 From: Andrey Platov Date: Tue, 28 Sep 2021 22:47:17 +0200 Subject: [PATCH] fix `mongo` $inc operator Signed-off-by: Andrey Platov --- packages/core/src/operator.ts | 9 +++++++++ server/mongo/src/storage.ts | 6 ++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/core/src/operator.ts b/packages/core/src/operator.ts index 561aedfa5c..74f1101229 100644 --- a/packages/core/src/operator.ts +++ b/packages/core/src/operator.ts @@ -78,6 +78,15 @@ const operators: Record = { $inc } +/** + * @public + */ +export function isOperator (o: Record): boolean { + if (o === null || typeof o !== 'object') { return false } + const keys = Object.keys(o) + return keys.length > 0 && keys.every((key) => key.startsWith('$')) +} + /** * @internal * @param name - diff --git a/server/mongo/src/storage.ts b/server/mongo/src/storage.ts index 608978515e..a41ac8e623 100644 --- a/server/mongo/src/storage.ts +++ b/server/mongo/src/storage.ts @@ -13,7 +13,8 @@ // limitations under the License. // -import core, { Tx, Ref, Doc, Class, DocumentQuery, FindResult, FindOptions, TxCreateDoc, TxUpdateDoc, TxMixin, TxPutBag, TxRemoveDoc, TxProcessor, Hierarchy, DOMAIN_TX, SortingOrder } from '@anticrm/core' +import type { Tx, Ref, Doc, Class, DocumentQuery, FindResult, FindOptions, TxCreateDoc, TxUpdateDoc, TxMixin, TxPutBag, TxRemoveDoc } from '@anticrm/core' +import core, { DOMAIN_TX, SortingOrder, TxProcessor, Hierarchy, isOperator } from '@anticrm/core' import type { DbAdapter, TxAdapter } from '@anticrm/server-core' @@ -114,7 +115,8 @@ class MongoAdapter extends MongoAdapterBase { protected override async txUpdateDoc (tx: TxUpdateDoc): Promise { const domain = this.hierarchy.getDomain(tx.objectClass) - await this.db.collection(domain).updateOne({ _id: tx.objectId }, { $set: tx.operations }) + const op = isOperator(tx.operations) ? tx.operations : { $set: tx.operations } + await this.db.collection(domain).updateOne({ _id: tx.objectId }, op) } override tx (tx: Tx): Promise {