tachybase_todo/packages/database/src/transaction-decorator.ts
ChengLei Shao 7a7ab2ef41
feat: add acl plugin (#169)
* feat: getRepository

* getRepository return type

* export action

* add: acl

* feat: setResourceAction

* feat: action alias

* chore: code struct

* feat: removeResourceAction

* chore: file name

* ignorecase

* remove ACL

* feat: ACL

* feat: role toJSON

* using emit

* chore: test

* feat: plugin-acl

* feat: acl with predicate

* grant universal action test

* grant action test

* update resource action test

* revoke resource action

* usingActionsConfig switch

* plugin-ui-schema-storage

* remove global acl instance

* fix: collection manager with sqlite

* add own action listener

* add acl middleware

* add acl allowConfigure strategy option

* add plugin-acl allowConfigure

* change acl resourceName

* add acl middleware merge params

* bugfix

* append fields on acl action params

* acl middleware parse template

* fix: collection-manager migrate

* add acl association field test

* feat(plugin-acl): grant association field actions

* chore(plugin-acl): type name

* feat(plugin-acl): regrant actions on resource action update

* feat(plugin-acl): regrant action on field destroy

* fix(plugin-acl): test

* fix(plugin-acl): test run

* feat(plugin-acl): set default role

* feat(plugin-users): set user default role

* test(plugin-users): create user with role

* feat(plugin-users): create user with role

Co-authored-by: chenos <chenlinxh@gmail.com>
2022-01-30 10:37:27 +08:00

59 lines
1.7 KiB
TypeScript

import lodash from 'lodash';
export function transactionWrapperBuilder(transactionGenerator) {
return function transaction(transactionInjector?) {
return (target, name, descriptor) => {
const oldValue = descriptor.value;
descriptor.value = async function () {
let transaction;
let newTransaction = false;
if (arguments.length > 0 && typeof arguments[0] === 'object') {
transaction = arguments[0]['transaction'];
}
if (!transaction) {
transaction = await transactionGenerator.apply(this);
newTransaction = true;
}
// 需要将 newTransaction 注入到被装饰函数参数内
if (newTransaction) {
try {
let callArguments;
if (lodash.isPlainObject(arguments[0])) {
callArguments = {
...arguments[0],
transaction,
};
} else if (transactionInjector) {
callArguments = transactionInjector(arguments, transaction);
} else if (lodash.isNull(arguments[0]) || lodash.isUndefined(arguments[0])) {
callArguments = {
transaction,
};
} else {
throw new Error(`please provide transactionInjector for ${name} call`);
}
const results = await oldValue.apply(this, [callArguments]);
await transaction.commit();
return results;
} catch (err) {
console.error({ err });
await transaction.rollback();
throw err;
}
} else {
return oldValue.apply(this, arguments);
}
};
return descriptor;
};
};
}