@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 3.4 kB
Source Map (JSON)
{"version":3,"file":"build-migrations.mjs","names":[],"sources":["../../../../src/libs/collection/migration/build-migrations.ts"],"sourcesContent":["import type { ServiceFn } from \"../../../types.js\";\nimport { copy } from \"../../i18n/index.js\";\nimport createTableQuery from \"./create-table-query.js\";\nimport modifyTableQuery from \"./modify-table-query.js\";\nimport removeTableQuery from \"./remove-table-query.js\";\nimport type { MigrationPlan } from \"./types.js\";\n\n/**\n * Builds and runs migrations based on the migration plan\n */\nconst buildMigrations: ServiceFn<\n\t[\n\t\t{\n\t\t\tmigrationPlan: MigrationPlan[];\n\t\t},\n\t],\n\tundefined\n> = async (context, data) => {\n\ttry {\n\t\tconst allTablePlans = data.migrationPlan.flatMap((mp) => mp.tables);\n\t\tconst sortedPlans = allTablePlans.sort((a, b) => {\n\t\t\tif (a.priority !== b.priority) {\n\t\t\t\treturn b.priority - a.priority;\n\t\t\t}\n\t\t\t//* within same priority, order by query type (move -> create -> modify)\n\t\t\tconst typeOrder = { remove: 0, create: 1, modify: 2 };\n\t\t\treturn typeOrder[a.type] - typeOrder[b.type];\n\t\t});\n\n\t\t//* get unique prio levels\n\t\tconst priorities = [\n\t\t\t...new Set(sortedPlans.map((plan) => plan.priority)),\n\t\t].sort((a, b) => b - a);\n\n\t\t//* build batches based on prio and process them\n\t\tfor (const priority of priorities) {\n\t\t\tconst batch = sortedPlans.filter((plan) => plan.priority === priority);\n\n\t\t\tfor (const plan of batch) {\n\t\t\t\tlet result: Awaited<ReturnType<typeof createTableQuery>> | undefined;\n\t\t\t\tswitch (plan.type) {\n\t\t\t\t\tcase \"create\": {\n\t\t\t\t\t\tresult = await createTableQuery(context, { migration: plan });\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tcase \"modify\": {\n\t\t\t\t\t\tresult = await modifyTableQuery(context, { migration: plan });\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tcase \"remove\": {\n\t\t\t\t\t\tresult = await removeTableQuery(context, { migration: plan });\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (result?.error) return result;\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\tdata: undefined,\n\t\t\terror: undefined,\n\t\t};\n\t} catch (err) {\n\t\treturn {\n\t\t\tdata: undefined,\n\t\t\terror: {\n\t\t\t\tmessage: copy(\"server:core.errors.default.message\", {\n\t\t\t\t\tdefaultMessage:\n\t\t\t\t\t\terr instanceof Error ? err.message : \"An error occurred\",\n\t\t\t\t}),\n\t\t\t},\n\t\t};\n\t}\n};\n\nexport default buildMigrations;\n"],"mappings":"mKAUA,MAAM,EAOF,MAAO,EAAS,IAAS,CAC5B,GAAI,CAEH,IAAM,EADgB,EAAK,cAAc,QAAS,GAAO,EAAG,MAC5B,CAAC,CAAC,MAAM,EAAG,IAAM,CAChD,GAAI,EAAE,WAAa,EAAE,SACpB,OAAO,EAAE,SAAW,EAAE,SAGvB,IAAM,EAAY,CAAE,OAAQ,EAAG,OAAQ,EAAG,OAAQ,CAAE,EACpD,OAAO,EAAU,EAAE,MAAQ,EAAU,EAAE,KACxC,CAAC,EAGK,EAAa,CAClB,GAAG,IAAI,IAAI,EAAY,IAAK,GAAS,EAAK,QAAQ,CAAC,CACpD,CAAC,CAAC,MAAM,EAAG,IAAM,EAAI,CAAC,EAGtB,IAAK,IAAM,KAAY,EAAY,CAClC,IAAM,EAAQ,EAAY,OAAQ,GAAS,EAAK,WAAa,CAAQ,EAErE,IAAK,IAAM,KAAQ,EAAO,CACzB,IAAI,EACJ,OAAQ,EAAK,KAAb,CACC,IAAK,SACJ,EAAS,MAAM,EAAiB,EAAS,CAAE,UAAW,CAAK,CAAC,EAC5D,MAED,IAAK,SACJ,EAAS,MAAM,EAAiB,EAAS,CAAE,UAAW,CAAK,CAAC,EAC5D,MAED,IAAK,SACJ,EAAS,MAAM,EAAiB,EAAS,CAAE,UAAW,CAAK,CAAC,EAC5D,KAEF,CAEA,GAAI,GAAQ,MAAO,OAAO,CAC3B,CACD,CAEA,MAAO,CACN,KAAM,IAAA,GACN,MAAO,IAAA,EACR,CACD,OAAS,EAAK,CACb,MAAO,CACN,KAAM,IAAA,GACN,MAAO,CACN,QAAS,EAAK,qCAAsC,CACnD,eACC,aAAe,MAAQ,EAAI,QAAU,mBACvC,CAAC,CACF,CACD,CACD,CACD"}