-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(mwcp): breaking change handler of Transactional(), remove ca…
…che operation use @Cacheable decorator instead
- Loading branch information
1 parent
d1fe174
commit c46ab4e
Showing
43 changed files
with
340 additions
and
520 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 2 additions & 103 deletions
105
packages/midway-component-kmore/src/decorator/index.decorator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
|
33 changes: 33 additions & 0 deletions
33
packages/midway-component-kmore/src/decorator/transactional.handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
Oops, something went wrong.