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
+4 -3
View File
@@ -13,7 +13,7 @@
// limitations under the License.
//
import type { Storage, DocumentQuery, FindResult } from '../storage'
import type { Storage, DocumentQuery, FindResult, TxResult } from '../storage'
import type { Class, Doc, Ref } from '../classes'
import type { Tx } from '../tx'
import core from '../component'
@@ -43,11 +43,12 @@ export async function connect (handler: (tx: Tx) => void): Promise<Storage> {
return {
findAll,
tx: async (tx: Tx): Promise<void> => {
tx: async (tx: Tx): Promise<TxResult> => {
if (tx.objectSpace === core.space.Model) {
hierarchy.tx(tx)
}
await Promise.all([model.tx(tx), transactions.tx(tx)])
const result = await Promise.all([model.tx(tx), transactions.tx(tx)])
return result[0]
// handler(tx) - we have only one client, should not update?
}
}
+3 -3
View File
@@ -15,7 +15,7 @@
import type { Doc, Ref, Class } from './classes'
import type { Tx } from './tx'
import type { Storage, DocumentQuery, FindOptions, FindResult, WithLookup } from './storage'
import type { Storage, DocumentQuery, FindOptions, FindResult, WithLookup, TxResult } from './storage'
import { SortingOrder } from './storage'
import { Hierarchy } from './hierarchy'
@@ -73,13 +73,13 @@ class ClientImpl implements Client {
return (await this.findAll(_class, query, options))[0]
}
async tx (tx: Tx): Promise<void> {
async tx (tx: Tx): Promise<TxResult> {
if (tx.objectSpace === core.space.Model) {
this.hierarchy.tx(tx)
await this.model.tx(tx)
}
this.notify?.(tx)
await this.conn.tx(tx)
return await this.conn.tx(tx)
}
async updateFromRemote (tx: Tx): Promise<void> {
+18 -12
View File
@@ -20,7 +20,7 @@ import core from './component'
import type { Hierarchy } from './hierarchy'
import { _getOperator } from './operator'
import { findProperty, resultSort } from './query'
import type { DocumentQuery, FindOptions, FindResult, Storage, WithLookup, LookupData, Refs } from './storage'
import type { DocumentQuery, FindOptions, FindResult, Storage, WithLookup, LookupData, Refs, TxResult } from './storage'
import { TxProcessor } from './tx'
import clone from 'just-clone'
@@ -149,28 +149,29 @@ export abstract class MemDb extends TxProcessor {
* @public
*/
export class TxDb extends MemDb implements Storage {
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.')
}
async tx (tx: Tx): Promise<void> {
async tx (tx: Tx): Promise<TxResult> {
this.addDoc(tx)
return {}
}
}
@@ -180,7 +181,7 @@ export class TxDb extends MemDb implements Storage {
* @public
*/
export class ModelDb extends MemDb implements Storage {
protected override async txPutBag (tx: TxPutBag<any>): Promise<void> {
protected override async txPutBag (tx: TxPutBag<any>): Promise<TxResult> {
const doc = this.getObject(tx.objectId) as any
let bag = doc[tx.bag]
if (bag === undefined) {
@@ -189,13 +190,15 @@ export class ModelDb extends MemDb implements Storage {
bag[tx.key] = tx.value
doc.modifiedBy = tx.modifiedBy
doc.modifiedOn = tx.modifiedOn
return {}
}
protected override async txCreateDoc (tx: TxCreateDoc<Doc>): Promise<void> {
protected override async txCreateDoc (tx: TxCreateDoc<Doc>): Promise<TxResult> {
this.addDoc(TxProcessor.createDoc2Doc(tx))
return {}
}
protected async txUpdateDoc (tx: TxUpdateDoc<Doc>): Promise<void> {
protected async txUpdateDoc (tx: TxUpdateDoc<Doc>): Promise<TxResult> {
const doc = this.getObject(tx.objectId) as any
const ops = tx.operations as any
for (const key in ops) {
@@ -208,15 +211,18 @@ export class ModelDb extends MemDb implements Storage {
}
doc.modifiedBy = tx.modifiedBy
doc.modifiedOn = tx.modifiedOn
return {}
}
protected async txRemoveDoc (tx: TxRemoveDoc<Doc>): Promise<void> {
protected async txRemoveDoc (tx: TxRemoveDoc<Doc>): Promise<TxResult> {
this.delDoc(tx.objectId)
return {}
}
// TODO: process ancessor mixins
protected async txMixin (tx: TxMixin<Doc, Doc>): Promise<void> {
protected async txMixin (tx: TxMixin<Doc, Doc>): Promise<TxResult> {
const obj = this.getObject(tx.objectId) as any
obj[tx.mixin] = tx.attributes
return {}
}
}
+7 -1
View File
@@ -101,6 +101,12 @@ export type WithLookup<T extends Doc> = T & {
*/
export type FindResult<T extends Doc> = WithLookup<T>[]
/**
* @public
*/
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface TxResult {}
/**
* @public
*/
@@ -110,5 +116,5 @@ export interface Storage {
query: DocumentQuery<T>,
options?: FindOptions<T>
) => Promise<FindResult<T>>
tx: (tx: Tx) => Promise<void>
tx: (tx: Tx) => Promise<TxResult>
}
+15 -14
View File
@@ -15,7 +15,7 @@
import type { KeysByType } from 'simplytyped'
import type { Class, Data, Doc, Domain, Ref, Account, Space, Arr, Mixin, PropertyType } from './classes'
import { DocumentQuery, FindOptions, FindResult, Storage, WithLookup } from './storage'
import type { DocumentQuery, FindOptions, FindResult, Storage, WithLookup, TxResult } from './storage'
import core from './component'
import { generateId } from './utils'
@@ -172,14 +172,14 @@ export const DOMAIN_TX = 'tx' as Domain
* @public
*/
export interface WithTx {
tx: (tx: Tx) => Promise<void>
tx: (tx: Tx) => Promise<TxResult>
}
/**
* @public
*/
export abstract class TxProcessor implements WithTx {
async tx (tx: Tx): Promise<void> {
async tx (tx: Tx): Promise<TxResult> {
switch (tx._class) {
case core.class.TxCreateDoc:
return await this.txCreateDoc(tx as TxCreateDoc<Doc>)
@@ -209,17 +209,18 @@ export abstract class TxProcessor implements WithTx {
} as T
}
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>
protected abstract txCreateDoc (tx: TxCreateDoc<Doc>): Promise<TxResult>
protected abstract txPutBag (tx: TxPutBag<PropertyType>): Promise<TxResult>
protected abstract txUpdateDoc (tx: TxUpdateDoc<Doc>): Promise<TxResult>
protected abstract txRemoveDoc (tx: TxRemoveDoc<Doc>): Promise<TxResult>
protected abstract txMixin (tx: TxMixin<Doc, Doc>): Promise<TxResult>
protected async txBulkWrite (bulkTx: TxBulkWrite): Promise<void> {
protected async txBulkWrite (bulkTx: TxBulkWrite): Promise<TxResult> {
for (const tx of bulkTx.txes) {
console.log('bulk', tx)
await this.tx(tx)
}
return {}
}
}
@@ -241,7 +242,7 @@ export class TxOperations implements Storage {
return (await this.findAll(_class, query, options))[0]
}
tx (tx: Tx): Promise<void> {
tx (tx: Tx): Promise<TxResult> {
return this.storage.tx(tx)
}
@@ -263,7 +264,7 @@ export class TxOperations implements Storage {
bag: string,
key: string,
value: P
): Promise<void> {
): Promise<TxResult> {
const tx = this.txFactory.createTxPutBag(_class, space, objectId, bag, key, value)
return this.storage.tx(tx)
}
@@ -273,7 +274,7 @@ export class TxOperations implements Storage {
space: Ref<Space>,
objectId: Ref<T>,
operations: DocumentUpdate<T>
): Promise<void> {
): Promise<TxResult> {
const tx = this.txFactory.createTxUpdateDoc(_class, space, objectId, operations)
return this.storage.tx(tx)
}
@@ -282,7 +283,7 @@ export class TxOperations implements Storage {
_class: Ref<Class<T>>,
space: Ref<Space>,
objectId: Ref<T>
): Promise<void> {
): Promise<TxResult> {
const tx = this.txFactory.createTxRemoveDoc(_class, space, objectId)
return this.storage.tx(tx)
}
@@ -292,7 +293,7 @@ export class TxOperations implements Storage {
objectClass: Ref<Class<D>>,
mixin: Ref<Mixin<M>>,
attributes: ExtendedAttributes<D, M>
): Promise<void> {
): Promise<TxResult> {
const tx = this.txFactory.createTxMixin(objectId, objectClass, mixin, attributes)
return this.storage.tx(tx)
}