From a2ff68d6d784c7da99d95d615ae15d5f03655fa0 Mon Sep 17 00:00:00 2001 From: Andrey Platov Date: Fri, 3 Sep 2021 16:34:08 +0200 Subject: [PATCH] fix mongodb lookup Signed-off-by: Andrey Platov --- packages/core/src/state.ts | 20 ++++++++++++++ packages/ui/src/components/Card.svelte | 2 +- server/mongo/src/storage.ts | 37 ++++++++++++++++++++++++-- server/server/build.sh | 20 ++++++++++++++ 4 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 packages/core/src/state.ts create mode 100755 server/server/build.sh diff --git a/packages/core/src/state.ts b/packages/core/src/state.ts new file mode 100644 index 0000000000..045158ae90 --- /dev/null +++ b/packages/core/src/state.ts @@ -0,0 +1,20 @@ +// +// Copyright © 2020, 2021 Anticrm Platform Contributors. +// Copyright © 2021 Hardcore Engineering Inc. +// +// Licensed under the Eclipse Public License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. You may +// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import type { Doc, UXObject } from './classes' + +export interface State extends Doc, UXObject { +} diff --git a/packages/ui/src/components/Card.svelte b/packages/ui/src/components/Card.svelte index 06086339e6..3fc5dedc9a 100644 --- a/packages/ui/src/components/Card.svelte +++ b/packages/ui/src/components/Card.svelte @@ -35,7 +35,7 @@
-
+
diff --git a/server/mongo/src/storage.ts b/server/mongo/src/storage.ts index 8355bc98b4..cec689c732 100644 --- a/server/mongo/src/storage.ts +++ b/server/mongo/src/storage.ts @@ -19,7 +19,8 @@ import type { DbAdapter, TxAdapter } from '@anticrm/server-core' import { MongoClient, Db, Filter, Document, Sort } from 'mongodb' -function translateQuery (query: DocumentQuery): Filter { +function translateQuery (clazz: Ref>, query: DocumentQuery): Filter { + // return Object.assign({}, query, { _class: clazz }) return query as Filter } @@ -37,13 +38,45 @@ abstract class MongoAdapterBase extends TxProcessor { async init (): Promise {} + private async lookup (clazz: Ref>, query: DocumentQuery, options: FindOptions): Promise> { + const pipeline = [] + pipeline.push({ $match: translateQuery(clazz, query) }) + const lookups = options.lookup as any + for (const key in lookups) { + const clazz = lookups[key] + const step = { + from: this.hierarchy.getDomain(clazz), + localField: key, + foreignField: '_id', + as: key + '_lookup' + } + pipeline.push({ $lookup: step }) + } + const domain = this.hierarchy.getDomain(clazz) + const cursor = this.db.collection(domain).aggregate(pipeline) + const result = await cursor.toArray() as FindResult + for (const row of result) { + const object = row as any + object.$lookup = {} + for (const key in lookups) { + const arr = object[key + '_lookup'] + object.$lookup[key] = arr[0] + } + } + return result + } + async findAll ( _class: Ref>, query: DocumentQuery, options?: FindOptions ): Promise> { + // TODO: rework this + if (options !== null && options !== undefined) { + if (options.lookup !== undefined) { return await this.lookup(_class, query, options) } + } const domain = this.hierarchy.getDomain(_class) - let cursor = this.db.collection(domain).find(translateQuery(query)) + let cursor = this.db.collection(domain).find(translateQuery(_class, query)) if (options !== null && options !== undefined) { if (options.sort !== undefined) { const sort: Sort = {} diff --git a/server/server/build.sh b/server/server/build.sh new file mode 100755 index 0000000000..0301e46926 --- /dev/null +++ b/server/server/build.sh @@ -0,0 +1,20 @@ +#!/bin/bash +# +# Copyright © 2020, 2021 Anticrm Platform Contributors. +# Copyright © 2021 Hardcore Engineering Inc. +# +# Licensed under the Eclipse Public License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. You may +# obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# +# See the License for the specific language governing permissions and +# limitations under the License. +# + +rushx bundle +rushx docker:build +rushx docker:push