Skip to content

Commit

Permalink
refactor(mwcp): breaking change handler of Transactional(), remove ca…
Browse files Browse the repository at this point in the history
…che operation

use @Cacheable decorator instead
  • Loading branch information
waitingsong committed May 6, 2024
1 parent d1fe174 commit c46ab4e
Show file tree
Hide file tree
Showing 43 changed files with 340 additions and 520 deletions.
6 changes: 3 additions & 3 deletions packages/midway-component-kmore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@
},
"license": "MIT",
"dependencies": {
"@mwcp/cache": "^26.0.0",
"@mwcp/otel": "^26.0.0",
"@mwcp/share": "^26.0.0",
"@mwcp/otel": "^26.5.1",
"@mwcp/share": "^26.5.0",
"@waiting/shared-core": "^23.6.1",
"kmore": "^59.5.6",
"knex": "^3.1.0"
},
"devDependencies": {
"@mwcp/cache": "^26.5.0",
"kmore-cli": "^59.5.6",
"kmore-types": "^59.5.6"
},
Expand Down
25 changes: 2 additions & 23 deletions packages/midway-component-kmore/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,14 @@ import {
Application,
IMidwayContainer,
MConfig,
RegisterDecoratorHandlerParam,
registerDecoratorHandler,
registerMiddleware,
deleteRouter,
} from '@mwcp/share'
import { sleep } from '@waiting/shared-core'


import * as DefaultConfig from './config/config.default.js'
import * as LocalConfig from './config/config.local.js'
import * as UnittestConfig from './config/config.unittest.js'
import {
METHOD_KEY_Transactional,
genDecoratorExecutorOptions,
transactionalDecoratorExecutor,
} from './decorator/decorator.helper.js'
import { useComponents } from './imports.js'
import { DbSourceManager } from './lib/db-source-manager.js'
import { Config, ConfigKey, KmorePropagationConfig, KmoreSourceConfig } from './lib/index.js'
Expand Down Expand Up @@ -67,7 +59,7 @@ export class AutoConfiguration implements ILifeCycle {

@MConfig(ConfigKey.propagationConfig) protected readonly propagationConfig: KmorePropagationConfig

@Inject() readonly dbSManager: DbSourceManager
@Inject() readonly dbSourceManager: DbSourceManager

@Inject() decoratorService: MidwayDecoratorService

Expand All @@ -86,19 +78,6 @@ export class AutoConfiguration implements ILifeCycle {

// 全局db处理中间件,请求结束时回滚/提交所有本次请求未提交事务
registerMiddleware(this.app, KmoreMiddleware)

const optsCacheable: RegisterDecoratorHandlerParam = {
decoratorKey: METHOD_KEY_Transactional,
decoratorService: this.decoratorService,
fnDecoratorExecutorAsync: transactionalDecoratorExecutor,
fnDecoratorExecutorSync: 'bypass',
fnGenDecoratorExecutorParam: genDecoratorExecutorOptions,
}
const aroundFactoryOptions = {
webApp: this.app,
config: this.propagationConfig,
}
registerDecoratorHandler(optsCacheable, aroundFactoryOptions)
}

@TraceInit({ namespace: ConfigKey.namespace })
Expand All @@ -112,7 +91,7 @@ export class AutoConfiguration implements ILifeCycle {
const out = 10000

const p1 = new Promise<void>(done => setTimeout(done, out))
const p2 = this.dbSManager.stop()
const p2 = this.dbSourceManager.stop()
await Promise.race([p1, p2])
.catch((ex: Error) => {
console.error(ex.message)
Expand Down
105 changes: 2 additions & 103 deletions packages/midway-component-kmore/src/decorator/index.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,105 +1,4 @@
import { assert } from 'console'

import {
CacheableArgs,
METHOD_KEY_CacheEvict,
METHOD_KEY_CachePut,
METHOD_KEY_Cacheable,
cacheableClassIgnoreIfMethodDecoratorKeys,
cacheableMethodIgnoreIfMethodDecoratorKeys,
} from '@mwcp/cache'
import {
CustomDecoratorFactoryParam,
customDecoratorFactory,
regCustomDecorator,
} from '@mwcp/share'

import {
MethodType,
TransactionalArgs,
TRX_CLASS_KEY,
METHOD_KEY_Transactional,
} from './decorator.helper.js'


export {
classDecoratorKeyMap,
methodDecoratorKeyMap,
} from './decorator.helper.js'

export {
TransactionalArgs as DecoratorArgs,
TRX_CLASS_KEY,
METHOD_KEY_Transactional,
METHOD_KEY_Transactional as TRX_METHOD_KEY,
}


/**
* 声明式事务装饰器
* Declarative Transactional Decorator
* @description default config can be set via `KmorePropagationConfig`
* in `src/config/config.{default|prod|local}.ts`
*/
export function Transactional<M extends MethodType | undefined = undefined>(
/**
* @default {@link Propagation.REQUIRED}
*/
propagationType?: TransactionalArgs['propagationType'],
propagationOptions?: TransactionalArgs['propagationOptions'],
cacheOptions: TransactionalArgs<M>['cacheOptions'] = false,
) {

const options: Partial<TransactionalArgs<M>> = {
// propagationType,
// propagationOptions,
// cacheOptions,
}
// !!
if (propagationType) {
options.propagationType = propagationType
}
if (propagationOptions) {
options.propagationOptions = propagationOptions
}
if (cacheOptions) {
options.cacheOptions = cacheOptions
}

const opts: CustomDecoratorFactoryParam<TransactionalArgs<M>> = {
decoratorKey: METHOD_KEY_Transactional,
decoratorArgs: options,
enableClassDecorator: true,
}
if (cacheOptions) {
let dkey = ''
switch (cacheOptions.op) {
case 'Cacheable':
dkey = METHOD_KEY_Cacheable
break

case 'CacheEvict':
dkey = METHOD_KEY_CacheEvict
break

case 'CachePut':
dkey = METHOD_KEY_CachePut
break
}
assert(dkey, `invalid cacheOptions.op: ${cacheOptions.op}`)

opts.before = (target, propertyName, descriptor) => {
const opts2: CustomDecoratorFactoryParam<CacheableArgs<M>> = {
decoratorKey: dkey,
decoratorArgs: cacheOptions,
enableClassDecorator: false,
classIgnoreIfMethodDecoratorKeys: cacheableClassIgnoreIfMethodDecoratorKeys,
methodIgnoreIfMethodDecoratorKeys: cacheableMethodIgnoreIfMethodDecoratorKeys,
}
regCustomDecorator(target, propertyName, descriptor, opts2)
}
}

return customDecoratorFactory<TransactionalArgs<M>>(opts)
}
export * from './transactional.js'
export * from './transactional.handler.js'

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Singleton } from '@midwayjs/core'
import { MConfig, DecoratorExecutorParamBase, DecoratorHandlerBase } from '@mwcp/share'

import { ConfigKey, KmorePropagationConfig } from '##/lib/types.js'

import { decoratorExecutorAsync, decoratorExecutorSync, genDecoratorExecutorOptionsAsync } from './transactional.helper.js'
import { DecoratorExecutorOptions, GenDecoratorExecutorOptionsExt } from './transactional.types.js'


@Singleton()
export class DecoratorHandlerTransactional extends DecoratorHandlerBase {
@MConfig(ConfigKey.propagationConfig) protected readonly propagationConfig: KmorePropagationConfig

override async genExecutorParamAsync(options: DecoratorExecutorParamBase): Promise<DecoratorExecutorOptions> {
const optsExt: GenDecoratorExecutorOptionsExt = {
propagationConfig: this.propagationConfig,
}
const ret = genDecoratorExecutorOptionsAsync(options, optsExt)
return ret
}

override async executorAsync(options: DecoratorExecutorOptions) {
return decoratorExecutorAsync(options)
}

/**
* @Caution Will not execute any method including database operations, just return the result
*/
override executorSync(options: DecoratorExecutorOptions) {
return decoratorExecutorSync(options)
}
}

Loading

0 comments on commit c46ab4e

Please sign in to comment.