2023-12-25 09:49:08 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2023 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.
|
|
|
|
//
|
|
|
|
|
2024-05-08 06:06:38 +00:00
|
|
|
import core, { Client, TxOperations, WorkspaceId, systemAccountEmail, toWorkspaceString } from '@hcengineering/core'
|
2024-07-18 18:04:48 +00:00
|
|
|
import { createClient, getTransactorEndpoint } from '@hcengineering/server-client'
|
2024-01-19 08:02:41 +00:00
|
|
|
import { Token, generateToken } from '@hcengineering/server-token'
|
2023-12-25 09:49:08 +00:00
|
|
|
|
2024-01-19 08:02:41 +00:00
|
|
|
async function connect (token: string): Promise<Client> {
|
2024-07-18 18:04:48 +00:00
|
|
|
const endpoint = await getTransactorEndpoint(token)
|
|
|
|
return await createClient(endpoint, token)
|
2023-12-25 09:49:08 +00:00
|
|
|
}
|
|
|
|
|
2024-01-19 08:02:41 +00:00
|
|
|
async function getTxOperations (client: Client, token: Token, isDerived: boolean = false): Promise<TxOperations> {
|
2024-09-12 07:04:03 +00:00
|
|
|
const account = client.getModel().getAccountByEmail(token.email)
|
2023-12-25 09:49:08 +00:00
|
|
|
const accountId = account?._id ?? core.account.System
|
|
|
|
|
|
|
|
return new TxOperations(client, accountId, isDerived)
|
|
|
|
}
|
2024-01-19 08:02:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface ClientFactoryParams {
|
|
|
|
derived: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2024-05-08 06:06:38 +00:00
|
|
|
export type ClientFactory = (params?: ClientFactoryParams) => Promise<TxOperations>
|
2024-01-19 08:02:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2024-05-08 06:06:38 +00:00
|
|
|
export function simpleClientFactory (token: Token): ClientFactory {
|
|
|
|
return async (params?: ClientFactoryParams) => {
|
|
|
|
const derived = params?.derived ?? false
|
|
|
|
const client = await connect(generateToken(token.email, token.workspace))
|
|
|
|
return await getTxOperations(client, token, derived)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function reusableClientFactory (token: Token, controller: Controller): ClientFactory {
|
|
|
|
return async (params?: ClientFactoryParams) => {
|
|
|
|
const derived = params?.derived ?? false
|
2024-01-19 08:02:41 +00:00
|
|
|
const workspaceClient = await controller.get(token.workspace)
|
|
|
|
return await getTxOperations(workspaceClient.client, token, derived)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export class Controller {
|
|
|
|
private readonly workspaces: Map<string, WorkspaceClient> = new Map<string, WorkspaceClient>()
|
|
|
|
|
|
|
|
async get (workspaceId: WorkspaceId): Promise<WorkspaceClient> {
|
|
|
|
const workspace = toWorkspaceString(workspaceId)
|
|
|
|
|
|
|
|
let client = this.workspaces.get(workspace)
|
|
|
|
if (client === undefined) {
|
|
|
|
client = await WorkspaceClient.create(workspaceId)
|
|
|
|
this.workspaces.set(workspace, client)
|
|
|
|
}
|
|
|
|
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
async close (): Promise<void> {
|
|
|
|
for (const workspace of this.workspaces.values()) {
|
|
|
|
await workspace.close()
|
|
|
|
}
|
|
|
|
this.workspaces.clear()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export class WorkspaceClient {
|
|
|
|
private constructor (
|
|
|
|
readonly workspace: WorkspaceId,
|
|
|
|
readonly client: Client
|
2024-05-08 06:06:38 +00:00
|
|
|
) {}
|
2024-01-19 08:02:41 +00:00
|
|
|
|
|
|
|
static async create (workspace: WorkspaceId): Promise<WorkspaceClient> {
|
|
|
|
const token = generateToken(systemAccountEmail, workspace)
|
|
|
|
const client = await connect(token)
|
|
|
|
return new WorkspaceClient(workspace, client)
|
|
|
|
}
|
|
|
|
|
|
|
|
async close (): Promise<void> {
|
|
|
|
await this.client.close()
|
|
|
|
}
|
|
|
|
}
|