@smartive/datocms-utils 2.3.3 → 3.0.0-next.10

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.
Files changed (46) hide show
  1. package/README.md +134 -56
  2. package/dist/cache-tags/index.d.ts +2 -0
  3. package/dist/cache-tags/index.js +3 -0
  4. package/dist/cache-tags/index.js.map +1 -0
  5. package/dist/cache-tags/provider/neon.d.ts +33 -0
  6. package/dist/cache-tags/provider/neon.js +44 -0
  7. package/dist/cache-tags/provider/neon.js.map +1 -0
  8. package/dist/cache-tags/provider/noop.d.ts +12 -0
  9. package/dist/cache-tags/provider/noop.js +24 -0
  10. package/dist/cache-tags/provider/noop.js.map +1 -0
  11. package/dist/cache-tags/provider/redis.d.ts +33 -0
  12. package/dist/cache-tags/provider/redis.js +71 -0
  13. package/dist/cache-tags/provider/redis.js.map +1 -0
  14. package/dist/cache-tags/types.d.ts +63 -0
  15. package/dist/cache-tags/types.js.map +1 -0
  16. package/dist/{utils.d.ts → cache-tags/utils.d.ts} +1 -1
  17. package/dist/cache-tags/utils.js.map +1 -0
  18. package/dist/classnames.d.ts +1 -1
  19. package/dist/classnames.js +3 -1
  20. package/dist/classnames.js.map +1 -1
  21. package/dist/index.d.ts +2 -4
  22. package/dist/index.js +2 -4
  23. package/dist/index.js.map +1 -1
  24. package/package.json +39 -10
  25. package/src/cache-tags/index.ts +2 -0
  26. package/src/cache-tags/provider/neon.ts +80 -0
  27. package/src/cache-tags/provider/noop.ts +32 -0
  28. package/src/cache-tags/provider/redis.ts +104 -0
  29. package/src/cache-tags/types.ts +66 -0
  30. package/src/{utils.ts → cache-tags/utils.ts} +1 -1
  31. package/src/classnames.ts +7 -1
  32. package/src/index.ts +2 -4
  33. package/dist/cache-tags-redis.d.ts +0 -39
  34. package/dist/cache-tags-redis.js +0 -80
  35. package/dist/cache-tags-redis.js.map +0 -1
  36. package/dist/cache-tags.d.ts +0 -43
  37. package/dist/cache-tags.js +0 -72
  38. package/dist/cache-tags.js.map +0 -1
  39. package/dist/types.d.ts +0 -25
  40. package/dist/types.js.map +0 -1
  41. package/dist/utils.js.map +0 -1
  42. package/src/cache-tags-redis.ts +0 -96
  43. package/src/cache-tags.ts +0 -88
  44. package/src/types.ts +0 -24
  45. /package/dist/{types.js → cache-tags/types.js} +0 -0
  46. /package/dist/{utils.js → cache-tags/utils.js} +0 -0
@@ -1,96 +0,0 @@
1
- import { Redis } from 'ioredis';
2
- import { type CacheTag } from './types';
3
-
4
- let redis: Redis | null = null;
5
-
6
- const getRedis = (): Redis => {
7
- redis ??= new Redis(process.env.REDIS_URL!, {
8
- maxRetriesPerRequest: 3,
9
- lazyConnect: true,
10
- });
11
-
12
- return redis;
13
- };
14
-
15
- const keyPrefix = process.env.REDIS_KEY_PREFIX ? `${process.env.REDIS_KEY_PREFIX}:` : '';
16
-
17
- /**
18
- * Stores the cache tags of a query in Redis.
19
- *
20
- * For each cache tag, adds the query ID to a Redis Set. Sets are unordered
21
- * collections of unique strings, perfect for tracking which queries use which tags.
22
- *
23
- * @param {string} queryId Unique query ID
24
- * @param {CacheTag[]} cacheTags Array of cache tags
25
- *
26
- */
27
- export const storeQueryCacheTags = async (queryId: string, cacheTags: CacheTag[]): Promise<void> => {
28
- if (!cacheTags?.length) {
29
- return;
30
- }
31
-
32
- const redis = getRedis();
33
- const pipeline = redis.pipeline();
34
-
35
- for (const tag of cacheTags) {
36
- pipeline.sadd(`${keyPrefix}${tag}`, queryId);
37
- }
38
-
39
- await pipeline.exec();
40
- };
41
-
42
- /**
43
- * Retrieves the query IDs that reference any of the specified cache tags.
44
- *
45
- * Uses Redis SUNION to efficiently find all queries associated with the given tags.
46
- *
47
- * @param {CacheTag[]} cacheTags Array of cache tags to check
48
- * @returns Array of unique query IDs
49
- *
50
- */
51
- export const queriesReferencingCacheTags = async (cacheTags: CacheTag[]): Promise<string[]> => {
52
- if (!cacheTags?.length) {
53
- return [];
54
- }
55
-
56
- const redis = getRedis();
57
- const keys = cacheTags.map((tag) => `${keyPrefix}${tag}`);
58
-
59
- return redis.sunion(...keys);
60
- };
61
-
62
- /**
63
- * Deletes the specified cache tags from Redis.
64
- *
65
- * This removes the cache tag keys entirely. When queries are revalidated and
66
- * run again, fresh cache tag mappings will be created.
67
- *
68
- * @param {CacheTag[]} cacheTags Array of cache tags to delete
69
- * @returns Number of keys deleted, or null if there was an error
70
- *
71
- */
72
- export const deleteCacheTags = async (cacheTags: CacheTag[]): Promise<number> => {
73
- if (!cacheTags?.length) {
74
- return 0;
75
- }
76
-
77
- const redis = getRedis();
78
- const keys = cacheTags.map((tag) => `${keyPrefix}${tag}`);
79
-
80
- return redis.del(...keys);
81
- };
82
-
83
- /**
84
- * Wipes out all cache tags from Redis.
85
- *
86
- * ⚠️ **Warning**: This will delete all cache tag data. Use with caution!
87
- */
88
- export const truncateCacheTags = async (): Promise<void> => {
89
- const redis = getRedis();
90
- const pattern = `${keyPrefix}*`;
91
- const keys = await redis.keys(pattern);
92
-
93
- if (keys.length > 0) {
94
- await redis.del(...keys);
95
- }
96
- };
package/src/cache-tags.ts DELETED
@@ -1,88 +0,0 @@
1
- import { sql } from '@vercel/postgres';
2
- import { type CacheTag } from './types';
3
-
4
- export { generateQueryId, parseXCacheTagsResponseHeader } from './utils';
5
-
6
- /**
7
- * Stores the cache tags of a query in the database.
8
- *
9
- * @param {string} queryId Unique query ID
10
- * @param {CacheTag[]} cacheTags Array of cache tags
11
- * @param {string} tableId Database table ID
12
- */
13
- export const storeQueryCacheTags = async (queryId: string, cacheTags: CacheTag[], tableId: string) => {
14
- if (!cacheTags?.length) {
15
- return;
16
- }
17
-
18
- const tags = cacheTags.flatMap((_, i) => [queryId, cacheTags[i]]);
19
- const placeholders = cacheTags.map((_, i) => `($${2 * i + 1}, $${2 * i + 2})`).join(',');
20
-
21
- await sql.query(`INSERT INTO ${tableId} VALUES ${placeholders} ON CONFLICT DO NOTHING`, tags);
22
- };
23
-
24
- /**
25
- * Retrieves the queries that reference cache tags.
26
- *
27
- * @param {CacheTag[]} cacheTags Array of cache tags
28
- * @param {string} tableId Database table ID
29
- * @returns Array of query IDs
30
- */
31
- export const queriesReferencingCacheTags = async (cacheTags: CacheTag[], tableId: string): Promise<string[]> => {
32
- if (!cacheTags?.length) {
33
- return [];
34
- }
35
-
36
- const placeholders = cacheTags.map((_, i) => `$${i + 1}`).join(',');
37
-
38
- const { rows }: { rows: { query_id: string }[] } = await sql.query(
39
- `SELECT DISTINCT query_id FROM ${tableId} WHERE cache_tag IN (${placeholders})`,
40
- cacheTags,
41
- );
42
-
43
- return rows.map((row) => row.query_id);
44
- };
45
-
46
- /**
47
- * Deletes the specified cache tags from the database.
48
- *
49
- * This removes the cache tag keys entirely. When queries are revalidated and
50
- * run again, fresh cache tag mappings will be created.
51
- *
52
- * @param {CacheTag[]} cacheTags Array of cache tags to delete
53
- * @param {string} tableId Database table ID
54
- *
55
- */
56
- export const deleteCacheTags = async (cacheTags: CacheTag[], tableId: string) => {
57
- if (cacheTags.length === 0) {
58
- return;
59
- }
60
- const placeholders = cacheTags.map((_, i) => `$${i + 1}`).join(',');
61
-
62
- await sql.query(`DELETE FROM ${tableId} WHERE cache_tag IN (${placeholders})`, cacheTags);
63
- };
64
-
65
- /**
66
- * Deletes the cache tags of a query from the database.
67
- *
68
- * @param {string} queryId Unique query ID
69
- * @param {string} tableId Database table ID
70
- * @deprecated Use `deleteCacheTags` instead.
71
- */
72
- export const deleteQueries = async (queryIds: string[], tableId: string) => {
73
- if (!queryIds?.length) {
74
- return;
75
- }
76
- const placeholders = queryIds.map((_, i) => `$${i + 1}`).join(',');
77
-
78
- await sql.query(`DELETE FROM ${tableId} WHERE query_id IN (${placeholders})`, queryIds);
79
- };
80
-
81
- /**
82
- * Wipes out all cache tags from the database.
83
- *
84
- * @param {string} tableId Database table ID
85
- */
86
- export async function truncateCacheTags(tableId: string) {
87
- await sql.query(`DELETE FROM ${tableId}`);
88
- }
package/src/types.ts DELETED
@@ -1,24 +0,0 @@
1
- /**
2
- * A branded type for cache tags. This is created by intersecting `string`
3
- * with `{ readonly _: unique symbol }`, making it a unique type.
4
- * Although it is fundamentally a string, it is treated as a distinct type
5
- * due to the unique symbol.
6
- */
7
- export type CacheTag = string & { readonly _: unique symbol };
8
-
9
- /**
10
- * A type representing the structure of a webhook payload for cache tag invalidation.
11
- * It includes the entity type, event type, and the entity details which contain
12
- * the cache tags to be invalidated.
13
- */
14
- export type CacheTagsInvalidateWebhook = {
15
- entity_type: 'cda_cache_tags';
16
- event_type: 'invalidate';
17
- entity: {
18
- id: 'cda_cache_tags';
19
- type: 'cda_cache_tags';
20
- attributes: {
21
- tags: CacheTag[];
22
- };
23
- };
24
- };
File without changes
File without changes