add TxPutBag

Signed-off-by: Andrey Platov <andrey@hardcoreeng.com>
This commit is contained in:
Andrey Platov 2021-09-15 12:53:11 +02:00
parent 2e23842170
commit 74d46558f1
No known key found for this signature in database
GPG Key ID: C8787EFEB4B64AF0
7 changed files with 147 additions and 16 deletions

View File

@ -46,7 +46,7 @@ class InMemoryTxAdapter implements TxAdapter {
}
class InMemoryAdapter implements DbAdapter {
private readonly modeldb: TxDb
private readonly modeldb: ModelDb
constructor (hierarchy: Hierarchy) {
this.modeldb = new ModelDb(hierarchy)

View File

@ -14,8 +14,8 @@
//
import type { Plugin, StatusCode } from '@anticrm/platform'
import { plugin } from '@anticrm/platform'
import type { Account, Class, Doc, Obj, Ref, Space, AnyAttribute, State, Type } from './classes'
import type { Tx, TxCreateDoc, TxCUD, TxMixin, TxRemoveDoc, TxUpdateDoc } from './tx'
import type { Account, Class, Doc, Obj, Ref, Space, AnyAttribute, State, Type, PropertyType } from './classes'
import type { Tx, TxCreateDoc, TxCUD, TxMixin, TxPutBag, TxRemoveDoc, TxUpdateDoc } from './tx'
/**
* @public
@ -34,6 +34,7 @@ export default plugin(coreId, {
TxMixin: '' as Ref<Class<TxMixin<Doc, Doc>>>,
TxUpdateDoc: '' as Ref<Class<TxUpdateDoc<Doc>>>,
TxRemoveDoc: '' as Ref<Class<TxRemoveDoc<Doc>>>,
TxPutBag: '' as Ref<Class<TxPutBag<PropertyType>>>,
Space: '' as Ref<Class<Space>>,
Account: '' as Ref<Class<Account>>,
State: '' as Ref<Class<State>>,

View File

@ -15,7 +15,7 @@
import { PlatformError, Severity, Status } from '@anticrm/platform'
import type { Class, Doc, Ref } from './classes'
import type { Tx, TxCreateDoc, TxMixin, TxRemoveDoc, TxUpdateDoc } from './tx'
import type { Tx, TxCreateDoc, TxMixin, TxPutBag, TxRemoveDoc, TxUpdateDoc } from './tx'
import core from './component'
import type { Hierarchy } from './hierarchy'
import { _getOperator } from './operator'
@ -26,7 +26,7 @@ import { TxProcessor } from './tx'
/**
* @public
*/
export class MemDb extends TxProcessor {
export abstract class MemDb extends TxProcessor {
protected readonly hierarchy: Hierarchy
private readonly objectsByClass = new Map<Ref<Class<Doc>>, Doc[]>()
private readonly objectById = new Map<Ref<Doc>, Doc>()
@ -147,6 +147,26 @@ export class MemDb extends TxProcessor {
* @public
*/
export class TxDb extends MemDb implements Storage {
protected txCreateDoc (tx: TxCreateDoc<Doc>): Promise<void> {
throw new Error('Method not implemented.')
}
protected txPutBag (tx: TxPutBag<any>): Promise<void> {
throw new Error('Method not implemented.')
}
protected txUpdateDoc (tx: TxUpdateDoc<Doc>): Promise<void> {
throw new Error('Method not implemented.')
}
protected txRemoveDoc (tx: TxRemoveDoc<Doc>): Promise<void> {
throw new Error('Method not implemented.')
}
protected txMixin (tx: TxMixin<Doc, Doc>): Promise<void> {
throw new Error('Method not implemented.')
}
async tx (tx: Tx): Promise<void> {
this.addDoc(tx)
}
@ -158,7 +178,18 @@ export class TxDb extends MemDb implements Storage {
* @public
*/
export class ModelDb extends MemDb implements Storage {
protected async txCreateDoc (tx: TxCreateDoc<Doc>): Promise<void> {
protected override async txPutBag (tx: TxPutBag<any>): Promise<void> {
const doc = this.getObject(tx.objectId) as any
let bag = doc[tx.bag]
if (bag === undefined) {
doc[tx.bag] = bag = {}
}
bag[tx.key] = tx.value
doc.modifiedBy = tx.modifiedBy
doc.modifiedOn = tx.modifiedOn
}
protected override async txCreateDoc (tx: TxCreateDoc<Doc>): Promise<void> {
this.addDoc(TxProcessor.createDoc2Doc(tx))
}

View File

@ -14,7 +14,7 @@
//
import type { KeysByType } from 'simplytyped'
import type { Class, Data, Doc, Domain, Ref, Account, Space, Arr, Mixin } from './classes'
import type { Class, Data, Doc, Domain, Ref, Account, Space, Arr, Mixin, PropertyType } from './classes'
import { DocumentQuery, FindOptions, FindResult, Storage, WithLookup } from './storage'
import core from './component'
import { generateId } from './utils'
@ -41,6 +41,15 @@ export interface TxCreateDoc<T extends Doc> extends TxCUD<T> {
attributes: Data<T>
}
/**
* @public
*/
export interface TxPutBag<T extends PropertyType> extends TxCUD<Doc> {
bag: string
key: string
value: T
}
/**
* @public
*/
@ -116,7 +125,7 @@ export interface WithTx {
/**
* @public
*/
export class TxProcessor implements WithTx {
export abstract class TxProcessor implements WithTx {
async tx (tx: Tx): Promise<void> {
switch (tx._class) {
case core.class.TxCreateDoc:
@ -127,6 +136,8 @@ export class TxProcessor implements WithTx {
return await this.txRemoveDoc(tx as TxRemoveDoc<Doc>)
case core.class.TxMixin:
return await this.txMixin(tx as TxMixin<Doc, Doc>)
case core.class.TxPutBag:
return await this.txPutBag(tx as TxPutBag<PropertyType>)
}
throw new Error('TxProcessor: unhandled transaction class: ' + tx._class)
}
@ -143,10 +154,11 @@ export class TxProcessor implements WithTx {
} as T
}
protected async txCreateDoc (tx: TxCreateDoc<Doc>): Promise<void> {}
protected async txUpdateDoc (tx: TxUpdateDoc<Doc>): Promise<void> {}
protected async txRemoveDoc (tx: TxRemoveDoc<Doc>): Promise<void> {}
protected async txMixin (tx: TxMixin<Doc, Doc>): Promise<void> {}
protected abstract txCreateDoc (tx: TxCreateDoc<Doc>): Promise<void>
protected abstract txPutBag (tx: TxPutBag<PropertyType>): Promise<void>
protected abstract txUpdateDoc (tx: TxUpdateDoc<Doc>): Promise<void>
protected abstract txRemoveDoc (tx: TxRemoveDoc<Doc>): Promise<void>
protected abstract txMixin (tx: TxMixin<Doc, Doc>): Promise<void>
}
/**
@ -182,6 +194,18 @@ export class TxOperations implements Storage {
return tx.objectId
}
putBag <P extends PropertyType>(
_class: Ref<Class<Doc>>,
space: Ref<Space>,
objectId: Ref<Doc>,
bag: string,
key: string,
value: P
): Promise<void> {
const tx = this.txFactory.createTxPutBag(_class, space, objectId, bag, key, value)
return this.storage.tx(tx)
}
updateDoc <T extends Doc>(
_class: Ref<Class<T>>,
space: Ref<Space>,
@ -232,6 +256,29 @@ export class TxFactory {
}
}
createTxPutBag <P extends PropertyType>(
_class: Ref<Class<Doc>>,
space: Ref<Space>,
objectId: Ref<Doc>,
bag: string,
key: string,
value: P
): TxPutBag<P> {
return {
_id: generateId(),
_class: core.class.TxUpdateDoc,
space: core.space.Tx,
modifiedBy: this.account,
modifiedOn: Date.now(),
objectId,
objectClass: _class,
objectSpace: space,
bag,
key,
value
}
}
createTxUpdateDoc <T extends Doc>(
_class: Ref<Class<T>>,
space: Ref<Space>,

View File

@ -16,7 +16,7 @@
import {
Ref, Class, Doc, Tx, DocumentQuery, TxCreateDoc, TxRemoveDoc, Client,
FindOptions, TxUpdateDoc, _getOperator, TxProcessor, resultSort, SortingQuery,
FindResult, Hierarchy, Refs, WithLookup, LookupData
FindResult, Hierarchy, Refs, WithLookup, LookupData, TxMixin, TxPutBag
} from '@anticrm/core'
interface Query {
@ -87,6 +87,14 @@ export class LiveQuery extends TxProcessor implements Client {
}
}
protected txPutBag (tx: TxPutBag<any>): Promise<void> {
throw new Error('Method not implemented.')
}
protected txMixin (tx: TxMixin<Doc, Doc>): Promise<void> {
throw new Error('Method not implemented.')
}
protected async txUpdateDoc (tx: TxUpdateDoc<Doc>): Promise<void> {
for (const q of this.queries) {
if (q.result instanceof Promise) {

View File

@ -14,7 +14,7 @@
// limitations under the License.
//
import core, { Hierarchy, AnyAttribute, Storage, DocumentQuery, FindOptions, FindResult, TxProcessor } from '@anticrm/core'
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 { IndexedDoc, FullTextAdapter, WithFind } from './types'
@ -33,6 +33,18 @@ export class FullTextIndex extends TxProcessor implements Storage {
super()
}
protected txPutBag (tx: TxPutBag<any>): Promise<void> {
throw new Error('Method not implemented.')
}
protected txRemoveDoc (tx: TxRemoveDoc<Doc>): Promise<void> {
throw new Error('Method not implemented.')
}
protected txMixin (tx: TxMixin<Doc, Doc>): Promise<void> {
throw new Error('Method not implemented.')
}
async findAll<T extends Doc> (_class: Ref<Class<T>>, query: DocumentQuery<T>, options?: FindOptions<T>): Promise<FindResult<T>> {
console.log('search', query)
const docs = await this.adapter.search(query)

View File

@ -13,8 +13,8 @@
// limitations under the License.
//
import type { Tx, Ref, Doc, Class, DocumentQuery, FindResult, FindOptions, TxCreateDoc, TxUpdateDoc } from '@anticrm/core'
import core, { TxProcessor, Hierarchy, DOMAIN_TX, SortingOrder } from '@anticrm/core'
import core, { Tx, Ref, Doc, Class, DocumentQuery, FindResult, FindOptions, TxCreateDoc, TxUpdateDoc, TxMixin, TxPutBag, TxRemoveDoc, TxProcessor, Hierarchy, DOMAIN_TX, SortingOrder } from '@anticrm/core'
import type { DbAdapter, TxAdapter } from '@anticrm/server-core'
import { MongoClient, Db, Filter, Document, Sort } from 'mongodb'
@ -92,6 +92,18 @@ abstract class MongoAdapterBase extends TxProcessor {
}
class MongoAdapter extends MongoAdapterBase {
protected txPutBag (tx: TxPutBag<any>): Promise<void> {
throw new Error('Method not implemented.')
}
protected txRemoveDoc (tx: TxRemoveDoc<Doc>): Promise<void> {
throw new Error('Method not implemented.')
}
protected txMixin (tx: TxMixin<Doc, Doc>): Promise<void> {
throw new Error('Method not implemented.')
}
protected override async txCreateDoc (tx: TxCreateDoc<Doc>): Promise<void> {
const doc = TxProcessor.createDoc2Doc(tx)
const domain = this.hierarchy.getDomain(doc._class)
@ -106,6 +118,26 @@ class MongoAdapter extends MongoAdapterBase {
}
class MongoTxAdapter extends MongoAdapterBase implements TxAdapter {
protected txCreateDoc (tx: TxCreateDoc<Doc>): Promise<void> {
throw new Error('Method not implemented.')
}
protected txPutBag (tx: TxPutBag<any>): Promise<void> {
throw new Error('Method not implemented.')
}
protected txUpdateDoc (tx: TxUpdateDoc<Doc>): Promise<void> {
throw new Error('Method not implemented.')
}
protected txRemoveDoc (tx: TxRemoveDoc<Doc>): Promise<void> {
throw new Error('Method not implemented.')
}
protected txMixin (tx: TxMixin<Doc, Doc>): Promise<void> {
throw new Error('Method not implemented.')
}
override async tx (tx: Tx): Promise<void> {
console.log('mongotx', tx)
await this.db.collection(DOMAIN_TX).insertOne(translateDoc(tx))