From ca8d6347c6706108e05122f5d6799fafc22815ef Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Sun, 18 Feb 2024 17:14:28 +0800 Subject: [PATCH] chore: field type map (#3516) * chore: field type map * chore: type map --- .../core/database/src/view/field-type-map.ts | 30 +++++++++++++++---- .../core/database/src/view/view-inference.ts | 12 +++++++- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/packages/core/database/src/view/field-type-map.ts b/packages/core/database/src/view/field-type-map.ts index 62e9dc352..44270f1d5 100644 --- a/packages/core/database/src/view/field-type-map.ts +++ b/packages/core/database/src/view/field-type-map.ts @@ -1,6 +1,7 @@ const postgres = { 'character varying': 'string', varchar: 'string', + character: 'string', text: 'text', char: 'string', oid: 'string', @@ -9,36 +10,53 @@ const postgres = { smallint: 'integer', integer: 'integer', bigint: 'bigInt', - decimal: 'float', + decimal: 'decimal', numeric: 'float', + real: 'float', 'double precision': 'float', 'timestamp without time zone': 'date', 'timestamp with time zone': 'date', + 'time without time zone': 'time', + date: 'date', boolean: 'boolean', json: ['json', 'array'], jsonb: ['json', 'array', 'jsonb'], - point: 'point', - path: 'lineString', - polygon: 'polygon', - circle: 'circle', + point: 'json', + path: 'json', + polygon: 'json', + circle: 'json', + uuid: 'string', }; const mysql = { + smallint: ['integer', 'boolean'], + tinyint: ['integer', 'boolean'], + mediumint: ['integer', 'boolean'], + + 'smallint unsigned': ['integer', 'boolean'], + 'tinyint unsigned': ['integer', 'boolean'], + 'mediumint unsigned': ['integer', 'boolean'], + + char: 'string', + date: 'date', + time: 'time', varchar: 'string', text: 'text', longtext: 'text', int: 'integer', + 'int unsigned': 'integer', integer: 'integer', bigint: 'bigInt', + 'bigint unsigned': 'bigInt', float: 'float', double: 'float', boolean: 'boolean', + decimal: 'decimal', - tinyint: 'integer', datetime: 'date', timestamp: 'date', json: ['json', 'array'], diff --git a/packages/core/database/src/view/view-inference.ts b/packages/core/database/src/view/view-inference.ts index 73f1b94b0..596697b2c 100644 --- a/packages/core/database/src/view/view-inference.ts +++ b/packages/core/database/src/view/view-inference.ts @@ -13,6 +13,16 @@ type InferredFieldResult = { }; export class ViewFieldInference { + static extractTypeFromDefinition(typeDefinition) { + const leftParenIndex = typeDefinition.indexOf('('); + + if (leftParenIndex === -1) { + return typeDefinition.toLowerCase(); + } + + return typeDefinition.substring(0, leftParenIndex).toLowerCase().trim(); + } + static async inferFields(options: { db: Database; viewName: string; @@ -128,7 +138,7 @@ export class ViewFieldInference { }; } - const queryType = options.type.toLowerCase().replace(/\(\d+\)/, ''); + const queryType = this.extractTypeFromDefinition(options.type); const mappedType = fieldTypeMap[queryType]; if (isArray(mappedType)) {