@directus/api
Version:
Directus is a real-time API and App dashboard for managing SQL database content
114 lines (113 loc) • 3.38 kB
JavaScript
//#region src/utils/get-local-type.ts
const localTypeMap = {
boolean: "boolean",
tinyint: "integer",
smallint: "integer",
mediumint: "integer",
int: "integer",
integer: "integer",
serial: "integer",
bigint: "bigInteger",
bigserial: "bigInteger",
clob: "text",
tinytext: "text",
mediumtext: "text",
longtext: "text",
text: "text",
varchar: "string",
longvarchar: "string",
varchar2: "string",
nvarchar: "string",
image: "binary",
ntext: "text",
char: "string",
date: "date",
datetime: "dateTime",
dateTime: "dateTime",
timestamp: "timestamp",
time: "time",
float: "float",
double: "float",
"double precision": "float",
real: "float",
decimal: "decimal",
numeric: "integer",
geometry: "geometry",
point: "geometry.Point",
linestring: "geometry.LineString",
polygon: "geometry.Polygon",
multipoint: "geometry.MultiPoint",
multilinestring: "geometry.MultiLineString",
multipolygon: "geometry.MultiPolygon",
string: "text",
year: "integer",
blob: "binary",
mediumblob: "binary",
"int unsigned": "integer",
"tinyint unsigned": "integer",
"smallint unsigned": "integer",
"mediumint unsigned": "integer",
"bigint unsigned": "integer",
bit: "boolean",
smallmoney: "float",
money: "float",
datetimeoffset: "timestamp",
datetime2: "dateTime",
smalldatetime: "dateTime",
nchar: "text",
binary: "binary",
varbinary: "binary",
uniqueidentifier: "uuid",
json: "json",
jsonb: "json",
uuid: "uuid",
int2: "integer",
serial4: "integer",
int4: "integer",
serial8: "bigInteger",
int8: "bigInteger",
bool: "boolean",
"character varying": "string",
character: "string",
interval: "string",
_varchar: "string",
bpchar: "string",
timestamptz: "timestamp",
"timestamp with time zone": "timestamp",
"timestamp with local time zone": "timestamp",
"timestamp without time zone": "dateTime",
"timestamp without local time zone": "dateTime",
timetz: "time",
"time with time zone": "time",
"time without time zone": "time",
float4: "float",
float8: "float",
citext: "text",
number: "integer",
sdo_geometry: "geometry",
integerfirst: "integer"
};
function getLocalType(column, field) {
if (!column) return "alias";
const dataType = column.data_type.toLowerCase();
const type = localTypeMap[dataType.split("(")[0]];
const special = field?.special;
if (special) {
if (special.includes("cast-json")) return "json";
if (special.includes("hash")) return "hash";
if (special.includes("cast-csv")) return "csv";
if (special.includes("uuid") || special.includes("file")) return "uuid";
if (special.includes("cast-timestamp")) return "timestamp";
if (special.includes("cast-datetime")) return "dateTime";
if (type?.startsWith("geometry")) return special[0] || "geometry";
}
/** Handle Postgres numeric decimals */
if (dataType === "numeric" && column.numeric_precision !== null && column.numeric_scale !== null) return "decimal";
/** Handle MS SQL varchar(MAX) and nvarchar(MAX) types */
if ((column.data_type === "nvarchar" || column.data_type === "varchar") && (column.max_length === -1 || column.max_length === null)) return "text";
/** Handle CockroachDB 64-bit integers (reported as 'integer' with precision 64) */
if ((dataType === "integer" || dataType === "int") && column.numeric_precision === 64) return "bigInteger";
return type ?? "unknown";
}
//#endregion
export { getLocalType as default };