UBERF-7749: Use MONGO_OPTIONS properly (#6200)

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
Andrey Sobolev 2024-07-31 15:57:20 +07:00 committed by GitHub
parent b6af63fcb0
commit ce807a4a28
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 94 additions and 88 deletions

View File

@ -73,7 +73,7 @@ import task, { type ProjectType, type Task, type TaskType } from '@hcengineering
import { updateYDocContent } from '@hcengineering/text' import { updateYDocContent } from '@hcengineering/text'
import tracker from '@hcengineering/tracker' import tracker from '@hcengineering/tracker'
import { deepEqual } from 'fast-equals' import { deepEqual } from 'fast-equals'
import { type Db, MongoClient } from 'mongodb' import { type Db } from 'mongodb'
export async function cleanWorkspace ( export async function cleanWorkspace (
ctx: MeasureContext, ctx: MeasureContext,
@ -157,10 +157,10 @@ export async function cleanWorkspace (
} }
} }
const client = new MongoClient(mongoUrl) const client = getMongoClient(mongoUrl)
try { try {
await client.connect() const _client = await client.getClient()
const db = getWorkspaceDB(client, workspaceId) const db = getWorkspaceDB(_client, workspaceId)
if (opt.removedTx) { if (opt.removedTx) {
const txes = await db.collection(DOMAIN_TX).find({}).toArray() const txes = await db.collection(DOMAIN_TX).find({}).toArray()
@ -173,7 +173,7 @@ export async function cleanWorkspace (
} }
} }
} finally { } finally {
await client.close() client.close()
} }
} catch (err: any) { } catch (err: any) {
console.trace(err) console.trace(err)
@ -420,10 +420,10 @@ export async function fixSkills (
const connection = (await connect(transactorUrl, workspaceId, undefined, { const connection = (await connect(transactorUrl, workspaceId, undefined, {
mode: 'backup' mode: 'backup'
})) as unknown as CoreClient & BackupClient })) as unknown as CoreClient & BackupClient
const client = new MongoClient(mongoUrl) const client = getMongoClient(mongoUrl)
try { try {
await client.connect() const _client = await client.getClient()
const db = getWorkspaceDB(client, workspaceId) const db = getWorkspaceDB(_client, workspaceId)
async function fixCount (): Promise<void> { async function fixCount (): Promise<void> {
console.log('fixing ref-count...') console.log('fixing ref-count...')
@ -664,7 +664,7 @@ export async function fixSkills (
} catch (err: any) { } catch (err: any) {
console.trace(err) console.trace(err)
} finally { } finally {
await client.close() client.close()
await connection.close() await connection.close()
} }
} }
@ -689,10 +689,10 @@ export async function restoreRecruitingTaskTypes (
mode: 'backup', mode: 'backup',
model: 'upgrade' model: 'upgrade'
})) as unknown as CoreClient & BackupClient })) as unknown as CoreClient & BackupClient
const client = new MongoClient(mongoUrl) const client = getMongoClient(mongoUrl)
try { try {
await client.connect() const _client = await client.getClient()
const db = getWorkspaceDB(client, workspaceId) const db = getWorkspaceDB(_client, workspaceId)
// Query all vacancy project types creations (in Model) // Query all vacancy project types creations (in Model)
// We only update new project types in model here and not old ones in spaces // We only update new project types in model here and not old ones in spaces
@ -839,7 +839,7 @@ export async function restoreRecruitingTaskTypes (
} catch (err: any) { } catch (err: any) {
console.trace(err) console.trace(err)
} finally { } finally {
await client.close() client.close()
await connection.close() await connection.close()
} }
} }
@ -853,10 +853,10 @@ export async function restoreHrTaskTypesFromUpdates (
mode: 'backup', mode: 'backup',
model: 'upgrade' model: 'upgrade'
})) as unknown as CoreClient & BackupClient })) as unknown as CoreClient & BackupClient
const client = new MongoClient(mongoUrl) const client = getMongoClient(mongoUrl)
try { try {
await client.connect() const _client = await client.getClient()
const db = getWorkspaceDB(client, workspaceId) const db = getWorkspaceDB(_client, workspaceId)
const hierarchy = connection.getHierarchy() const hierarchy = connection.getHierarchy()
const descr = connection.getModel().getObject(recruit.descriptors.VacancyType) const descr = connection.getModel().getObject(recruit.descriptors.VacancyType)
const knownCategories = [ const knownCategories = [
@ -1047,7 +1047,7 @@ export async function restoreHrTaskTypesFromUpdates (
} catch (err: any) { } catch (err: any) {
console.trace(err) console.trace(err)
} finally { } finally {
await client.close() client.close()
await connection.close() await connection.close()
} }
} }

View File

@ -15,9 +15,8 @@
import { Client as ElasticClient } from '@elastic/elasticsearch' import { Client as ElasticClient } from '@elastic/elasticsearch'
import core, { DOMAIN_DOC_INDEX_STATE, toWorkspaceString, type WorkspaceId } from '@hcengineering/core' import core, { DOMAIN_DOC_INDEX_STATE, toWorkspaceString, type WorkspaceId } from '@hcengineering/core'
import { getWorkspaceDB } from '@hcengineering/mongo' import { getMongoClient, getWorkspaceDB } from '@hcengineering/mongo'
import { type StorageAdapter } from '@hcengineering/server-core' import { type StorageAdapter } from '@hcengineering/server-core'
import { MongoClient } from 'mongodb'
export async function rebuildElastic ( export async function rebuildElastic (
mongoUrl: string, mongoUrl: string,
@ -25,15 +24,15 @@ export async function rebuildElastic (
storageAdapter: StorageAdapter, storageAdapter: StorageAdapter,
elasticUrl: string elasticUrl: string
): Promise<void> { ): Promise<void> {
const client = new MongoClient(mongoUrl) const client = getMongoClient(mongoUrl)
try { try {
await client.connect() const _client = await client.getClient()
const db = getWorkspaceDB(client, workspaceId) const db = getWorkspaceDB(_client, workspaceId)
await db await db
.collection(DOMAIN_DOC_INDEX_STATE) .collection(DOMAIN_DOC_INDEX_STATE)
.updateMany({ _class: core.class.DocIndexState }, { $set: { elastic: false } }) .updateMany({ _class: core.class.DocIndexState }, { $set: { elastic: false } })
} finally { } finally {
await client.close() client.close()
} }
await dropElastic(elasticUrl, workspaceId) await dropElastic(elasticUrl, workspaceId)

View File

@ -9,11 +9,11 @@ import core, {
type WorkspaceId, type WorkspaceId,
getCollaborativeDocId getCollaborativeDocId
} from '@hcengineering/core' } from '@hcengineering/core'
import { getWorkspaceDB } from '@hcengineering/mongo' import { getMongoClient, getWorkspaceDB } from '@hcengineering/mongo'
import { type StorageAdapter } from '@hcengineering/server-core' import { type StorageAdapter } from '@hcengineering/server-core'
import { connect } from '@hcengineering/server-tool' import { connect } from '@hcengineering/server-tool'
import { jsonToText } from '@hcengineering/text' import { jsonToText } from '@hcengineering/text'
import { type Db, MongoClient } from 'mongodb' import { type Db } from 'mongodb'
export async function fixJsonMarkup ( export async function fixJsonMarkup (
ctx: MeasureContext, ctx: MeasureContext,
@ -27,8 +27,9 @@ export async function fixJsonMarkup (
})) as unknown as CoreClient })) as unknown as CoreClient
const hierarchy = connection.getHierarchy() const hierarchy = connection.getHierarchy()
const client = new MongoClient(mongoUrl) const client = getMongoClient(mongoUrl)
const db = getWorkspaceDB(client, workspaceId) const _client = await client.getClient()
const db = getWorkspaceDB(_client, workspaceId)
try { try {
const classes = hierarchy.getDescendants(core.class.Doc) const classes = hierarchy.getDescendants(core.class.Doc)
@ -48,7 +49,7 @@ export async function fixJsonMarkup (
await processFixJsonMarkupFor(ctx, domain, _class, filtered, workspaceId, db, storageAdapter) await processFixJsonMarkupFor(ctx, domain, _class, filtered, workspaceId, db, storageAdapter)
} }
} finally { } finally {
await client.close() client.close()
await connection.close() await connection.close()
} }
} }

View File

@ -27,9 +27,8 @@ import core, {
SortingOrder, SortingOrder,
type WorkspaceId type WorkspaceId
} from '@hcengineering/core' } from '@hcengineering/core'
import { getWorkspaceDB } from '@hcengineering/mongo' import { getMongoClient, getWorkspaceDB } from '@hcengineering/mongo'
import { connect } from '@hcengineering/server-tool' import { connect } from '@hcengineering/server-tool'
import { MongoClient } from 'mongodb'
interface PropertyInfo { interface PropertyInfo {
name: string name: string
@ -100,10 +99,10 @@ export async function fixMixinForeignAttributes (
} }
if (result.size > 0) { if (result.size > 0) {
const client = new MongoClient(mongoUrl) const client = getMongoClient(mongoUrl)
try { try {
await client.connect() const _client = await client.getClient()
const db = getWorkspaceDB(client, workspaceId) const db = getWorkspaceDB(_client, workspaceId)
for (const [mixin, objects] of result) { for (const [mixin, objects] of result) {
console.log('fixing', mixin) console.log('fixing', mixin)
@ -134,7 +133,7 @@ export async function fixMixinForeignAttributes (
} }
} }
} finally { } finally {
await client.close() client.close()
} }
} }
} catch (err: any) { } catch (err: any) {

View File

@ -15,13 +15,13 @@
// //
import { DOMAIN_TX, type MeasureContext, type Ref, type WorkspaceId } from '@hcengineering/core' import { DOMAIN_TX, type MeasureContext, type Ref, type WorkspaceId } from '@hcengineering/core'
import { type StorageAdapter } from '@hcengineering/server-core'
import { DOMAIN_ATTACHMENT } from '@hcengineering/model-attachment' import { DOMAIN_ATTACHMENT } from '@hcengineering/model-attachment'
import contact, { DOMAIN_CHANNEL } from '@hcengineering/model-contact' import contact, { DOMAIN_CHANNEL } from '@hcengineering/model-contact'
import { DOMAIN_TELEGRAM } from '@hcengineering/model-telegram' import { DOMAIN_TELEGRAM } from '@hcengineering/model-telegram'
import { getWorkspaceDB } from '@hcengineering/mongo' import { getMongoClient, getWorkspaceDB } from '@hcengineering/mongo'
import { type StorageAdapter } from '@hcengineering/server-core'
import telegram, { type SharedTelegramMessage, type SharedTelegramMessages } from '@hcengineering/telegram' import telegram, { type SharedTelegramMessage, type SharedTelegramMessages } from '@hcengineering/telegram'
import { type Document, MongoClient, type UpdateFilter } from 'mongodb' import { type Document, type UpdateFilter } from 'mongodb'
const LastMessages = 'last-msgs' const LastMessages = 'last-msgs'
@ -35,11 +35,11 @@ export async function clearTelegramHistory (
tgDb: string, tgDb: string,
storageAdapter: StorageAdapter storageAdapter: StorageAdapter
): Promise<void> { ): Promise<void> {
const client = new MongoClient(mongoUrl) const client = getMongoClient(mongoUrl)
try { try {
await client.connect() const _client = await client.getClient()
const workspaceDB = getWorkspaceDB(client, workspaceId) const workspaceDB = getWorkspaceDB(_client, workspaceId)
const telegramDB = client.db(tgDb) const telegramDB = _client.db(tgDb)
const sharedMessages = await workspaceDB const sharedMessages = await workspaceDB
.collection(DOMAIN_TELEGRAM) .collection(DOMAIN_TELEGRAM)
@ -99,6 +99,6 @@ export async function clearTelegramHistory (
workspace: workspaceId workspace: workspaceId
}) })
} finally { } finally {
await client.close() client.close()
} }
} }

View File

@ -16,25 +16,24 @@
import contact from '@hcengineering/contact' import contact from '@hcengineering/contact'
import core, { import core, {
type Client as CoreClient,
type BackupClient, type BackupClient,
DOMAIN_TX, type Client as CoreClient,
type Tx,
type WorkspaceId,
type Ref,
type Doc, type Doc,
DOMAIN_DOC_INDEX_STATE DOMAIN_DOC_INDEX_STATE,
DOMAIN_TX,
type Ref,
type Tx,
type WorkspaceId
} from '@hcengineering/core' } from '@hcengineering/core'
import { getWorkspaceDB } from '@hcengineering/mongo' import { getMongoClient, getWorkspaceDB } from '@hcengineering/mongo'
import { MongoClient } from 'mongodb'
import { generateModelDiff, printDiff } from './mdiff'
import { connect } from '@hcengineering/server-tool' import { connect } from '@hcengineering/server-tool'
import { generateModelDiff, printDiff } from './mdiff'
export async function diffWorkspace (mongoUrl: string, workspace: WorkspaceId, rawTxes: Tx[]): Promise<void> { export async function diffWorkspace (mongoUrl: string, workspace: WorkspaceId, rawTxes: Tx[]): Promise<void> {
const client = new MongoClient(mongoUrl) const client = getMongoClient(mongoUrl)
try { try {
await client.connect() const _client = await client.getClient()
const db = getWorkspaceDB(client, workspace) const db = getWorkspaceDB(_client, workspace)
console.log('diffing transactions...') console.log('diffing transactions...')
@ -68,7 +67,7 @@ export async function diffWorkspace (mongoUrl: string, workspace: WorkspaceId, r
} }
} }
} finally { } finally {
await client.close() client.close()
} }
} }
@ -81,18 +80,18 @@ export async function updateField (
const connection = (await connect(transactorUrl, workspaceId, undefined, { const connection = (await connect(transactorUrl, workspaceId, undefined, {
mode: 'backup' mode: 'backup'
})) as unknown as CoreClient & BackupClient })) as unknown as CoreClient & BackupClient
const client = new MongoClient(mongoUrl) const client = getMongoClient(mongoUrl)
let valueToPut: string | number = cmd.value let valueToPut: string | number = cmd.value
if (cmd.type === 'number') valueToPut = parseFloat(valueToPut) if (cmd.type === 'number') valueToPut = parseFloat(valueToPut)
try { try {
const _client = await client.getClient()
try { try {
await client.connect() const db = getWorkspaceDB(_client, workspaceId)
const db = getWorkspaceDB(client, workspaceId)
await db await db
.collection(cmd.domain) .collection(cmd.domain)
.updateOne({ _id: cmd.objectId as Ref<Doc> }, { $set: { [cmd.attribute]: valueToPut } }) .updateOne({ _id: cmd.objectId as Ref<Doc> }, { $set: { [cmd.attribute]: valueToPut } })
} finally { } finally {
await client.close() client.close()
} }
} finally { } finally {
await connection.close() await connection.close()
@ -104,19 +103,19 @@ export async function recreateElastic (
workspaceId: WorkspaceId, workspaceId: WorkspaceId,
transactorUrl: string transactorUrl: string
): Promise<void> { ): Promise<void> {
const client = new MongoClient(mongoUrl) const client = getMongoClient(mongoUrl)
const _client = await client.getClient()
const connection = (await connect(transactorUrl, workspaceId, undefined, { const connection = (await connect(transactorUrl, workspaceId, undefined, {
mode: 'backup' mode: 'backup'
})) as unknown as CoreClient & BackupClient })) as unknown as CoreClient & BackupClient
try { try {
await client.connect() const db = getWorkspaceDB(_client, workspaceId)
const db = getWorkspaceDB(client, workspaceId)
await db await db
.collection(DOMAIN_DOC_INDEX_STATE) .collection(DOMAIN_DOC_INDEX_STATE)
.updateMany({ _class: core.class.DocIndexState }, { $set: { stages: {} } }) .updateMany({ _class: core.class.DocIndexState }, { $set: { stages: {} } })
await connection.sendForceClose() await connection.sendForceClose()
} finally { } finally {
await client.close() client.close()
await connection.close() await connection.close()
} }
} }

View File

@ -16,6 +16,7 @@
"_phase:docker-staging": "rushx docker:staging", "_phase:docker-staging": "rushx docker:staging",
"bundle": "mkdir -p bundle && esbuild src/__start.ts --bundle --define:process.env.MODEL_VERSION=$(node ../../common/scripts/show_version.js) --minify --platform=node > bundle/bundle.js", "bundle": "mkdir -p bundle && esbuild src/__start.ts --bundle --define:process.env.MODEL_VERSION=$(node ../../common/scripts/show_version.js) --minify --platform=node > bundle/bundle.js",
"docker:build": "../../common/scripts/docker_build.sh hardcoreeng/account", "docker:build": "../../common/scripts/docker_build.sh hardcoreeng/account",
"docker:tbuild": "docker build -t hardcoreeng/account . --platform=linux/amd64 && ../../common/scripts/docker_tag_push.sh hardcoreeng/account",
"docker:staging": "../../common/scripts/docker_tag.sh hardcoreeng/account staging", "docker:staging": "../../common/scripts/docker_tag.sh hardcoreeng/account staging",
"docker:push": "../../common/scripts/docker_tag.sh hardcoreeng/account", "docker:push": "../../common/scripts/docker_tag.sh hardcoreeng/account",
"run-local": "cross-env MONGO_URL=mongodb://localhost:27017 MINIO_ACCESS_KEY=minioadmi MINIO_SECRET_KEY=minioadmin MINIO_ENDPOINT=localhost SERVER_SECRET='secret' TRANSACTOR_URL=ws://localhost:3333 ts-node src/__start.ts", "run-local": "cross-env MONGO_URL=mongodb://localhost:27017 MINIO_ACCESS_KEY=minioadmi MINIO_SECRET_KEY=minioadmin MINIO_ENDPOINT=localhost SERVER_SECRET='secret' TRANSACTOR_URL=ws://localhost:3333 ts-node src/__start.ts",

View File

@ -47,6 +47,7 @@
"@hcengineering/platform": "^0.6.11", "@hcengineering/platform": "^0.6.11",
"@hcengineering/auth-providers": "^0.6.0", "@hcengineering/auth-providers": "^0.6.0",
"@hcengineering/core": "^0.6.32", "@hcengineering/core": "^0.6.32",
"@hcengineering/mongo": "^0.6.1",
"mongodb": "^6.8.0", "mongodb": "^6.8.0",
"koa": "^2.15.3", "koa": "^2.15.3",
"koa-router": "^12.0.1", "koa-router": "^12.0.1",

View File

@ -7,10 +7,10 @@ import account, {
EndpointKind, EndpointKind,
UpgradeWorker, UpgradeWorker,
accountId, accountId,
cleanInProgressWorkspaces,
getMethods,
cleanExpiredOtp, cleanExpiredOtp,
getAllTransactors cleanInProgressWorkspaces,
getAllTransactors,
getMethods
} from '@hcengineering/account' } from '@hcengineering/account'
import accountEn from '@hcengineering/account/lang/en.json' import accountEn from '@hcengineering/account/lang/en.json'
import accountRu from '@hcengineering/account/lang/ru.json' import accountRu from '@hcengineering/account/lang/ru.json'
@ -25,6 +25,7 @@ import {
type Version type Version
} from '@hcengineering/core' } from '@hcengineering/core'
import { type MigrateOperation } from '@hcengineering/model' import { type MigrateOperation } from '@hcengineering/model'
import { getMongoClient, type MongoClientReference } from '@hcengineering/mongo'
import platform, { Severity, Status, addStringsLoader, setMetadata } from '@hcengineering/platform' import platform, { Severity, Status, addStringsLoader, setMetadata } from '@hcengineering/platform'
import serverClientPlugin from '@hcengineering/server-client' import serverClientPlugin from '@hcengineering/server-client'
import serverToken, { decodeToken } from '@hcengineering/server-token' import serverToken, { decodeToken } from '@hcengineering/server-token'
@ -34,7 +35,7 @@ import { type IncomingHttpHeaders } from 'http'
import Koa from 'koa' import Koa from 'koa'
import bodyParser from 'koa-bodyparser' import bodyParser from 'koa-bodyparser'
import Router from 'koa-router' import Router from 'koa-router'
import { MongoClient } from 'mongodb' import type { MongoClient } from 'mongodb'
import os from 'os' import os from 'os'
/** /**
@ -106,7 +107,8 @@ export function serveAccount (
} }
setMetadata(serverClientPlugin.metadata.UserAgent, 'AccountService') setMetadata(serverClientPlugin.metadata.UserAgent, 'AccountService')
let client: MongoClient | Promise<MongoClient> = MongoClient.connect(dbUri) const client: MongoClientReference = getMongoClient(dbUri)
let _client: MongoClient | Promise<MongoClient> = client.getClient()
let worker: UpgradeWorker | undefined let worker: UpgradeWorker | undefined
@ -120,7 +122,7 @@ export function serveAccount (
) )
app.use(bodyParser()) app.use(bodyParser())
void client.then(async (p: MongoClient) => { void client.getClient().then(async (p: MongoClient) => {
const db = p.db(ACCOUNT_DB) const db = p.db(ACCOUNT_DB)
registerProviders(measureCtx, app, router, db, productId, serverSecret, frontURL, brandings) registerProviders(measureCtx, app, router, db, productId, serverSecret, frontURL, brandings)
@ -237,10 +239,10 @@ export function serveAccount (
ctx.body = JSON.stringify(response) ctx.body = JSON.stringify(response)
} }
if (client instanceof Promise) { if (_client instanceof Promise) {
client = await client _client = await _client
} }
const db = client.db(ACCOUNT_DB) const db = _client.db(ACCOUNT_DB)
let host: string | undefined let host: string | undefined
const origin = ctx.request.headers.origin ?? ctx.request.headers.referer const origin = ctx.request.headers.origin ?? ctx.request.headers.referer
@ -269,7 +271,7 @@ export function serveAccount (
if (client instanceof Promise) { if (client instanceof Promise) {
void client.then((c) => c.close()) void client.then((c) => c.close())
} else { } else {
void client.close() client.close()
} }
server.close() server.close()
} }

View File

@ -258,7 +258,6 @@ export async function upgradeModel (
throw Error('Model txes must target only core.space.Model') throw Error('Model txes must target only core.space.Model')
} }
// const client = new MongoClient(mongodbUri)
const _client = getMongoClient(mongodbUri) const _client = getMongoClient(mongodbUri)
const client = await _client.getClient() const client = await _client.getClient()
const storageConfig: StorageConfiguration = storageConfigFromEnv() const storageConfig: StorageConfiguration = storageConfigFromEnv()

View File

@ -61,6 +61,7 @@
"@hcengineering/contact": "^0.6.24", "@hcengineering/contact": "^0.6.24",
"@hcengineering/core": "^0.6.32", "@hcengineering/core": "^0.6.32",
"@hcengineering/platform": "^0.6.11", "@hcengineering/platform": "^0.6.11",
"@hcengineering/mongo": "^0.6.1",
"@hcengineering/setting": "^0.6.17", "@hcengineering/setting": "^0.6.17",
"@hcengineering/text": "^0.6.5", "@hcengineering/text": "^0.6.5",
"@hcengineering/server-client": "^0.6.0", "@hcengineering/server-client": "^0.6.0",

View File

@ -13,16 +13,17 @@
// limitations under the License. // limitations under the License.
// //
import { MongoClientReference, getMongoClient } from '@hcengineering/mongo'
import { MongoClient } from 'mongodb' import { MongoClient } from 'mongodb'
import config from './config' import config from './config'
const clientRef: MongoClientReference = getMongoClient(config.MongoURI)
let client: MongoClient | undefined let client: MongoClient | undefined
export const getDB = (() => { export const getDB = (() => {
return async () => { return async () => {
if (client === undefined) { if (client === undefined) {
client = new MongoClient(config.MongoURI) client = await clientRef.getClient()
await client.connect()
} }
return client.db(config.MongoDB) return client.db(config.MongoDB)
@ -30,7 +31,5 @@ export const getDB = (() => {
})() })()
export const closeDB: () => Promise<void> = async () => { export const closeDB: () => Promise<void> = async () => {
if (client !== undefined) { clientRef.close()
await client.close()
}
} }

View File

@ -58,6 +58,7 @@
"@hcengineering/client": "^0.6.18", "@hcengineering/client": "^0.6.18",
"@hcengineering/client-resources": "^0.6.27", "@hcengineering/client-resources": "^0.6.27",
"@hcengineering/contact": "^0.6.24", "@hcengineering/contact": "^0.6.24",
"@hcengineering/mongo": "^0.6.1",
"@hcengineering/core": "^0.6.32", "@hcengineering/core": "^0.6.32",
"@hcengineering/gmail": "^0.6.22", "@hcengineering/gmail": "^0.6.22",
"@hcengineering/server-token": "^0.6.11", "@hcengineering/server-token": "^0.6.11",

View File

@ -14,16 +14,17 @@
// limitations under the License. // limitations under the License.
// //
import { MongoClientReference, getMongoClient } from '@hcengineering/mongo'
import { MongoClient } from 'mongodb' import { MongoClient } from 'mongodb'
import config from './config' import config from './config'
const clientRef: MongoClientReference = getMongoClient(config.MongoURI)
let client: MongoClient | undefined let client: MongoClient | undefined
export const getDB = (() => { export const getDB = (() => {
return async () => { return async () => {
if (client === undefined) { if (client === undefined) {
client = new MongoClient(config.MongoURI) client = await clientRef.getClient()
await client.connect()
} }
return client.db(config.MongoDB) return client.db(config.MongoDB)
@ -31,7 +32,5 @@ export const getDB = (() => {
})() })()
export const closeDB: () => Promise<void> = async () => { export const closeDB: () => Promise<void> = async () => {
if (client !== undefined) { clientRef.close()
await client.close()
}
} }

View File

@ -66,6 +66,7 @@
"@hcengineering/telegram": "^0.6.21", "@hcengineering/telegram": "^0.6.21",
"@hcengineering/server-token": "^0.6.11", "@hcengineering/server-token": "^0.6.11",
"@hcengineering/server-client": "^0.6.0", "@hcengineering/server-client": "^0.6.0",
"@hcengineering/mongo": "^0.6.1",
"big-integer": "^1.6.51", "big-integer": "^1.6.51",
"dotenv": "~16.0.0", "dotenv": "~16.0.0",
"cors": "^2.8.5", "cors": "^2.8.5",

View File

@ -1,16 +1,20 @@
import { MongoClientReference, getMongoClient } from '@hcengineering/mongo'
import { MongoClient } from 'mongodb' import { MongoClient } from 'mongodb'
import config from './config' import config from './config'
export const getDB = (() => { const clientRef: MongoClientReference = getMongoClient(config.MongoURI)
let client: MongoClient | undefined let client: MongoClient | undefined
export const getDB = (() => {
return async () => { return async () => {
if (client === undefined) { if (client === undefined) {
client = new MongoClient(config.MongoURI) client = await clientRef.getClient()
await client.connect()
} }
return client.db(config.MongoDB) return client.db(config.MongoDB)
} }
})() })()
export const closeDB: () => Promise<void> = async () => {
clientRef.close()
}