slonik-interceptor-query-cache 2.2.0 → 2.4.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/README.md +1 -1
- package/dist/src/factories/createQueryCacheInterceptor.js +1 -1
- package/dist/src/utilities/extractCacheAttributes.d.ts +2 -1
- package/dist/src/utilities/extractCacheAttributes.js +6 -2
- package/package.json +1 -1
- package/src/factories/createQueryCacheInterceptor.ts +1 -1
- package/src/utilities/extractCacheAttributes.ts +13 -2
package/README.md
CHANGED
|
@@ -17,7 +17,6 @@ Which queries are cached is controlled using cache attributes. Cache attributes
|
|
|
17
17
|
## Behavior
|
|
18
18
|
|
|
19
19
|
* Does not cache queries inside of a transaction.
|
|
20
|
-
* Does not take into account the query parameters.
|
|
21
20
|
|
|
22
21
|
## Cache attributes
|
|
23
22
|
|
|
@@ -25,6 +24,7 @@ Which queries are cached is controlled using cache attributes. Cache attributes
|
|
|
25
24
|
|---|---|---|---|
|
|
26
25
|
|`@cache-ttl`|Number (in seconds) to cache the query for.|Yes|N/A|
|
|
27
26
|
|`@cache-key`|Key (`/^[A-Za-z0-9\-_:]+$/`) that uniquely identifies the query.|Yes|N/A|
|
|
27
|
+
|`@cache-hash-values`|Sets this value to `false` to ignore values and only use `@cache-key`.|No|`true`|
|
|
28
28
|
|
|
29
29
|
### Example usage
|
|
30
30
|
|
|
@@ -44,7 +44,7 @@ const createQueryCacheInterceptor = (configurationInput) => {
|
|
|
44
44
|
if (context.transactionId) {
|
|
45
45
|
return null;
|
|
46
46
|
}
|
|
47
|
-
const cacheAttributes = (0, utilities_1.extractCacheAttributes)(query.sql);
|
|
47
|
+
const cacheAttributes = (0, utilities_1.extractCacheAttributes)(query.sql, query.values);
|
|
48
48
|
if (!cacheAttributes) {
|
|
49
49
|
return null;
|
|
50
50
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import type { PrimitiveValueExpression } from 'slonik';
|
|
2
|
+
export declare const extractCacheAttributes: (subject: string, values: readonly PrimitiveValueExpression[]) => {
|
|
2
3
|
key: string;
|
|
3
4
|
ttl: number;
|
|
4
5
|
} | null;
|
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.extractCacheAttributes = void 0;
|
|
4
|
-
const
|
|
4
|
+
const node_crypto_1 = require("node:crypto");
|
|
5
|
+
const extractCacheAttributes = (subject, values) => {
|
|
5
6
|
var _a, _b;
|
|
6
7
|
const ttl = (_a = /-- @cache-ttl (\d+)/u.exec(subject)) === null || _a === void 0 ? void 0 : _a[1];
|
|
7
8
|
if (ttl) {
|
|
8
|
-
|
|
9
|
+
let key = (_b = /-- @cache-key ([a-zA-Z0-9\-_:/]+)/ui.exec(subject)) === null || _b === void 0 ? void 0 : _b[1];
|
|
9
10
|
if (!key) {
|
|
10
11
|
throw new Error('@cache-key must be specified when @cache-ttl is specified.');
|
|
11
12
|
}
|
|
13
|
+
if (!/-- @cache-hash-values false/ui.test(subject) && values.length > 0) {
|
|
14
|
+
key += ':' + (0, node_crypto_1.createHash)('sha256').update(JSON.stringify(values)).digest('hex');
|
|
15
|
+
}
|
|
12
16
|
return {
|
|
13
17
|
key,
|
|
14
18
|
ttl: Number(ttl),
|
package/package.json
CHANGED
|
@@ -86,7 +86,7 @@ export const createQueryCacheInterceptor = (configurationInput: ConfigurationInp
|
|
|
86
86
|
return null;
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
-
const cacheAttributes = extractCacheAttributes(query.sql);
|
|
89
|
+
const cacheAttributes = extractCacheAttributes(query.sql, query.values);
|
|
90
90
|
|
|
91
91
|
if (!cacheAttributes) {
|
|
92
92
|
return null;
|
|
@@ -1,13 +1,24 @@
|
|
|
1
|
-
|
|
1
|
+
import {
|
|
2
|
+
createHash,
|
|
3
|
+
} from 'node:crypto';
|
|
4
|
+
import type {
|
|
5
|
+
PrimitiveValueExpression,
|
|
6
|
+
} from 'slonik';
|
|
7
|
+
|
|
8
|
+
export const extractCacheAttributes = (subject: string, values: readonly PrimitiveValueExpression[]) => {
|
|
2
9
|
const ttl = /-- @cache-ttl (\d+)/u.exec(subject)?.[1];
|
|
3
10
|
|
|
4
11
|
if (ttl) {
|
|
5
|
-
|
|
12
|
+
let key = /-- @cache-key ([a-zA-Z0-9\-_:/]+)/ui.exec(subject)?.[1];
|
|
6
13
|
|
|
7
14
|
if (!key) {
|
|
8
15
|
throw new Error('@cache-key must be specified when @cache-ttl is specified.');
|
|
9
16
|
}
|
|
10
17
|
|
|
18
|
+
if (!/-- @cache-hash-values false/ui.test(subject) && values.length > 0) {
|
|
19
|
+
key += ':' + createHash('sha256').update(JSON.stringify(values)).digest('hex');
|
|
20
|
+
}
|
|
21
|
+
|
|
11
22
|
return {
|
|
12
23
|
key,
|
|
13
24
|
ttl: Number(ttl),
|