|
1 | | -# Many2Many |
| 1 | +# Auto Remove Plugin |
2 | 2 |
|
3 | | -This plugin lets you manage many-to-many relationships. |
| 3 | +This plugin removes records from resources based on **count-based** or **time-based** rules. |
4 | 4 |
|
5 | | -## Installation |
| 5 | +It is designed for cleaning up: |
6 | 6 |
|
7 | | -Install the plugin: |
| 7 | +* old records |
| 8 | +* logs |
| 9 | +* demo/test data |
| 10 | +* temporary entities |
8 | 11 |
|
9 | | -```bash |
10 | | -npm i @adminforth/many2many |
11 | | -``` |
12 | | - |
13 | | -## Setting up |
14 | | - |
15 | | -Let's say we want to implement a relationship where every apartment can have many realtors and each realtor can have many apartments. |
16 | | -We'll also need a junction resource to connect realtors and apartments. |
17 | | - |
18 | | -First, create the `realtors` table and the junction table `realtorsAparts`: |
19 | | - |
20 | | - |
21 | | -```ts title="./schema.prisma" |
22 | | -//diff-add |
23 | | -model realtors { |
24 | | - //diff-add |
25 | | - id String @id |
26 | | - //diff-add |
27 | | - name String |
28 | | - //diff-add |
29 | | - surname String |
30 | | - //diff-add |
31 | | -} |
32 | | -//diff-add |
33 | | -model realtorsAparts { |
34 | | - //diff-add |
35 | | - id String @id |
36 | | - //diff-add |
37 | | - realtorId String |
38 | | - //diff-add |
39 | | - apartmentId String |
40 | | - //diff-add |
41 | | -} |
42 | | -``` |
43 | | - |
44 | | -Migrate the Prisma schema: |
45 | | - |
46 | | -```bash |
47 | | -npm run makemigration -- --name add-realtors-and-realtorsAparts; npm run migrate:local |
48 | | -``` |
49 | | - |
50 | | -Now create a resource for the realtors: |
51 | | - |
52 | | -```ts title="./resources/realtors.ts" |
53 | | -import { AdminForthDataTypes, AdminForthResourceInput } from 'adminforth'; |
| 12 | +--- |
54 | 13 |
|
55 | | -export default { |
56 | | - dataSource: 'maindb', |
57 | | - table: 'realtors', |
58 | | - resourceId: 'realtors', |
59 | | - label: 'Realtors', |
60 | | - recordLabel: (r) => ` ${r.name}`, |
61 | | - columns: [ |
62 | | - { |
63 | | - name: 'id', |
64 | | - type: AdminForthDataTypes.STRING, |
65 | | - label: 'Identifier', |
66 | | - showIn: { |
67 | | - list: false, |
68 | | - edit: false, |
69 | | - create: false, |
70 | | - }, |
71 | | - primaryKey: true, |
72 | | - fillOnCreate: ({ initialRecord, adminUser }) => Math.random().toString(36).substring(7), |
73 | | - }, |
74 | | - { |
75 | | - name: 'name', |
76 | | - required: true, |
77 | | - showIn: { all: true }, |
78 | | - type: AdminForthDataTypes.STRING, |
79 | | - maxLength: 255, |
80 | | - minLength: 3, |
81 | | - }, |
82 | | - { |
83 | | - name: "surname", |
84 | | - required: true, |
85 | | - showIn: { all: true }, |
86 | | - type: AdminForthDataTypes.STRING, |
87 | | - maxLength: 100, |
88 | | - minLength: 3, |
89 | | - } |
90 | | - ], |
91 | | - options: { |
92 | | - listPageSize: 12, |
93 | | - allowedActions: { |
94 | | - edit: true, |
95 | | - delete: true, |
96 | | - show: true, |
97 | | - filter: true, |
98 | | - }, |
99 | | - }, |
100 | | -} as AdminForthResourceInput; |
101 | | -``` |
102 | | - |
103 | | -And one for the junction table `realtorsAparts`: |
| 14 | +## Instalation |
104 | 15 |
|
105 | | -```ts title="./resources/realtorsAparts.ts" |
106 | | -import { AdminForthDataTypes, AdminForthResourceInput } from 'adminforth'; |
| 16 | +To install the plugin: |
107 | 17 |
|
108 | | -export default { |
109 | | - dataSource: 'maindb', |
110 | | - table: 'realtorsAparts', |
111 | | - resourceId: 'realtorsAparts', |
112 | | - label: 'Realtors-aparts', |
113 | | - columns: [ |
114 | | - { |
115 | | - name: 'id', |
116 | | - type: AdminForthDataTypes.STRING, |
117 | | - label: 'Identifier', |
118 | | - showIn: { |
119 | | - list: false, |
120 | | - edit: false, |
121 | | - create: false, |
122 | | - }, |
123 | | - primaryKey: true, |
124 | | - fillOnCreate: ({ initialRecord, adminUser }) => Math.random().toString(36).substring(7), |
125 | | - }, |
126 | | - { |
127 | | - name: 'realtorId', |
128 | | - foreignResource: { |
129 | | - resourceId: 'realtors', |
130 | | - searchableFields: ['name'], |
131 | | - searchIsCaseSensitive: true |
132 | | - } |
133 | | - }, |
134 | | - { |
135 | | - name: 'apartmentId', |
136 | | - foreignResource: { |
137 | | - resourceId: 'aparts', |
138 | | - searchableFields: ['title'], |
139 | | - searchIsCaseSensitive: true |
140 | | - } |
141 | | - }, |
142 | | - ], |
143 | | - options: { |
144 | | - listPageSize: 12, |
145 | | - allowedActions: { |
146 | | - edit: true, |
147 | | - delete: true, |
148 | | - show: true, |
149 | | - filter: true, |
150 | | - }, |
151 | | - }, |
152 | | -} as AdminForthResourceInput; |
| 18 | +```bash |
| 19 | +npm install @adminforth/auto-remove |
153 | 20 | ``` |
154 | 21 |
|
155 | | -Now add the plugin resources to the main config file: |
156 | | - |
157 | | -```ts title="./index.ts" |
158 | | - |
159 | | -//diff-add |
160 | | -import realtorsResource from './resources/realtors.js'; |
161 | | -//diff-add |
162 | | -import realtorsApartsResource from './resources/realtorsAparts.js'; |
163 | | - |
164 | | - ... |
165 | | - |
166 | | - |
167 | | -dataSources: [ |
168 | | - { |
169 | | - id: 'maindb', |
170 | | - url: `${process.env.DATABASE_URL}` |
171 | | - }, |
172 | | - ], |
173 | | - resources: [ |
174 | | - ... |
175 | | - //diff-add |
176 | | - realtorsResource, |
177 | | - //diff-add |
178 | | - realtorsApartsResource |
179 | | - ] |
180 | | - |
181 | | - ... |
182 | | - |
183 | | - menu: [ |
184 | | - |
185 | | - ... |
186 | | - //diff-add |
187 | | - { |
188 | | - //diff-add |
189 | | - label: 'Realtors', |
190 | | - //diff-add |
191 | | - resourceId: 'realtors' |
192 | | - //diff-add |
193 | | - }, |
194 | | - //diff-add |
195 | | - { |
196 | | - //diff-add |
197 | | - label: 'Realtors-aparts', |
198 | | - //diff-add |
199 | | - resourceId: 'realtorsAparts' |
200 | | - //diff-add |
201 | | - }, |
202 | | - |
203 | | - ... |
204 | | - |
205 | | - ] |
206 | | - |
| 22 | +Import it into your resource: |
| 23 | +```bash |
| 24 | +import AutoRemovePlugin from '../../plugins/adminforth-auto-remove/index.js'; |
207 | 25 | ``` |
208 | 26 |
|
209 | | -Finally, add the plugin to the `apartments` resource: |
210 | | - |
211 | | - |
212 | | -```ts title="./resources/apartments.ts" |
213 | | -//diff-add |
214 | | -import Many2ManyPlugin from '@adminforth/many2many'; |
215 | | - |
216 | | - ... |
217 | | - |
218 | | - plugins: [ |
219 | | - ... |
220 | | - |
221 | | - //diff-add |
222 | | - new Many2ManyPlugin({ |
223 | | - //diff-add |
224 | | - linkedResourceId: 'realtors', |
225 | | - //diff-add |
226 | | - }) |
227 | | - |
228 | | - ... |
229 | | - ] |
230 | | - |
231 | | - ... |
| 27 | +## Plugin Options |
232 | 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 | +} |
233 | 64 | ``` |
| 65 | +--- |
234 | 66 |
|
| 67 | +## Usage |
| 68 | +To use the plugin, add it to your resource file. Here's an example: |
235 | 69 |
|
236 | | -The plugin is set up. Create some records in the `realtors` table: |
237 | | - |
238 | | -Now, when creating an apartment, you can select (link) one or more realtors. |
239 | | - |
240 | | -After saving the record, rows in the junction table are created automatically: |
241 | | - |
242 | | - |
243 | | - |
244 | | -## Disable automatic cleanup of the junction table |
245 | | -By default, when you delete a realtor or an apartment, all related rows in the junction table are deleted automatically. To avoid this behavior, add: |
246 | | - |
| 70 | +for count-based mode |
247 | 71 | ```ts |
248 | | - |
249 | | - ... |
250 | | - |
251 | | - new Many2ManyPlugin({ |
252 | | - linkedResourceId: 'realtors', |
253 | | - //diff-add |
254 | | - dontDeleteJunctionRecords: true, // prevents automatic deletion of related junction rows |
255 | | - }) |
256 | | - |
257 | | - ... |
258 | | - |
| 72 | +new AutoRemovePlugin({ |
| 73 | + createdAtField: 'created_at', |
| 74 | + mode: 'count-based', |
| 75 | + keepAtLeast: '200', |
| 76 | + interval: '1s', |
| 77 | + minItemsKeep: '180', |
| 78 | + }), |
259 | 79 | ``` |
260 | 80 |
|
261 | | -## Making editable fields for the both resources |
262 | | - |
263 | | -There might be cases, when you want to make fields editable on both resources. For these cases you can just create second instance of plugin on the second resource and it will allow you to use this plugin from the both resources. |
264 | | - |
265 | | -```ts title="./resources/apartments.ts" |
266 | | -//diff-add |
267 | | -import Many2ManyPlugin from '@adminforth/many2many'; |
268 | | - |
269 | | - ... |
270 | | - |
271 | | - plugins: [ |
272 | | - ... |
273 | | - |
274 | | - //diff-add |
275 | | - new Many2ManyPlugin({ |
276 | | - //diff-add |
277 | | - linkedResourceId: 'realtors', |
278 | | - //diff-add |
279 | | - }) |
280 | | - |
281 | | - ... |
282 | | - ] |
283 | | - |
284 | | - ... |
285 | | - |
| 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 | + }), |
286 | 89 | ``` |
287 | 90 |
|
| 91 | +--- |
288 | 92 |
|
289 | | -and |
290 | | - |
291 | | - |
292 | | -```ts title="./resources/realtors.ts" |
293 | | -//diff-add |
294 | | -import Many2ManyPlugin from '@adminforth/many2many'; |
295 | | - |
296 | | - ... |
297 | | - |
298 | | - plugins: [ |
299 | | - ... |
| 93 | +## Result |
| 94 | +After running **AutoRemovePlugin**, old or excess records are deleted automatically: |
300 | 95 |
|
301 | | - //diff-add |
302 | | - new Many2ManyPlugin({ |
303 | | - //diff-add |
304 | | - linkedResourceId: 'aparts', |
305 | | - //diff-add |
306 | | - }) |
| 96 | +- **Count-based mode:** keeps the newest `keepAtLeast` records, deletes older ones. |
| 97 | + Example: `keepAtLeast = 500` → table with 650 records deletes 150 oldest. |
307 | 98 |
|
308 | | - ... |
309 | | - ] |
| 99 | +- **Time-based mode:** deletes records older than `deleteOlderThan`. |
| 100 | + Example: `deleteOlderThan = '7d'` → removes records older than 7 days. |
310 | 101 |
|
311 | | - ... |
| 102 | +- **Manual cleanup:** `POST /plugin/{pluginInstanceId}/cleanup`, returns `{ "ok": true }`. |
312 | 103 |
|
313 | | -``` |
| 104 | +Logs show how many records were removed per run. |
0 commit comments