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.39 kB
{"version":3,"file":"get-table-priority.mjs","names":[],"sources":["../../../../src/libs/collection/helpers/get-table-priority.ts"],"sourcesContent":["import type {\n\tCollectionSchemaTable,\n\tCoreTableType,\n\tTableType,\n} from \"../../../libs/collection/schema/types.js\";\nimport type { InferredTable, ServiceResponse } from \"../../../types.js\";\nimport { copy } from \"../../i18n/index.js\";\nimport {\n\tgetFieldDatabaseConfig,\n\tgetStorageModeBasePriority,\n\tisCustomFieldTableType,\n\tisStorageMode,\n} from \"../custom-fields/storage/index.js\";\nimport { treeTableMode } from \"../custom-fields/storage/tree-table.js\";\nimport inferTableType from \"./infer-table-type.js\";\n\nconst TABLE_PRIORITY: Record<CoreTableType, number> = {\n\tdocument: 1000,\n\tversions: 900,\n\t\"document-fields\": 800,\n\tbrick: 700,\n};\nconst EXTERNAL_REFERENCE_PRIORITY = 100;\n\n/**\n * Works out the table priority based on its type and foreign keys\n */\nconst getTablePriority = (\n\ttype: \"db-inferred\" | \"collection-inferred\",\n\ttable: InferredTable | CollectionSchemaTable,\n): Awaited<ServiceResponse<number>> => {\n\tlet tableType: TableType;\n\n\tif (type === \"db-inferred\") {\n\t\tconst tableTypeRes = inferTableType(table.name);\n\t\tif (tableTypeRes.error) return tableTypeRes;\n\t\ttableType = tableTypeRes.data;\n\t} else tableType = (table as CollectionSchemaTable).type;\n\n\tlet basePriority: number;\n\tif (isCustomFieldTableType(tableType)) {\n\t\tconst databaseConfig = getFieldDatabaseConfig(tableType);\n\t\tif (!databaseConfig) {\n\t\t\treturn {\n\t\t\t\tdata: undefined,\n\t\t\t\terror: {\n\t\t\t\t\tmessage: copy(\n\t\t\t\t\t\t\"server:core.collections.schema.table.name.format.insufficient.parts\",\n\t\t\t\t\t),\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t\tbasePriority = getStorageModeBasePriority(databaseConfig.mode);\n\t} else {\n\t\tbasePriority = TABLE_PRIORITY[tableType];\n\t}\n\n\tconst hasExternalReferences = table.columns.some((column) => {\n\t\tif (!column.foreignKey) return false;\n\t\treturn true;\n\t});\n\tif (hasExternalReferences) {\n\t\tbasePriority -= EXTERNAL_REFERENCE_PRIORITY;\n\t}\n\n\tif (type === \"collection-inferred\") {\n\t\tif (isCustomFieldTableType(tableType)) {\n\t\t\tconst databaseConfig = getFieldDatabaseConfig(tableType);\n\t\t\tconst treeDepth = (table as CollectionSchemaTable).key.fieldPath?.length;\n\n\t\t\tif (\n\t\t\t\tdatabaseConfig &&\n\t\t\t\tisStorageMode(databaseConfig, \"tree-table\") &&\n\t\t\t\ttreeDepth\n\t\t\t) {\n\t\t\t\tbasePriority -= treeTableMode.getPriorityOffsetForDepth(treeDepth);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn {\n\t\tdata: basePriority,\n\t\terror: undefined,\n\t};\n};\n\nexport default getTablePriority;\n"],"mappings":"oTAgBA,MAAM,EAAgD,CACrD,SAAU,IACV,SAAU,IACV,kBAAmB,IACnB,MAAO,GACR,EAMM,GACL,EACA,IACsC,CACtC,IAAI,EAEJ,GAAI,IAAS,cAAe,CAC3B,IAAM,EAAe,EAAe,EAAM,IAAI,EAC9C,GAAI,EAAa,MAAO,OAAO,EAC/B,EAAY,EAAa,IAC1B,KAAO,GAAa,EAAgC,KAEpD,IAAI,EACJ,GAAI,EAAuB,CAAS,EAAG,CACtC,IAAM,EAAiB,EAAuB,CAAS,EACvD,GAAI,CAAC,EACJ,MAAO,CACN,KAAM,IAAA,GACN,MAAO,CACN,QAAS,EACR,qEACD,CACD,CACD,EAED,EAAe,EAA2B,EAAe,IAAI,CAC9D,KACC,GAAe,EAAe,GAW/B,GAR8B,EAAM,QAAQ,KAAM,GACjD,EAAK,EAAO,UAGW,IACvB,GAAgB,KAGb,IAAS,uBACR,EAAuB,CAAS,EAAG,CACtC,IAAM,EAAiB,EAAuB,CAAS,EACjD,EAAa,EAAgC,IAAI,WAAW,OAGjE,GACA,EAAc,EAAgB,YAAY,GAC1C,IAEA,GAAgB,EAAc,0BAA0B,CAAS,EAEnE,CAGD,MAAO,CACN,KAAM,EACN,MAAO,IAAA,EACR,CACD"}