payloadcms-cloudflare-kv-plugin 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,682 @@
1
+ import { getCacheOptions, getFromCache, invalidateByPattern, setInCache } from './cache.js';
2
+ import { debugLog, DEFAULT_TTL, generateCacheKey, getCollectionPattern, getGlobalPattern, shouldCacheCollection } from './utils.js';
3
+ export function dbAdapterWithCache({ baseAdapter, kv, config = {
4
+ defaultCacheOptions: {
5
+ ttl: DEFAULT_TTL
6
+ },
7
+ kv: {}
8
+ } }) {
9
+ return {
10
+ ...baseAdapter,
11
+ count: async (args)=>{
12
+ const { collection } = args;
13
+ const cache = getCacheOptions({
14
+ slug: collection,
15
+ args,
16
+ config
17
+ });
18
+ if (cache?.skip || !shouldCacheCollection({
19
+ slug: collection,
20
+ config
21
+ })) {
22
+ debugLog({
23
+ config,
24
+ message: `Cache SKIP: count ${collection}`
25
+ });
26
+ return baseAdapter.count(args);
27
+ }
28
+ const cacheKey = generateCacheKey({
29
+ slug: collection,
30
+ args,
31
+ config,
32
+ operation: 'count'
33
+ });
34
+ const cached = await getFromCache({
35
+ key: cacheKey,
36
+ kv
37
+ });
38
+ if (cached) {
39
+ debugLog({
40
+ config,
41
+ message: `Cache HIT: count ${collection}`
42
+ });
43
+ return cached;
44
+ }
45
+ const result = await baseAdapter.count(args);
46
+ await setInCache({
47
+ data: result,
48
+ key: cacheKey,
49
+ kv,
50
+ ttl: cache?.ttl ?? DEFAULT_TTL
51
+ });
52
+ debugLog({
53
+ config,
54
+ message: `Cache MISS: count ${collection}`
55
+ });
56
+ return result;
57
+ },
58
+ countGlobalVersions: async (args)=>{
59
+ const { global } = args;
60
+ const cache = getCacheOptions({
61
+ slug: global,
62
+ args,
63
+ config
64
+ });
65
+ if (cache?.skip || !shouldCacheCollection({
66
+ slug: global,
67
+ config
68
+ })) {
69
+ debugLog({
70
+ config,
71
+ message: `Cache SKIP: countGlobalVersions ${global}`
72
+ });
73
+ return baseAdapter.countGlobalVersions(args);
74
+ }
75
+ const cacheKey = generateCacheKey({
76
+ slug: global,
77
+ args,
78
+ config,
79
+ operation: 'countGlobalVersions',
80
+ versions: true
81
+ });
82
+ const cached = await getFromCache({
83
+ key: cacheKey,
84
+ kv
85
+ });
86
+ if (cached) {
87
+ debugLog({
88
+ config,
89
+ message: `Cache HIT: countGlobalVersions ${global}`
90
+ });
91
+ return cached;
92
+ }
93
+ const result = await baseAdapter.countGlobalVersions(args);
94
+ await setInCache({
95
+ data: result,
96
+ key: cacheKey,
97
+ kv,
98
+ ttl: cache?.ttl ?? DEFAULT_TTL
99
+ });
100
+ debugLog({
101
+ config,
102
+ message: `Cache MISS: countGlobalVersions ${global}`
103
+ });
104
+ return result;
105
+ },
106
+ countVersions: async (args)=>{
107
+ const { collection } = args;
108
+ const cache = getCacheOptions({
109
+ slug: collection,
110
+ args,
111
+ config
112
+ });
113
+ if (cache?.skip || !shouldCacheCollection({
114
+ slug: collection,
115
+ config
116
+ })) {
117
+ debugLog({
118
+ config,
119
+ message: `Cache SKIP: countVersions ${collection}`
120
+ });
121
+ return baseAdapter.countVersions(args);
122
+ }
123
+ const cacheKey = generateCacheKey({
124
+ slug: collection,
125
+ args,
126
+ config,
127
+ operation: 'countVersions',
128
+ versions: true
129
+ });
130
+ const cached = await getFromCache({
131
+ key: cacheKey,
132
+ kv
133
+ });
134
+ if (cached) {
135
+ debugLog({
136
+ config,
137
+ message: `Cache HIT: countVersions ${collection}`
138
+ });
139
+ return cached;
140
+ }
141
+ const result = await baseAdapter.countVersions(args);
142
+ await setInCache({
143
+ data: result,
144
+ key: cacheKey,
145
+ kv,
146
+ ttl: cache?.ttl ?? DEFAULT_TTL
147
+ });
148
+ debugLog({
149
+ config,
150
+ message: `Cache MISS: countVersions ${collection}`
151
+ });
152
+ return result;
153
+ },
154
+ create: async (args)=>{
155
+ const { collection } = args;
156
+ const result = await baseAdapter.create(args);
157
+ const cache = getCacheOptions({
158
+ slug: collection,
159
+ args,
160
+ config
161
+ });
162
+ if (cache?.skip || !shouldCacheCollection({
163
+ slug: collection,
164
+ config
165
+ })) {
166
+ debugLog({
167
+ config,
168
+ message: `Cache SKIP: create ${collection}`
169
+ });
170
+ return result;
171
+ }
172
+ const pattern = getCollectionPattern({
173
+ collection,
174
+ config
175
+ });
176
+ await invalidateByPattern({
177
+ kv,
178
+ pattern
179
+ });
180
+ debugLog({
181
+ config,
182
+ message: `Cache INVALIDATE: create ${collection}`
183
+ });
184
+ return result;
185
+ },
186
+ deleteMany: async (args)=>{
187
+ const { collection } = args;
188
+ const result = await baseAdapter.deleteMany(args);
189
+ const cache = getCacheOptions({
190
+ slug: collection,
191
+ args,
192
+ config
193
+ });
194
+ if (cache?.skip || !shouldCacheCollection({
195
+ slug: collection,
196
+ config
197
+ })) {
198
+ debugLog({
199
+ config,
200
+ message: `Cache SKIP: deleteMany ${collection}`
201
+ });
202
+ return result;
203
+ }
204
+ const pattern = getCollectionPattern({
205
+ collection,
206
+ config
207
+ });
208
+ await invalidateByPattern({
209
+ kv,
210
+ pattern
211
+ });
212
+ debugLog({
213
+ config,
214
+ message: `Cache INVALIDATE: deleteMany ${collection}`
215
+ });
216
+ return result;
217
+ },
218
+ deleteOne: async (args)=>{
219
+ const { collection } = args;
220
+ const result = await baseAdapter.deleteOne(args);
221
+ const cache = getCacheOptions({
222
+ slug: collection,
223
+ args,
224
+ config
225
+ });
226
+ if (cache?.skip || !shouldCacheCollection({
227
+ slug: collection,
228
+ config
229
+ })) {
230
+ debugLog({
231
+ config,
232
+ message: `Cache SKIP: deleteOne ${collection}`
233
+ });
234
+ return result;
235
+ }
236
+ const pattern = getCollectionPattern({
237
+ collection,
238
+ config
239
+ });
240
+ await invalidateByPattern({
241
+ kv,
242
+ pattern
243
+ });
244
+ debugLog({
245
+ config,
246
+ message: `Cache INVALIDATE: deleteOne ${collection}`
247
+ });
248
+ return result;
249
+ },
250
+ deleteVersions: async (args)=>{
251
+ const { collection } = args;
252
+ const result = await baseAdapter.deleteVersions(args);
253
+ const cache = getCacheOptions({
254
+ slug: collection,
255
+ args,
256
+ config
257
+ });
258
+ if (cache?.skip || !shouldCacheCollection({
259
+ slug: collection,
260
+ config
261
+ })) {
262
+ debugLog({
263
+ config,
264
+ message: `Cache SKIP: deleteVersions ${collection}`
265
+ });
266
+ return result;
267
+ }
268
+ const pattern = getCollectionPattern({
269
+ collection,
270
+ config
271
+ });
272
+ await invalidateByPattern({
273
+ kv,
274
+ pattern
275
+ });
276
+ debugLog({
277
+ config,
278
+ message: `Cache INVALIDATE: deleteVersions ${collection}`
279
+ });
280
+ return result;
281
+ },
282
+ find: async (args)=>{
283
+ const { collection } = args;
284
+ const cache = getCacheOptions({
285
+ slug: collection,
286
+ args,
287
+ config
288
+ });
289
+ if (cache?.skip || !shouldCacheCollection({
290
+ slug: collection,
291
+ config
292
+ })) {
293
+ debugLog({
294
+ config,
295
+ message: `Cache SKIP: find ${collection}`
296
+ });
297
+ return baseAdapter.find(args);
298
+ }
299
+ const cacheKey = generateCacheKey({
300
+ slug: collection,
301
+ args,
302
+ config,
303
+ operation: 'find'
304
+ });
305
+ const cached = await getFromCache({
306
+ key: cacheKey,
307
+ kv
308
+ });
309
+ if (cached) {
310
+ debugLog({
311
+ config,
312
+ message: `Cache HIT: find ${collection}`
313
+ });
314
+ return cached;
315
+ }
316
+ const result = await baseAdapter.find(args);
317
+ await setInCache({
318
+ data: result,
319
+ key: cacheKey,
320
+ kv,
321
+ ttl: cache?.ttl ?? DEFAULT_TTL
322
+ });
323
+ debugLog({
324
+ config,
325
+ message: `Cache MISS: find ${collection}`
326
+ });
327
+ return result;
328
+ },
329
+ findGlobal: async (args)=>{
330
+ const { slug } = args;
331
+ const cache = getCacheOptions({
332
+ slug,
333
+ args,
334
+ config
335
+ });
336
+ if (cache?.skip || !shouldCacheCollection({
337
+ slug,
338
+ config
339
+ })) {
340
+ debugLog({
341
+ config,
342
+ message: `Cache SKIP: findGlobal ${slug}`
343
+ });
344
+ return baseAdapter.findGlobal(args);
345
+ }
346
+ const cacheKey = generateCacheKey({
347
+ slug,
348
+ args,
349
+ config,
350
+ operation: 'findGlobal'
351
+ });
352
+ const cached = await getFromCache({
353
+ key: cacheKey,
354
+ kv
355
+ });
356
+ if (cached) {
357
+ debugLog({
358
+ config,
359
+ message: `Cache HIT: findGlobal ${slug}`
360
+ });
361
+ return cached;
362
+ }
363
+ const result = await baseAdapter.findGlobal(args);
364
+ await setInCache({
365
+ data: result,
366
+ key: cacheKey,
367
+ kv,
368
+ ttl: cache?.ttl ?? DEFAULT_TTL
369
+ });
370
+ debugLog({
371
+ config,
372
+ message: `Cache MISS: findGlobal ${slug}`
373
+ });
374
+ return result;
375
+ },
376
+ findGlobalVersions: async (args)=>{
377
+ const { global } = args;
378
+ const cache = getCacheOptions({
379
+ slug: global,
380
+ args,
381
+ config
382
+ });
383
+ if (cache?.skip || !shouldCacheCollection({
384
+ slug: global,
385
+ config
386
+ })) {
387
+ debugLog({
388
+ config,
389
+ message: `Cache SKIP: findGlobalVersions ${global}`
390
+ });
391
+ return baseAdapter.findGlobalVersions(args);
392
+ }
393
+ const cacheKey = generateCacheKey({
394
+ slug: global,
395
+ args,
396
+ config,
397
+ operation: 'findGlobalVersions',
398
+ versions: true
399
+ });
400
+ const cached = await getFromCache({
401
+ key: cacheKey,
402
+ kv
403
+ });
404
+ if (cached) {
405
+ debugLog({
406
+ config,
407
+ message: `Cache HIT: findGlobalVersions ${global}`
408
+ });
409
+ return cached;
410
+ }
411
+ const result = await baseAdapter.findGlobalVersions(args);
412
+ await setInCache({
413
+ data: result,
414
+ key: cacheKey,
415
+ kv,
416
+ ttl: cache?.ttl ?? DEFAULT_TTL
417
+ });
418
+ debugLog({
419
+ config,
420
+ message: `Cache MISS: findGlobalVersions ${global}`
421
+ });
422
+ return result;
423
+ },
424
+ findOne: async (args)=>{
425
+ const { collection } = args;
426
+ const cache = getCacheOptions({
427
+ slug: collection,
428
+ args,
429
+ config
430
+ });
431
+ if (cache?.skip || !shouldCacheCollection({
432
+ slug: collection,
433
+ config
434
+ })) {
435
+ debugLog({
436
+ config,
437
+ message: `Cache SKIP: findOne ${collection}`
438
+ });
439
+ return baseAdapter.findOne(args);
440
+ }
441
+ const cacheKey = generateCacheKey({
442
+ slug: collection,
443
+ args,
444
+ config,
445
+ operation: 'findOne'
446
+ });
447
+ const cached = await getFromCache({
448
+ key: cacheKey,
449
+ kv
450
+ });
451
+ if (cached) {
452
+ debugLog({
453
+ config,
454
+ message: `Cache HIT: findOne ${collection}`
455
+ });
456
+ return cached;
457
+ }
458
+ const result = await baseAdapter.findOne(args);
459
+ await setInCache({
460
+ data: result,
461
+ key: cacheKey,
462
+ kv,
463
+ ttl: cache?.ttl ?? DEFAULT_TTL
464
+ });
465
+ debugLog({
466
+ config,
467
+ message: `Cache MISS: findOne ${collection}`
468
+ });
469
+ return result;
470
+ },
471
+ queryDrafts: async (args)=>{
472
+ const { collection } = args;
473
+ const cache = getCacheOptions({
474
+ slug: collection,
475
+ args,
476
+ config
477
+ });
478
+ if (cache?.skip || !shouldCacheCollection({
479
+ slug: collection,
480
+ config
481
+ })) {
482
+ debugLog({
483
+ config,
484
+ message: `Cache SKIP: queryDrafts ${collection}`
485
+ });
486
+ return baseAdapter.queryDrafts(args);
487
+ }
488
+ const cacheKey = generateCacheKey({
489
+ slug: collection,
490
+ args,
491
+ config,
492
+ operation: 'queryDrafts'
493
+ });
494
+ const cached = await getFromCache({
495
+ key: cacheKey,
496
+ kv
497
+ });
498
+ if (cached) {
499
+ debugLog({
500
+ config,
501
+ message: `Cache HIT: queryDrafts ${collection}`
502
+ });
503
+ return cached;
504
+ }
505
+ const result = await baseAdapter.queryDrafts(args);
506
+ await setInCache({
507
+ data: result,
508
+ key: cacheKey,
509
+ kv,
510
+ ttl: cache?.ttl ?? DEFAULT_TTL
511
+ });
512
+ debugLog({
513
+ config,
514
+ message: `Cache MISS: queryDrafts ${collection}`
515
+ });
516
+ return result;
517
+ },
518
+ updateGlobal: async (args)=>{
519
+ const { slug } = args;
520
+ const result = await baseAdapter.updateGlobal(args);
521
+ const cache = getCacheOptions({
522
+ slug,
523
+ args,
524
+ config
525
+ });
526
+ if (cache?.skip || !shouldCacheCollection({
527
+ slug,
528
+ config
529
+ })) {
530
+ debugLog({
531
+ config,
532
+ message: `Cache SKIP: updateGlobal ${slug}`
533
+ });
534
+ return result;
535
+ }
536
+ const pattern = getGlobalPattern({
537
+ config,
538
+ global: slug
539
+ });
540
+ await invalidateByPattern({
541
+ kv,
542
+ pattern
543
+ });
544
+ debugLog({
545
+ config,
546
+ message: `Cache INVALIDATE: updateGlobal ${slug}`
547
+ });
548
+ return result;
549
+ },
550
+ updateGlobalVersion: async (args)=>{
551
+ const { global } = args;
552
+ const result = await baseAdapter.updateGlobalVersion(args);
553
+ const cache = getCacheOptions({
554
+ slug: global,
555
+ args,
556
+ config
557
+ });
558
+ if (cache?.skip || !shouldCacheCollection({
559
+ slug: global,
560
+ config
561
+ })) {
562
+ debugLog({
563
+ config,
564
+ message: `Cache SKIP: updateGlobalVersion ${global}`
565
+ });
566
+ return result;
567
+ }
568
+ const pattern = getGlobalPattern({
569
+ config,
570
+ global: args.global,
571
+ versions: true
572
+ });
573
+ await invalidateByPattern({
574
+ kv,
575
+ pattern
576
+ });
577
+ debugLog({
578
+ config,
579
+ message: `Cache INVALIDATE: updateGlobalVersion ${global}`
580
+ });
581
+ return result;
582
+ },
583
+ updateMany: async (args)=>{
584
+ const { collection } = args;
585
+ const result = await baseAdapter.updateMany(args);
586
+ const cache = getCacheOptions({
587
+ slug: collection,
588
+ args,
589
+ config
590
+ });
591
+ if (cache?.skip || !shouldCacheCollection({
592
+ slug: collection,
593
+ config
594
+ })) {
595
+ debugLog({
596
+ config,
597
+ message: `Cache SKIP: updateMany ${collection}`
598
+ });
599
+ return result;
600
+ }
601
+ const pattern = getCollectionPattern({
602
+ collection: args.collection,
603
+ config
604
+ });
605
+ await invalidateByPattern({
606
+ kv,
607
+ pattern
608
+ });
609
+ debugLog({
610
+ config,
611
+ message: `Cache INVALIDATE: updateMany ${collection}`
612
+ });
613
+ return result;
614
+ },
615
+ updateOne: async (args)=>{
616
+ const { collection } = args;
617
+ const result = await baseAdapter.updateOne(args);
618
+ const cache = getCacheOptions({
619
+ slug: collection,
620
+ args,
621
+ config
622
+ });
623
+ if (cache?.skip || !shouldCacheCollection({
624
+ slug: collection,
625
+ config
626
+ })) {
627
+ debugLog({
628
+ config,
629
+ message: `Cache SKIP: updateOne ${collection}`
630
+ });
631
+ return result;
632
+ }
633
+ const pattern = getCollectionPattern({
634
+ collection: args.collection,
635
+ config
636
+ });
637
+ await invalidateByPattern({
638
+ kv,
639
+ pattern
640
+ });
641
+ debugLog({
642
+ config,
643
+ message: `Cache INVALIDATE: updateOne ${collection}`
644
+ });
645
+ return result;
646
+ },
647
+ upsert: async (args)=>{
648
+ const { collection } = args;
649
+ const result = await baseAdapter.upsert(args);
650
+ const cache = getCacheOptions({
651
+ slug: collection,
652
+ args,
653
+ config
654
+ });
655
+ if (cache?.skip || !shouldCacheCollection({
656
+ slug: collection,
657
+ config
658
+ })) {
659
+ debugLog({
660
+ config,
661
+ message: `Cache SKIP: upsert ${collection}`
662
+ });
663
+ return result;
664
+ }
665
+ const pattern = getCollectionPattern({
666
+ collection: args.collection,
667
+ config
668
+ });
669
+ await invalidateByPattern({
670
+ kv,
671
+ pattern
672
+ });
673
+ debugLog({
674
+ config,
675
+ message: `Cache INVALIDATE: upsert ${collection}`
676
+ });
677
+ return result;
678
+ }
679
+ };
680
+ }
681
+
682
+ //# sourceMappingURL=adapter.js.map