Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 事务注解增加数据源选项 #135

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions core/transaction-decorator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
事务注解

## Usage
### 传播机制
```ts
export class Foo {

Expand All @@ -16,6 +17,18 @@ export class Foo {
console.log('has msg: ', msg);
}

}
```

### 数据源
```ts
export class Bar {

@Transactional({ dataSourceName: 'xx' })
async bar() {
await this.foo();
}

}

```
Expand Down
6 changes: 6 additions & 0 deletions core/transaction-decorator/src/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ export enum PropagationType {
export interface TransactionalParams {
/** 事务传播方式,默认 REQUIRED */
propagation?: PropagationType;
/**
* 数据源,默认使用 module 的数据源,非 module 时将使用 default 数据源
* 需要注意的是数据源之间的连接是隔离的,回滚也是独立的
* 比如函数 B 在函数 A 中执行,A 执行异常时,不会回滚 B 中执行的 sql
* */
datasourceName?: string;
}
2 changes: 2 additions & 0 deletions core/transaction-decorator/src/decorator/Transactional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ export function Transactional(params?: TransactionalParams) {
if (!Object.values(PropagationType).includes(propagation)) {
throw new Error(`unknown propagation type ${propagation}`);
}
const datasourceName = params?.datasourceName;

return function(target: any, propertyKey: PropertyKey) {
const constructor: EggProtoImplClass = target.constructor;
TransactionMetadataUtil.setIsTransactionClazz(constructor);
TransactionMetadataUtil.addTransactionMetadata(constructor, {
propagation,
method: propertyKey,
datasourceName,
});
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import { PropagationType } from '../Common';
export interface TransactionMetadata {
propagation: PropagationType;
method: PropertyKey;
datasourceName?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,35 @@ describe('test/builder/TransactionMetaBuilder.test.ts', () => {
assert.deepStrictEqual(fooBuilder.build(), [{
propagation: PropagationType.REQUIRED,
method: 'defaultPropagation',
datasourceName: undefined,
}, {
propagation: PropagationType.REQUIRED,
method: 'requiredPropagation',
datasourceName: 'testDatasourceName1',
}, {
propagation: PropagationType.ALWAYS_NEW,
method: 'alwaysNewPropagation',
datasourceName: undefined,
}]);

assert.ok(TransactionMetadataUtil.isTransactionClazz(Bar));
const barBuilder = new TransactionMetaBuilder(Bar);
assert.deepStrictEqual(barBuilder.build(), [{
propagation: PropagationType.REQUIRED,
method: 'foo',
datasourceName: 'datasourceName2',
}, {
propagation: PropagationType.ALWAYS_NEW,
method: 'bar',
datasourceName: undefined,
}]);

assert.ok(TransactionMetadataUtil.isTransactionClazz(FooBar));
const fooBarBuilder = new TransactionMetaBuilder(FooBar);
assert.deepStrictEqual(fooBarBuilder.build(), [{
propagation: PropagationType.ALWAYS_NEW,
method: 'foo',
datasourceName: undefined,
}]);

const barFooBuilder = new TransactionMetaBuilder(BarFoo);
Expand Down
6 changes: 4 additions & 2 deletions core/transaction-decorator/test/fixtures/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export class Foo {
console.log('msg: ', msg);
}

@Transactional({})
@Transactional({
datasourceName: 'testDatasourceName1',
})
async requiredPropagation(msg) {
console.log('msg: ', msg);
}
Expand All @@ -22,7 +24,7 @@ export class Foo {

export class Bar {

@Transactional()
@Transactional({ datasourceName: 'datasourceName2' })
async foo(msg) {
console.log('msg: ', msg);
}
Expand Down