@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 4.94 kB
Source Map (JSON)
{"version":3,"file":"get-single.mjs","names":["collectionsFormatter"],"sources":["../../../src/services/collections/get-single.ts"],"sourcesContent":["import getMigrationStatus from \"../../libs/collection/get-collection-migration-status.js\";\nimport { getTableNames } from \"../../libs/collection/schema/runtime/runtime-schema-selectors.js\";\nimport { collectionsFormatter } from \"../../libs/formatters/index.js\";\nimport { copy } from \"../../libs/i18n/index.js\";\nimport { DocumentsRepository } from \"../../libs/repositories/index.js\";\nimport type { Collection } from \"../../types/response.js\";\nimport { tenantAccessAllowed } from \"../../utils/helpers/index.js\";\nimport type { ServiceFn } from \"../../utils/services/types.js\";\n\n/**\n * Gets a single collection\n */\nconst getSingle: ServiceFn<\n\t[\n\t\t{\n\t\t\tkey: string;\n\t\t},\n\t],\n\tCollection\n> = async (context, data) => {\n\tconst collection = context.config.collections?.find(\n\t\t(c) => c.key === data.key,\n\t);\n\n\tif (collection === undefined) {\n\t\treturn {\n\t\t\terror: {\n\t\t\t\ttype: \"basic\",\n\t\t\t\tmessage: copy(\"server:core.collections.not.found.message\"),\n\t\t\t\tstatus: 404,\n\t\t\t},\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\n\tif (\n\t\t!tenantAccessAllowed(collection.getData.tenants, context.request.tenantKey)\n\t) {\n\t\treturn {\n\t\t\terror: {\n\t\t\t\ttype: \"basic\",\n\t\t\t\tmessage: copy(\"server:core.collections.not.found.message\"),\n\t\t\t\tstatus: 404,\n\t\t\t},\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\n\tconst tablesRes = await getTableNames(context, collection.key);\n\tif (tablesRes.error) return tablesRes;\n\n\tconst migrationStatus = await getMigrationStatus(context, {\n\t\tcollection: collection,\n\t});\n\tif (migrationStatus.error) return migrationStatus;\n\n\tconst adminTranslations = context.translate\n\t\t.forLocale(context.config.i18n.defaultLocale)\n\t\t.adminBundle();\n\n\tif (collection.getData.mode === \"single\") {\n\t\tconst Documents = new DocumentsRepository(\n\t\t\tcontext.db.client,\n\t\t\tcontext.config.db,\n\t\t);\n\n\t\tconst documentRes = await Documents.selectSingle(\n\t\t\t{\n\t\t\t\tselect: [\"id\"],\n\t\t\t\twhere: [\n\t\t\t\t\t{\n\t\t\t\t\t\tkey: \"is_deleted\",\n\t\t\t\t\t\toperator: \"=\",\n\t\t\t\t\t\tvalue: context.config.db.getDefault(\"boolean\", \"false\"),\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t},\n\t\t\t{\n\t\t\t\ttableName: tablesRes.data.document,\n\t\t\t},\n\t\t);\n\t\tif (documentRes.error) return documentRes;\n\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: collectionsFormatter.formatSingle({\n\t\t\t\tcollection: collection,\n\t\t\t\tallCollections: context.config.collections ?? [],\n\t\t\t\ttenantKey: context.request.tenantKey,\n\t\t\t\tqueueSupportsScheduling: context.queue.support.scheduling,\n\t\t\t\tadminTranslations,\n\t\t\t\tinclude: {\n\t\t\t\t\tbricks: true,\n\t\t\t\t\tfields: true,\n\t\t\t\t\tdocumentId: true,\n\t\t\t\t},\n\t\t\t\tdocuments: documentRes.data\n\t\t\t\t\t? [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tid: documentRes.data.id,\n\t\t\t\t\t\t\t\tcollection_key: collection.key,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t]\n\t\t\t\t\t: undefined,\n\t\t\t}),\n\t\t};\n\t}\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: collectionsFormatter.formatSingle({\n\t\t\tcollection: collection,\n\t\t\tallCollections: context.config.collections ?? [],\n\t\t\ttenantKey: context.request.tenantKey,\n\t\t\tqueueSupportsScheduling: context.queue.support.scheduling,\n\t\t\tadminTranslations,\n\t\t\tmigrationStatus: migrationStatus.data,\n\t\t\tinclude: {\n\t\t\t\tbricks: true,\n\t\t\t\tfields: true,\n\t\t\t\tdocumentId: true,\n\t\t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