chore: field type map (#3516)

* chore: field type map

* chore: type map
This commit is contained in:
ChengLei Shao 2024-02-18 17:14:28 +08:00 committed by GitHub
parent c0b8fa27ec
commit ca8d6347c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 7 deletions

View File

@ -1,6 +1,7 @@
const postgres = { const postgres = {
'character varying': 'string', 'character varying': 'string',
varchar: 'string', varchar: 'string',
character: 'string',
text: 'text', text: 'text',
char: 'string', char: 'string',
oid: 'string', oid: 'string',
@ -9,36 +10,53 @@ const postgres = {
smallint: 'integer', smallint: 'integer',
integer: 'integer', integer: 'integer',
bigint: 'bigInt', bigint: 'bigInt',
decimal: 'float', decimal: 'decimal',
numeric: 'float', numeric: 'float',
real: 'float',
'double precision': 'float', 'double precision': 'float',
'timestamp without time zone': 'date', 'timestamp without time zone': 'date',
'timestamp with time zone': 'date', 'timestamp with time zone': 'date',
'time without time zone': 'time',
date: 'date', date: 'date',
boolean: 'boolean', boolean: 'boolean',
json: ['json', 'array'], json: ['json', 'array'],
jsonb: ['json', 'array', 'jsonb'], jsonb: ['json', 'array', 'jsonb'],
point: 'point', point: 'json',
path: 'lineString', path: 'json',
polygon: 'polygon', polygon: 'json',
circle: 'circle', circle: 'json',
uuid: 'string',
}; };
const mysql = { 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', varchar: 'string',
text: 'text', text: 'text',
longtext: 'text', longtext: 'text',
int: 'integer', int: 'integer',
'int unsigned': 'integer',
integer: 'integer', integer: 'integer',
bigint: 'bigInt', bigint: 'bigInt',
'bigint unsigned': 'bigInt',
float: 'float', float: 'float',
double: 'float', double: 'float',
boolean: 'boolean', boolean: 'boolean',
decimal: 'decimal',
tinyint: 'integer',
datetime: 'date', datetime: 'date',
timestamp: 'date', timestamp: 'date',
json: ['json', 'array'], json: ['json', 'array'],

View File

@ -13,6 +13,16 @@ type InferredFieldResult = {
}; };
export class ViewFieldInference { 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: { static async inferFields(options: {
db: Database; db: Database;
viewName: string; 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]; const mappedType = fieldTypeMap[queryType];
if (isArray(mappedType)) { if (isArray(mappedType)) {