@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 6.1 kB
Source Map (JSON)
{"version":3,"file":"sync-locales.mjs","names":["constants","formatter"],"sources":["../../../src/services/sync/sync-locales.ts"],"sourcesContent":["import constants from \"../../constants/constants.js\";\nimport formatter from \"../../libs/formatters/index.js\";\nimport cacheKeys from \"../../libs/kv/cache-keys.js\";\nimport logger from \"../../libs/logger/index.js\";\nimport { LocalesRepository } from \"../../libs/repositories/index.js\";\nimport type { ServiceContext, ServiceFn } from \"../../utils/services/types.js\";\n\n/**\n * Syncs the locales in the database with the locales in the config. Handles, creating, soft-deleting, and restoring.\n */\nconst syncLocales: ServiceFn<[], undefined> = async (\n\tcontext: ServiceContext,\n) => {\n\tconst Locales = new LocalesRepository(context.db.client, context.config.db);\n\tconst localeCodes = context.config.localization.locales.map(\n\t\t(locale) => locale.code,\n\t);\n\n\tconst localesRes = await Locales.selectMultiple({\n\t\tselect: [\"code\", \"is_deleted\"],\n\t\tvalidation: {\n\t\t\tenabled: true,\n\t\t},\n\t});\n\tif (localesRes.error) return localesRes;\n\n\tconst localeCodesFromDB = localesRes.data.map((locale) => locale.code);\n\n\t// Get locale codes that are in the config but not in the database\n\tconst missingLocales = localeCodes.filter(\n\t\t(locale) => !localeCodesFromDB.includes(locale),\n\t);\n\tif (missingLocales.length > 0) {\n\t\tlogger.debug({\n\t\t\tmessage: `Syncing new locales to the DB: ${missingLocales.join(\", \")}`,\n\t\t\tscope: constants.logScopes.sync,\n\t\t});\n\t}\n\n\t// Get locale codes that are in the database but not in the config\n\tconst localesToDelete = localesRes.data.filter(\n\t\t(locale) =>\n\t\t\t!localeCodes.includes(locale.code) &&\n\t\t\tformatter.formatBoolean(locale.is_deleted) === false,\n\t);\n\tconst localesToDeleteCodes = localesToDelete.map((locale) => locale.code);\n\tif (localesToDeleteCodes.length > 0) {\n\t\tlogger.debug({\n\t\t\tmessage: `Marking the following locales as deleted: ${localesToDeleteCodes.join(\", \")}`,\n\t\t\tscope: constants.logScopes.sync,\n\t\t});\n\t}\n\n\t// Get locals that are in the database as is_deleted but in the config\n\tconst unDeletedLocales = localesRes.data.filter(\n\t\t(locale) =>\n\t\t\tformatter.formatBoolean(locale.is_deleted) &&\n\t\t\tlocaleCodes.includes(locale.code),\n\t);\n\tconst unDeletedLocalesCodes = unDeletedLocales.map((locale) => locale.code);\n\tif (unDeletedLocalesCodes.length > 0) {\n\t\tlogger.debug({\n\t\t\tmessage: `Restoring previously deleted locales: ${unDeletedLocalesCodes.join(\", \")}`,\n\t\t\tscope: constants.logScopes.sync,\n\t\t});\n\t}\n\n\tconst [createRes, deleteRes, restoreRes] = await Promise.all([\n\t\tmissingLocales.length > 0 &&\n\t\t\tLocales.createMultiple({\n\t\t\t\tdata: missingLocales.map((locale) => ({\n\t\t\t\t\tcode: locale,\n\t\t\t\t})),\n\t\t\t}),\n\t\tlocalesToDeleteCodes.length > 0 &&\n\t\t\tLocales.updateSingle({\n\t\t\t\tdata: {\n\t\t\t\t\tis_deleted: true,\n\t\t\t\t\tis_deleted_at: new Date().toISOString(),\n\t\t\t\t\tupdated_at: new Date().toISOString(),\n\t\t\t\t},\n\t\t\t\twhere: [\n\t\t\t\t\t{\n\t\t\t\t\t\tkey: \"code\",\n\t\t\t\t\t\toperator: \"in\",\n\t\t\t\t\t\tvalue: localesToDeleteCodes,\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t\treturning: [\"code\"],\n\t\t\t\tvalidation: {\n\t\t\t\t\tenabled: true,\n\t\t\t\t},\n\t\t\t}),\n\t\tunDeletedLocalesCodes.length > 0 &&\n\t\t\tLocales.updateSingle({\n\t\t\t\tdata: {\n\t\t\t\t\tis_deleted: false,\n\t\t\t\t\tis_deleted_at: null,\n\t\t\t\t\tupdated_at: new Date().toISOString(),\n\t\t\t\t},\n\t\t\t\twhere: [\n\t\t\t\t\t{\n\t\t\t\t\t\tkey: \"code\",\n\t\t\t\t\t\toperator: \"in\",\n\t\t\t\t\t\tvalue: unDeletedLocales.map((locale) => locale.code),\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t\treturning: [\"code\"],\n\t\t\t\tvalidation: {\n\t\t\t\t\tenabled: true,\n\t\t\t\t},\n\t\t\t}),\n\t]);\n\tif (typeof createRes !== \"boolean\" && createRes.error) return createRes;\n\tif (typeof deleteRes !== \"boolean\" && deleteRes.error) return deleteRes;\n\tif (typeof restoreRes !== \"boolean\" && restoreRes.error) return restoreRes;\n\n\tawait context.kv.delete(context, {\n\t\tkey: cacheKeys.http.static.clientLocales,\n\t\thash: true,\n\t});\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: undefined,\n\t};\n};\n\nexport default syncLocales;\n"],"mappings":"sOAUA,MAAM,EAAwC,KAC7C,IACI,CACJ,IAAM,EAAU,IAAI,EAAkB,EAAQ,GAAG,OAAQ,EAAQ,OAAO,EAAE,EACpE,EAAc,EAAQ,OAAO,aAAa,QAAQ,IACtD,GAAW,EAAO,IACpB,EAEM,EAAa,MAAM,EAAQ,eAAe,CAC/C,OAAQ,CAAC,OAAQ,YAAY,EAC7B,WAAY,CACX,QAAS,EACV,CACD,CAAC,EACD,GAAI,EAAW,MAAO,OAAO,EAE7B,IAAM,EAAoB,EAAW,KAAK,IAAK,GAAW,EAAO,IAAI,EAG/D,EAAiB,EAAY,OACjC,GAAW,CAAC,EAAkB,SAAS,CAAM,CAC/C,EACI,EAAe,OAAS,GAC3B,EAAO,MAAM,CACZ,QAAS,kCAAkC,EAAe,KAAK,IAAI,IACnE,MAAOA,EAAU,UAAU,IAC5B,CAAC,EASF,IAAM,EALkB,EAAW,KAAK,OACtC,GACA,CAAC,EAAY,SAAS,EAAO,IAAI,GACjCC,EAAU,cAAc,EAAO,UAAU,IAAM,EAEN,CAAC,CAAC,IAAK,GAAW,EAAO,IAAI,EACpE,EAAqB,OAAS,GACjC,EAAO,MAAM,CACZ,QAAS,6CAA6C,EAAqB,KAAK,IAAI,IACpF,MAAOD,EAAU,UAAU,IAC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