UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

1 lines 2.35 kB
{"version":3,"file":"clear-expired-collections.mjs","names":[],"sources":["../../../src/services/crons/clear-expired-collections.ts"],"sourcesContent":["import { CollectionsRepository } from \"../../libs/repositories/index.js\";\nimport type { ServiceFn } from \"../../utils/services/types.js\";\nimport getRetentionDays from \"./helpers/get-retention-days.js\";\n\n/**\n * Finds all expired collections and queues them for deletion\n */\nconst clearExpiredCollections: ServiceFn<[], undefined> = async (context) => {\n\tconst Collections = new CollectionsRepository(\n\t\tcontext.db.client,\n\t\tcontext.config.db,\n\t);\n\n\tconst compDate = getRetentionDays(\n\t\tcontext.config.retention,\n\t\t\"removedCollections\",\n\t);\n\n\tconst expiredCollectionsRes = await Collections.selectMultiple({\n\t\tselect: [\"key\"],\n\t\twhere: [\n\t\t\t{\n\t\t\t\tkey: \"is_deleted_at\",\n\t\t\t\toperator: \"<\",\n\t\t\t\tvalue: compDate,\n\t\t\t},\n\t\t\t{\n\t\t\t\tkey: \"is_deleted\",\n\t\t\t\toperator: \"=\",\n\t\t\t\tvalue: context.config.db.getDefault(\"boolean\", \"true\"),\n\t\t\t},\n\t\t],\n\t\tvalidation: {\n\t\t\tenabled: true,\n\t\t},\n\t});\n\tif (expiredCollectionsRes.error) return expiredCollectionsRes;\n\n\tif (expiredCollectionsRes.data.length === 0) {\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\n\tconst queueRes = await context.queue.addBatch(context, {\n\t\tevent: \"collections:delete\",\n\t\tpayloads: expiredCollectionsRes.data.map((collection) => ({\n\t\t\tcollectionKey: collection.key,\n\t\t})),\n\t});\n\tif (queueRes.error) return queueRes;\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: undefined,\n\t};\n};\n\nexport default clearExpiredCollections;\n"],"mappings":"uGAOA,MAAM,EAAoD,KAAO,IAAY,CAC5E,IAAM,EAAc,IAAI,EACvB,EAAQ,GAAG,OACX,EAAQ,OAAO,EAChB,EAEM,EAAW,EAChB,EAAQ,OAAO,UACf,oBACD,EAEM,EAAwB,MAAM,EAAY,eAAe,CAC9D,OAAQ,CAAC,KAAK,EACd,MAAO,CACN,CACC,IAAK,gBACL,SAAU,IACV,MAAO,CACR,EACA,CACC,IAAK,aACL,SAAU,IACV,MAAO,EAAQ,OAAO,GAAG,WAAW,UAAW,MAAM,CACtD,CACD,EACA,WAAY,CACX,QAAS,EACV,CACD,CAAC,EACD,GAAI,EAAsB,MAAO,OAAO,EAExC,GAAI,EAAsB,KAAK,SAAW,EACzC,MAAO,CACN,MAAO,IAAA,GACP,KAAM,IAAA,EACP,EAGD,IAAM,EAAW,MAAM,EAAQ,MAAM,SAAS,EAAS,CACtD,MAAO,qBACP,SAAU,EAAsB,KAAK,IAAK,IAAgB,CACzD,cAAe,EAAW,GAC3B,EAAE,CACH,CAAC,EAGD,OAFI,EAAS,MAAc,EAEpB,CACN,MAAO,IAAA,GACP,KAAM,IAAA,EACP,CACD"}