UNPKG

@lucidcms/core

Version:

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

1 lines 3.89 kB
{"version":3,"file":"index.mjs","names":["constants"],"sources":["../../../../../src/libs/collection/custom-fields/storage/index.ts"],"sourcesContent":["import constants from \"../../../../constants/constants.js\";\nimport type { CustomFieldTableType, TableType } from \"../../schema/types.js\";\nimport registeredFields, {\n\tregisteredFieldTypes,\n} from \"../registered-fields.js\";\nimport type { FieldDatabaseConfig, FieldDatabaseMode } from \"../types.js\";\nimport { columnMode } from \"./column.js\";\nimport { ignoreMode } from \"./ignore.js\";\nimport { relationTableMode } from \"./relation-table.js\";\nimport { treeTableMode } from \"./tree-table.js\";\nimport type { TableBackedFieldDatabaseConfig } from \"./types.js\";\n\nexport const storageModes = {\n\tcolumn: columnMode,\n\tignore: ignoreMode,\n\t\"relation-table\": relationTableMode,\n\t\"tree-table\": treeTableMode,\n} as const;\n\nconst isTableBackedFieldDatabaseConfig = (\n\tconfig: FieldDatabaseConfig,\n): config is TableBackedFieldDatabaseConfig => {\n\treturn \"tableType\" in config && \"separator\" in config;\n};\n\n/**\n * Returns true when the provided table type is a custom-field table type.\n */\nexport const isCustomFieldTableType = (\n\ttableType: TableType,\n): tableType is CustomFieldTableType => {\n\treturn tableType.startsWith(constants.db.customFieldTablePrefix);\n};\n\n/**\n * Resolves the registered field database config for a custom-field table type.\n * Returns null when the table is not custom-field-backed or has no matching field.\n */\nexport const getFieldDatabaseConfig = (\n\ttableType: TableType,\n): TableBackedFieldDatabaseConfig | null => {\n\tif (!isCustomFieldTableType(tableType)) return null;\n\n\tfor (const fieldType of registeredFieldTypes) {\n\t\tconst databaseConfig = registeredFields[fieldType].config.database;\n\t\tif (!isTableBackedFieldDatabaseConfig(databaseConfig)) continue;\n\t\tif (databaseConfig.tableType !== tableType) continue;\n\n\t\treturn databaseConfig;\n\t}\n\n\treturn null;\n};\n\n/**\n * Type guard that narrows a field database config by storage mode.\n */\nexport const isStorageMode = <M extends FieldDatabaseMode>(\n\tconfig: FieldDatabaseConfig,\n\tmode: M,\n): config is Extract<FieldDatabaseConfig, { mode: M }> => {\n\treturn config.mode === mode;\n};\n\n/**\n * Returns the base migration priority for a non-column storage mode.\n */\nexport const getStorageModeBasePriority = (\n\tmode: Exclude<FieldDatabaseMode, \"column\" | \"ignore\">,\n): number => {\n\treturn storageModes[mode].baseTablePriority;\n};\n\n/**\n * Returns true when the provided table type is backed by tree-table storage mode.\n */\nexport const isTreeTableType = (tableType: TableType): boolean => {\n\tconst databaseConfig = getFieldDatabaseConfig(tableType);\n\treturn !!databaseConfig && isStorageMode(databaseConfig, \"tree-table\");\n};\n\n/**\n * Normalizes an arbitrary mode string into a valid field database mode.\n */\nexport const normalizeFieldDatabaseMode = (mode: string): FieldDatabaseMode => {\n\tswitch (mode) {\n\t\tcase \"ignore\":\n\t\t\treturn \"ignore\";\n\t\tcase \"tree-table\":\n\t\t\treturn \"tree-table\";\n\t\tcase \"relation-table\":\n\t\t\treturn \"relation-table\";\n\t\tdefault:\n\t\t\treturn \"column\";\n\t}\n};\n"],"mappings":"oTAYA,MAAa,EAAe,CAC3B,OAAQ,EACR,OAAQ,EACR,iBAAkB,EAClB,aAAc,CACf,EAEM,EACL,GAEO,cAAe,GAAU,cAAe,EAMnC,EACZ,GAEO,EAAU,WAAWA,EAAU,GAAG,sBAAsB,EAOnD,EACZ,GAC2C,CAC3C,GAAI,CAAC,EAAuB,CAAS,EAAG,OAAO,KAE/C,IAAK,IAAM,KAAa,EAAsB,CAC7C,IAAM,EAAiB,EAAiB,EAAU,CAAC,OAAO,SACrD,KAAiC,CAAc,GAChD,EAAe,YAAc,EAEjC,OAAO,CACR,CAEA,OAAO,IACR,EAKa,GACZ,EACA,IAEO,EAAO,OAAS,EAMX,EACZ,GAEO,EAAa,EAAK,CAAC,kBAMd,EAAmB,GAAkC,CACjE,IAAM,EAAiB,EAAuB,CAAS,EACvD,MAAO,CAAC,CAAC,GAAkB,EAAc,EAAgB,YAAY,CACtE,EAKa,EAA8B,GAAoC,CAC9E,OAAQ,EAAR,CACC,IAAK,SACJ,MAAO,SACR,IAAK,aACJ,MAAO,aACR,IAAK,iBACJ,MAAO,iBACR,QACC,MAAO,QACT,CACD"}