|
1 | | -# AdminForth Auto Remove Plugin |
| 1 | +# Auto Remove Plugin |
2 | 2 |
|
3 | | -Allows to remove old records. |
| 3 | +This plugin removes records from resources based on **count-based** or **time-based** rules. |
4 | 4 |
|
| 5 | +It is designed for cleaning up: |
| 6 | + |
| 7 | +* old records |
| 8 | +* logs |
| 9 | +* demo/test data |
| 10 | +* temporary entities |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +## Instalation |
| 15 | + |
| 16 | +To install the plugin: |
| 17 | + |
| 18 | +```bash |
| 19 | +npm install @adminforth/auto-remove |
| 20 | +``` |
| 21 | + |
| 22 | +Import it into your resource: |
| 23 | +```bash |
| 24 | +import AutoRemovePlugin from '../../plugins/adminforth-auto-remove/index.js'; |
| 25 | +``` |
| 26 | + |
| 27 | +## Plugin Options |
| 28 | + |
| 29 | +```ts |
| 30 | +export interface PluginOptions { |
| 31 | + createdAtField: string; |
| 32 | + |
| 33 | + /** |
| 34 | + * - count-based: Delete items > maxItems |
| 35 | + * - time-based: Delete age > maxAge |
| 36 | + */ |
| 37 | + mode: AutoRemoveMode; |
| 38 | + |
| 39 | + /** |
| 40 | + * for count-based mode (100', '1k', '10k', '1m') |
| 41 | + */ |
| 42 | + keepAtLeast?: HumanNumber; |
| 43 | + |
| 44 | + /** |
| 45 | + * Minimum number of items to always keep in count-based mode. |
| 46 | + * This acts as a safety threshold together with `keepAtLeast`. |
| 47 | + * Example formats: '100', '1k', '10k', '1m'. |
| 48 | + * |
| 49 | + * Validation ensures that minItemsKeep <= keepAtLeast. |
| 50 | + */ |
| 51 | + minItemsKeep?: HumanNumber; |
| 52 | + |
| 53 | + /** |
| 54 | + * Max age of item for time-based mode ('1d', '7d', '1mon', '1y') |
| 55 | + */ |
| 56 | + deleteOlderThan?: HumanDuration; |
| 57 | + |
| 58 | + /** |
| 59 | + * Interval for running cleanup (e.g. '1h', '1d') |
| 60 | + * Default '1d' |
| 61 | + */ |
| 62 | + interval?: HumanDuration; |
| 63 | +} |
| 64 | +``` |
| 65 | +--- |
| 66 | + |
| 67 | +## Usage |
| 68 | +To use the plugin, add it to your resource file. Here's an example: |
| 69 | + |
| 70 | +for count-based mode |
| 71 | +```ts |
| 72 | +new AutoRemovePlugin({ |
| 73 | + createdAtField: 'created_at', |
| 74 | + mode: 'count-based', |
| 75 | + keepAtLeast: '200', |
| 76 | + interval: '1s', |
| 77 | + minItemsKeep: '180', |
| 78 | + }), |
| 79 | +``` |
| 80 | + |
| 81 | +for time-based mode |
| 82 | +```ts |
| 83 | +new AutoRemovePlugin({ |
| 84 | + createdAtField: 'created_at', |
| 85 | + mode: 'time-based', |
| 86 | + deleteOlderThan: '3min', |
| 87 | + interval: '5s', |
| 88 | + }), |
| 89 | +``` |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +## Result |
| 94 | +After running **AutoRemovePlugin**, old or excess records are deleted automatically: |
| 95 | + |
| 96 | +- **Count-based mode:** keeps the newest `keepAtLeast` records, deletes older ones. |
| 97 | + Example: `keepAtLeast = 500` → table with 650 records deletes 150 oldest. |
| 98 | + |
| 99 | +- **Time-based mode:** deletes records older than `deleteOlderThan`. |
| 100 | + Example: `deleteOlderThan = '7d'` → removes records older than 7 days. |
| 101 | + |
| 102 | +- **Manual cleanup:** `POST /plugin/{pluginInstanceId}/cleanup`, returns `{ "ok": true }`. |
| 103 | + |
| 104 | +Logs show how many records were removed per run. |
0 commit comments