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

View File

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

View File

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