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 = {
'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'],

View File

@ -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)) {