UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

1 lines 4.24 kB
{"version":3,"file":"tree-table.mjs","names":[],"sources":["../../../../../src/libs/collection/custom-fields/storage/tree-table.ts"],"sourcesContent":["import type { FieldDatabaseConfig } from \"../types.js\";\nimport type { TreeTableModeDefinition } from \"./types.js\";\n\nexport const treeTableSchemaColumns = {\n\trootId: \"brick_id\",\n\tparentId: \"parent_id\",\n\tparentIdRef: \"parent_id_ref\",\n} as const;\n\nconst TREE_TABLE_BASE_PRIORITY = 500;\nconst TREE_TABLE_DEPTH_PRIORITY = 10;\n\nexport const treeTableMode: TreeTableModeDefinition = {\n\tmode: \"tree-table\",\n\tbaseTablePriority: TREE_TABLE_BASE_PRIORITY,\n\tisDatabaseConfig: (\n\t\tconfig,\n\t): config is Extract<\n\t\tFieldDatabaseConfig,\n\t\t{\n\t\t\tmode: \"tree-table\";\n\t\t}\n\t> => config.mode === \"tree-table\",\n\t/**\n\t * Builds the core columns required for tree-table storage.\n\t */\n\tgetSchemaDefinition: (props) => {\n\t\tconst hasParent = props.table.depth > 1;\n\n\t\treturn {\n\t\t\tcolumns: [\n\t\t\t\t{\n\t\t\t\t\tname: treeTableSchemaColumns.rootId,\n\t\t\t\t\ttype: props.db.getDataType(\"integer\"),\n\t\t\t\t\tnullable: false,\n\t\t\t\t\tforeignKey: {\n\t\t\t\t\t\ttable: props.table.root,\n\t\t\t\t\t\tcolumn: \"id\",\n\t\t\t\t\t\tonDelete: \"cascade\",\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tname: treeTableSchemaColumns.parentId,\n\t\t\t\t\ttype: props.db.getDataType(\"integer\"),\n\t\t\t\t\tnullable: true,\n\t\t\t\t\tforeignKey:\n\t\t\t\t\t\thasParent && props.table.parent !== props.table.root\n\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\ttable: props.table.parent,\n\t\t\t\t\t\t\t\t\tcolumn: \"id\",\n\t\t\t\t\t\t\t\t\tonDelete: \"cascade\",\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t: undefined,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tname: treeTableSchemaColumns.parentIdRef,\n\t\t\t\t\ttype: props.db.getDataType(\"integer\"),\n\t\t\t\t\tnullable: true,\n\t\t\t\t},\n\t\t\t],\n\t\t};\n\t},\n\t/**\n\t * Reads nested child fields from a tree-table field config.\n\t */\n\tgetChildFieldConfigs: (field) => {\n\t\tif (!(\"fields\" in field)) return null;\n\t\tif (!Array.isArray(field.fields)) return null;\n\n\t\treturn field.fields;\n\t},\n\t/**\n\t * Returns insertion priority based on tree depth in the current field path.\n\t */\n\tgetInsertPriority: (fieldPath) => fieldPath?.length ?? 0,\n\t/**\n\t * Returns migration priority offset for a given tree depth.\n\t */\n\tgetPriorityOffsetForDepth: (depth) => depth * TREE_TABLE_DEPTH_PRIORITY,\n\tclientTypeGen: (props) => {\n\t\tconst childFields = treeTableMode.getChildFieldConfigs(props.field) ?? [];\n\t\tconst renderedChildFields = props.helpers.renderFieldMap(childFields, {\n\t\t\tbuilder: props.builder,\n\t\t\tcollectionUsesTranslations: props.collectionUsesTranslations,\n\t\t\twithinGroup: true,\n\t\t});\n\n\t\treturn {\n\t\t\tfieldType:\n\t\t\t\tprops.fieldType ??\n\t\t\t\tprops.helpers.renderBaseFieldType({\n\t\t\t\t\tfield: props.field,\n\t\t\t\t\tmode: \"groups\",\n\t\t\t\t\tgroupFieldsType: renderedChildFields.typeText,\n\t\t\t\t\thasGroupRef: props.hasGroupRef,\n\t\t\t\t}),\n\t\t\tdeclarations: [\n\t\t\t\t...(props.declarations ?? []),\n\t\t\t\t...renderedChildFields.declarations,\n\t\t\t],\n\t\t};\n\t},\n};\n"],"mappings":"AAGA,MAAa,EAAyB,CACrC,OAAQ,WACR,SAAU,YACV,YAAa,eACd,EAKa,EAAyC,CACrD,KAAM,aACN,kBAAmB,IACnB,iBACC,GAMI,EA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