Signed-off-by: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com>
This commit is contained in:
Denis Bykhov 2022-04-11 15:49:05 +06:00 committed by GitHub
parent 124e37d29e
commit aa0851654d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 8 deletions

View File

@ -48,7 +48,7 @@ export interface Response<R> {
* @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<D> (response: string): Response<D> {
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 -

View File

@ -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<T>
let result = (await cursor.toArray()) as WithLookup<T>[]
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 {