From 16f861ad7d735dcb5c3ce7ad32ff9439742dc4d2 Mon Sep 17 00:00:00 2001 From: Junyi Date: Sun, 22 May 2022 08:48:19 +0800 Subject: [PATCH] fix(type): use sequelize native Transactionable instead of TransactionAble (#410) --- .../multiple-relation-repository.ts | 5 ++-- .../single-relation-repository.ts | 12 +++++----- packages/core/database/src/repository.ts | 10 ++++---- .../core/database/src/update-associations.ts | 3 +-- .../src/models/application.ts | 4 ++-- .../ui-schema-storage/src/repository.ts | 24 +++++++++---------- .../plugins/users/src/models/UserModel.ts | 4 ++-- 7 files changed, 29 insertions(+), 33 deletions(-) diff --git a/packages/core/database/src/relation-repository/multiple-relation-repository.ts b/packages/core/database/src/relation-repository/multiple-relation-repository.ts index 137999b2a..175672cb7 100644 --- a/packages/core/database/src/relation-repository/multiple-relation-repository.ts +++ b/packages/core/database/src/relation-repository/multiple-relation-repository.ts @@ -1,5 +1,5 @@ import { omit } from 'lodash'; -import { MultiAssociationAccessors, Op, Sequelize, Transaction } from 'sequelize'; +import { MultiAssociationAccessors, Op, Sequelize, Transaction, Transactionable } from 'sequelize'; import { CommonFindOptions, CountOptions, @@ -9,7 +9,6 @@ import { FindOptions, TargetKey, TK, - TransactionAble, UpdateOptions } from '../repository'; import { updateModelByValues } from '../update-associations'; @@ -20,7 +19,7 @@ export interface FindAndCountOptions extends CommonFindOptions {} export interface FindOneOptions extends CommonFindOptions, FilterByTk {} -export interface AssociatedOptions extends TransactionAble { +export interface AssociatedOptions extends Transactionable { tk?: TK; } diff --git a/packages/core/database/src/relation-repository/single-relation-repository.ts b/packages/core/database/src/relation-repository/single-relation-repository.ts index f315c8e17..9c59a215c 100644 --- a/packages/core/database/src/relation-repository/single-relation-repository.ts +++ b/packages/core/database/src/relation-repository/single-relation-repository.ts @@ -1,24 +1,24 @@ import lodash from 'lodash'; -import { SingleAssociationAccessors } from 'sequelize'; +import { SingleAssociationAccessors, Transactionable } from 'sequelize'; import { Model } from '../model'; -import { Appends, Except, Fields, Filter, TargetKey, TransactionAble, UpdateOptions } from '../repository'; +import { Appends, Except, Fields, Filter, TargetKey, UpdateOptions } from '../repository'; import { updateModelByValues } from '../update-associations'; import { RelationRepository, transaction } from './relation-repository'; -export interface SingleRelationFindOption extends TransactionAble { +export interface SingleRelationFindOption extends Transactionable { fields?: Fields; except?: Except; appends?: Appends; filter?: Filter; } -interface SetOption extends TransactionAble { +interface SetOption extends Transactionable { tk?: TargetKey; } export abstract class SingleRelationRepository extends RelationRepository { @transaction() - async remove(options?: TransactionAble): Promise { + async remove(options?: Transactionable): Promise { const transaction = await this.getTransaction(options); const sourceModel = await this.getSourceModel(transaction); return await sourceModel[this.accessors().set](null, { @@ -63,7 +63,7 @@ export abstract class SingleRelationRepository extends RelationRepository { } @transaction() - async destroy(options?: TransactionAble): Promise { + async destroy(options?: Transactionable): Promise { const transaction = await this.getTransaction(options); const target = await this.find({ diff --git a/packages/core/database/src/repository.ts b/packages/core/database/src/repository.ts index d9b80f887..ba15f1e4f 100644 --- a/packages/core/database/src/repository.ts +++ b/packages/core/database/src/repository.ts @@ -8,7 +8,7 @@ import { FindOptions as SequelizeFindOptions, ModelCtor, Op, - Transaction, + Transactionable, UpdateOptions as SequelizeUpdateOptions } from 'sequelize'; import { Collection } from './collection'; @@ -34,9 +34,7 @@ interface CreateManyOptions extends BulkCreateOptions { records: Values[]; } -export interface TransactionAble { - transaction?: Transaction; -} +export { Transactionable } from 'sequelize'; export interface FilterAble { filter: Filter; @@ -57,7 +55,7 @@ export type AssociationKeysToBeUpdate = string[]; export type Values = any; -export interface CountOptions extends Omit, TransactionAble { +export interface CountOptions extends Omit, Transactionable { fields?: Fields; filter?: Filter; } @@ -68,7 +66,7 @@ export interface FilterByTk { export interface FindOptions extends SequelizeFindOptions, CommonFindOptions, FilterByTk {} -export interface CommonFindOptions extends TransactionAble { +export interface CommonFindOptions extends Transactionable { filter?: Filter; fields?: Fields; appends?: Appends; diff --git a/packages/core/database/src/update-associations.ts b/packages/core/database/src/update-associations.ts index 15faba8e6..f30881482 100644 --- a/packages/core/database/src/update-associations.ts +++ b/packages/core/database/src/update-associations.ts @@ -9,7 +9,6 @@ import { Transactionable, } from 'sequelize'; import { Model } from './model'; -import { TransactionAble } from './repository'; import { UpdateGuard } from './update-guard'; function isUndefinedOrNull(value: any) { @@ -46,7 +45,7 @@ export function modelAssociationByKey(instance: Model, key: string): Association type UpdateValue = { [key: string]: any }; -interface UpdateOptions extends TransactionAble { +interface UpdateOptions extends Transactionable { filter?: any; filterByTk?: number | string; // 字段白名单 diff --git a/packages/plugins/multi-app-manager/src/models/application.ts b/packages/plugins/multi-app-manager/src/models/application.ts index 56a269398..ee1371d9c 100644 --- a/packages/plugins/multi-app-manager/src/models/application.ts +++ b/packages/plugins/multi-app-manager/src/models/application.ts @@ -1,9 +1,9 @@ -import Database, { IDatabaseOptions, Model, TransactionAble } from '@nocobase/database'; +import Database, { IDatabaseOptions, Model, Transactionable } from '@nocobase/database'; import { Application } from '@nocobase/server'; import lodash from 'lodash'; import * as path from 'path'; -export interface registerAppOptions extends TransactionAble { +export interface registerAppOptions extends Transactionable { skipInstall?: boolean; } diff --git a/packages/plugins/ui-schema-storage/src/repository.ts b/packages/plugins/ui-schema-storage/src/repository.ts index e35521a03..2807d5ed0 100644 --- a/packages/plugins/ui-schema-storage/src/repository.ts +++ b/packages/plugins/ui-schema-storage/src/repository.ts @@ -1,4 +1,4 @@ -import { Repository, TransactionAble } from '@nocobase/database'; +import { Repository, Transactionable } from '@nocobase/database'; import { uid } from '@nocobase/utils'; import lodash from 'lodash'; import { Transaction } from 'sequelize'; @@ -13,7 +13,7 @@ type BreakRemoveOnType = { [key: string]: any; }; -export interface removeParentOptions extends TransactionAble { +export interface removeParentOptions extends Transactionable { removeParentsIfNoChildren?: boolean; breakRemoveOn?: BreakRemoveOnType; } @@ -144,7 +144,7 @@ export class UiSchemaRepository extends Repository { return carry; } - async getProperties(uid: string, options: TransactionAble = {}) { + async getProperties(uid: string, options: Transactionable = {}) { const { transaction } = options; const db = this.database; @@ -256,7 +256,7 @@ export class UiSchemaRepository extends Repository { } @transaction() - async clearAncestor(uid: string, options?: TransactionAble) { + async clearAncestor(uid: string, options?: Transactionable) { const db = this.database; const treeTable = this.uiSchemaTreePathTableName; @@ -397,7 +397,7 @@ export class UiSchemaRepository extends Repository { return null; } - async removeEmptyParents(options: TransactionAble & { uid: string; breakRemoveOn?: BreakRemoveOnType }) { + async removeEmptyParents(options: Transactionable & { uid: string; breakRemoveOn?: BreakRemoveOnType }) { const { transaction, uid, breakRemoveOn } = options; const removeParent = async (nodeUid: string) => { @@ -429,7 +429,7 @@ export class UiSchemaRepository extends Repository { return true; } - async recursivelyRemoveIfNoChildren(options: TransactionAble & { uid: string; breakRemoveOn?: BreakRemoveOnType }) { + async recursivelyRemoveIfNoChildren(options: Transactionable & { uid: string; breakRemoveOn?: BreakRemoveOnType }) { const { uid, transaction, breakRemoveOn } = options; const removeLeafNode = async (nodeUid: string) => { @@ -456,7 +456,7 @@ export class UiSchemaRepository extends Repository { } @transaction() - async remove(uid: string, options?: TransactionAble & removeParentOptions) { + async remove(uid: string, options?: Transactionable & removeParentOptions) { let { transaction } = options; if (options?.removeParentsIfNoChildren) { @@ -548,7 +548,7 @@ export class UiSchemaRepository extends Repository { }); } - private async schemaExists(schema: any, options?: TransactionAble): Promise { + private async schemaExists(schema: any, options?: Transactionable): Promise { if (lodash.isObject(schema) && !schema['x-uid']) { return false; } @@ -631,7 +631,7 @@ export class UiSchemaRepository extends Repository { } @transaction() - async insertNodes(nodes: SchemaNode[], options?: TransactionAble) { + async insertNodes(nodes: SchemaNode[], options?: Transactionable) { const { transaction } = options; const insertedNodes = []; @@ -649,7 +649,7 @@ export class UiSchemaRepository extends Repository { } @transaction() - async insert(schema: any, options?: TransactionAble) { + async insert(schema: any, options?: Transactionable) { const nodes = UiSchemaRepository.schemaToSingleNodes(schema); const insertedNodes = await this.insertNodes(nodes, options); return this.getJsonSchema(insertedNodes[0].get('x-uid'), { @@ -660,7 +660,7 @@ export class UiSchemaRepository extends Repository { @transaction() async insertNewSchema( schema: any, - options?: TransactionAble & { + options?: Transactionable & { returnNode?: boolean; }, ) { @@ -760,7 +760,7 @@ export class UiSchemaRepository extends Repository { return { uid, name, async, childOptions }; } - async insertSingleNode(schema: SchemaNode, options: TransactionAble & removeParentOptions) { + async insertSingleNode(schema: SchemaNode, options: Transactionable & removeParentOptions) { const { transaction } = options; const db = this.database; diff --git a/packages/plugins/users/src/models/UserModel.ts b/packages/plugins/users/src/models/UserModel.ts index 70f635e11..c8f2d2d3d 100644 --- a/packages/plugins/users/src/models/UserModel.ts +++ b/packages/plugins/users/src/models/UserModel.ts @@ -1,7 +1,7 @@ -import Database, { Model, TransactionAble } from '@nocobase/database'; +import Database, { Model, Transactionable } from '@nocobase/database'; export class UserModel extends Model { - async setDefaultRole(roleName: string, options: TransactionAble = {}) { + async setDefaultRole(roleName: string, options: Transactionable = {}) { if (roleName == 'anonymous') { return false; }