Allow Use client from client-resources from NodeJS (#545)

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
Andrey Sobolev
2021-12-06 17:57:35 +01:00
committed by GitHub
parent 420fc93967
commit beecb23c79
16 changed files with 207 additions and 93 deletions
+24
View File
@@ -0,0 +1,24 @@
import client from '@anticrm/client'
import clientResources from '@anticrm/client-resources'
import core, { TxOperations } from '@anticrm/core'
import { setMetadata } from '@anticrm/platform'
import { encode } from 'jwt-simple'
// eslint-disable-next-line
const WebSocket = require('ws')
export async function connect (transactorUrl: string, workspace: string): Promise<{ connection: TxOperations, close: () => Promise<void>}> {
console.log('connecting to transactor...')
const token = encode({ email: 'anticrm@hc.engineering', workspace }, 'secret')
// We need to override default factory with 'ws' one.
setMetadata(client.metadata.ClientSocketFactory, (url) => new WebSocket(url))
const connection = await (await clientResources()).function.GetClient(token, transactorUrl)
return {
connection: new TxOperations(connection, core.account.System),
close: async () => {
await connection.close()
}
}
}
+11 -24
View File
@@ -14,26 +14,17 @@
// limitations under the License.
//
import { program } from 'commander'
import { MongoClient, Db } from 'mongodb'
import {
getAccount,
createAccount,
assignWorkspace,
createWorkspace,
ACCOUNT_DB,
dropWorkspace,
dropAccount,
listWorkspaces
ACCOUNT_DB, assignWorkspace, createAccount, createWorkspace, dropAccount, dropWorkspace, getAccount, listWorkspaces
} from '@anticrm/account'
import { createContributingClient } from '@anticrm/contrib'
import core, { TxOperations } from '@anticrm/core'
import { encode } from 'jwt-simple'
import { Client } from 'minio'
import { initWorkspace, upgradeWorkspace, dumpWorkspace } from './workspace'
import contact, { combineName } from '@anticrm/contact'
import core from '@anticrm/core'
import { program } from 'commander'
import { Client } from 'minio'
import { Db, MongoClient } from 'mongodb'
import { connect } from './connect'
import { clearTelegramHistory } from './telegram'
import { dumpWorkspace, initWorkspace, upgradeWorkspace } from './workspace'
const mongodbUri = process.env.MONGO_URL
if (mongodbUri === undefined) {
@@ -112,28 +103,24 @@ program
await assignWorkspace(db, email, workspace)
console.log('connecting to transactor...')
const token = encode({ email: 'anticrm@hc.engineering', workspace }, 'secret')
const url = new URL(`/${token}`, transactorUrl)
const contrib = await createContributingClient(url.href)
const txop = new TxOperations(contrib, core.account.System)
const { connection, close } = await connect(transactorUrl, workspace)
const name = combineName(account.first, account.last)
console.log('create user in target workspace...')
const employee = await txop.createDoc(contact.class.Employee, contact.space.Employee, {
const employee = await connection.createDoc(contact.class.Employee, contact.space.Employee, {
name,
city: 'Mountain View',
channels: []
})
console.log('create account in target workspace...')
await txop.createDoc(contact.class.EmployeeAccount, core.space.Model, {
await connection.createDoc(contact.class.EmployeeAccount, core.space.Model, {
email,
employee,
name
})
contrib.close()
await close()
})
})
+10 -13
View File
@@ -14,24 +14,22 @@
// limitations under the License.
//
import { MongoClient, Document } from 'mongodb'
import core, { DOMAIN_TX, Tx } from '@anticrm/core'
import { createContributingClient } from '@anticrm/contrib'
import { encode } from 'jwt-simple'
import { BucketItem, Client } from 'minio'
import contact from '@anticrm/contact'
import core, { DOMAIN_TX, Tx } from '@anticrm/core'
import builder from '@anticrm/model-all'
import { existsSync } from 'fs'
import { mkdir, writeFile } from 'fs/promises'
import { BucketItem, Client } from 'minio'
import { Document, MongoClient } from 'mongodb'
import { join } from 'path'
import { connect } from './connect'
const txes = JSON.parse(JSON.stringify(builder.getTxes())) as Tx[]
/**
* @public
*/
export async function initWorkspace (mongoUrl: string, dbName: string, clientUrl: string, minio: Client): Promise<void> {
export async function initWorkspace (mongoUrl: string, dbName: string, transactorUrl: string, minio: Client): Promise<void> {
const client = new MongoClient(mongoUrl)
try {
await client.connect()
@@ -47,13 +45,12 @@ export async function initWorkspace (mongoUrl: string, dbName: string, clientUrl
console.log('creating data...')
const data = txes.filter((tx) => tx.objectSpace !== core.space.Model)
const token = encode({ email: 'anticrm@hc.engineering', workspace: dbName }, 'secret')
const url = new URL(`/${token}`, clientUrl)
const contrib = await createContributingClient(url.href)
const { connection, close } = await connect(transactorUrl, dbName)
for (const tx of data) {
await contrib.tx(tx)
await connection.tx(tx)
}
contrib.close()
await close()
console.log('create minio bucket')
if (!(await minio.bucketExists(dbName))) {
@@ -70,7 +67,7 @@ export async function initWorkspace (mongoUrl: string, dbName: string, clientUrl
export async function upgradeWorkspace (
mongoUrl: string,
dbName: string,
clientUrl: string,
transactorUrl: string,
minio: Client
): Promise<void> {
const client = new MongoClient(mongoUrl)