@n0safe/indirectus
Version:
Directus Tools CLI.
285 lines • 12.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.has_system_collections = has_system_collections;
exports.has_non_system_collections = has_non_system_collections;
exports.filter_untype_system_collections = filter_untype_system_collections;
exports.to_system_method_suffix = to_system_method_suffix;
exports.to_collection_text = to_collection_text;
exports.to_collection_string = to_collection_string;
exports.to_collection_name = to_collection_name;
exports.to_ts_type = to_ts_type;
exports.only_system_fields = only_system_fields;
exports.only_custom_fields = only_custom_fields;
exports.only_with_custom_fields = only_with_custom_fields;
const json_1 = require("./json");
const quote_1 = require("./quote");
const ts_pattern_1 = require("ts-pattern");
const drop_first_1 = require("./drop_first");
const regex_replace_1 = require("./regex_replace");
const split_1 = require("./split");
const string_cases_1 = require("./string_cases");
const relationships_1 = require("../../../types/relationships");
function has_system_collections(context, collections) {
return collections.some((collection) => collection.is_system);
}
function has_non_system_collections(context, collections) {
return collections.some((collection) => !collection.is_system);
}
function filter_untype_system_collections(context, collections) {
return collections.filter((collection) => !['directus_versions'].includes(collection.name.raw));
}
function to_system_method_suffix(context, collection) {
if (!collection.is_system) {
throw new Error("Collection: " + collection.name.raw + " is not a system collection");
}
return to_collection_name(context, collection.name.raw, true).replace("Directus", "");
}
function to_collection_text(context, value, prefix = "", suffix = "") {
return `${prefix}${(0, string_cases_1.lower_case)(context, (0, string_cases_1.space_case)(context, value))}${suffix}`;
}
function to_collection_string(context, value) {
return (0, quote_1.quoted)(context, value);
}
function to_collection_name(context, value, partial = false) {
value = `${value}`;
const system = context.registry.collections.find((c) => c.name.raw == value)?.is_system;
let name = (0, string_cases_1.pascal_case)(context, value);
if (system) {
if (partial) {
name = (0, regex_replace_1.regex_replace)(context, (0, string_cases_1.pascal_case)(context, (0, drop_first_1.drop_first)(context, (0, split_1.split)(context, value, "_"))), "s$", "");
name = name == "Setting" ? "Settings" : name;
name = name == "Acces" ? "Access" : name;
name = name == "Policie" ? "Policy" : name;
}
else {
name = (0, regex_replace_1.regex_replace)(context, (0, string_cases_1.pascal_case)(context, value), "s$", "");
name = name == "DirectusSetting" ? "DirectusSettings" : name;
name = name == "DirectusPolicie" ? "DirectusPolicy" : name;
}
}
return name;
}
function to_ts_type(context, field) {
if (!field.type.is_data) {
return "never";
}
const types = [];
const schema = field.type;
const meta = field.type.raw?.meta;
let nullable = false;
const db_type = (0, ts_pattern_1.match)(field?.type?.database?.split("(", 2)[0].toLowerCase().trim())
.returnType()
.with("uuid", () => "Types.UUID")
.with("json", () => "Types.JSON")
.with("text", () => "Types.String")
.with("integer", () => "Types.Integer")
.with("decimal", () => "Types.Decimal")
.with("numeric", () => "Types.Number")
.with("bigint", () => "Types.BigInteger")
.with("boolean", () => "Types.Boolean")
.with("character varying", () => "Types.String")
.with("date", () => "Types.Date")
.with("time", () => "Types.DateTime")
.with("time with time zone", () => "Types.DateTime")
.with("time without time zone", () => "Types.DateTime")
.with("timestamp", () => "Types.DateTime")
.with("timestamp with time zone", () => "Types.DateTime")
.with("timestamp without time zone", () => "Types.DateTime")
// Shared
.with("boolean", () => "Types.Boolean")
.with("tinyint", () => "Types.Integer")
.with("smallint", () => "Types.Integer")
.with("mediumint", () => "Types.Integer")
.with("int", () => "Types.Integer")
.with("integer", () => "Types.Integer")
.with("serial", () => "Types.Integer")
.with("bigint", () => "Types.BigInteger")
.with("bigserial", () => "Types.BigInteger")
.with("clob", () => "Types.Text")
.with("tinytext", () => "Types.Text")
.with("mediumtext", () => "Types.Text")
.with("longtext", () => "Types.Text")
.with("text", () => "Types.Text")
.with("varchar", () => "Types.String")
.with("longvarchar", () => "Types.String")
.with("varchar2", () => "Types.String")
.with("nvarchar", () => "Types.String")
.with("image", () => "Types.Binary")
.with("ntext", () => "Types.Text")
.with("char", () => "Types.String")
.with("date", () => "Types.Date")
.with("datetime", () => "Types.DateTime")
.with("dateTime", () => "Types.DateTime")
.with("timestamp", () => "Types.DateTime")
.with("time", () => "Types.DateTime")
.with("float", () => "Types.Float")
.with("double", () => "Types.Float")
.with("double precision", () => "Types.Float")
.with("real", () => "Types.Float")
.with("decimal", () => "Types.Decimal")
.with("numeric", () => "Types.Integer")
// Geometries
.with("geometry", () => "Types.Geometry.Geometry")
.with("point", () => "Types.Geometry.Point")
.with("linestring", () => "Types.Geometry.LineString")
.with("polygon", () => "Types.Geometry.Polygon")
.with("multipoint", () => "Types.Geometry.MultiPoint")
.with("multilinestring", () => "Types.Geometry.MultiLineString")
.with("multipolygon", () => "Types.Geometry.MultiPolygon")
// MySQL
.with("string", () => "Types.Text")
.with("year", () => "Types.Integer")
.with("blob", () => "Types.Binary")
.with("mediumblob", () => "Types.Binary")
.with("int unsigned", () => "Types.Integer")
.with("tinyint unsigned", () => "Types.Integer")
.with("smallint unsigned", () => "Types.Integer")
.with("mediumint unsigned", () => "Types.Integer")
.with("bigint unsigned", () => "Types.Integer")
// MS SQL
.with("bit", () => "Types.Boolean")
.with("smallmoney", () => "Types.Float")
.with("money", () => "Types.Float")
.with("datetimeoffset", () => "Types.DateTime")
.with("datetime2", () => "Types.DateTime")
.with("smalldatetime", () => "Types.DateTime")
.with("nchar", () => "Types.Text")
.with("binary", () => "Types.Binary")
.with("varbinary", () => "Types.Binary")
.with("uniqueidentifier", () => "Types.UUID")
// Postgres
.with("json", () => "Types.JSON")
.with("jsonb", () => "Types.JSON")
.with("uuid", () => "Types.UUID")
.with("int2", () => "Types.Integer")
.with("serial4", () => "Types.Integer")
.with("int4", () => "Types.Integer")
.with("serial8", () => "Types.Integer")
.with("int8", () => "Types.Integer")
.with("bool", () => "Types.Boolean")
.with("character varying", () => "Types.String")
.with("character", () => "Types.String")
.with("interval", () => "Types.String")
.with("_varchar", () => "Types.String")
.with("bpchar", () => "Types.String")
.with("timestamptz", () => "Types.DateTime")
.with("timestamp with time zone", () => "Types.DateTime")
.with("timestamp with local time zone", () => "Types.DateTime")
.with("timestamp without time zone", () => "Types.Date")
.with("timestamp without local time zone", () => "Types.Date")
.with("timetz", () => "Types.DateTime")
.with("time with time zone", () => "Types.DateTime")
.with("time without time zone", () => "Types.DateTime")
.with("float4", () => "Types.Float")
.with("float8", () => "Types.Float")
.with("citext", () => "Types.Text")
.with("enum", () => "Types.Enum")
// Oracle
.with("number", () => "Types.Integer")
.with("sdo_geometry", () => "Types.Geometry.Geometry")
// SQLite
.with("integerfirst", () => "Types.Integer")
.otherwise(() => false);
if (db_type) {
types.push(db_type);
}
let json_type = false;
if (field.type.is_json) {
if ("json_schema" in schema) {
json_type = "Types.JSONSchema";
}
else {
json_type = "Types.JSON";
}
}
switch (meta?.interface) {
case "tags":
types.unshift("Types.String[]");
break;
case "select-dropdown":
const values = (meta?.options?.choices ?? []).map((v) => (0, quote_1.quote)(context, v.value));
for (const value of values) {
if (value == null) {
nullable = true;
}
else {
types.unshift(value);
}
}
json_type = false;
if (!meta?.options?.allowOther) {
const filteredTypes = types.filter((type) => type !== "Types.String");
types.length = 0;
types.push(...filteredTypes);
}
break;
}
if (schema.raw?.schema?.is_nullable) {
// types.push('null')
nullable = true;
}
if (json_type != false) {
types.unshift(json_type);
}
if (field.type.is_relationship) {
//if (
// field.type.is_special("user-created") ||
// field.type.is_special("user-updated")
//) {
// types.push("Collections.DirectusUser");
//} else if (field.type.is_special("file")) {
// types.push("Collections.DirectusFile");
//} else if (field.type.is_special("files")) {
// types.push("Collections.DirectusFile[]");
//} else
if (field.is_translations) {
types.push(`${to_collection_name(context, field.translations_collection)}[]`);
}
else {
const suffix = (((0, relationships_1.isManyToOne)(field.type.relationship) ||
(0, relationships_1.isOneToMany)(field.type.relationship)) &&
field.type.relationship.many) ??
field.type.is_special("m2m")
? "[]"
: "";
if (field.type.relationship?.type == "o2m") {
types.push(`Collections.${to_collection_name(context, field.type.relationship.ref.collection)}${suffix}`);
}
if (field.type.relationship?.type == "m2o") {
types.push(`Collections.${to_collection_name(context, field.type.relationship.ref.collection)}${suffix}`);
}
if (field.type.relationship?.type == "a2o") {
field.type.relationship.refs.forEach((ref) => {
types.push(`Collections.${to_collection_name(context, ref.collection)}${suffix}`);
});
}
if (field.type.relationship?.type == "unmapped") {
types.push("any");
}
}
}
if (types.length <= 0) {
const schemaStr = (0, json_1.json)(context, schema);
const metaStr = (0, json_1.json)(context, meta);
const unknown = `Types.UnknownType<{ schema: ${schemaStr}, meta: ${metaStr} }>`;
types.unshift(unknown);
}
let output = types.join(" | ");
if (nullable) {
output = `Types.Optional<${output}>`;
}
if (field.type.raw?.schema?.is_primary_key ?? false) {
output = `Types.PrimaryKey<${output}>`;
}
return output;
}
function only_system_fields(context, fields) {
return fields.filter((field) => field.is_system);
}
function only_custom_fields(context, fields) {
return fields.filter((field) => !field.is_system);
}
function only_with_custom_fields(context, collections) {
return collections.filter((field) => field.fields.filter((field) => !field.is_system).length > 0);
}
//# sourceMappingURL=directus.js.map