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.57 kB
{"version":3,"file":"apply-collection-migrations.mjs","names":[],"sources":["../../../src/libs/collection/apply-collection-migrations.ts"],"sourcesContent":["import type { ServiceFn } from \"../../types.js\";\nimport serviceWrapper from \"../../utils/services/service-wrapper.js\";\nimport { CollectionMigrationsRepository } from \"../repositories/index.js\";\nimport buildMigrations from \"./migration/build-migrations.js\";\nimport type {\n\tCollectionMigrationPlan,\n\tMigrationPlan,\n} from \"./migration/types.js\";\nimport type { CollectionSchema } from \"./schema/types.js\";\n\ntype CollectionMigrationBatch = {\n\tmigrationPlan: MigrationPlan;\n\tmigrationEntry: {\n\t\tcollection_key: string;\n\t\ttable_name_map: string;\n\t\tmigration_plans: MigrationPlan;\n\t\tcollection_schema: CollectionSchema;\n\t};\n};\n\n/**\n * Applies one collection's planned table operations and persists its resulting\n * schema snapshot atomically when the configured database supports transactions.\n */\nconst applyCollectionMigrationBatch = serviceWrapper<\n\t[CollectionMigrationBatch],\n\tundefined\n>(\n\tasync (context, data) => {\n\t\tconst migrationRes = await buildMigrations(context, {\n\t\t\tmigrationPlan: [data.migrationPlan],\n\t\t});\n\t\tif (migrationRes.error) return migrationRes;\n\n\t\tconst CollectionMigrations = new CollectionMigrationsRepository(\n\t\t\tcontext.db.client,\n\t\t\tcontext.config.db,\n\t\t);\n\t\tconst migrationEntryRes = await CollectionMigrations.createSingle({\n\t\t\tdata: data.migrationEntry,\n\t\t});\n\t\tif (migrationEntryRes.error) return migrationEntryRes;\n\n\t\treturn { data: undefined, error: undefined };\n\t},\n\t{ transaction: true },\n);\n\n/**\n * Applies a collection migration plan exactly as provided and records\n * the inferred collection schemas that the plan brings into effect.\n */\nconst applyCollectionMigrations: ServiceFn<\n\t[CollectionMigrationPlan],\n\tundefined\n> = async (context, plan) => {\n\tconst batches = plan.collections.map(({ inferredSchema, migrationPlan }) => {\n\t\tconst tableNameMap = Object.fromEntries(\n\t\t\tinferredSchema.tables.map((table) => [\n\t\t\t\ttable.name,\n\t\t\t\ttable.rawName ?? table.name,\n\t\t\t]),\n\t\t);\n\n\t\treturn {\n\t\t\tmigrationPlan,\n\t\t\tmigrationEntry: {\n\t\t\t\tcollection_key: inferredSchema.key,\n\t\t\t\ttable_name_map: JSON.stringify(tableNameMap),\n\t\t\t\tmigration_plans: migrationPlan,\n\t\t\t\tcollection_schema: inferredSchema,\n\t\t\t},\n\t\t} satisfies CollectionMigrationBatch;\n\t});\n\n\tfor (const batch of batches) {\n\t\tconst migrationRes = await applyCollectionMigrationBatch(context, batch);\n\t\tif (migrationRes.error) return migrationRes;\n\t}\n\n\treturn { data: undefined, error: undefined };\n};\n\nexport default applyCollectionMigrations;\n"],"mappings":"iKAwBA,MAAM,EAAgC,EAIrC,MAAO,EAAS,IAAS,CACxB,IAAM,EAAe,MAAM,EAAgB,EAAS,CACnD,cAAe,CAAC,EAAK,aAAa,CACnC,CAAC,EACD,GAAI,EAAa,MAAO,OAAO,EAM/B,IAAM,EAAoB,MAAM,IAJC,EAChC,EAAQ,GAAG,OACX,EAAQ,OAAO,EAEmC,CAAC,CAAC,aAAa,CACjE,KAAM,EAAK,cACZ,CAAC,EAGD,OAFI,EAAkB,MAAc,EAE7B,CAAE,KAAM,IAAA,GAAW,MAAO,IAAA,EAAU,CAC5C,EACA,CAAE,YAAa,EAAK,CACrB,EAMM,EAGF,MAAO,EAAS,IAAS,CAC5B,IAAM,EAAU,EAAK,YAAY,KAAK,CAAE,iBAAgB,mBAAoB,CAC3E,IAAM,EAAe,OAAO,YAC3B,EAAe,OAAO,IAAK,GAAU,CACpC,EAAM,KACN,EAAM,SAAW,EAAM,IACxB,CAAC,CACF,EAEA,MAAO,CACN,gBACA,eAAgB,CACf,eAAgB,EAAe,IAC/B,eAAgB,KAAK,UAAU,CAAY,EAC3C,gBAAiB,EACjB,kBAAmB,CACpB,CACD,CACD,CAAC,EAED,IAAK,IAAM,KAAS,EAAS,CAC5B,IAAM,EAAe,MAAM,EAA8B,EAAS,CAAK,EACvE,GAAI,EAAa,MAAO,OAAO,CAChC,CAEA,MAAO,CAAE,KAAM,IAAA,GAAW,MAAO,IAAA,EAAU,CAC5C"}