@smartive/datocms-utils 1.0.0 → 1.2.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.
- package/CHANGELOG.md +2 -2
- package/dist/cache-tags.d.ts +2 -2
- package/dist/cache-tags.js +10 -1
- package/dist/cache-tags.js.map +1 -1
- package/package.json +1 -1
- package/src/cache-tags.ts +12 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
# 1.0.0 (2024-09-
|
|
1
|
+
# [1.2.0](https://github.com/smartive/datocms-utils/compare/v1.1.0...v1.2.0) (2024-09-27)
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
### Features
|
|
5
5
|
|
|
6
|
-
*
|
|
6
|
+
* check for empty cachtags ([#4](https://github.com/smartive/datocms-utils/issues/4)) ([e8aa483](https://github.com/smartive/datocms-utils/commit/e8aa483ca9f27462331e08c8e80bd257adca064f))
|
package/dist/cache-tags.d.ts
CHANGED
|
@@ -12,10 +12,10 @@ export declare function parseXCacheTagsResponseHeader(string?: null | string): C
|
|
|
12
12
|
* Generates a unique query ID based on the query document and its variables.
|
|
13
13
|
*
|
|
14
14
|
* @param {DocumentNode} document Query document
|
|
15
|
-
* @param {
|
|
15
|
+
* @param {TVariables} variables Query variables
|
|
16
16
|
* @returns Unique query ID
|
|
17
17
|
*/
|
|
18
|
-
export declare const generateQueryId: (document: DocumentNode, variables?:
|
|
18
|
+
export declare const generateQueryId: <TVariables = unknown>(document: DocumentNode, variables?: TVariables) => string;
|
|
19
19
|
/**
|
|
20
20
|
* Stores the cache tags of a query in the database.
|
|
21
21
|
*
|
package/dist/cache-tags.js
CHANGED
|
@@ -18,7 +18,7 @@ export function parseXCacheTagsResponseHeader(string) {
|
|
|
18
18
|
* Generates a unique query ID based on the query document and its variables.
|
|
19
19
|
*
|
|
20
20
|
* @param {DocumentNode} document Query document
|
|
21
|
-
* @param {
|
|
21
|
+
* @param {TVariables} variables Query variables
|
|
22
22
|
* @returns Unique query ID
|
|
23
23
|
*/
|
|
24
24
|
export const generateQueryId = (document, variables) => {
|
|
@@ -34,6 +34,9 @@ export const generateQueryId = (document, variables) => {
|
|
|
34
34
|
* @param {CacheTag[]} cacheTags Array of cache tags
|
|
35
35
|
*/
|
|
36
36
|
export const storeQueryCacheTags = async (queryId, cacheTags) => {
|
|
37
|
+
if (!cacheTags?.length) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
37
40
|
await sql.query(`INSERT INTO query_cache_tags VALUES ${cacheTags.map((cacheTag) => `('${queryId}', '${cacheTag}')`).join()} ON CONFLICT DO NOTHING`);
|
|
38
41
|
};
|
|
39
42
|
/**
|
|
@@ -43,6 +46,9 @@ export const storeQueryCacheTags = async (queryId, cacheTags) => {
|
|
|
43
46
|
* @returns Array of query IDs
|
|
44
47
|
*/
|
|
45
48
|
export const queriesReferencingCacheTags = async (cacheTags) => {
|
|
49
|
+
if (!cacheTags?.length) {
|
|
50
|
+
return [];
|
|
51
|
+
}
|
|
46
52
|
const { rows } = await sql.query(`SELECT DISTINCT query_id FROM query_cache_tags WHERE cache_tag IN (${cacheTags.map((cacheTag) => `'${cacheTag}'`).join(', ')})`);
|
|
47
53
|
return rows.map((row) => row.query_id);
|
|
48
54
|
};
|
|
@@ -52,6 +58,9 @@ export const queriesReferencingCacheTags = async (cacheTags) => {
|
|
|
52
58
|
* @param {string} queryId Unique query ID
|
|
53
59
|
*/
|
|
54
60
|
export const deleteQueries = async (queryIds) => {
|
|
61
|
+
if (!queryIds?.length) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
55
64
|
await sql.query(`DELETE FROM query_cache_tags WHERE query_id IN (${queryIds.map((id) => `'${id}'`).join(', ')})`);
|
|
56
65
|
};
|
|
57
66
|
//# sourceMappingURL=cache-tags.js.map
|
package/dist/cache-tags.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache-tags.js","sourceRoot":"","sources":["../src/cache-tags.ts"],"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CAAC,GAAG,CAAC,CAAC;AACpH,CAAC,CAAC"}
|
package/package.json
CHANGED
package/src/cache-tags.ts
CHANGED
|
@@ -23,11 +23,11 @@ export function parseXCacheTagsResponseHeader(string?: null | string) {
|
|
|
23
23
|
* Generates a unique query ID based on the query document and its variables.
|
|
24
24
|
*
|
|
25
25
|
* @param {DocumentNode} document Query document
|
|
26
|
-
* @param {
|
|
26
|
+
* @param {TVariables} variables Query variables
|
|
27
27
|
* @returns Unique query ID
|
|
28
28
|
*/
|
|
29
29
|
|
|
30
|
-
export const generateQueryId = (document: DocumentNode, variables?:
|
|
30
|
+
export const generateQueryId = <TVariables = unknown>(document: DocumentNode, variables?: TVariables): string => {
|
|
31
31
|
return createHash('sha1')
|
|
32
32
|
.update(print(document))
|
|
33
33
|
.update(JSON.stringify(variables) || '')
|
|
@@ -42,6 +42,10 @@ export const generateQueryId = (document: DocumentNode, variables?: Record<strin
|
|
|
42
42
|
*/
|
|
43
43
|
|
|
44
44
|
export const storeQueryCacheTags = async (queryId: string, cacheTags: CacheTag[]) => {
|
|
45
|
+
if (!cacheTags?.length) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
45
49
|
await sql.query(
|
|
46
50
|
`INSERT INTO query_cache_tags VALUES ${cacheTags.map((cacheTag) => `('${queryId}', '${cacheTag}')`).join()} ON CONFLICT DO NOTHING`,
|
|
47
51
|
);
|
|
@@ -55,6 +59,9 @@ export const storeQueryCacheTags = async (queryId: string, cacheTags: CacheTag[]
|
|
|
55
59
|
*/
|
|
56
60
|
|
|
57
61
|
export const queriesReferencingCacheTags = async (cacheTags: CacheTag[]): Promise<string[]> => {
|
|
62
|
+
if (!cacheTags?.length) {
|
|
63
|
+
return [];
|
|
64
|
+
}
|
|
58
65
|
const { rows }: { rows: { query_id: string }[] } = await sql.query(
|
|
59
66
|
`SELECT DISTINCT query_id FROM query_cache_tags WHERE cache_tag IN (${cacheTags.map((cacheTag) => `'${cacheTag}'`).join(', ')})`,
|
|
60
67
|
);
|
|
@@ -69,5 +76,8 @@ export const queriesReferencingCacheTags = async (cacheTags: CacheTag[]): Promis
|
|
|
69
76
|
*/
|
|
70
77
|
|
|
71
78
|
export const deleteQueries = async (queryIds: string[]) => {
|
|
79
|
+
if (!queryIds?.length) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
72
82
|
await sql.query(`DELETE FROM query_cache_tags WHERE query_id IN (${queryIds.map((id) => `'${id}'`).join(', ')})`);
|
|
73
83
|
};
|