fix: unbind on error throwing (#914)

This commit is contained in:
chenos 2022-10-15 09:59:27 +08:00
parent d45623ee2e
commit 3e22a47be6
6 changed files with 42 additions and 22 deletions

View File

@ -1,7 +1,7 @@
import { omit } from 'lodash'; import { omit } from 'lodash';
import { BelongsToOptions as SequelizeBelongsToOptions, Utils } from 'sequelize'; import { BelongsToOptions as SequelizeBelongsToOptions, Utils } from 'sequelize';
import { BaseRelationFieldOptions, RelationField } from './relation-field';
import { checkIdentifier } from '../utils'; import { checkIdentifier } from '../utils';
import { BaseRelationFieldOptions, RelationField } from './relation-field';
export class BelongsToField extends RelationField { export class BelongsToField extends RelationField {
static type = 'belongsTo'; static type = 'belongsTo';
@ -43,7 +43,12 @@ export class BelongsToField extends RelationField {
this.options.foreignKey = association.foreignKey; this.options.foreignKey = association.foreignKey;
} }
checkIdentifier(this.options.foreignKey); try {
checkIdentifier(this.options.foreignKey);
} catch (error) {
this.unbind();
throw error;
}
if (!this.options.sourceKey) { if (!this.options.sourceKey) {
// @ts-ignore // @ts-ignore

View File

@ -1,8 +1,8 @@
import { omit } from 'lodash'; import { omit } from 'lodash';
import { BelongsToManyOptions as SequelizeBelongsToManyOptions, Utils } from 'sequelize'; import { BelongsToManyOptions as SequelizeBelongsToManyOptions, Utils } from 'sequelize';
import { Collection } from '../collection'; import { Collection } from '../collection';
import { MultipleRelationFieldOptions, RelationField } from './relation-field';
import { checkIdentifier } from '../utils'; import { checkIdentifier } from '../utils';
import { MultipleRelationFieldOptions, RelationField } from './relation-field';
export class BelongsToManyField extends RelationField { export class BelongsToManyField extends RelationField {
get through() { get through() {
@ -53,8 +53,6 @@ export class BelongsToManyField extends RelationField {
this.options.foreignKey = association.foreignKey; this.options.foreignKey = association.foreignKey;
} }
checkIdentifier(this.options.foreignKey);
if (!this.options.sourceKey) { if (!this.options.sourceKey) {
this.options.sourceKey = association.sourceKey; this.options.sourceKey = association.sourceKey;
} }
@ -63,7 +61,13 @@ export class BelongsToManyField extends RelationField {
this.options.otherKey = association.otherKey; 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) { if (!this.options.through) {
this.options.through = this.through; this.options.through = this.through;

View File

@ -5,11 +5,11 @@ import {
ForeignKeyOptions, ForeignKeyOptions,
HasManyOptions, HasManyOptions,
HasManyOptions as SequelizeHasManyOptions, HasManyOptions as SequelizeHasManyOptions,
Utils, Utils
} from 'sequelize'; } from 'sequelize';
import { Collection } from '../collection'; import { Collection } from '../collection';
import { MultipleRelationFieldOptions, RelationField } from './relation-field';
import { checkIdentifier } from '../utils'; import { checkIdentifier } from '../utils';
import { MultipleRelationFieldOptions, RelationField } from './relation-field';
export interface HasManyFieldOptions extends HasManyOptions { export interface HasManyFieldOptions extends HasManyOptions {
/** /**
@ -110,7 +110,12 @@ export class HasManyField extends RelationField {
this.options.foreignKey = association.foreignKey; this.options.foreignKey = association.foreignKey;
} }
checkIdentifier(this.options.foreignKey); try {
checkIdentifier(this.options.foreignKey);
} catch (error) {
this.unbind();
throw error;
}
if (!this.options.sourceKey) { if (!this.options.sourceKey) {
// @ts-ignore // @ts-ignore

View File

@ -5,11 +5,11 @@ import {
ForeignKeyOptions, ForeignKeyOptions,
HasOneOptions, HasOneOptions,
HasOneOptions as SequelizeHasOneOptions, HasOneOptions as SequelizeHasOneOptions,
Utils, Utils
} from 'sequelize'; } from 'sequelize';
import { Collection } from '../collection'; import { Collection } from '../collection';
import { BaseRelationFieldOptions, RelationField } from './relation-field';
import { checkIdentifier } from '../utils'; import { checkIdentifier } from '../utils';
import { BaseRelationFieldOptions, RelationField } from './relation-field';
export interface HasOneFieldOptions extends HasOneOptions { export interface HasOneFieldOptions extends HasOneOptions {
/** /**
@ -108,7 +108,12 @@ export class HasOneField extends RelationField {
this.options.foreignKey = association.foreignKey; this.options.foreignKey = association.foreignKey;
} }
checkIdentifier(this.options.foreignKey); try {
checkIdentifier(this.options.foreignKey);
} catch (error) {
this.unbind();
throw error;
}
if (!this.options.sourceKey) { if (!this.options.sourceKey) {
// @ts-ignore // @ts-ignore

View File

@ -94,19 +94,18 @@ export class FieldModel extends MagicAttributeModel {
} }
async migrate({ isNew, ...options }: MigrateOptions = {}) { async migrate({ isNew, ...options }: MigrateOptions = {}) {
const field = await this.load({ let field;
transaction: options.transaction,
});
if (!field) {
return;
}
try { try {
field = await this.load({
transaction: options.transaction,
});
if (!field) {
return;
}
await migrate(field, options); await migrate(field, options);
} catch (error) { } catch (error) {
// field sync failed, delete from memory // field sync failed, delete from memory
if (isNew) { if (isNew && field) {
// update field should not remove field from memory // update field should not remove field from memory
field.remove(); field.remove();
} }

View File

@ -23,13 +23,15 @@ async function findUserByToken(ctx: Context) {
ctx.state.currentUserAppends.push(field.name); 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, appends: ctx.state.currentUserAppends,
filter: { filter: {
id: userId, id: userId,
}, },
}); });
return user;
} catch (error) { } catch (error) {
console.log(error);
return null; return null;
} }
} }