From 3e22a47be6e1d91c77e24b4dba96049b4d9854c8 Mon Sep 17 00:00:00 2001 From: chenos Date: Sat, 15 Oct 2022 09:59:27 +0800 Subject: [PATCH] fix: unbind on error throwing (#914) --- .../database/src/fields/belongs-to-field.ts | 9 +++++++-- .../src/fields/belongs-to-many-field.ts | 12 ++++++++---- .../core/database/src/fields/has-many-field.ts | 11 ++++++++--- .../core/database/src/fields/has-one-field.ts | 11 ++++++++--- .../collection-manager/src/models/field.ts | 17 ++++++++--------- .../plugins/users/src/middlewares/parseToken.ts | 4 +++- 6 files changed, 42 insertions(+), 22 deletions(-) diff --git a/packages/core/database/src/fields/belongs-to-field.ts b/packages/core/database/src/fields/belongs-to-field.ts index eb210cfba..027d6d083 100644 --- a/packages/core/database/src/fields/belongs-to-field.ts +++ b/packages/core/database/src/fields/belongs-to-field.ts @@ -1,7 +1,7 @@ import { omit } from 'lodash'; import { BelongsToOptions as SequelizeBelongsToOptions, Utils } from 'sequelize'; -import { BaseRelationFieldOptions, RelationField } from './relation-field'; import { checkIdentifier } from '../utils'; +import { BaseRelationFieldOptions, RelationField } from './relation-field'; export class BelongsToField extends RelationField { static type = 'belongsTo'; @@ -43,7 +43,12 @@ export class BelongsToField extends RelationField { this.options.foreignKey = association.foreignKey; } - checkIdentifier(this.options.foreignKey); + try { + checkIdentifier(this.options.foreignKey); + } catch (error) { + this.unbind(); + throw error; + } if (!this.options.sourceKey) { // @ts-ignore diff --git a/packages/core/database/src/fields/belongs-to-many-field.ts b/packages/core/database/src/fields/belongs-to-many-field.ts index e5ea07cc2..775701d7f 100644 --- a/packages/core/database/src/fields/belongs-to-many-field.ts +++ b/packages/core/database/src/fields/belongs-to-many-field.ts @@ -1,8 +1,8 @@ import { omit } from 'lodash'; import { BelongsToManyOptions as SequelizeBelongsToManyOptions, Utils } from 'sequelize'; import { Collection } from '../collection'; -import { MultipleRelationFieldOptions, RelationField } from './relation-field'; import { checkIdentifier } from '../utils'; +import { MultipleRelationFieldOptions, RelationField } from './relation-field'; export class BelongsToManyField extends RelationField { get through() { @@ -53,8 +53,6 @@ export class BelongsToManyField extends RelationField { this.options.foreignKey = association.foreignKey; } - checkIdentifier(this.options.foreignKey); - if (!this.options.sourceKey) { this.options.sourceKey = association.sourceKey; } @@ -63,7 +61,13 @@ export class BelongsToManyField extends RelationField { this.options.otherKey = association.otherKey; } - checkIdentifier(this.options.otherKey); + try { + checkIdentifier(this.options.foreignKey); + checkIdentifier(this.options.otherKey); + } catch (error) { + this.unbind(); + throw error; + } if (!this.options.through) { this.options.through = this.through; diff --git a/packages/core/database/src/fields/has-many-field.ts b/packages/core/database/src/fields/has-many-field.ts index faeb09a7f..6a3dcd449 100644 --- a/packages/core/database/src/fields/has-many-field.ts +++ b/packages/core/database/src/fields/has-many-field.ts @@ -5,11 +5,11 @@ import { ForeignKeyOptions, HasManyOptions, HasManyOptions as SequelizeHasManyOptions, - Utils, + Utils } from 'sequelize'; import { Collection } from '../collection'; -import { MultipleRelationFieldOptions, RelationField } from './relation-field'; import { checkIdentifier } from '../utils'; +import { MultipleRelationFieldOptions, RelationField } from './relation-field'; export interface HasManyFieldOptions extends HasManyOptions { /** @@ -110,7 +110,12 @@ export class HasManyField extends RelationField { this.options.foreignKey = association.foreignKey; } - checkIdentifier(this.options.foreignKey); + try { + checkIdentifier(this.options.foreignKey); + } catch (error) { + this.unbind(); + throw error; + } if (!this.options.sourceKey) { // @ts-ignore diff --git a/packages/core/database/src/fields/has-one-field.ts b/packages/core/database/src/fields/has-one-field.ts index a44fc0ac2..6d4a0a879 100644 --- a/packages/core/database/src/fields/has-one-field.ts +++ b/packages/core/database/src/fields/has-one-field.ts @@ -5,11 +5,11 @@ import { ForeignKeyOptions, HasOneOptions, HasOneOptions as SequelizeHasOneOptions, - Utils, + Utils } from 'sequelize'; import { Collection } from '../collection'; -import { BaseRelationFieldOptions, RelationField } from './relation-field'; import { checkIdentifier } from '../utils'; +import { BaseRelationFieldOptions, RelationField } from './relation-field'; export interface HasOneFieldOptions extends HasOneOptions { /** @@ -108,7 +108,12 @@ export class HasOneField extends RelationField { this.options.foreignKey = association.foreignKey; } - checkIdentifier(this.options.foreignKey); + try { + checkIdentifier(this.options.foreignKey); + } catch (error) { + this.unbind(); + throw error; + } if (!this.options.sourceKey) { // @ts-ignore diff --git a/packages/plugins/collection-manager/src/models/field.ts b/packages/plugins/collection-manager/src/models/field.ts index 7e344e863..19abc4c05 100644 --- a/packages/plugins/collection-manager/src/models/field.ts +++ b/packages/plugins/collection-manager/src/models/field.ts @@ -94,19 +94,18 @@ export class FieldModel extends MagicAttributeModel { } async migrate({ isNew, ...options }: MigrateOptions = {}) { - const field = await this.load({ - transaction: options.transaction, - }); - - if (!field) { - return; - } - + let field; try { + field = await this.load({ + transaction: options.transaction, + }); + if (!field) { + return; + } await migrate(field, options); } catch (error) { // field sync failed, delete from memory - if (isNew) { + if (isNew && field) { // update field should not remove field from memory field.remove(); } diff --git a/packages/plugins/users/src/middlewares/parseToken.ts b/packages/plugins/users/src/middlewares/parseToken.ts index ff80a1633..bc59a72e2 100644 --- a/packages/plugins/users/src/middlewares/parseToken.ts +++ b/packages/plugins/users/src/middlewares/parseToken.ts @@ -23,13 +23,15 @@ async function findUserByToken(ctx: Context) { ctx.state.currentUserAppends.push(field.name); } } - return await ctx.db.getRepository('users').findOne({ + const user = await ctx.db.getRepository('users').findOne({ appends: ctx.state.currentUserAppends, filter: { id: userId, }, }); + return user; } catch (error) { + console.log(error); return null; } }