From b81b83d3f3ffdd5657f7d47862132e948d637455 Mon Sep 17 00:00:00 2001 From: chenos Date: Fri, 8 Jan 2021 15:49:37 +0800 Subject: [PATCH] fix: do not change the field type --- .../database/src/__tests__/associations.test.ts | 4 ++-- .../database/src/__tests__/fields/types.test.ts | 3 ++- packages/database/src/fields/field-types.ts | 17 +++++++++++------ packages/database/src/fields/index.ts | 4 ++-- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/packages/database/src/__tests__/associations.test.ts b/packages/database/src/__tests__/associations.test.ts index f1cc0a5c7..86867cdf9 100644 --- a/packages/database/src/__tests__/associations.test.ts +++ b/packages/database/src/__tests__/associations.test.ts @@ -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' diff --git a/packages/database/src/__tests__/fields/types.test.ts b/packages/database/src/__tests__/fields/types.test.ts index 132af8bac..32f4ece07 100644 --- a/packages/database/src/__tests__/fields/types.test.ts +++ b/packages/database/src/__tests__/fields/types.test.ts @@ -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); } diff --git a/packages/database/src/fields/field-types.ts b/packages/database/src/fields/field-types.ts index a452290c4..1b90efd65 100644 --- a/packages/database/src/fields/field-types.ts +++ b/packages/database/src/fields/field-types.ts @@ -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[(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)); }, } } diff --git a/packages/database/src/fields/index.ts b/packages/database/src/fields/index.ts index 1de6dd58e..6b3f19e7e 100644 --- a/packages/database/src/fields/index.ts +++ b/packages/database/src/fields/index.ts @@ -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); }