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 { 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;
}
try {
checkIdentifier(this.options.foreignKey);
} catch (error) {
this.unbind();
throw error;
}
if (!this.options.sourceKey) {
// @ts-ignore

View File

@ -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;
}
try {
checkIdentifier(this.options.foreignKey);
checkIdentifier(this.options.otherKey);
} catch (error) {
this.unbind();
throw error;
}
if (!this.options.through) {
this.options.through = this.through;

View File

@ -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;
}
try {
checkIdentifier(this.options.foreignKey);
} catch (error) {
this.unbind();
throw error;
}
if (!this.options.sourceKey) {
// @ts-ignore

View File

@ -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;
}
try {
checkIdentifier(this.options.foreignKey);
} catch (error) {
this.unbind();
throw error;
}
if (!this.options.sourceKey) {
// @ts-ignore

View File

@ -94,19 +94,18 @@ export class FieldModel extends MagicAttributeModel {
}
async migrate({ isNew, ...options }: MigrateOptions = {}) {
const field = await this.load({
let field;
try {
field = await this.load({
transaction: options.transaction,
});
if (!field) {
return;
}
try {
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();
}

View File

@ -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;
}
}