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
+7 -5
View File
@@ -13,14 +13,14 @@
// limitations under the License.
//
import type { Tx, Storage, Ref, Doc, Class, DocumentQuery, FindResult, FindOptions, TxHander, ServerStorage, TxResult } from '@anticrm/core'
import type { Class, ClientConnection, Doc, DocumentQuery, FindOptions, FindResult, Ref, ServerStorage, Tx, TxHander, TxResult } from '@anticrm/core'
import { DOMAIN_TX } from '@anticrm/core'
import { createInMemoryAdapter, createInMemoryTxAdapter } from '@anticrm/dev-storage'
import { createServerStorage, FullTextAdapter, IndexedDoc } from '@anticrm/server-core'
import { protoDeserialize, protoSerialize } from '@anticrm/platform'
import type { DbConfiguration } from '@anticrm/server-core'
import { protoSerialize, protoDeserialize } from '@anticrm/platform'
import { createServerStorage, FullTextAdapter, IndexedDoc } from '@anticrm/server-core'
class ServerStorageWrapper implements Storage {
class ServerStorageWrapper implements ClientConnection {
constructor (private readonly storage: ServerStorage, private readonly handler: TxHander) {}
findAll <T extends Doc>(_class: Ref<Class<T>>, query: DocumentQuery<T>, options?: FindOptions<T>): Promise<FindResult<T>> {
@@ -34,6 +34,8 @@ class ServerStorageWrapper implements Storage {
for (const tx of derived) { this.handler(tx) }
return result
}
async close (): Promise<void> {}
}
class NullFullTextAdapter implements FullTextAdapter {
@@ -54,7 +56,7 @@ async function createNullFullTextAdapter (): Promise<FullTextAdapter> {
return new NullFullTextAdapter()
}
export async function connect (handler: (tx: Tx) => void): Promise<Storage> {
export async function connect (handler: (tx: Tx) => void): Promise<ClientConnection> {
const conf: DbConfiguration = {
domains: {
[DOMAIN_TX]: 'InMemoryTx'
+7 -4
View File
@@ -32,20 +32,23 @@
"eslint-config-standard-with-typescript": "^21.0.1",
"prettier": "^2.4.1",
"@rushstack/heft": "^0.41.1",
"typescript": "^4.3.5"
"typescript": "^4.3.5",
"@types/ws": "^8.2.1"
},
"dependencies": {
"mongodb": "^4.1.1",
"commander": "^8.1.0",
"@anticrm/account": "~0.6.0",
"jwt-simple": "^0.5.6",
"@anticrm/contrib": "~0.6.0",
"@anticrm/core": "~0.6.11",
"@anticrm/contact": "~0.6.2",
"@anticrm/workspace": "~0.6.0",
"minio": "^7.0.19",
"@anticrm/model-all": "~0.6.0",
"@anticrm/model-telegram": "~0.6.0",
"@anticrm/telegram": "~0.6.0"
"@anticrm/telegram": "~0.6.0",
"@anticrm/client-resources": "~0.6.4",
"ws": "^8.2.0",
"@anticrm/client": "~0.6.1",
"@anticrm/platform": "~0.6.5"
}
}
+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)