introduce TxResult

Signed-off-by: Andrey Platov <andrey@hardcoreeng.com>
This commit is contained in:
Andrey Platov
2021-10-13 18:46:48 +02:00
parent e080f6e724
commit d75d17bfec
19 changed files with 127 additions and 92 deletions
+3 -2
View File
@@ -14,7 +14,7 @@
// limitations under the License.
//
import { Tx, Storage, Class, Doc, DocumentQuery, FindOptions, Ref, FindResult } from '@anticrm/core'
import type { Tx, Storage, Class, Doc, DocumentQuery, FindOptions, Ref, FindResult, TxResult } from '@anticrm/core'
import { serialize } from '@anticrm/platform'
import WebSocket from 'ws'
@@ -29,11 +29,12 @@ export class ContributingClient implements Storage {
throw new Error('findAll not implemeneted for contributing client')
}
async tx (tx: Tx): Promise<void> {
async tx (tx: Tx): Promise<TxResult> {
this.websocket.send(serialize({
method: 'tx',
params: [tx]
}))
return {}
}
close (): void {
+11 -8
View File
@@ -15,7 +15,7 @@
//
import core, { Hierarchy, AnyAttribute, Storage, DocumentQuery, FindOptions, FindResult, TxProcessor, TxMixin, TxPutBag, TxRemoveDoc } from '@anticrm/core'
import type { AttachedDoc, TxUpdateDoc, TxCreateDoc, Doc, Ref, Class, Obj } from '@anticrm/core'
import type { AttachedDoc, TxUpdateDoc, TxCreateDoc, Doc, Ref, Class, Obj, TxResult } from '@anticrm/core'
import type { IndexedDoc, FullTextAdapter, WithFind } from './types'
@@ -33,15 +33,17 @@ export class FullTextIndex extends TxProcessor implements Storage {
super()
}
protected override async txPutBag (tx: TxPutBag<any>): Promise<void> {
protected override async txPutBag (tx: TxPutBag<any>): Promise<TxResult> {
console.log('FullTextIndex.txPutBag: Method not implemented.')
return {}
}
protected override async txRemoveDoc (tx: TxRemoveDoc<Doc>): Promise<void> {
protected override async txRemoveDoc (tx: TxRemoveDoc<Doc>): Promise<TxResult> {
console.log('FullTextIndex.txRemoveDoc: Method not implemented.')
return {}
}
protected txMixin (tx: TxMixin<Doc, Doc>): Promise<void> {
protected txMixin (tx: TxMixin<Doc, Doc>): Promise<TxResult> {
throw new Error('Method not implemented.')
}
@@ -74,9 +76,9 @@ export class FullTextIndex extends TxProcessor implements Storage {
}
}
protected override async txCreateDoc (tx: TxCreateDoc<Doc>): Promise<void> {
protected override async txCreateDoc (tx: TxCreateDoc<Doc>): Promise<TxResult> {
const attributes = this.getFullTextAttributes(tx.objectClass)
if (attributes === undefined) return
if (attributes === undefined) return {}
const doc = TxProcessor.createDoc2Doc(tx)
const content = attributes.map(attr => (doc as any)[attr.name]) // buildContent(doc, attributes) // (doc as any)[attribute.name]
const indexedDoc: IndexedDoc = {
@@ -100,9 +102,9 @@ export class FullTextIndex extends TxProcessor implements Storage {
return await this.adapter.index(indexedDoc)
}
protected override async txUpdateDoc (tx: TxUpdateDoc<Doc>): Promise<void> {
protected override async txUpdateDoc (tx: TxUpdateDoc<Doc>): Promise<TxResult> {
const attributes = this.getFullTextAttributes(tx.objectClass)
if (attributes === undefined) return
if (attributes === undefined) return {}
const ops: any = tx.operations
const update: any = {}
let i = 0
@@ -117,5 +119,6 @@ export class FullTextIndex extends TxProcessor implements Storage {
if (shouldUpdate) {
return await this.adapter.update(tx.objectId, update)
}
return {}
}
}
+3 -2
View File
@@ -14,7 +14,7 @@
// limitations under the License.
//
import type { ServerStorage, Domain, Tx, TxCUD, Doc, Ref, Class, DocumentQuery, FindResult, FindOptions, Storage, TxBulkWrite } from '@anticrm/core'
import type { ServerStorage, Domain, Tx, TxCUD, Doc, Ref, Class, DocumentQuery, FindResult, FindOptions, Storage, TxBulkWrite, TxResult } from '@anticrm/core'
import core, { Hierarchy, DOMAIN_TX, ModelDb } from '@anticrm/core'
import type { FullTextAdapterFactory, FullTextAdapter } from './types'
import { FullTextIndex } from './fulltext'
@@ -88,7 +88,7 @@ class TServerStorage implements ServerStorage {
return adapter
}
private async routeTx (tx: Tx): Promise<void> {
private async routeTx (tx: Tx): Promise<TxResult> {
if (this.hierarchy.isDerived(tx._class, core.class.TxCUD)) {
const txCUD = tx as TxCUD<Doc>
const domain = this.hierarchy.getDomain(txCUD.objectClass)
@@ -102,6 +102,7 @@ class TServerStorage implements ServerStorage {
} else {
throw new Error('not implemented (routeTx)')
}
return {}
}
}
+3 -3
View File
@@ -14,7 +14,7 @@
// limitations under the License.
//
import type { Tx, Ref, Doc, Class, Space, Timestamp, Account, FindResult, DocumentQuery, FindOptions } from '@anticrm/core'
import type { Tx, Ref, Doc, Class, Space, Timestamp, Account, FindResult, DocumentQuery, FindOptions, TxResult } from '@anticrm/core'
import { TxFactory, Hierarchy } from '@anticrm/core'
import type { Resource } from '@anticrm/platform'
@@ -67,8 +67,8 @@ export type SearchQuery = any // TODO: replace with DocumentQuery
* @public
*/
export interface FullTextAdapter {
index: (doc: IndexedDoc) => Promise<void>
update: (id: Ref<Doc>, update: Record<string, any>) => Promise<void>
index: (doc: IndexedDoc) => Promise<TxResult>
update: (id: Ref<Doc>, update: Record<string, any>) => Promise<TxResult>
search: (query: SearchQuery) => Promise<IndexedDoc[]>
}
+5 -3
View File
@@ -14,7 +14,7 @@
// limitations under the License.
//
import type { Doc, Ref } from '@anticrm/core'
import type { Doc, Ref, TxResult } from '@anticrm/core'
import type { FullTextAdapter, IndexedDoc, SearchQuery } from '@anticrm/server-core'
import { Client } from '@elastic/elasticsearch'
@@ -58,7 +58,7 @@ class ElasticAdapter implements FullTextAdapter {
return hits.map(hit => hit._source)
}
async index (doc: IndexedDoc): Promise<void> {
async index (doc: IndexedDoc): Promise<TxResult> {
console.log('eastic: index', doc)
if (doc.data === undefined) {
const resp = await this.client.index({
@@ -81,9 +81,10 @@ class ElasticAdapter implements FullTextAdapter {
console.log('resp', resp)
console.log('error', (resp.meta as any)?.body?.error)
}
return {}
}
async update (id: Ref<Doc>, update: Record<string, any>): Promise<void> {
async update (id: Ref<Doc>, update: Record<string, any>): Promise<TxResult> {
const resp = await this.client.update({
index: this.db,
id,
@@ -92,6 +93,7 @@ class ElasticAdapter implements FullTextAdapter {
}
})
console.log('update', resp)
return {}
}
}
+17 -13
View File
@@ -13,7 +13,7 @@
// limitations under the License.
//
import type { Tx, Ref, Doc, Class, DocumentQuery, FindResult, FindOptions, TxCreateDoc, TxUpdateDoc, TxMixin, TxPutBag, TxRemoveDoc } from '@anticrm/core'
import type { Tx, Ref, Doc, Class, DocumentQuery, FindResult, FindOptions, TxCreateDoc, TxUpdateDoc, TxMixin, TxPutBag, TxRemoveDoc, TxResult } from '@anticrm/core'
import core, { DOMAIN_TX, DOMAIN_MODEL, SortingOrder, TxProcessor, Hierarchy, isOperator, ModelDb } from '@anticrm/core'
import type { DbAdapter, TxAdapter } from '@anticrm/server-core'
@@ -121,27 +121,29 @@ abstract class MongoAdapterBase extends TxProcessor {
}
class MongoAdapter extends MongoAdapterBase {
protected override async txPutBag (tx: TxPutBag<any>): Promise<void> {
protected override async txPutBag (tx: TxPutBag<any>): Promise<TxResult> {
const domain = this.hierarchy.getDomain(tx.objectClass)
console.log('mongo', { $set: { [tx.bag + '.' + tx.key]: tx.value } })
await this.db.collection(domain).updateOne({ _id: tx.objectId }, { $set: { [tx.bag + '.' + tx.key]: tx.value } })
return {}
}
protected txRemoveDoc (tx: TxRemoveDoc<Doc>): Promise<void> {
protected txRemoveDoc (tx: TxRemoveDoc<Doc>): Promise<TxResult> {
throw new Error('Method not implemented.')
}
protected txMixin (tx: TxMixin<Doc, Doc>): Promise<void> {
protected txMixin (tx: TxMixin<Doc, Doc>): Promise<TxResult> {
throw new Error('Method not implemented.')
}
protected override async txCreateDoc (tx: TxCreateDoc<Doc>): Promise<void> {
protected override async txCreateDoc (tx: TxCreateDoc<Doc>): Promise<TxResult> {
const doc = TxProcessor.createDoc2Doc(tx)
const domain = this.hierarchy.getDomain(doc._class)
await this.db.collection(domain).insertOne(translateDoc(doc))
return {}
}
protected override async txUpdateDoc (tx: TxUpdateDoc<Doc>): Promise<void> {
protected override async txUpdateDoc (tx: TxUpdateDoc<Doc>): Promise<TxResult> {
const domain = this.hierarchy.getDomain(tx.objectClass)
if (isOperator(tx.operations)) {
const operator = Object.keys(tx.operations)[0]
@@ -182,38 +184,40 @@ class MongoAdapter extends MongoAdapterBase {
} else {
await this.db.collection(domain).updateOne({ _id: tx.objectId }, { $set: tx.operations })
}
return {}
}
override tx (tx: Tx): Promise<void> {
override tx (tx: Tx): Promise<TxResult> {
console.log('mongo', tx)
return super.tx(tx)
}
}
class MongoTxAdapter extends MongoAdapterBase implements TxAdapter {
protected txCreateDoc (tx: TxCreateDoc<Doc>): Promise<void> {
protected txCreateDoc (tx: TxCreateDoc<Doc>): Promise<TxResult> {
throw new Error('Method not implemented.')
}
protected txPutBag (tx: TxPutBag<any>): Promise<void> {
protected txPutBag (tx: TxPutBag<any>): Promise<TxResult> {
throw new Error('Method not implemented.')
}
protected txUpdateDoc (tx: TxUpdateDoc<Doc>): Promise<void> {
protected txUpdateDoc (tx: TxUpdateDoc<Doc>): Promise<TxResult> {
throw new Error('Method not implemented.')
}
protected txRemoveDoc (tx: TxRemoveDoc<Doc>): Promise<void> {
protected txRemoveDoc (tx: TxRemoveDoc<Doc>): Promise<TxResult> {
throw new Error('Method not implemented.')
}
protected txMixin (tx: TxMixin<Doc, Doc>): Promise<void> {
protected txMixin (tx: TxMixin<Doc, Doc>): Promise<TxResult> {
throw new Error('Method not implemented.')
}
override async tx (tx: Tx): Promise<void> {
override async tx (tx: Tx): Promise<TxResult> {
console.log('mongotx', tx)
await this.db.collection(DOMAIN_TX).insertOne(translateDoc(tx))
return {}
}
async getModel (): Promise<Tx[]> {
+2 -2
View File
@@ -14,7 +14,7 @@
// limitations under the License.
//
import { Class, Doc, DocumentQuery, DOMAIN_MODEL, DOMAIN_TX, FindOptions, FindResult, Hierarchy, ModelDb, Ref, Tx } from '@anticrm/core'
import { Class, Doc, DocumentQuery, DOMAIN_MODEL, DOMAIN_TX, FindOptions, FindResult, Hierarchy, ModelDb, Ref, Tx, TxResult } from '@anticrm/core'
import { start as startJsonRpc } from '@anticrm/server-ws'
import { createMongoAdapter, createMongoTxAdapter } from '@anticrm/mongo'
import { createElasticAdapter } from '@anticrm/elastic'
@@ -29,7 +29,7 @@ import { serverViewId } from '@anticrm/server-view'
class NullDbAdapter implements DbAdapter {
async init (model: Tx[]): Promise<void> {}
async findAll <T extends Doc>(_class: Ref<Class<T>>, query: DocumentQuery<T>, options?: FindOptions<T> | undefined): Promise<FindResult<T>> { return [] }
async tx (tx: Tx): Promise<void> {}
async tx (tx: Tx): Promise<TxResult> { return {} }
}
async function createNullAdapter (hierarchy: Hierarchy, url: string, db: string, modelDb: ModelDb): Promise<DbAdapter> {
+3 -2
View File
@@ -20,7 +20,7 @@ import { createServer, IncomingMessage } from 'http'
import WebSocket, { Server } from 'ws'
import { decode } from 'jwt-simple'
import type { Doc, Ref, Class, FindOptions, FindResult, Tx, DocumentQuery, Storage, ServerStorage } from '@anticrm/core'
import type { Doc, Ref, Class, FindOptions, FindResult, Tx, DocumentQuery, Storage, ServerStorage, TxResult } from '@anticrm/core'
let LOGGING_ENABLED = true
@@ -39,12 +39,13 @@ class Session implements Storage {
return await this.storage.findAll(_class, query, options)
}
async tx (tx: Tx): Promise<void> {
async tx (tx: Tx): Promise<TxResult> {
const derived = await this.storage.tx(tx)
this.manager.broadcast(this, this.token, { result: tx })
for (const tx of derived) {
this.manager.broadcast(null, this.token, { result: tx })
}
return {}
}
}