@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 3.27 kB
Source Map (JSON)
{"version":3,"file":"get-runtime-schema.mjs","names":[],"sources":["../../../../../src/libs/collection/schema/runtime/get-runtime-schema.ts"],"sourcesContent":["import type { ServiceFn } from \"../../../../utils/services/types.js\";\nimport { copy } from \"../../../i18n/index.js\";\nimport { CollectionMigrationsRepository } from \"../../../repositories/index.js\";\nimport inferSchema from \"../infer-schema.js\";\nimport type { CollectionSchema } from \"../types.js\";\nimport buildRuntimeSchema from \"./build-runtime-schema.js\";\nimport diffSnapshotVsConfig from \"./diff-snapshot-vs-config.js\";\nimport { resolveRuntimeSchema } from \"./runtime-schema-cache.js\";\n\n/**\n * Builds the runtime schema for a collection by comparing the latest persisted\n * migration snapshot to the current config-inferred schema.\n */\nconst getRuntimeSchema: ServiceFn<\n\t[\n\t\t{\n\t\t\tcollectionKey: string;\n\t\t},\n\t],\n\tCollectionSchema\n> = async (context, data) => {\n\tconst CollectionMigrations = new CollectionMigrationsRepository(\n\t\tcontext.db.client,\n\t\tcontext.config.db,\n\t);\n\n\treturn await resolveRuntimeSchema(context, data.collectionKey, async () => {\n\t\tconst collection = context.config.collections.find(\n\t\t\t(c) => c.key === data.collectionKey,\n\t\t);\n\t\tif (!collection) {\n\t\t\treturn {\n\t\t\t\tdata: undefined,\n\t\t\t\terror: {\n\t\t\t\t\tmessage: copy(\"server:core.collections.not.found.message\"),\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\n\t\tconst localSchemaRes = inferSchema(collection, context.config.db);\n\t\tif (localSchemaRes.error) return localSchemaRes;\n\t\tconst latestMigrationRes =\n\t\t\tawait CollectionMigrations.selectLatestByCollectionKey({\n\t\t\t\tcollectionKey: data.collectionKey,\n\t\t\t});\n\t\tif (latestMigrationRes.error) return latestMigrationRes;\n\t\tif (!latestMigrationRes.data) {\n\t\t\treturn {\n\t\t\t\tdata: undefined,\n\t\t\t\terror: {\n\t\t\t\t\ttype: \"basic\",\n\t\t\t\t\tname: copy(\"server:core.error.schema.migration.required.name\"),\n\t\t\t\t\tmessage: copy(\"server:core.error.schema.migration.required.message\"),\n\t\t\t\t\tstatus: 400,\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\n\t\tconst diff = diffSnapshotVsConfig(\n\t\t\tlatestMigrationRes.data.collection_schema,\n\t\t\tlocalSchemaRes.data,\n\t\t);\n\n\t\t//* remove additions not yet present in the persisted migration snapshot\n\t\tconst filteredSchema = buildRuntimeSchema(localSchemaRes.data, diff);\n\n\t\treturn {\n\t\t\tdata: filteredSchema,\n\t\t\terror: undefined,\n\t\t};\n\t});\n};\n\nexport default getRuntimeSchema;\n"],"mappings":"ySAaA,MAAM,EAOF,MAAO,EAAS,IAAS,CAC5B,IAAM,EAAuB,IAAI,EAChC,EAAQ,GAAG,OACX,EAAQ,OAAO,EAChB,EAEA,OAAO,MAAM,EAAqB,EAAS,EAAK,cAAe,SAAY,CAC1E,IAAM,EAAa,EAAQ,OAAO,YAAY,KAC5C,GAAM,EAAE,MAAQ,EAAK,aACvB,EACA,GAAI,CAAC,EACJ,MAAO,CACN,KAAM,IAAA,GACN,MAAO,CACN,QAAS,EAAK,2CAA2C,CAC1D,CACD,EAGD,IAAM,EAAiB,EAAY,EAAY,EAAQ,OAAO,EAAE,EAChE,GAAI,EAAe,MAAO,OAAO,EACjC,IAAM,EACL,MAAM,EAAqB,4BAA4B,CACtD,cAAe,EAAK,aACrB,CAAC,EACF,GAAI,EAAmB,MAAO,OAAO,EACrC,GAAI,CAAC,EAAmB,KACvB,MAAO,CACN,KAAM,IAAA,GACN,MAAO,CACN,KAAM,QACN,KAAM,EAAK,kDAAkD,EAC7D,QAAS,EAAK,qDAAqD,EACnE,OAAQ,GACT,CACD,EAGD,IAAM,EAAO,EACZ,EAAmB,KAAK,kBACxB,EAAe,IAChB,EAKA,MAAO,CACN,KAHsB,EAAmB,EAAe,KAAM,CAG3C,EACnB,MAAO,IAAA,EACR,CACD,CAAC,CACF"}