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 * @returns
*/ */
export function protoSerialize (object: object): string { 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 * @returns
*/ */
export function protoDeserialize (data: string): any { 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) 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 * @public
* @param request - * @param request -

View File

@ -38,7 +38,8 @@ import core, {
TxRemoveDoc, TxRemoveDoc,
TxResult, TxResult,
TxUpdateDoc, TxUpdateDoc,
toFindResult toFindResult,
WithLookup
} from '@anticrm/core' } from '@anticrm/core'
import type { DbAdapter, TxAdapter } from '@anticrm/server-core' import type { DbAdapter, TxAdapter } from '@anticrm/server-core'
import { Collection, Db, Document, Filter, MongoClient, Sort } from 'mongodb' import { Collection, Db, Document, Filter, MongoClient, Sort } from 'mongodb'
@ -303,21 +304,24 @@ abstract class MongoAdapterBase extends TxProcessor {
} }
pipeline.push({ $sort: sort }) pipeline.push({ $sort: sort })
} }
if (options.limit !== undefined) {
pipeline.push({ $limit: options.limit })
}
const domain = this.hierarchy.getDomain(clazz) const domain = this.hierarchy.getDomain(clazz)
let cursor = this.db.collection(domain).aggregate(pipeline) let cursor = this.db.collection(domain).aggregate(pipeline)
if (options?.projection !== undefined) { if (options?.projection !== undefined) {
cursor = cursor.project(options.projection) 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) { for (const row of result) {
row.$lookup = {} row.$lookup = {}
await this.fillLookupValue(options.lookup, row) await this.fillLookupValue(options.lookup, row)
this.clearExtraLookups(row) this.clearExtraLookups(row)
} }
return result return toFindResult(result, total)
} }
private clearExtraLookups (row: any): void { private clearExtraLookups (row: any): void {