From 1a1639108bca13dfbf4ffd5079e3546feaf37c7d Mon Sep 17 00:00:00 2001 From: Andrey Platov Date: Thu, 5 Aug 2021 09:23:13 +0200 Subject: [PATCH] refactor `withOperations` Signed-off-by: Andrey Platov --- packages/core/src/__tests__/client.test.ts | 4 +- packages/core/src/__tests__/memdb.test.ts | 14 +- packages/core/src/tx.ts | 127 +++++++++--------- .../default/config/eslint.config.json | 3 +- 4 files changed, 74 insertions(+), 74 deletions(-) diff --git a/packages/core/src/__tests__/client.test.ts b/packages/core/src/__tests__/client.test.ts index 799659e2b0..1f6f822b4c 100644 --- a/packages/core/src/__tests__/client.test.ts +++ b/packages/core/src/__tests__/client.test.ts @@ -17,13 +17,13 @@ import { Space } from '../classes' import { createClient } from '../client' import core from '../component' -import { withOperations } from '../tx' +import { TxOperations } from '../tx' import { connect } from './connection' describe('client', () => { it('client', async () => { const klass = core.class.Space - const client = withOperations(core.account.System, await createClient(connect)) + const client = new TxOperations(await createClient(connect), core.account.System) const result = await client.findAll(klass, {}) expect(result).toHaveLength(2) diff --git a/packages/core/src/__tests__/memdb.test.ts b/packages/core/src/__tests__/memdb.test.ts index dc2e72ac89..8f508eacfe 100644 --- a/packages/core/src/__tests__/memdb.test.ts +++ b/packages/core/src/__tests__/memdb.test.ts @@ -18,7 +18,7 @@ import core from '../component' import { Hierarchy } from '../hierarchy' import { ModelDb, TxDb } from '../memdb' import { SortingOrder } from '../storage' -import { withOperations } from '../tx' +import { TxOperations } from '../tx' import { genMinModel } from './minmodel' const txes = genMinModel() @@ -52,7 +52,7 @@ describe('memdb', () => { const result = await model.findAll(core.class.Space, {}) expect(result.length).toBe(2) - const ops = withOperations(core.account.System, model) + const ops = new TxOperations(model, core.account.System) await ops.removeDoc(result[0]._class, result[0].space, result[0]._id) const result2 = await model.findAll(core.class.Space, {}) expect(result2).toHaveLength(1) @@ -122,7 +122,7 @@ describe('memdb', () => { it('should push to array', async () => { const hierarchy = new Hierarchy() for (const tx of txes) await hierarchy.tx(tx) - const model = withOperations(core.account.System, new ModelDb(hierarchy)) + const model = new TxOperations(new ModelDb(hierarchy), core.account.System) for (const tx of txes) await model.tx(tx) const space = await model.createDoc(core.class.Space, core.space.Model, { name: 'name', @@ -131,15 +131,15 @@ describe('memdb', () => { members: [] }) const account = await model.createDoc(core.class.Account, core.space.Model, {}) - await model.updateDoc(core.class.Space, core.space.Model, space._id, { $push: { members: account._id } }) - const txSpace = await model.findAll(core.class.Space, { _id: space._id }) - expect(txSpace[0].members).toEqual(expect.arrayContaining([account._id])) + await model.updateDoc(core.class.Space, core.space.Model, space, { $push: { members: account } }) + const txSpace = await model.findAll(core.class.Space, { _id: space }) + expect(txSpace[0].members).toEqual(expect.arrayContaining([account])) }) it('limit and sorting', async () => { const hierarchy = new Hierarchy() for (const tx of txes) hierarchy.tx(tx) - const model = withOperations(core.account.System, new ModelDb(hierarchy)) + const model = new TxOperations(new ModelDb(hierarchy), core.account.System) for (const tx of txes) await model.tx(tx) const without = await model.findAll(core.class.Space, {}) diff --git a/packages/core/src/tx.ts b/packages/core/src/tx.ts index d10a59a5a8..a6c71ccd12 100644 --- a/packages/core/src/tx.ts +++ b/packages/core/src/tx.ts @@ -15,14 +15,10 @@ import type { KeysByType } from 'simplytyped' import type { Class, Data, Doc, Domain, Ref, Account, Space, Arr, Mixin } from './classes' +import { DocumentQuery, FindOptions, FindResult, Storage } from './storage' import core from './component' import { generateId } from './utils' -// export interface TxFactory { -// createTxCreateDoc: (_class: Ref>, space: Ref, attributes: Data) => TxCreateDoc -// createTxMixin: (objectId: Ref, objectClass: Ref>, mixin: Ref>, attributes: ExtendedAttributes) => TxMixin -// } - /** * @public */ @@ -139,82 +135,49 @@ export class TxProcessor implements WithTx { /** * @public */ -export interface TxOperations { - createDoc: (_class: Ref>, space: Ref, attributes: Data) => Promise - updateDoc: ( - _class: Ref>, - space: Ref, - objectId: Ref, - operations: DocumentUpdate - ) => Promise - removeDoc: (_class: Ref>, space: Ref, objectId: Ref) => Promise -} +export class TxOperations implements Storage { + private readonly txFactory: TxFactory -/** - * @public - */ -export function withOperations (user: Ref, storage: T): T & TxOperations { - const result = storage as T & TxOperations + constructor (private readonly storage: Storage, user: Ref) { + this.txFactory = new TxFactory(user) + } - result.createDoc = async ( + findAll (_class: Ref>, query: DocumentQuery, options?: FindOptions | undefined): Promise> { + return this.storage.findAll(_class, query, options) + } + + tx (tx: Tx): Promise { + return this.storage.tx(tx) + } + + async createDoc ( _class: Ref>, space: Ref, attributes: Data - ): Promise => { - const tx: TxCreateDoc = { - _id: generateId(), - _class: core.class.TxCreateDoc, - space: core.space.Tx, - modifiedBy: user, - modifiedOn: Date.now(), - objectId: generateId(), - objectClass: _class, - objectSpace: space, - attributes - } - await storage.tx(tx) - return TxProcessor.createDoc2Doc(tx) + ): Promise> { + const tx = this.txFactory.createTxCreateDoc(_class, space, attributes) + await this.storage.tx(tx) + return tx.objectId } - result.updateDoc = async ( + updateDoc ( _class: Ref>, space: Ref, objectId: Ref, operations: DocumentUpdate - ): Promise => { - const tx: TxUpdateDoc = { - _id: generateId(), - _class: core.class.TxUpdateDoc, - space: core.space.Tx, - modifiedBy: user, - modifiedOn: Date.now(), - objectId, - objectClass: _class, - objectSpace: space, - operations - } - await storage.tx(tx) + ): Promise { + const tx = this.txFactory.createTxUpdateDoc(_class, space, objectId, operations) + return this.storage.tx(tx) } - result.removeDoc = async ( + removeDoc ( _class: Ref>, space: Ref, objectId: Ref - ): Promise => { - const tx: TxRemoveDoc = { - _id: generateId(), - _class: core.class.TxRemoveDoc, - space: core.space.Tx, - modifiedBy: user, - modifiedOn: Date.now(), - objectId, - objectClass: _class, - objectSpace: space - } - await storage.tx(tx) + ): Promise { + const tx = this.txFactory.createTxRemoveDoc(_class, space, objectId) + return this.storage.tx(tx) } - - return result } /** @@ -237,6 +200,42 @@ export class TxFactory { } } + createTxUpdateDoc ( + _class: Ref>, + space: Ref, + objectId: Ref, + operations: DocumentUpdate + ): TxUpdateDoc { + return { + _id: generateId(), + _class: core.class.TxUpdateDoc, + space: core.space.Tx, + modifiedBy: this.account, + modifiedOn: Date.now(), + objectId, + objectClass: _class, + objectSpace: space, + operations + } + } + + createTxRemoveDoc ( + _class: Ref>, + space: Ref, + objectId: Ref + ): TxRemoveDoc { + return { + _id: generateId(), + _class: core.class.TxRemoveDoc, + space: core.space.Tx, + modifiedBy: this.account, + modifiedOn: Date.now(), + objectId, + objectClass: _class, + objectSpace: space + } + } + createTxMixin(objectId: Ref, objectClass: Ref>, mixin: Ref>, attributes: ExtendedAttributes): TxMixin { return { _id: generateId(), diff --git a/packages/platform-rig/profiles/default/config/eslint.config.json b/packages/platform-rig/profiles/default/config/eslint.config.json index c93dcfcbb0..71fa2b5ee5 100644 --- a/packages/platform-rig/profiles/default/config/eslint.config.json +++ b/packages/platform-rig/profiles/default/config/eslint.config.json @@ -3,6 +3,7 @@ "standard-with-typescript" ], "rules": { - "@typescript-eslint/array-type": "off" + "@typescript-eslint/array-type": "off", + "@typescript-eslint/promise-function-async": "off" } }