diff --git a/packages/core/src/client.ts b/packages/core/src/client.ts index ca6b939458..c33beaf79b 100644 --- a/packages/core/src/client.ts +++ b/packages/core/src/client.ts @@ -17,6 +17,7 @@ import type { Doc, Ref, Class } from './classes' import type { Tx } from './tx' import type { Storage, DocumentQuery, FindOptions, FindResult } from './storage' +import { SortingOrder } from './storage' import { Hierarchy } from './hierarchy' import { ModelDb } from './memdb' import { DOMAIN_MODEL } from './classes' @@ -96,7 +97,7 @@ export async function createClient ( } const conn = await connect(txHander) - const txes = await conn.findAll(core.class.Tx, { objectSpace: core.space.Model }) + const txes = await conn.findAll(core.class.Tx, { objectSpace: core.space.Model }, { sort: { _id: SortingOrder.Ascending } }) const txMap = new Map, Ref>() for (const tx of txes) txMap.set(tx._id, tx._id) diff --git a/packages/core/src/storage.ts b/packages/core/src/storage.ts index 0394850884..2044414207 100644 --- a/packages/core/src/storage.ts +++ b/packages/core/src/storage.ts @@ -57,7 +57,7 @@ export type FindOptions = { * @public */ export type SortingQuery = { - [P in keyof T]?: T[P] extends object ? never : SortingOrder + [P in keyof T]?: SortingOrder } /** diff --git a/server/mongo/src/storage.ts b/server/mongo/src/storage.ts index be5e383737..4eda6f6318 100644 --- a/server/mongo/src/storage.ts +++ b/server/mongo/src/storage.ts @@ -13,11 +13,10 @@ // limitations under the License. // -import type { Tx, Ref, Doc, Class, DocumentQuery, FindResult, FindOptions, TxCreateDoc, ServerStorage } from '@anticrm/core' -import core, { DOMAIN_MODEL, TxProcessor, ModelDb, Hierarchy, DOMAIN_TX, TxFactory } from '@anticrm/core' +import core, { Tx, Ref, Doc, Class, DocumentQuery, FindResult, FindOptions, TxCreateDoc, ServerStorage, SortingOrder, DOMAIN_MODEL, TxProcessor, ModelDb, Hierarchy, DOMAIN_TX, TxFactory } from '@anticrm/core' import { Triggers } from '@anticrm/server-core' -import { MongoClient, Db, Filter, Document } from 'mongodb' +import { MongoClient, Db, Filter, Document, Sort } from 'mongodb' function translateQuery (query: DocumentQuery): Filter { return query as Filter @@ -69,7 +68,18 @@ class MongoStorage implements ServerStorage { const domain = this.hierarchy.getDomain(_class) console.log('findAll', _class, domain, query) if (domain === DOMAIN_MODEL) return await this.modeldb.findAll(_class, query, options) - return await this.db.collection(domain).find(translateQuery(query)).toArray() + let cursor = this.db.collection(domain).find(translateQuery(query)) + if (options !== null && options !== undefined) { + if (options.sort !== undefined) { + const sort: Sort = {} + for (const key in options.sort) { + const order = options.sort[key] === SortingOrder.Ascending ? 1 : -1 + sort[key] = order + } + cursor = cursor.sort(sort) + } + } + return await cursor.toArray() } async tx (tx: Tx): Promise { @@ -97,7 +107,7 @@ export async function createStorage (url: string, dbName: string): Promise({ objectSpace: core.space.Model }).toArray() + const txes = await db.collection(DOMAIN_TX).find({ objectSpace: core.space.Model }).sort({ _id: 1 }).toArray() for (const tx of txes) { hierarchy.tx(tx) await triggers.tx(tx) diff --git a/server/server/package.json b/server/server/package.json index cd4c0f48d2..f60b7a90b6 100644 --- a/server/server/package.json +++ b/server/server/package.json @@ -1,6 +1,6 @@ { "name": "@anticrm/server", - "version": "0.6.0", + "version": "0.6.1", "main": "lib/index.js", "author": "Anticrm Platform Contributors", "license": "EPL-2.0",