UNPKG

@lucidcms/core

Version:

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

1 lines 5.46 kB
{"version":3,"file":"runtime-schema-selectors.mjs","names":[],"sources":["../../../../../src/libs/collection/schema/runtime/runtime-schema-selectors.ts"],"sourcesContent":["import type {\n\tCollectionTableNames,\n\tLucidBrickTableName,\n\tLucidDocumentTableName,\n\tLucidVersionTableName,\n\tServiceResponse,\n} from \"../../../../types.js\";\nimport type { ServiceContext } from \"../../../../utils/services/types.js\";\nimport { copy } from \"../../../i18n/index.js\";\nimport type { CollectionSchemaTable } from \"../types.js\";\nimport getRuntimeSchema from \"./get-runtime-schema.js\";\n\n/**\n * Returns runtime schema tables for brick content on a collection.\n * - document-fields\n * - tree-table custom-field tables\n * - brick\n */\nexport const getBricksTableSchema = async (\n\tcontext: ServiceContext,\n\tcollectionKey: string,\n): ServiceResponse<Array<CollectionSchemaTable<LucidBrickTableName>>> => {\n\tconst schemaRes = await getRuntimeSchema(context, { collectionKey });\n\tif (schemaRes.error) return schemaRes;\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: schemaRes.data.tables.filter(\n\t\t\t(table) => table.type !== \"document\" && table.type !== \"versions\",\n\t\t) as Array<CollectionSchemaTable<LucidBrickTableName>>,\n\t};\n};\n\n/**\n * Returns runtime schema for the document table on a collection.\n * - document\n */\nexport const getDocumentTableSchema = async (\n\tcontext: ServiceContext,\n\tcollectionKey: string,\n): ServiceResponse<\n\tCollectionSchemaTable<LucidDocumentTableName> | undefined\n> => {\n\tconst schema = await getRuntimeSchema(context, { collectionKey });\n\tif (schema.error) return schema;\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: schema.data.tables.find((t) => t.type === \"document\") as\n\t\t\t| CollectionSchemaTable<LucidDocumentTableName>\n\t\t\t| undefined,\n\t};\n};\n\n/**\n * Returns runtime schema for the top-level document-fields table.\n * - document-fields\n */\nexport const getDocumentFieldsTableSchema = async (\n\tcontext: ServiceContext,\n\tcollectionKey: string,\n): ServiceResponse<CollectionSchemaTable<LucidBrickTableName> | undefined> => {\n\tconst schemaRes = await getRuntimeSchema(context, { collectionKey });\n\tif (schemaRes.error) return schemaRes;\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: schemaRes.data.tables.find((t) => t.type === \"document-fields\") as\n\t\t\t| CollectionSchemaTable<LucidBrickTableName>\n\t\t\t| undefined,\n\t};\n};\n\n/**\n * Returns runtime schema for the document versions table.\n * - versions\n */\nexport const getDocumentVersionTableSchema = async (\n\tcontext: ServiceContext,\n\tcollectionKey: string,\n): ServiceResponse<\n\tCollectionSchemaTable<LucidVersionTableName> | undefined\n> => {\n\tconst schemaRes = await getRuntimeSchema(context, { collectionKey });\n\tif (schemaRes.error) return schemaRes;\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: schemaRes.data.tables.find((t) => t.type === \"versions\") as\n\t\t\t| CollectionSchemaTable<LucidVersionTableName>\n\t\t\t| undefined,\n\t};\n};\n\n/**\n * Returns runtime table names for a collection.\n * - version\n * - document\n * - document-fields\n */\nexport const getTableNames = async (\n\tcontext: ServiceContext,\n\tcollectionKey: string,\n): ServiceResponse<CollectionTableNames> => {\n\tconst [versionTableRes, documentTableRes, documentFieldsRes] =\n\t\tawait Promise.all([\n\t\t\tgetDocumentVersionTableSchema(context, collectionKey),\n\t\t\tgetDocumentTableSchema(context, collectionKey),\n\t\t\tgetDocumentFieldsTableSchema(context, collectionKey),\n\t\t]);\n\tif (versionTableRes.error) return versionTableRes;\n\tif (documentTableRes.error) return documentTableRes;\n\tif (documentFieldsRes.error) return documentFieldsRes;\n\n\tif (\n\t\t!versionTableRes.data?.name ||\n\t\t!documentTableRes.data?.name ||\n\t\t!documentFieldsRes.data?.name\n\t) {\n\t\treturn {\n\t\t\terror: {\n\t\t\t\tmessage: copy(\"server:core.collections.names.fetch.failed\"),\n\t\t\t\tstatus: 500,\n\t\t\t},\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\n\treturn {\n\t\tdata: {\n\t\t\tversion: versionTableRes.data.name,\n\t\t\tdocument: documentTableRes.data.name,\n\t\t\tdocumentFields: documentFieldsRes.data.name,\n\t\t},\n\t\terror: undefined,\n\t};\n};\n"],"mappings":"sFAkBA,MAAa,EAAuB,MACnC,EACA,IACwE,CACxE,IAAM,EAAY,MAAM,EAAiB,EAAS,CAAE,eAAc,CAAC,EAGnE,OAFI,EAAU,MAAc,EAErB,CACN,MAAO,IAAA,GACP,KAAM,EAAU,KAAK,OAAO,OAC1B,GAAU,EAAM,OAAS,YAAc,EAAM,OAAS,UACxD,CACD,CACD,EAMa,EAAyB,MACrC,EACA,IAGI,CACJ,IAAM,EAAS,MAAM,EAAiB,EAAS,CAAE,eAAc,CAAC,EAGhE,OAFI,EAAO,MAAc,EAElB,CACN,MAAO,IAAA,GACP,KAAM,EAAO,KAAK,OAAO,KAAM,GAAM,EAAE,OAAS,UAAU,CAG3D,CACD,EAMa,EAA+B,MAC3C,EACA,IAC6E,CAC7E,IAAM,EAAY,MAAM,EAAiB,EAAS,CAAE,eAAc,CAAC,EAGnE,OAFI,EAAU,MAAc,EAErB,CACN,MAAO,IAAA,GACP,KAAM,EAAU,KAAK,OAAO,KAAM,GAAM,EAAE,OAAS,iBAAiB,CAGrE,CACD,EAMa,EAAgC,MAC5C,EACA,IAGI,CACJ,IAAM,EAAY,MAAM,EAAiB,EAAS,CAAE,eAAc,CAAC,EAGnE,OAFI,EAAU,MAAc,EAErB,CACN,MAAO,IAAA,GACP,KAAM,EAAU,KAAK,OAAO,KAAM,GAAM,EAAE,OAAS,UAAU,CAG9D,CACD,EAQa,EAAgB,MAC5B,EACA,IAC2C,CAC3C,GAAM,CAAC,EAAiB,EAAkB,GACzC,MAAM,QAAQ,IAAI,CACjB,EAA8B,EAAS,CAAa,EACpD,EAAuB,EAAS,CAAa,EAC7C,EAA6B,EAAS,CAAa,CACpD,CAAC,EAmBF,OAlBI,EAAgB,MAAc,EAC9B,EAAiB,MAAc,EAC/B,EAAkB,MAAc,EAGnC,CAAC,EAAgB,MAAM,MACvB,CAAC,EAAiB,MAAM,MACxB,CAAC,EAAkB,MAAM,KAElB,CACN,MAAO,CACN,QAAS,EAAK,4CAA4C,EAC1D,OAAQ,GACT,EACA,KAAM,IAAA,EACP,EAGM,CACN,KAAM,CACL,QAAS,EAAgB,KAAK,KAC9B,SAAU,EAAiB,KAAK,KAChC,eAAgB,EAAkB,KAAK,IACxC,EACA,MAAO,IAAA,EACR,CACD"}