fix: do not change the field type

This commit is contained in:
chenos 2021-01-08 15:49:37 +08:00
parent 772d83cf78
commit b81b83d3f3
4 changed files with 17 additions and 11 deletions

View File

@ -623,7 +623,7 @@ describe('associations', () => {
through: 'posts_tags',
sourceKey: 'slug',
foreignKey: 'post_slug',
type: 'BELONGSTOMANY',
type: 'belongsToMany',
name: 'tags',
targetKey: 'name',
otherKey: 'tag_name'
@ -634,7 +634,7 @@ describe('associations', () => {
through: 'posts_tags',
sourceKey: 'name',
foreignKey: 'tag_name',
type: 'BELONGSTOMANY',
type: 'belongsToMany',
name: 'posts',
targetKey: 'slug',
otherKey: 'post_slug'

View File

@ -1,5 +1,6 @@
import {
buildField,
getDataTypeKey,
Column,
BOOLEAN as Boolean,
INTEGER as Integer,
@ -45,7 +46,7 @@ describe('field types', () => {
if (actual instanceof ABSTRACT) {
expect(type).toBeInstanceOf(field.getDataType());
// postgres 的 text 不限制长度,无需参数
if (db.sequelize.getDialect() !== 'postgres' || field.getType() !== 'TEXT') {
if (db.sequelize.getDialect() !== 'postgres' || getDataTypeKey(type) !== 'TEXT') {
// 非严谨比较undefined == null
expect(type).toEqual(actual);
}

View File

@ -41,7 +41,10 @@ export class Field implements IField {
constructor(options: any, context: FieldContext) {
const { type } = options;
this.options = {...options, type: getDataTypeKey(type)};
this.options = {
...options,
// type: getDataTypeKey(type)
};
this.context = context;
}
@ -62,9 +65,9 @@ export class Column extends Field {
public getDataType() {
const { type } = this.options;
if (DataTypes[type]) {
return DataTypes[type];
const dataType = getDataTypeKey(type);
if (DataTypes[dataType]) {
return DataTypes[dataType];
}
return DataTypes[(<typeof Column>this.constructor).name.toUpperCase()];
}
@ -96,6 +99,8 @@ export class INTEGER extends NUMBER {
public getDataType(): Function {
const { type } = this.options;
const dataType = getDataTypeKey(type);
return {
INT: DataTypes.INTEGER,
INTEGER: DataTypes.INTEGER,
@ -107,7 +112,7 @@ export class INTEGER extends NUMBER {
MEDIUMINTEGER: DataTypes.MEDIUMINT,
BIGINT: DataTypes.BIGINT,
BIGINTEGER: DataTypes.BIGINT,
}[type as string] || DataTypes.INTEGER;
}[dataType] || DataTypes.INTEGER;
}
public getAttributeOptions() {
@ -302,7 +307,7 @@ export class PASSWORD extends STRING {
...restOptions,
type: this.getDataTypeInstance({ length, binary }),
set(this: Model, value: string) {
this.setDataValue(name, bcrypt.hashSync(value, 10));
value && this.setDataValue(name, bcrypt.hashSync(value, 10));
},
}
}

View File

@ -99,8 +99,8 @@ export function buildField(options: FieldOptions, context: Fields.FieldContext)
if (type instanceof ABSTRACT) {
options = {...type.options, ...options};
}
type = getDataTypeKey(type);
const Field = getField(type);
const dataType = getDataTypeKey(type);
const Field = getField(dataType);
return new Field({type, ...options}, context);
}