diff --git a/packages/core/database/src/fields/belongs-to-field.ts b/packages/core/database/src/fields/belongs-to-field.ts index eb210cfbaf..027d6d0832 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 e5ea07cc21..775701d7fd 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 faeb09a7fe..6a3dcd4499 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 a44fc0ac29..6d4a0a8791 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 7e344e8638..19abc4c054 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 ff80a1633e..bc59a72e2a 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; } }