Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
61 lines
2.0 KiB
Plaintext
61 lines
2.0 KiB
Plaintext
export const enforceMaxVersions = async ({ id, collection, global: globalConfig, max, payload, req })=>{
|
|
const entityType = collection ? 'collection' : 'global';
|
|
const slug = collection ? collection.slug : globalConfig?.slug;
|
|
try {
|
|
const where = {};
|
|
let oldestAllowedDoc;
|
|
if (collection) {
|
|
where.parent = {
|
|
equals: id
|
|
};
|
|
const query = await payload.db.findVersions({
|
|
collection: collection.slug,
|
|
limit: 1,
|
|
page: max + 1,
|
|
pagination: false,
|
|
req,
|
|
sort: '-updatedAt',
|
|
where
|
|
});
|
|
[oldestAllowedDoc] = query.docs;
|
|
} else if (globalConfig) {
|
|
const query = await payload.db.findGlobalVersions({
|
|
global: globalConfig.slug,
|
|
limit: 1,
|
|
page: max + 1,
|
|
pagination: false,
|
|
req,
|
|
sort: '-updatedAt',
|
|
where
|
|
});
|
|
[oldestAllowedDoc] = query.docs;
|
|
}
|
|
if (oldestAllowedDoc?.updatedAt) {
|
|
const deleteQuery = {
|
|
updatedAt: {
|
|
less_than_equal: oldestAllowedDoc.updatedAt
|
|
}
|
|
};
|
|
if (collection) {
|
|
deleteQuery.parent = {
|
|
equals: id
|
|
};
|
|
}
|
|
const deleteVersionsArgs = {
|
|
req,
|
|
where: deleteQuery
|
|
};
|
|
if (globalConfig) {
|
|
deleteVersionsArgs.globalSlug = slug;
|
|
} else {
|
|
deleteVersionsArgs.collection = slug;
|
|
}
|
|
await payload.db.deleteVersions(deleteVersionsArgs);
|
|
}
|
|
} catch (err) {
|
|
payload.logger.error(err);
|
|
payload.logger.error(`There was an error cleaning up old versions for the ${entityType} ${slug}`);
|
|
}
|
|
};
|
|
|
|
//# sourceMappingURL=enforceMaxVersions.js.map |