fix mongodb lookup

Signed-off-by: Andrey Platov <andrey@hardcoreeng.com>
This commit is contained in:
Andrey Platov 2021-09-03 16:34:08 +02:00
parent 246a4b3b5e
commit a2ff68d6d7
No known key found for this signature in database
GPG Key ID: C8787EFEB4B64AF0
4 changed files with 76 additions and 3 deletions

View File

@ -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 {
}

View File

@ -35,7 +35,7 @@
<div class="card-bg" />
<div class="flex-between header">
<div class="overflow-label label"><Label {label} /></div>
<div class="tool"><Button label={okLabel} size={'small'} transparent on:click={() => { dispatch('close') }} /></div>
<div class="tool"><Button label={okLabel} size={'small'} transparent on:click={() => { okAction(); dispatch('close') }} /></div>
</div>
<div class="content"><slot /></div>
</form>

View File

@ -19,7 +19,8 @@ import type { DbAdapter, TxAdapter } from '@anticrm/server-core'
import { MongoClient, Db, Filter, Document, Sort } from 'mongodb'
function translateQuery<T extends Doc> (query: DocumentQuery<T>): Filter<Document> {
function translateQuery<T extends Doc> (clazz: Ref<Class<T>>, query: DocumentQuery<T>): Filter<Document> {
// return Object.assign({}, query, { _class: clazz })
return query as Filter<Document>
}
@ -37,13 +38,45 @@ abstract class MongoAdapterBase extends TxProcessor {
async init (): Promise<void> {}
private async lookup<T extends Doc> (clazz: Ref<Class<T>>, query: DocumentQuery<T>, options: FindOptions<T>): Promise<FindResult<T>> {
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<T>
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<T extends Doc> (
_class: Ref<Class<T>>,
query: DocumentQuery<T>,
options?: FindOptions<T>
): Promise<FindResult<T>> {
// 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<T>(translateQuery(query))
let cursor = this.db.collection(domain).find<T>(translateQuery(_class, query))
if (options !== null && options !== undefined) {
if (options.sort !== undefined) {
const sort: Sort = {}

20
server/server/build.sh Executable file
View File

@ -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