Skip to content

Commit b791072

Browse files
committed
perf(core): improved log deletion performance
1 parent 8c2679b commit b791072

7 files changed

Lines changed: 32 additions & 32 deletions

File tree

src/Constant/Constant.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { LabelFunction, StaticLabel } from 'payload'
2-
import type { AllCollectionHooks } from 'src/types/pluginOptions.js'
32

4-
import type { Duration } from './../utils/toMS.js'
3+
import type { AllCollectionHooks } from './../types/pluginOptions.js'
54

65
import { sharedLogic } from './../core/log-builders/collections/shared.js'
76

@@ -23,8 +22,6 @@ export const defaultCollectionValues: DefaultCollectionValues = {
2322
},
2423
}
2524

26-
export const defaultAutoDeleteLog: Duration = '1mo'
27-
2825
export const defaultPluginOpts = {
2926
collection: {
3027
trackCollections: [],

src/Constant/automation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ManualStrategy } from 'src/types/pluginOptions.js'
1+
import type { ManualStrategy } from './../types/pluginOptions.js'
22

33
// export const defaultAutomationValues =
44
// // PluginOptions['automation']
@@ -13,13 +13,13 @@ import type { ManualStrategy } from 'src/types/pluginOptions.js'
1313
// },
1414
// }
1515

16-
export const cleanupStrategies = {
16+
export const cleanupStrategiesDefaultValues = {
1717
// count: {
1818
// name: 'count',
1919
// amountToDelete: 100,
2020
// deletionCount: 50,
2121
// } as CountStrategy,
22-
manual: { name: 'manual', amount: 200, olderThan: '1w' } as ManualStrategy,
22+
manual: { name: 'manual', amount: 200, olderThan: '1w' } as Required<ManualStrategy>,
2323
// time: {
2424
// name: 'time',
2525
// olderThan: '30s',

src/collections/auditor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { CollectionConfig } from 'payload'
2-
import type { hookTypes } from 'src/pluginUtils/configHelpers.js'
32

43
import type { AuditHookOperationType } from '../types/pluginOptions.js'
4+
import type { hookTypes } from './../pluginUtils/configHelpers.js'
55

66
import { defaultCollectionValues } from '../Constant/Constant.js'
77

src/collections/hooks/beforeChange.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import type { BeforeChangeHook } from 'node_modules/payload/dist/collections/con
22

33
import type { Duration } from './../../utils/toMS.js'
44

5-
import { defaultAutoDeleteLog, defaultCollectionValues } from './../../Constant/Constant.js'
5+
import { cleanupStrategiesDefaultValues } from './../../Constant/automation.js'
6+
import { defaultCollectionValues } from './../../Constant/Constant.js'
67
import ms from './../../utils/toMS.js'
78

89
type AutoLogCleanerProps = {
@@ -17,30 +18,32 @@ export const autoLogCleaner: BeforeChangeHook<AutoLogCleanerProps> = async ({
1718
req,
1819
}) => {
1920
try {
20-
const millisecondsAgo = new Date(Date.now() - ms(data.olderThan || defaultAutoDeleteLog))
21-
const oldLogs = await req.payload.find({
22-
collection: context.pluginOptions.collection?.slug
23-
? context.pluginOptions.collection?.slug
24-
: defaultCollectionValues.slug,
25-
limit: context.pluginOptions.automation?.logCleanup?.strategy?.amount || 100,
21+
const millisecondsAgo = new Date(
22+
Date.now() - ms(data.olderThan || cleanupStrategiesDefaultValues.manual.olderThan),
23+
)
24+
const collectionSlug = context.pluginOptions.collection?.slug ?? defaultCollectionValues.slug
25+
const limit = context.pluginOptions.automation?.logCleanup?.strategy?.amount ?? 100
26+
27+
const oldLogsToDelete = await req.payload.find({
28+
collection: collectionSlug,
29+
limit,
2630
where: {
2731
createdAt: {
2832
less_than: millisecondsAgo.toISOString(),
2933
},
3034
},
3135
})
3236

33-
if (oldLogs.docs.length > 0) {
34-
const deletePromises = oldLogs.docs.map((log) =>
35-
req.payload.delete({
36-
id: log.id,
37-
collection: context.pluginOptions.collection?.slug
38-
? context.pluginOptions.collection?.slug
39-
: defaultCollectionValues.slug,
40-
}),
41-
)
42-
43-
await Promise.all(deletePromises)
37+
const ids = oldLogsToDelete.docs.map((doc) => doc.id)
38+
if (ids.length > 0) {
39+
await req.payload.delete({
40+
collection: collectionSlug,
41+
where: {
42+
id: {
43+
in: ids,
44+
},
45+
},
46+
})
4447
}
4548
} catch (err) {
4649
console.error('Error cleaning old logs:', err)

src/pluginUtils/configHelpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { AllCollectionHooks, PluginOptions } from './../types/pluginOptions
55
import auditor from '../collections/auditor.js'
66
import { bufferManager } from '../core/buffer/bufferManager.js'
77
import { autoLogCleaner } from './../collections/hooks/beforeChange.js'
8-
import { cleanupStrategies } from './../Constant/automation.js'
8+
import { cleanupStrategiesDefaultValues } from './../Constant/automation.js'
99
import { hookMap } from './../Constant/Constant.js'
1010

1111
type AccessOps = 'create' | 'delete' | 'read' | 'update'
@@ -140,7 +140,7 @@ export const attachAutomationConfig = (
140140
data: {
141141
olderThan:
142142
pluginOpts.automation?.logCleanup?.strategy?.olderThan ??
143-
cleanupStrategies.manual.olderThan,
143+
cleanupStrategiesDefaultValues.manual.olderThan,
144144
},
145145
}),
146146
],

src/utils/prettyDebugLog.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { hookTypes } from 'src/pluginUtils/configHelpers.js'
2-
import type { AuditHookOperationType } from 'src/types/pluginOptions.js'
1+
import type { hookTypes } from './../pluginUtils/configHelpers.js'
2+
import type { AuditHookOperationType } from './../types/pluginOptions.js'
33

44
/* eslint-disable no-console */
55
export const prettyDebugLog = (

src/utils/toMS.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { defaultAutoDeleteLog } from './../Constant/Constant.js'
1+
import { cleanupStrategiesDefaultValues } from 'src/Constant/automation.js'
22

33
export type Duration = `${number}${'d' | 'h' | 'm' | 'mo' | 's' | 'w' | 'y'}`
4-
const ms = (duration: Duration = defaultAutoDeleteLog): number => {
4+
const ms = (duration: Duration = cleanupStrategiesDefaultValues.manual.olderThan): number => {
55
const match = /^(\d+)([smhdwy]|mo)$/.exec(duration)
66

77
if (!match) {

0 commit comments

Comments
 (0)