From aa0851654def245feee37c8de3288ee71cedf9b7 Mon Sep 17 00:00:00 2001 From: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com> Date: Mon, 11 Apr 2022 15:49:05 +0600 Subject: [PATCH] Fix total (#1355) Signed-off-by: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com> --- packages/platform/src/rpc.ts | 25 +++++++++++++++++++++++-- server/mongo/src/storage.ts | 16 ++++++++++------ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/packages/platform/src/rpc.ts b/packages/platform/src/rpc.ts index 18264ae623..913869e1ac 100644 --- a/packages/platform/src/rpc.ts +++ b/packages/platform/src/rpc.ts @@ -48,7 +48,7 @@ export interface Response { * @returns */ export function protoSerialize (object: object): string { - return JSON.stringify(object, (key, value) => { return value ?? null }) + return JSON.stringify(object, replacer) } /** @@ -57,7 +57,7 @@ export function protoSerialize (object: object): string { * @returns */ export function protoDeserialize (data: string): any { - return JSON.parse(data) + return JSON.parse(data, receiver) } /** @@ -78,6 +78,27 @@ export function readResponse (response: string): Response { return protoDeserialize(response) } +function replacer (key: string, value: any): any { + if (Array.isArray(value) && (value as any).total !== undefined) { + return { + dataType: 'TotalArray', + total: (value as any).total, + value: [...value] + } + } else { + return value ?? null + } +} + +function receiver (key: string, value: any): any { + if (typeof value === 'object' && value !== null) { + if (value.dataType === 'TotalArray') { + return Object.assign(value.value, { total: value.total }) + } + } + return value +} + /** * @public * @param request - diff --git a/server/mongo/src/storage.ts b/server/mongo/src/storage.ts index 71af7e2c85..247e9a927b 100644 --- a/server/mongo/src/storage.ts +++ b/server/mongo/src/storage.ts @@ -38,7 +38,8 @@ import core, { TxRemoveDoc, TxResult, TxUpdateDoc, - toFindResult + toFindResult, + WithLookup } from '@anticrm/core' import type { DbAdapter, TxAdapter } from '@anticrm/server-core' import { Collection, Db, Document, Filter, MongoClient, Sort } from 'mongodb' @@ -303,21 +304,24 @@ abstract class MongoAdapterBase extends TxProcessor { } pipeline.push({ $sort: sort }) } - if (options.limit !== undefined) { - pipeline.push({ $limit: options.limit }) - } const domain = this.hierarchy.getDomain(clazz) let cursor = this.db.collection(domain).aggregate(pipeline) if (options?.projection !== undefined) { cursor = cursor.project(options.projection) } - const result = (await cursor.toArray()) as FindResult + let result = (await cursor.toArray()) as WithLookup[] + + const total = result.length + if (options.limit !== undefined) { + result = result.slice(0, options.limit) + } + for (const row of result) { row.$lookup = {} await this.fillLookupValue(options.lookup, row) this.clearExtraLookups(row) } - return result + return toFindResult(result, total) } private clearExtraLookups (row: any): void {