@hypequery/clickhouse 2.14.0 → 2.15.1
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/dist/cli/describe-relation.js +57 -0
- package/dist/cli/generate-types.js +13 -10
- package/dist/cli/type-parsing.js +9 -0
- package/dist/core/adapters/clickhouse-adapter.d.ts +20 -1
- package/dist/core/adapters/clickhouse-adapter.d.ts.map +1 -1
- package/dist/core/adapters/clickhouse-adapter.js +51 -7
- package/dist/core/adapters/database-adapter.d.ts +6 -0
- package/dist/core/adapters/database-adapter.d.ts.map +1 -1
- package/dist/core/cross-filter.d.ts.map +1 -1
- package/dist/core/cross-filter.js +3 -12
- package/dist/core/features/aggregations.d.ts +4 -1
- package/dist/core/features/aggregations.d.ts.map +1 -1
- package/dist/core/features/aggregations.js +6 -3
- package/dist/core/features/executor.d.ts +3 -0
- package/dist/core/features/executor.d.ts.map +1 -1
- package/dist/core/features/executor.js +25 -8
- package/dist/core/query-builder.d.ts +58 -9
- package/dist/core/query-builder.d.ts.map +1 -1
- package/dist/core/query-builder.js +45 -0
- package/dist/core/tests/integration/setup.d.ts +2 -0
- package/dist/core/tests/integration/setup.d.ts.map +1 -1
- package/dist/core/tests/test-utils.d.ts +2 -0
- package/dist/core/tests/test-utils.d.ts.map +1 -1
- package/dist/core/types/aggregate-types.d.ts +54 -0
- package/dist/core/types/aggregate-types.d.ts.map +1 -0
- package/dist/core/types/aggregate-types.js +1 -0
- package/dist/core/utils/aggregate-detection.d.ts +3 -0
- package/dist/core/utils/aggregate-detection.d.ts.map +1 -0
- package/dist/core/utils/aggregate-detection.js +26 -0
- package/dist/core/utils/arrow-compression.d.ts +18 -0
- package/dist/core/utils/arrow-compression.d.ts.map +1 -0
- package/dist/core/utils/arrow-compression.js +37 -0
- package/dist/core/utils/byte-stream.d.ts +22 -0
- package/dist/core/utils/byte-stream.d.ts.map +1 -0
- package/dist/core/utils/byte-stream.js +144 -0
- package/dist/core/utils/clickhouse-adapter-config.js +1 -1
- package/dist/core/utils/compound-parameter.d.ts.map +1 -1
- package/dist/core/utils/compound-parameter.js +5 -1
- package/dist/core/utils/date-literal.d.ts +21 -0
- package/dist/core/utils/date-literal.d.ts.map +1 -0
- package/dist/core/utils/date-literal.js +42 -0
- package/dist/core/utils/format-clause.d.ts +17 -0
- package/dist/core/utils/format-clause.d.ts.map +1 -0
- package/dist/core/utils/format-clause.js +138 -0
- package/dist/core/utils/integer-json-encoding.d.ts.map +1 -1
- package/dist/core/utils/integer-json-encoding.js +8 -17
- package/dist/core/utils/readonly-setting-error.d.ts +7 -0
- package/dist/core/utils/readonly-setting-error.d.ts.map +1 -0
- package/dist/core/utils/readonly-setting-error.js +17 -0
- package/dist/core/utils/sql-expressions.d.ts.map +1 -1
- package/dist/core/utils/sql-expressions.js +15 -1
- package/dist/core/utils/sql-parens.d.ts +5 -0
- package/dist/core/utils/sql-parens.d.ts.map +1 -1
- package/dist/core/utils/sql-parens.js +19 -0
- package/dist/core/utils/stream-exception.d.ts +37 -0
- package/dist/core/utils/stream-exception.d.ts.map +1 -0
- package/dist/core/utils/stream-exception.js +83 -0
- package/dist/core/utils/streaming-helpers.d.ts +11 -0
- package/dist/core/utils/streaming-helpers.d.ts.map +1 -1
- package/dist/core/utils/streaming-helpers.js +36 -23
- package/dist/core/utils.d.ts.map +1 -1
- package/dist/core/utils.js +2 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/types/clickhouse-types.d.ts +8 -2
- package/dist/types/clickhouse-types.d.ts.map +1 -1
- package/package.json +7 -7
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQL renderings of a JavaScript `Date`, which is a UTC instant.
|
|
3
|
+
*
|
|
4
|
+
* A quoted ISO string (`'2026-03-01T00:00:00.000Z'`) or any string with
|
|
5
|
+
* fractional seconds is rejected with TYPE_MISMATCH when ClickHouse compares it
|
|
6
|
+
* with a `DateTime` sorting-key column, and a bare `'2026-03-01 00:00:00'` is
|
|
7
|
+
* read in the server's time zone, not UTC. A conversion function with an
|
|
8
|
+
* explicit `'UTC'` avoids both and still lets the primary index prune.
|
|
9
|
+
*/
|
|
10
|
+
// DateTime covers 1970-01-01 00:00:00 to 2106-02-07 06:28:15 UTC.
|
|
11
|
+
const DATETIME_MAX_MS = 4294967295 * 1000;
|
|
12
|
+
function assertValidDate(value) {
|
|
13
|
+
if (!Number.isFinite(value.getTime())) {
|
|
14
|
+
throw new Error('Cannot render an invalid Date as a SQL parameter');
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** `2026-03-01T00:00:00.123Z` → `2026-03-01 00:00:00.123` (UTC). */
|
|
18
|
+
function utcText(value) {
|
|
19
|
+
return value.toISOString().replace('T', ' ').replace('Z', '');
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A ClickHouse expression for the instant. Whole seconds in the `DateTime`
|
|
23
|
+
* range render as `toDateTime(..., 'UTC')`, so they also fit `IN` lists over
|
|
24
|
+
* `DateTime` columns; anything else renders as `toDateTime64(..., 3, 'UTC')`.
|
|
25
|
+
*/
|
|
26
|
+
export function dateToSqlExpression(value) {
|
|
27
|
+
assertValidDate(value);
|
|
28
|
+
const ms = value.getTime();
|
|
29
|
+
const text = utcText(value);
|
|
30
|
+
if (ms % 1000 === 0 && ms >= 0 && ms <= DATETIME_MAX_MS) {
|
|
31
|
+
return `toDateTime('${text.slice(0, 19)}', 'UTC')`;
|
|
32
|
+
}
|
|
33
|
+
return `toDateTime64('${text}', 3, 'UTC')`;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The instant as a quoted ISO 8601 string, for values ClickHouse parses as
|
|
37
|
+
* text (the elements of an array, tuple or map parameter read through CAST).
|
|
38
|
+
*/
|
|
39
|
+
export function dateToSqlText(value) {
|
|
40
|
+
assertValidDate(value);
|
|
41
|
+
return `'${value.toISOString()}'`;
|
|
42
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ends a statement with `FORMAT <format>`, so the format is part of the query
|
|
3
|
+
* rather than a setting a `readonly = 1` user may not change.
|
|
4
|
+
*
|
|
5
|
+
* Trailing semicolons, whitespace and comments are dropped first, because a
|
|
6
|
+
* clause appended after them would open a second statement or be commented
|
|
7
|
+
* out. Strings, quoted identifiers, heredocs and nested block comments are
|
|
8
|
+
* skipped as ClickHouse's lexer does, so markers inside them are not mistaken
|
|
9
|
+
* for the end of the statement.
|
|
10
|
+
*
|
|
11
|
+
* A statement that already ends in `FORMAT <format>` is returned trimmed.
|
|
12
|
+
* Nothing else is treated as a format clause: a trailing `format DESC` is a
|
|
13
|
+
* sort on a column named `format`. A statement with a different `FORMAT`
|
|
14
|
+
* clause gets a second one, which ClickHouse rejects as a syntax error.
|
|
15
|
+
*/
|
|
16
|
+
export declare function withFormatClause(sql: string, format: string): string;
|
|
17
|
+
//# sourceMappingURL=format-clause.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format-clause.d.ts","sourceRoot":"","sources":["../../../src/core/utils/format-clause.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAMpE"}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ends a statement with `FORMAT <format>`, so the format is part of the query
|
|
3
|
+
* rather than a setting a `readonly = 1` user may not change.
|
|
4
|
+
*
|
|
5
|
+
* Trailing semicolons, whitespace and comments are dropped first, because a
|
|
6
|
+
* clause appended after them would open a second statement or be commented
|
|
7
|
+
* out. Strings, quoted identifiers, heredocs and nested block comments are
|
|
8
|
+
* skipped as ClickHouse's lexer does, so markers inside them are not mistaken
|
|
9
|
+
* for the end of the statement.
|
|
10
|
+
*
|
|
11
|
+
* A statement that already ends in `FORMAT <format>` is returned trimmed.
|
|
12
|
+
* Nothing else is treated as a format clause: a trailing `format DESC` is a
|
|
13
|
+
* sort on a column named `format`. A statement with a different `FORMAT`
|
|
14
|
+
* clause gets a second one, which ClickHouse rejects as a syntax error.
|
|
15
|
+
*/
|
|
16
|
+
export function withFormatClause(sql, format) {
|
|
17
|
+
const { end, trailingFormat } = scanStatementEnd(sql);
|
|
18
|
+
const statement = sql.slice(0, end);
|
|
19
|
+
if (trailingFormat?.toLowerCase() === format.toLowerCase())
|
|
20
|
+
return statement;
|
|
21
|
+
return `${statement}\nFORMAT ${format}`;
|
|
22
|
+
}
|
|
23
|
+
function scanStatementEnd(sql) {
|
|
24
|
+
let end = 0;
|
|
25
|
+
let last;
|
|
26
|
+
let beforeLast;
|
|
27
|
+
const significant = (token, tokenEnd) => {
|
|
28
|
+
beforeLast = last;
|
|
29
|
+
last = token;
|
|
30
|
+
end = tokenEnd;
|
|
31
|
+
};
|
|
32
|
+
let i = 0;
|
|
33
|
+
while (i < sql.length) {
|
|
34
|
+
const char = sql[i];
|
|
35
|
+
const next = sql[i + 1];
|
|
36
|
+
if (/\s/.test(char) || char === ';') {
|
|
37
|
+
i++;
|
|
38
|
+
}
|
|
39
|
+
else if ((char === '-' && next === '-') || (char === '#' && (next === ' ' || next === '!'))) {
|
|
40
|
+
const newline = sql.indexOf('\n', i);
|
|
41
|
+
i = newline < 0 ? sql.length : newline + 1;
|
|
42
|
+
}
|
|
43
|
+
else if (char === '/' && next === '*') {
|
|
44
|
+
const close = blockCommentEnd(sql, i);
|
|
45
|
+
// An unclosed comment is left for ClickHouse to report.
|
|
46
|
+
if (close < 0) {
|
|
47
|
+
significant({ kind: 'other', text: sql.slice(i) }, sql.length);
|
|
48
|
+
i = sql.length;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
i = close;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
else if (char === "'" || char === '"' || char === '`') {
|
|
55
|
+
const close = quotedEnd(sql, i);
|
|
56
|
+
const text = sql.slice(i + 1, close - 1);
|
|
57
|
+
significant({ kind: char === "'" ? 'other' : 'quoted-identifier', text }, close);
|
|
58
|
+
i = close;
|
|
59
|
+
}
|
|
60
|
+
else if (char === '$' && heredocEnd(sql, i) > 0) {
|
|
61
|
+
const close = heredocEnd(sql, i);
|
|
62
|
+
significant({ kind: 'other', text: sql.slice(i, close) }, close);
|
|
63
|
+
i = close;
|
|
64
|
+
}
|
|
65
|
+
else if (isWordChar(char)) {
|
|
66
|
+
let close = i + 1;
|
|
67
|
+
while (close < sql.length && isWordChar(sql[close]))
|
|
68
|
+
close++;
|
|
69
|
+
significant({ kind: 'word', text: sql.slice(i, close) }, close);
|
|
70
|
+
i = close;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
significant({ kind: 'other', text: char }, i + 1);
|
|
74
|
+
i++;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const endsInFormat = beforeLast?.kind === 'word'
|
|
78
|
+
&& beforeLast.text.toUpperCase() === 'FORMAT'
|
|
79
|
+
&& (last?.kind === 'word' || last?.kind === 'quoted-identifier');
|
|
80
|
+
return { end, trailingFormat: endsInFormat ? last.text : undefined };
|
|
81
|
+
}
|
|
82
|
+
function isWordChar(char) {
|
|
83
|
+
return /[A-Za-z0-9_$]/.test(char) || char.charCodeAt(0) > 0x7f;
|
|
84
|
+
}
|
|
85
|
+
/** Index past a nested block comment opened at `start`, or -1 when it is not closed. */
|
|
86
|
+
function blockCommentEnd(sql, start) {
|
|
87
|
+
let depth = 0;
|
|
88
|
+
let i = start;
|
|
89
|
+
while (i + 1 < sql.length) {
|
|
90
|
+
if (sql[i] === '/' && sql[i + 1] === '*') {
|
|
91
|
+
depth++;
|
|
92
|
+
i += 2;
|
|
93
|
+
}
|
|
94
|
+
else if (sql[i] === '*' && sql[i + 1] === '/') {
|
|
95
|
+
depth--;
|
|
96
|
+
i += 2;
|
|
97
|
+
if (depth === 0)
|
|
98
|
+
return i;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
i++;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return -1;
|
|
105
|
+
}
|
|
106
|
+
/** Index past a quoted string or identifier opened at `start`; the end of `sql` when unclosed. */
|
|
107
|
+
function quotedEnd(sql, start) {
|
|
108
|
+
const quote = sql[start];
|
|
109
|
+
let i = start + 1;
|
|
110
|
+
while (i < sql.length) {
|
|
111
|
+
const char = sql[i];
|
|
112
|
+
if (char === '\\') {
|
|
113
|
+
i += 2;
|
|
114
|
+
}
|
|
115
|
+
else if (char === quote) {
|
|
116
|
+
// A doubled quote is an escaped quote, not the end.
|
|
117
|
+
if (sql[i + 1] === quote) {
|
|
118
|
+
i += 2;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
return i + 1;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
i++;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return sql.length;
|
|
129
|
+
}
|
|
130
|
+
/** Index past a `$tag$…$tag$` heredoc opened at `start`, or -1 when there is none. */
|
|
131
|
+
function heredocEnd(sql, start) {
|
|
132
|
+
const match = /^\$[A-Za-z0-9_]*\$/.exec(sql.slice(start));
|
|
133
|
+
if (!match)
|
|
134
|
+
return -1;
|
|
135
|
+
const delimiter = match[0];
|
|
136
|
+
const close = sql.indexOf(delimiter, start + delimiter.length);
|
|
137
|
+
return close < 0 ? -1 : close + delimiter.length;
|
|
138
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"integer-json-encoding.d.ts","sourceRoot":"","sources":["../../../src/core/utils/integer-json-encoding.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,oCAAoC,CAAC;AAC7E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"integer-json-encoding.d.ts","sourceRoot":"","sources":["../../../src/core/utils/integer-json-encoding.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,oCAAoC,CAAC;AAC7E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAK/D,MAAM,WAAW,0BAA0B;IACzC,QAAQ,EAAE,kBAAkB,CAAC;IAC7B,qBAAqB,EAAE,OAAO,CAAC;CAChC;AAMD;;;GAGG;AACH,wBAAgB,wBAAwB,CACtC,QAAQ,EAAE,mBAAmB,EAC7B,cAAc,CAAC,EAAE,kBAAkB,EACnC,cAAc,CAAC,EAAE,kBAAkB,GAClC,0BAA0B,CAa5B;AAED;;;;GAIG;AACH,wBAAgB,8BAA8B,CAC5C,KAAK,EAAE,OAAO,EACd,qBAAqB,EAAE,OAAO,GAC7B,KAAK,GAAG,SAAS,CAanB"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isReadonlySettingError } from './readonly-setting-error.js';
|
|
1
2
|
const QUOTE_64BIT = 'output_format_json_quote_64bit_integers';
|
|
2
3
|
function hasQuote64BitSetting(settings) {
|
|
3
4
|
return settings !== undefined && Object.prototype.hasOwnProperty.call(settings, QUOTE_64BIT);
|
|
@@ -25,23 +26,13 @@ export function buildIntegerJsonSettings(encoding, configSettings, optionSetting
|
|
|
25
26
|
* operations, whose original errors must be preserved.
|
|
26
27
|
*/
|
|
27
28
|
export function createReadonlyIntegerJsonError(error, adapterDefaultApplied) {
|
|
28
|
-
if (!adapterDefaultApplied || !
|
|
29
|
+
if (!adapterDefaultApplied || !isReadonlySettingError(error, QUOTE_64BIT))
|
|
29
30
|
return undefined;
|
|
30
31
|
return new Error(`ClickHouse rejected HypeQuery's precision-safe setting (${QUOTE_64BIT}=1) ` +
|
|
31
|
-
`because this connection uses readonly = 1.
|
|
32
|
-
`
|
|
33
|
-
`
|
|
34
|
-
`
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (!error || typeof error !== 'object')
|
|
38
|
-
return false;
|
|
39
|
-
const { code, type } = error;
|
|
40
|
-
const message = error.message;
|
|
41
|
-
if (typeof message !== 'string' || !message.includes(QUOTE_64BIT))
|
|
42
|
-
return false;
|
|
43
|
-
return code === '164'
|
|
44
|
-
|| code === 164
|
|
45
|
-
|| type === 'READONLY'
|
|
46
|
-
|| /in readonly mode/i.test(message);
|
|
32
|
+
`because this connection uses readonly = 1, whose profile does not already set it. ` +
|
|
33
|
+
`To keep Int64 and wider values exact, either add ${QUOTE_64BIT} = 1 to the ` +
|
|
34
|
+
`user's settings profile (readonly = 1 accepts a setting that matches the profile) ` +
|
|
35
|
+
`or use readonly = 2. Otherwise set integerJsonEncoding: 'server-default' in the ` +
|
|
36
|
+
`HypeQuery adapter or query-builder configuration to omit it; the server may then ` +
|
|
37
|
+
`return Int64 and wider values as JSON numbers, which lose precision beyond 2^53.`, { cause: error });
|
|
47
38
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether ClickHouse rejected `setting` because the connection uses
|
|
3
|
+
* `readonly = 1`. Code 164 also covers other settings and forbidden
|
|
4
|
+
* operations, so the message must name this setting.
|
|
5
|
+
*/
|
|
6
|
+
export declare function isReadonlySettingError(error: unknown, setting: string): boolean;
|
|
7
|
+
//# sourceMappingURL=readonly-setting-error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readonly-setting-error.d.ts","sourceRoot":"","sources":["../../../src/core/utils/readonly-setting-error.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAU/E"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether ClickHouse rejected `setting` because the connection uses
|
|
3
|
+
* `readonly = 1`. Code 164 also covers other settings and forbidden
|
|
4
|
+
* operations, so the message must name this setting.
|
|
5
|
+
*/
|
|
6
|
+
export function isReadonlySettingError(error, setting) {
|
|
7
|
+
if (!error || typeof error !== 'object')
|
|
8
|
+
return false;
|
|
9
|
+
const { code, type } = error;
|
|
10
|
+
const message = error.message;
|
|
11
|
+
if (typeof message !== 'string' || !message.includes(setting))
|
|
12
|
+
return false;
|
|
13
|
+
return code === '164'
|
|
14
|
+
|| code === 164
|
|
15
|
+
|| type === 'READONLY'
|
|
16
|
+
|| /in readonly mode/i.test(message);
|
|
17
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sql-expressions.d.ts","sourceRoot":"","sources":["../../../src/core/utils/sql-expressions.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,WAAW,aAAa,CAAC,CAAC,GAAG,OAAO;IACxC,MAAM,EAAE,YAAY,GAAG,oBAAoB,CAAC;IAC5C,KAAK,IAAI,MAAM,CAAC;IAChB,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC;CACzC;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,aAAa,CAAC,CAAC,CAAC;IACrG,MAAM,EAAE,oBAAoB,CAAC;IAC7B,KAAK,EAAE,KAAK,CAAC;CACd;AAED;;;;GAIG;AACH,wBAAgB,GAAG,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,aAAa,CAAC,CAAC,CAAC,CAM9D;AAED,wBAAgB,UAAU,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC;AACvE,wBAAgB,UAAU,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,SAAS,MAAM,GAAG,MAAM,EACnE,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,KAAK,GACX,iBAAiB,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;AAQ/B;;;;;GAKG;AACH,wBAAgB,KAAK,CAAC,CAAC,GAAG,OAAO,EA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('toStartOfYear', field);
|
|
90
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* ClickHouse function for each `datePart` unit. Spelled out because the names
|
|
93
|
+
* are not uniform: ClickHouse has no `toDay`, so `day` maps to `toDayOfMonth`.
|
|
94
|
+
*/
|
|
95
|
+
const DATE_PART_FUNCTIONS = {
|
|
96
|
+
year: 'toYear',
|
|
97
|
+
quarter: 'toQuarter',
|
|
98
|
+
month: 'toMonth',
|
|
99
|
+
week: 'toWeek',
|
|
100
|
+
day: 'toDayOfMonth',
|
|
101
|
+
hour: 'toHour',
|
|
102
|
+
minute: 'toMinute',
|
|
103
|
+
second: 'toSecond',
|
|
104
|
+
};
|
|
91
105
|
export function datePart(part, field, alias) {
|
|
92
|
-
const functionName =
|
|
106
|
+
const functionName = DATE_PART_FUNCTIONS[part];
|
|
93
107
|
return alias
|
|
94
108
|
? rawAs(`${functionName}(${field})`, alias)
|
|
95
109
|
: raw(`${functionName}(${field})`);
|
|
@@ -4,6 +4,11 @@
|
|
|
4
4
|
* Scanners use it so markers inside such a region stay data.
|
|
5
5
|
*/
|
|
6
6
|
export declare function skipNonCode(sql: string, start: number): number | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* The SQL with every non-code region (literals, quoted identifiers, heredocs,
|
|
9
|
+
* comments) replaced by a space, so pattern checks only see SQL code.
|
|
10
|
+
*/
|
|
11
|
+
export declare function maskNonCode(sql: string): string;
|
|
7
12
|
/**
|
|
8
13
|
* Split on positional parameter markers that occur in SQL code. Question marks
|
|
9
14
|
* inside literals, quoted identifiers, heredocs, or comments are data, not
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sql-parens.d.ts","sourceRoot":"","sources":["../../../src/core/utils/sql-parens.ts"],"names":[],"mappings":"AAuEA;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAqB1E;AASD;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,CAqB1D;AAED;;;;GAIG;AACH,wBAAgB,4BAA4B,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAchE;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CA8B/D;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAM3D;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAuBzD"}
|
|
1
|
+
{"version":3,"file":"sql-parens.d.ts","sourceRoot":"","sources":["../../../src/core/utils/sql-parens.ts"],"names":[],"mappings":"AAuEA;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAqB1E;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAc/C;AASD;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,CAqB1D;AAED;;;;GAIG;AACH,wBAAgB,4BAA4B,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAchE;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CA8B/D;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAM3D;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAuBzD"}
|
|
@@ -92,6 +92,25 @@ export function skipNonCode(sql, start) {
|
|
|
92
92
|
}
|
|
93
93
|
return undefined;
|
|
94
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* The SQL with every non-code region (literals, quoted identifiers, heredocs,
|
|
97
|
+
* comments) replaced by a space, so pattern checks only see SQL code.
|
|
98
|
+
*/
|
|
99
|
+
export function maskNonCode(sql) {
|
|
100
|
+
let code = '';
|
|
101
|
+
let i = 0;
|
|
102
|
+
while (i < sql.length) {
|
|
103
|
+
const skippedTo = skipNonCode(sql, i);
|
|
104
|
+
if (skippedTo !== undefined) {
|
|
105
|
+
code += ' ';
|
|
106
|
+
i = skippedTo;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
code += sql[i];
|
|
110
|
+
i += 1;
|
|
111
|
+
}
|
|
112
|
+
return code;
|
|
113
|
+
}
|
|
95
114
|
function matchingCloser(ch) {
|
|
96
115
|
if (ch === '(')
|
|
97
116
|
return ')';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The header ClickHouse (25.11+) sends before any data, naming the tag it will
|
|
3
|
+
* wrap a mid-stream exception in.
|
|
4
|
+
*/
|
|
5
|
+
export declare const EXCEPTION_TAG_HEADER = "x-clickhouse-exception-tag";
|
|
6
|
+
/**
|
|
7
|
+
* Upper bound on the exception block a byte stream holds back. ClickHouse's
|
|
8
|
+
* messages are a few hundred bytes; a longer one still fails the stream, only
|
|
9
|
+
* without its text.
|
|
10
|
+
*/
|
|
11
|
+
export declare const EXCEPTION_BLOCK_MAX_BYTES: number;
|
|
12
|
+
export interface StreamException {
|
|
13
|
+
/** The bytes before the exception block: the part of the result that was sent. */
|
|
14
|
+
data: Uint8Array;
|
|
15
|
+
error: Error;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Splits a failed response's trailing exception block from the data before it.
|
|
19
|
+
*
|
|
20
|
+
* ClickHouse cannot change the status once data has been sent, so it ends the
|
|
21
|
+
* body with:
|
|
22
|
+
*
|
|
23
|
+
* \r\n__exception__\r\n<tag>\r\n<message>\n<length> <tag>\r\n__exception__\r\n
|
|
24
|
+
*
|
|
25
|
+
* Returns `undefined` when `bytes` does not end that way, so a body that merely
|
|
26
|
+
* contains the marker is never mistaken for a failure.
|
|
27
|
+
*/
|
|
28
|
+
export declare function splitStreamException(bytes: Uint8Array, tag: string): StreamException | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Finds the plain exception text a ClickHouse server older than 25.11 appends
|
|
31
|
+
* when a query fails after data was sent. Those servers mark nothing, so this
|
|
32
|
+
* is only meaningful once the body is already known to be incomplete: binary
|
|
33
|
+
* data could contain the same text by chance.
|
|
34
|
+
*/
|
|
35
|
+
export declare function findUntaggedStreamException(bytes: Uint8Array): StreamException | undefined;
|
|
36
|
+
export declare function endsWith(bytes: Uint8Array, suffix: Uint8Array): boolean;
|
|
37
|
+
//# sourceMappingURL=stream-exception.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-exception.d.ts","sourceRoot":"","sources":["../../../src/core/utils/stream-exception.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,eAAO,MAAM,oBAAoB,+BAA+B,CAAC;AAEjE;;;;GAIG;AACH,eAAO,MAAM,yBAAyB,QAAY,CAAC;AAQnD,MAAM,WAAW,eAAe;IAC9B,kFAAkF;IAClF,IAAI,EAAE,UAAU,CAAC;IACjB,KAAK,EAAE,KAAK,CAAC;CACd;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,UAAU,EACjB,GAAG,EAAE,MAAM,GACV,eAAe,GAAG,SAAS,CAoB7B;AAED;;;;;GAKG;AACH,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,UAAU,GAAG,eAAe,GAAG,SAAS,CAS1F;AAED,wBAAgB,QAAQ,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,GAAG,OAAO,CAOvE"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { parseError } from '@clickhouse/client-common';
|
|
2
|
+
/**
|
|
3
|
+
* The header ClickHouse (25.11+) sends before any data, naming the tag it will
|
|
4
|
+
* wrap a mid-stream exception in.
|
|
5
|
+
*/
|
|
6
|
+
export const EXCEPTION_TAG_HEADER = 'x-clickhouse-exception-tag';
|
|
7
|
+
/**
|
|
8
|
+
* Upper bound on the exception block a byte stream holds back. ClickHouse's
|
|
9
|
+
* messages are a few hundred bytes; a longer one still fails the stream, only
|
|
10
|
+
* without its text.
|
|
11
|
+
*/
|
|
12
|
+
export const EXCEPTION_BLOCK_MAX_BYTES = 16 * 1024;
|
|
13
|
+
/** The start of the exception text servers before 25.11 append to a failed body. */
|
|
14
|
+
const UNTAGGED_EXCEPTION_RE = /Code: \d+\. DB::Exception: /g;
|
|
15
|
+
const encoder = new TextEncoder();
|
|
16
|
+
const decoder = new TextDecoder('utf-8');
|
|
17
|
+
/**
|
|
18
|
+
* Splits a failed response's trailing exception block from the data before it.
|
|
19
|
+
*
|
|
20
|
+
* ClickHouse cannot change the status once data has been sent, so it ends the
|
|
21
|
+
* body with:
|
|
22
|
+
*
|
|
23
|
+
* \r\n__exception__\r\n<tag>\r\n<message>\n<length> <tag>\r\n__exception__\r\n
|
|
24
|
+
*
|
|
25
|
+
* Returns `undefined` when `bytes` does not end that way, so a body that merely
|
|
26
|
+
* contains the marker is never mistaken for a failure.
|
|
27
|
+
*/
|
|
28
|
+
export function splitStreamException(bytes, tag) {
|
|
29
|
+
const end = encoder.encode(` ${tag}\r\n__exception__\r\n`);
|
|
30
|
+
if (!endsWith(bytes, end)) {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
const start = encoder.encode(`\r\n__exception__\r\n${tag}\r\n`);
|
|
34
|
+
const startAt = lastIndexOf(bytes, start);
|
|
35
|
+
if (startAt < 0) {
|
|
36
|
+
return {
|
|
37
|
+
data: new Uint8Array(0),
|
|
38
|
+
error: new Error('ClickHouse failed mid-stream; its error message was too long to recover.'),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
// "<message>\n<length>": the length hint follows the message's last newline.
|
|
42
|
+
const body = decoder.decode(bytes.subarray(startAt + start.length, bytes.length - end.length));
|
|
43
|
+
const hintAt = body.lastIndexOf('\n');
|
|
44
|
+
const message = (hintAt >= 0 ? body.slice(0, hintAt) : body).trim();
|
|
45
|
+
return { data: bytes.subarray(0, startAt), error: parseError(message) };
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Finds the plain exception text a ClickHouse server older than 25.11 appends
|
|
49
|
+
* when a query fails after data was sent. Those servers mark nothing, so this
|
|
50
|
+
* is only meaningful once the body is already known to be incomplete: binary
|
|
51
|
+
* data could contain the same text by chance.
|
|
52
|
+
*/
|
|
53
|
+
export function findUntaggedStreamException(bytes) {
|
|
54
|
+
// windows-1252 decodes one character per byte, so indices stay byte offsets.
|
|
55
|
+
const text = new TextDecoder('windows-1252').decode(bytes);
|
|
56
|
+
let startAt = -1;
|
|
57
|
+
for (const match of text.matchAll(UNTAGGED_EXCEPTION_RE))
|
|
58
|
+
startAt = match.index;
|
|
59
|
+
if (startAt < 0)
|
|
60
|
+
return undefined;
|
|
61
|
+
const message = decoder.decode(bytes.subarray(startAt)).trim();
|
|
62
|
+
return { data: bytes.subarray(0, startAt), error: parseError(message) };
|
|
63
|
+
}
|
|
64
|
+
export function endsWith(bytes, suffix) {
|
|
65
|
+
if (bytes.length < suffix.length)
|
|
66
|
+
return false;
|
|
67
|
+
const offset = bytes.length - suffix.length;
|
|
68
|
+
for (let i = 0; i < suffix.length; i++) {
|
|
69
|
+
if (bytes[offset + i] !== suffix[i])
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
function lastIndexOf(bytes, needle) {
|
|
75
|
+
outer: for (let at = bytes.length - needle.length; at >= 0; at--) {
|
|
76
|
+
for (let i = 0; i < needle.length; i++) {
|
|
77
|
+
if (bytes[at + i] !== needle[i])
|
|
78
|
+
continue outer;
|
|
79
|
+
}
|
|
80
|
+
return at;
|
|
81
|
+
}
|
|
82
|
+
return -1;
|
|
83
|
+
}
|
|
@@ -1,2 +1,13 @@
|
|
|
1
|
+
type StreamReaderResult = {
|
|
2
|
+
done: boolean;
|
|
3
|
+
value?: any;
|
|
4
|
+
};
|
|
5
|
+
export interface StreamReader {
|
|
6
|
+
readNext(): Promise<StreamReaderResult>;
|
|
7
|
+
close(): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
/** Reads a Node or web stream chunk by chunk; `close()` always releases the source. */
|
|
10
|
+
export declare function openStreamReader(stream: NodeJS.ReadableStream | ReadableStream<unknown>): Promise<StreamReader>;
|
|
1
11
|
export declare function createJsonEachRowStream<T>(stream: NodeJS.ReadableStream | ReadableStream<T[]>, abortSignal?: AbortSignal): ReadableStream<T[]>;
|
|
12
|
+
export {};
|
|
2
13
|
//# sourceMappingURL=streaming-helpers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"streaming-helpers.d.ts","sourceRoot":"","sources":["../../../src/core/utils/streaming-helpers.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"streaming-helpers.d.ts","sourceRoot":"","sources":["../../../src/core/utils/streaming-helpers.ts"],"names":[],"mappings":"AAGA,KAAK,kBAAkB,GAAG;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,CAAC,EAAE,GAAG,CAAA;CAAE,CAAC;AAEzD,MAAM,WAAW,YAAY;IAC3B,QAAQ,IAAI,OAAO,CAAC,kBAAkB,CAAC,CAAC;IACxC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AA2JD,uFAAuF;AACvF,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,MAAM,CAAC,cAAc,GAAG,cAAc,CAAC,OAAO,CAAC,GACtD,OAAO,CAAC,YAAY,CAAC,CAIvB;AAED,wBAAgB,uBAAuB,CAAC,CAAC,EACvC,MAAM,EAAE,MAAM,CAAC,cAAc,GAAG,cAAc,CAAC,CAAC,EAAE,CAAC,EACnD,WAAW,CAAC,EAAE,WAAW,GACxB,cAAc,CAAC,CAAC,EAAE,CAAC,CA0ErB"}
|
|
@@ -18,10 +18,19 @@ function createBufferFlusher() {
|
|
|
18
18
|
}
|
|
19
19
|
return rows;
|
|
20
20
|
};
|
|
21
|
+
// Decodes across chunk boundaries, so a character split between two chunks survives.
|
|
22
|
+
const decoder = new TextDecoder('utf-8');
|
|
21
23
|
const append = (value) => {
|
|
22
|
-
buffer += typeof value === 'string' ? value :
|
|
24
|
+
buffer += typeof value === 'string' ? value : decoder.decode(value, { stream: true });
|
|
23
25
|
};
|
|
24
|
-
|
|
26
|
+
/** The last row, when the body does not end in a newline. */
|
|
27
|
+
const end = () => {
|
|
28
|
+
buffer += decoder.decode();
|
|
29
|
+
const last = buffer.trim();
|
|
30
|
+
buffer = '';
|
|
31
|
+
return last.length ? [JSON.parse(last)] : [];
|
|
32
|
+
};
|
|
33
|
+
return { flush, append, end };
|
|
25
34
|
}
|
|
26
35
|
async function normalizeChunk(chunk, flush, append) {
|
|
27
36
|
if (chunk == null) {
|
|
@@ -117,19 +126,18 @@ async function createWebStreamReader(webStream) {
|
|
|
117
126
|
};
|
|
118
127
|
return { readNext, close };
|
|
119
128
|
}
|
|
129
|
+
/** Reads a Node or web stream chunk by chunk; `close()` always releases the source. */
|
|
130
|
+
export function openStreamReader(stream) {
|
|
131
|
+
return typeof stream?.getReader === 'function'
|
|
132
|
+
? createWebStreamReader(stream)
|
|
133
|
+
: createChunkReader(stream);
|
|
134
|
+
}
|
|
120
135
|
export function createJsonEachRowStream(stream, abortSignal) {
|
|
121
|
-
const { flush, append } = createBufferFlusher();
|
|
136
|
+
const { flush, append, end } = createBufferFlusher();
|
|
122
137
|
let readerPromise;
|
|
123
138
|
let onAbort;
|
|
124
139
|
const ensureReader = () => {
|
|
125
|
-
|
|
126
|
-
if (typeof stream?.getReader === 'function') {
|
|
127
|
-
readerPromise = createWebStreamReader(stream);
|
|
128
|
-
}
|
|
129
|
-
else {
|
|
130
|
-
readerPromise = createChunkReader(stream);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
140
|
+
readerPromise ??= openStreamReader(stream);
|
|
133
141
|
return readerPromise;
|
|
134
142
|
};
|
|
135
143
|
const detachAbort = () => {
|
|
@@ -160,19 +168,24 @@ export function createJsonEachRowStream(stream, abortSignal) {
|
|
|
160
168
|
async pull(controller) {
|
|
161
169
|
try {
|
|
162
170
|
const reader = await ensureReader();
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
const
|
|
167
|
-
if (
|
|
168
|
-
|
|
171
|
+
// A pull that enqueues nothing is not called again, so a chunk ending
|
|
172
|
+
// mid-row must not end the pull: read until a row completes.
|
|
173
|
+
for (;;) {
|
|
174
|
+
const { done, value } = await reader.readNext();
|
|
175
|
+
if (done) {
|
|
176
|
+
detachAbort();
|
|
177
|
+
const remaining = [...flush(), ...end()];
|
|
178
|
+
if (remaining.length) {
|
|
179
|
+
controller.enqueue(remaining);
|
|
180
|
+
}
|
|
181
|
+
controller.close();
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const rows = await normalizeChunk(value, flush, append);
|
|
185
|
+
if (rows.length) {
|
|
186
|
+
controller.enqueue(rows);
|
|
187
|
+
return;
|
|
169
188
|
}
|
|
170
|
-
controller.close();
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
const rows = await normalizeChunk(value, flush, append);
|
|
174
|
-
if (rows.length) {
|
|
175
|
-
controller.enqueue(rows);
|
|
176
189
|
}
|
|
177
190
|
}
|
|
178
191
|
catch (error) {
|
package/dist/core/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/core/utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/core/utils.ts"],"names":[],"mappings":"AAGA,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CA2BlD;AAED,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,OAAO,EAAE,GAAG,MAAM,CAapF"}
|
package/dist/core/utils.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { dateToSqlExpression } from './utils/date-literal.js';
|
|
1
2
|
import { splitSqlPlaceholders } from './utils/sql-parens.js';
|
|
2
3
|
export function escapeValue(value) {
|
|
3
4
|
if (value === null) {
|
|
@@ -20,10 +21,7 @@ export function escapeValue(value) {
|
|
|
20
21
|
return `'${escaped}'`;
|
|
21
22
|
}
|
|
22
23
|
else if (value instanceof Date) {
|
|
23
|
-
|
|
24
|
-
throw new Error('Cannot render an invalid Date as a SQL parameter');
|
|
25
|
-
}
|
|
26
|
-
return `'${value.toISOString()}'`;
|
|
24
|
+
return dateToSqlExpression(value);
|
|
27
25
|
}
|
|
28
26
|
else {
|
|
29
27
|
const serialized = JSON.stringify(value);
|
package/dist/index.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export { createClickHouseAdapter, ClickHouseAdapter } from './core/adapters/clic
|
|
|
9
9
|
export { ClickHouseDialect } from './core/dialects/clickhouse-dialect.js';
|
|
10
10
|
export type { DatabaseAdapter, InsertExecutionOptions, InsertResultSummary, QueryExecutionOptions } from './core/adapters/database-adapter.js';
|
|
11
11
|
export type { SqlDialect } from './core/dialects/sql-dialect.js';
|
|
12
|
-
export type { ClickHouseConfig, ClickHouseAdapterConfig, ClickHouseAdapterOptions, ClickHouseClientConfig, CreateQueryBuilderConfig, ExecuteOptions, IntegerJsonEncoding, StreamOptions } from './core/query-builder.js';
|
|
12
|
+
export type { ClickHouseConfig, ClickHouseAdapterConfig, ClickHouseAdapterOptions, ClickHouseClientConfig, CreateQueryBuilderConfig, ExecuteOptions, IntegerJsonEncoding, ArrowCompression, StreamOptions } from './core/query-builder.js';
|
|
13
13
|
export { isClientConfig } from './core/query-builder.js';
|
|
14
14
|
export { substituteParameters, escapeValue } from './core/utils.js';
|
|
15
15
|
export type { CacheOptions, CacheConfig, CacheProvider, CacheEntry, CacheStatus } from './core/cache/types.js';
|