UNPKG

@n0safe/indirectus

Version:
190 lines 6.17 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Registry = exports.Collection = exports.Field = exports.Identifier = exports.Type = void 0; exports.createRegistry = createRegistry; const cc = __importStar(require("@wolfpkgs/core/strings")); const contains_1 = require("../default/extensions/filters/contains"); const relationships_1 = require("./relationships"); class Type { raw; raw_relation; constructor(raw, raw_relation) { this.raw = raw; this.raw_relation = raw_relation; } get directus() { return this.raw?.type ?? "unknown"; } get database() { return this.raw?.schema?.data_type ?? "unknown"; } get is_json() { return this.database == "json" || this.is_special("cast-json"); } is_special(flag) { return (0, contains_1.contains)(null, this.raw?.meta?.special ?? [], flag); } get is_data() { return !this.is_special("no-data"); } get is_relationship() { return this.raw_relation !== null && this.raw_relation !== undefined; } get is_unmapped() { return (this.raw_relation !== null && this.raw_relation !== undefined && (0, relationships_1.isUnmapped)(this.raw_relation)); } get relationship() { if (!this.raw_relation) { return undefined; } return this.raw_relation; } get is_system() { return "system" in (this.raw?.meta ?? {}) && !!this.raw?.meta?.system; } } exports.Type = Type; class Identifier { raw; constructor(raw) { this.raw = raw; } get pascal() { return cc.toPascal(this.raw); } get camel() { return cc.toCamel(this.raw); } toString() { return this.raw; } } exports.Identifier = Identifier; class Field { type; name; constructor(name, type) { this.type = type; this.name = new Identifier(name); } get is_system() { return this.type.is_system; } get is_translations() { return (0, contains_1.contains)(null, this.type.raw?.meta?.special ?? [], "translations"); } get is_data() { return !(0, contains_1.contains)(null, this.type.raw?.meta?.special ?? [], "no-data"); } get is_alias() { return (0, contains_1.contains)(null, this.type.raw?.meta?.special ?? [], "alias"); } get translations_collection() { return this.type.relationship?.ref?.collection; } } exports.Field = Field; class Collection { raw; name; fields = []; constructor(raw) { this.raw = raw; this.name = new Identifier(raw.collection); } get is_system() { return this.raw.meta?.system ?? false; } get is_singleton() { return this.raw.meta?.singleton ?? false; } } exports.Collection = Collection; class Registry { schema; collections = []; constructor(schema) { this.schema = schema; const data = {}; for (const $ of schema.collections) { const collectionInstance = new Collection($); this.collections.push(collectionInstance); data[$.collection] = { collection: collectionInstance, fields: schema.fields.filter((field) => field.collection == $.collection), relations: schema.relations.filter((field) => field.collection == $.collection), }; } for (const $ of Object.values(data)) { for (const field of $.fields) { const newField = new Field(field.field, new Type(field, (0, relationships_1.getRelationship)(this.schema.fields, this.schema.relations, { collection: $.collection.name.raw, field: field.field, }))); $.collection.fields.push(newField); } // Sort by system then sort index $.collection.fields.sort((a, b) => { if (a.is_system && !b.is_system) { return -1; } else if (!a.is_system && b.is_system) { return 1; } return (a.type.raw?.meta?.sort ?? 0) - (b.type.raw?.meta?.sort ?? 0); }); for (const relation of $.relations) { relation.collection = $.collection.name.raw; } } } collection(name) { return this.collections.find((collection) => collection.name.raw == name); } toJSON() { return this.schema; } static fromJSON(json) { return new Registry(json); } } exports.Registry = Registry; function createRegistry(schema) { return new Registry(schema); } //# sourceMappingURL=registry.js.map