@514labs/moose-lib 0.6.296-ci-27-g94fe722c → 0.6.297-ci-28-g84f3192e
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/{browserCompatible-B8CAYjv5.d.ts → browserCompatible-CMEunMFq.d.ts} +1 -1
- package/dist/{browserCompatible-ChWHzgtb.d.mts → browserCompatible-FzU17dxm.d.mts} +1 -1
- package/dist/browserCompatible.d.mts +2 -2
- package/dist/browserCompatible.d.ts +2 -2
- package/dist/browserCompatible.js +2443 -2160
- package/dist/browserCompatible.js.map +1 -1
- package/dist/browserCompatible.mjs +2440 -2159
- package/dist/browserCompatible.mjs.map +1 -1
- package/dist/dmv2/index.d.mts +1 -1
- package/dist/dmv2/index.d.ts +1 -1
- package/dist/dmv2/index.js +2334 -2051
- package/dist/dmv2/index.js.map +1 -1
- package/dist/dmv2/index.mjs +2299 -2018
- package/dist/dmv2/index.mjs.map +1 -1
- package/dist/{index-rQOQo9sv.d.mts → index-CcHF2cVT.d.mts} +16 -5
- package/dist/{index-rQOQo9sv.d.ts → index-CcHF2cVT.d.ts} +16 -5
- package/dist/index.d.mts +76 -6
- package/dist/index.d.ts +76 -6
- package/dist/index.js +3063 -2719
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2961 -2618
- package/dist/index.mjs.map +1 -1
- package/dist/moose-runner.js +1715 -1136
- package/dist/moose-runner.js.map +1 -1
- package/dist/moose-runner.mjs +1706 -1129
- package/dist/moose-runner.mjs.map +1 -1
- package/package.json +1 -1
package/dist/dmv2/index.mjs
CHANGED
|
@@ -8,6 +8,247 @@ var __export = (target, all) => {
|
|
|
8
8
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
9
|
};
|
|
10
10
|
|
|
11
|
+
// src/dmv2/utils/stackTrace.ts
|
|
12
|
+
function shouldSkipStackLine(line) {
|
|
13
|
+
return line.includes("node_modules") || // Skip npm installed packages (prod)
|
|
14
|
+
line.includes("node:internal") || // Skip Node.js internals (modern format)
|
|
15
|
+
line.includes("internal/modules") || // Skip Node.js internals (older format)
|
|
16
|
+
line.includes("ts-node") || // Skip TypeScript execution
|
|
17
|
+
line.includes("/ts-moose-lib/src/") || // Skip dev/linked moose-lib src (Unix)
|
|
18
|
+
line.includes("\\ts-moose-lib\\src\\") || // Skip dev/linked moose-lib src (Windows)
|
|
19
|
+
line.includes("/ts-moose-lib/dist/") || // Skip dev/linked moose-lib dist (Unix)
|
|
20
|
+
line.includes("\\ts-moose-lib\\dist\\");
|
|
21
|
+
}
|
|
22
|
+
function parseStackLine(line) {
|
|
23
|
+
const match = line.match(/\((.*):(\d+):(\d+)\)/) || line.match(/at (.*):(\d+):(\d+)/);
|
|
24
|
+
if (match && match[1]) {
|
|
25
|
+
return {
|
|
26
|
+
file: match[1],
|
|
27
|
+
line: match[2]
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
return void 0;
|
|
31
|
+
}
|
|
32
|
+
function getSourceFileInfo(stack) {
|
|
33
|
+
if (!stack) return {};
|
|
34
|
+
const lines = stack.split("\n");
|
|
35
|
+
for (const line of lines) {
|
|
36
|
+
if (shouldSkipStackLine(line)) continue;
|
|
37
|
+
const info = parseStackLine(line);
|
|
38
|
+
if (info) return info;
|
|
39
|
+
}
|
|
40
|
+
return {};
|
|
41
|
+
}
|
|
42
|
+
function getSourceLocationFromStack(stack) {
|
|
43
|
+
if (!stack) return void 0;
|
|
44
|
+
const lines = stack.split("\n");
|
|
45
|
+
for (const line of lines.slice(1)) {
|
|
46
|
+
if (shouldSkipStackLine(line)) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
const v8Match = line.match(/at\s+(?:.*?\s+\()?(.+):(\d+):(\d+)\)?/);
|
|
50
|
+
if (v8Match) {
|
|
51
|
+
return {
|
|
52
|
+
file: v8Match[1],
|
|
53
|
+
line: parseInt(v8Match[2], 10),
|
|
54
|
+
column: parseInt(v8Match[3], 10)
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const smMatch = line.match(/(?:.*@)?(.+):(\d+):(\d+)/);
|
|
58
|
+
if (smMatch) {
|
|
59
|
+
return {
|
|
60
|
+
file: smMatch[1],
|
|
61
|
+
line: parseInt(smMatch[2], 10),
|
|
62
|
+
column: parseInt(smMatch[3], 10)
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return void 0;
|
|
67
|
+
}
|
|
68
|
+
function getSourceFileFromStack(stack) {
|
|
69
|
+
const location = getSourceLocationFromStack(stack);
|
|
70
|
+
return location?.file;
|
|
71
|
+
}
|
|
72
|
+
var init_stackTrace = __esm({
|
|
73
|
+
"src/dmv2/utils/stackTrace.ts"() {
|
|
74
|
+
"use strict";
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// src/dmv2/typedBase.ts
|
|
79
|
+
var TypedBase;
|
|
80
|
+
var init_typedBase = __esm({
|
|
81
|
+
"src/dmv2/typedBase.ts"() {
|
|
82
|
+
"use strict";
|
|
83
|
+
init_stackTrace();
|
|
84
|
+
TypedBase = class {
|
|
85
|
+
/** The JSON schema representation of type T. Injected by the compiler plugin. */
|
|
86
|
+
schema;
|
|
87
|
+
/** The name assigned to this resource instance. */
|
|
88
|
+
name;
|
|
89
|
+
/** A dictionary mapping column names (keys of T) to their Column definitions. */
|
|
90
|
+
columns;
|
|
91
|
+
/** An array containing the Column definitions for this resource. Injected by the compiler plugin. */
|
|
92
|
+
columnArray;
|
|
93
|
+
/** The configuration object specific to this resource type. */
|
|
94
|
+
config;
|
|
95
|
+
/** Typia validation functions for type T. Injected by the compiler plugin for OlapTable. */
|
|
96
|
+
validators;
|
|
97
|
+
/** Optional metadata for the resource, always present as an object. */
|
|
98
|
+
metadata;
|
|
99
|
+
/**
|
|
100
|
+
* Whether this resource allows extra fields beyond the defined columns.
|
|
101
|
+
* When true, extra fields in payloads are passed through to streaming functions.
|
|
102
|
+
* Injected by the compiler plugin when the type has an index signature.
|
|
103
|
+
*/
|
|
104
|
+
allowExtraFields;
|
|
105
|
+
/**
|
|
106
|
+
* @internal Constructor intended for internal use by subclasses and the compiler plugin.
|
|
107
|
+
* It expects the schema and columns to be provided, typically injected by the compiler.
|
|
108
|
+
*
|
|
109
|
+
* @param name The name for the resource instance.
|
|
110
|
+
* @param config The configuration object for the resource.
|
|
111
|
+
* @param schema The JSON schema for the resource's data type T (injected).
|
|
112
|
+
* @param columns The array of Column definitions for T (injected).
|
|
113
|
+
* @param allowExtraFields Whether extra fields are allowed (injected when type has index signature).
|
|
114
|
+
*/
|
|
115
|
+
constructor(name, config, schema, columns, validators, allowExtraFields) {
|
|
116
|
+
if (schema === void 0 || columns === void 0) {
|
|
117
|
+
throw new Error(
|
|
118
|
+
"Supply the type param T so that the schema is inserted by the compiler plugin."
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
this.schema = schema;
|
|
122
|
+
this.columnArray = columns;
|
|
123
|
+
const columnsObj = {};
|
|
124
|
+
columns.forEach((column) => {
|
|
125
|
+
columnsObj[column.name] = column;
|
|
126
|
+
});
|
|
127
|
+
this.columns = columnsObj;
|
|
128
|
+
this.name = name;
|
|
129
|
+
this.config = config;
|
|
130
|
+
this.validators = validators;
|
|
131
|
+
this.allowExtraFields = allowExtraFields ?? false;
|
|
132
|
+
this.metadata = config?.metadata ? { ...config.metadata } : {};
|
|
133
|
+
if (!this.metadata.source) {
|
|
134
|
+
const stack = new Error().stack;
|
|
135
|
+
if (stack) {
|
|
136
|
+
const info = getSourceFileInfo(stack);
|
|
137
|
+
this.metadata.source = { file: info.file, line: info.line };
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
// src/dataModels/dataModelTypes.ts
|
|
146
|
+
function isArrayNestedType(dt) {
|
|
147
|
+
return typeof dt === "object" && dt !== null && dt.elementType !== null && typeof dt.elementType === "object" && dt.elementType.hasOwnProperty("columns") && Array.isArray(dt.elementType.columns);
|
|
148
|
+
}
|
|
149
|
+
function isNestedType(dt) {
|
|
150
|
+
return typeof dt === "object" && dt !== null && Array.isArray(dt.columns);
|
|
151
|
+
}
|
|
152
|
+
var init_dataModelTypes = __esm({
|
|
153
|
+
"src/dataModels/dataModelTypes.ts"() {
|
|
154
|
+
"use strict";
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// src/sqlHelpers.ts
|
|
159
|
+
function createClickhouseParameter(parameterIndex, value) {
|
|
160
|
+
return `{p${parameterIndex}:${mapToClickHouseType(value)}}`;
|
|
161
|
+
}
|
|
162
|
+
function emptyIfUndefined(value) {
|
|
163
|
+
return value === void 0 ? "" : value;
|
|
164
|
+
}
|
|
165
|
+
var quoteIdentifier, toStaticQuery, toQuery, getValueFromParameter, mapToClickHouseType;
|
|
166
|
+
var init_sqlHelpers = __esm({
|
|
167
|
+
"src/sqlHelpers.ts"() {
|
|
168
|
+
"use strict";
|
|
169
|
+
quoteIdentifier = (name) => {
|
|
170
|
+
return name.startsWith("`") && name.endsWith("`") ? name : `\`${name}\``;
|
|
171
|
+
};
|
|
172
|
+
toStaticQuery = (sql3) => {
|
|
173
|
+
const [query, params] = toQuery(sql3);
|
|
174
|
+
if (Object.keys(params).length !== 0) {
|
|
175
|
+
throw new Error(
|
|
176
|
+
"Dynamic SQL is not allowed in the select statement in view creation."
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
return query;
|
|
180
|
+
};
|
|
181
|
+
toQuery = (sql3) => {
|
|
182
|
+
const parameterizedStubs = sql3.values.map(
|
|
183
|
+
(v, i) => createClickhouseParameter(i, v)
|
|
184
|
+
);
|
|
185
|
+
const query = sql3.strings.map(
|
|
186
|
+
(s, i) => s != "" ? `${s}${emptyIfUndefined(parameterizedStubs[i])}` : ""
|
|
187
|
+
).join("");
|
|
188
|
+
const query_params = sql3.values.reduce(
|
|
189
|
+
(acc, v, i) => ({
|
|
190
|
+
...acc,
|
|
191
|
+
[`p${i}`]: getValueFromParameter(v)
|
|
192
|
+
}),
|
|
193
|
+
{}
|
|
194
|
+
);
|
|
195
|
+
return [query, query_params];
|
|
196
|
+
};
|
|
197
|
+
getValueFromParameter = (value) => {
|
|
198
|
+
if (Array.isArray(value)) {
|
|
199
|
+
const [type, val] = value;
|
|
200
|
+
if (type === "Identifier") return val;
|
|
201
|
+
}
|
|
202
|
+
return value;
|
|
203
|
+
};
|
|
204
|
+
mapToClickHouseType = (value) => {
|
|
205
|
+
if (typeof value === "number") {
|
|
206
|
+
return Number.isInteger(value) ? "Int" : "Float";
|
|
207
|
+
}
|
|
208
|
+
if (typeof value === "boolean") return "Bool";
|
|
209
|
+
if (value instanceof Date) return "DateTime";
|
|
210
|
+
if (Array.isArray(value)) {
|
|
211
|
+
const [type, _] = value;
|
|
212
|
+
return type;
|
|
213
|
+
}
|
|
214
|
+
return "String";
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
// src/blocks/helpers.ts
|
|
220
|
+
function dropView(name) {
|
|
221
|
+
return `DROP VIEW IF EXISTS ${quoteIdentifier(name)}`.trim();
|
|
222
|
+
}
|
|
223
|
+
function createMaterializedView(options) {
|
|
224
|
+
return `CREATE MATERIALIZED VIEW IF NOT EXISTS ${quoteIdentifier(options.name)}
|
|
225
|
+
TO ${quoteIdentifier(options.destinationTable)}
|
|
226
|
+
AS ${options.select}`.trim();
|
|
227
|
+
}
|
|
228
|
+
var init_helpers = __esm({
|
|
229
|
+
"src/blocks/helpers.ts"() {
|
|
230
|
+
"use strict";
|
|
231
|
+
init_sqlHelpers();
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
// src/dataModels/types.ts
|
|
236
|
+
var init_types = __esm({
|
|
237
|
+
"src/dataModels/types.ts"() {
|
|
238
|
+
"use strict";
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
// src/browserCompatible.ts
|
|
243
|
+
var init_browserCompatible = __esm({
|
|
244
|
+
"src/browserCompatible.ts"() {
|
|
245
|
+
"use strict";
|
|
246
|
+
init_dmv2();
|
|
247
|
+
init_types();
|
|
248
|
+
init_sqlHelpers();
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
|
|
11
252
|
// src/commons.ts
|
|
12
253
|
var commons_exports = {};
|
|
13
254
|
__export(commons_exports, {
|
|
@@ -175,6 +416,298 @@ var init_commons = __esm({
|
|
|
175
416
|
}
|
|
176
417
|
});
|
|
177
418
|
|
|
419
|
+
// src/secrets.ts
|
|
420
|
+
var init_secrets = __esm({
|
|
421
|
+
"src/secrets.ts"() {
|
|
422
|
+
"use strict";
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
// src/consumption-apis/helpers.ts
|
|
427
|
+
import {
|
|
428
|
+
Client as TemporalClient,
|
|
429
|
+
Connection
|
|
430
|
+
} from "@temporalio/client";
|
|
431
|
+
import { createHash, randomUUID } from "crypto";
|
|
432
|
+
var init_helpers2 = __esm({
|
|
433
|
+
"src/consumption-apis/helpers.ts"() {
|
|
434
|
+
"use strict";
|
|
435
|
+
init_internal();
|
|
436
|
+
init_sqlHelpers();
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
// src/consumption-apis/webAppHelpers.ts
|
|
441
|
+
var init_webAppHelpers = __esm({
|
|
442
|
+
"src/consumption-apis/webAppHelpers.ts"() {
|
|
443
|
+
"use strict";
|
|
444
|
+
}
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
// src/scripts/task.ts
|
|
448
|
+
var init_task = __esm({
|
|
449
|
+
"src/scripts/task.ts"() {
|
|
450
|
+
"use strict";
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
// src/cluster-utils.ts
|
|
455
|
+
import cluster from "cluster";
|
|
456
|
+
import { availableParallelism } from "os";
|
|
457
|
+
import { exit } from "process";
|
|
458
|
+
var init_cluster_utils = __esm({
|
|
459
|
+
"src/cluster-utils.ts"() {
|
|
460
|
+
"use strict";
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
// src/consumption-apis/runner.ts
|
|
465
|
+
import * as jose from "jose";
|
|
466
|
+
var init_runner = __esm({
|
|
467
|
+
"src/consumption-apis/runner.ts"() {
|
|
468
|
+
"use strict";
|
|
469
|
+
init_commons();
|
|
470
|
+
init_helpers2();
|
|
471
|
+
init_cluster_utils();
|
|
472
|
+
init_sqlHelpers();
|
|
473
|
+
init_internal();
|
|
474
|
+
}
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
// src/clients/redisClient.ts
|
|
478
|
+
import { createClient as createClient2 } from "redis";
|
|
479
|
+
var init_redisClient = __esm({
|
|
480
|
+
"src/clients/redisClient.ts"() {
|
|
481
|
+
"use strict";
|
|
482
|
+
}
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
// src/consumption-apis/standalone.ts
|
|
486
|
+
var init_standalone = __esm({
|
|
487
|
+
"src/consumption-apis/standalone.ts"() {
|
|
488
|
+
"use strict";
|
|
489
|
+
init_helpers2();
|
|
490
|
+
init_commons();
|
|
491
|
+
init_sqlHelpers();
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
// src/utilities/json.ts
|
|
496
|
+
var init_json = __esm({
|
|
497
|
+
"src/utilities/json.ts"() {
|
|
498
|
+
"use strict";
|
|
499
|
+
}
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
// src/utilities/dataParser.ts
|
|
503
|
+
import { parse } from "csv-parse";
|
|
504
|
+
var CSV_DELIMITERS, DEFAULT_CSV_CONFIG;
|
|
505
|
+
var init_dataParser = __esm({
|
|
506
|
+
"src/utilities/dataParser.ts"() {
|
|
507
|
+
"use strict";
|
|
508
|
+
init_json();
|
|
509
|
+
CSV_DELIMITERS = {
|
|
510
|
+
COMMA: ",",
|
|
511
|
+
TAB: " ",
|
|
512
|
+
SEMICOLON: ";",
|
|
513
|
+
PIPE: "|"
|
|
514
|
+
};
|
|
515
|
+
DEFAULT_CSV_CONFIG = {
|
|
516
|
+
delimiter: CSV_DELIMITERS.COMMA,
|
|
517
|
+
columns: true,
|
|
518
|
+
skipEmptyLines: true,
|
|
519
|
+
trim: true
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
});
|
|
523
|
+
|
|
524
|
+
// src/utilities/index.ts
|
|
525
|
+
var init_utilities = __esm({
|
|
526
|
+
"src/utilities/index.ts"() {
|
|
527
|
+
"use strict";
|
|
528
|
+
init_dataParser();
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
// src/connectors/dataSource.ts
|
|
533
|
+
var init_dataSource = __esm({
|
|
534
|
+
"src/connectors/dataSource.ts"() {
|
|
535
|
+
"use strict";
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
// src/index.ts
|
|
540
|
+
var init_index = __esm({
|
|
541
|
+
"src/index.ts"() {
|
|
542
|
+
"use strict";
|
|
543
|
+
init_browserCompatible();
|
|
544
|
+
init_helpers();
|
|
545
|
+
init_commons();
|
|
546
|
+
init_secrets();
|
|
547
|
+
init_helpers2();
|
|
548
|
+
init_webAppHelpers();
|
|
549
|
+
init_task();
|
|
550
|
+
init_runner();
|
|
551
|
+
init_redisClient();
|
|
552
|
+
init_helpers2();
|
|
553
|
+
init_standalone();
|
|
554
|
+
init_sqlHelpers();
|
|
555
|
+
init_utilities();
|
|
556
|
+
init_dataSource();
|
|
557
|
+
init_types();
|
|
558
|
+
}
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
// src/dmv2/internal.ts
|
|
562
|
+
import process2 from "process";
|
|
563
|
+
var isClientOnlyMode, moose_internal, defaultRetentionPeriod, getMooseInternal, dlqSchema, dlqColumns;
|
|
564
|
+
var init_internal = __esm({
|
|
565
|
+
"src/dmv2/internal.ts"() {
|
|
566
|
+
"use strict";
|
|
567
|
+
init_index();
|
|
568
|
+
init_commons();
|
|
569
|
+
isClientOnlyMode = () => process2.env.MOOSE_CLIENT_ONLY === "true";
|
|
570
|
+
moose_internal = {
|
|
571
|
+
tables: /* @__PURE__ */ new Map(),
|
|
572
|
+
streams: /* @__PURE__ */ new Map(),
|
|
573
|
+
ingestApis: /* @__PURE__ */ new Map(),
|
|
574
|
+
apis: /* @__PURE__ */ new Map(),
|
|
575
|
+
sqlResources: /* @__PURE__ */ new Map(),
|
|
576
|
+
workflows: /* @__PURE__ */ new Map(),
|
|
577
|
+
webApps: /* @__PURE__ */ new Map()
|
|
578
|
+
};
|
|
579
|
+
defaultRetentionPeriod = 60 * 60 * 24 * 7;
|
|
580
|
+
getMooseInternal = () => globalThis.moose_internal;
|
|
581
|
+
if (getMooseInternal() === void 0) {
|
|
582
|
+
globalThis.moose_internal = moose_internal;
|
|
583
|
+
}
|
|
584
|
+
dlqSchema = {
|
|
585
|
+
version: "3.1",
|
|
586
|
+
components: {
|
|
587
|
+
schemas: {
|
|
588
|
+
DeadLetterModel: {
|
|
589
|
+
type: "object",
|
|
590
|
+
properties: {
|
|
591
|
+
originalRecord: {
|
|
592
|
+
$ref: "#/components/schemas/Recordstringany"
|
|
593
|
+
},
|
|
594
|
+
errorMessage: {
|
|
595
|
+
type: "string"
|
|
596
|
+
},
|
|
597
|
+
errorType: {
|
|
598
|
+
type: "string"
|
|
599
|
+
},
|
|
600
|
+
failedAt: {
|
|
601
|
+
type: "string",
|
|
602
|
+
format: "date-time"
|
|
603
|
+
},
|
|
604
|
+
source: {
|
|
605
|
+
oneOf: [
|
|
606
|
+
{
|
|
607
|
+
const: "api"
|
|
608
|
+
},
|
|
609
|
+
{
|
|
610
|
+
const: "transform"
|
|
611
|
+
},
|
|
612
|
+
{
|
|
613
|
+
const: "table"
|
|
614
|
+
}
|
|
615
|
+
]
|
|
616
|
+
}
|
|
617
|
+
},
|
|
618
|
+
required: [
|
|
619
|
+
"originalRecord",
|
|
620
|
+
"errorMessage",
|
|
621
|
+
"errorType",
|
|
622
|
+
"failedAt",
|
|
623
|
+
"source"
|
|
624
|
+
]
|
|
625
|
+
},
|
|
626
|
+
Recordstringany: {
|
|
627
|
+
type: "object",
|
|
628
|
+
properties: {},
|
|
629
|
+
required: [],
|
|
630
|
+
description: "Construct a type with a set of properties K of type T",
|
|
631
|
+
additionalProperties: {}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
},
|
|
635
|
+
schemas: [
|
|
636
|
+
{
|
|
637
|
+
$ref: "#/components/schemas/DeadLetterModel"
|
|
638
|
+
}
|
|
639
|
+
]
|
|
640
|
+
};
|
|
641
|
+
dlqColumns = [
|
|
642
|
+
{
|
|
643
|
+
name: "originalRecord",
|
|
644
|
+
data_type: "Json",
|
|
645
|
+
primary_key: false,
|
|
646
|
+
required: true,
|
|
647
|
+
unique: false,
|
|
648
|
+
default: null,
|
|
649
|
+
annotations: [],
|
|
650
|
+
ttl: null,
|
|
651
|
+
codec: null,
|
|
652
|
+
materialized: null,
|
|
653
|
+
comment: null
|
|
654
|
+
},
|
|
655
|
+
{
|
|
656
|
+
name: "errorMessage",
|
|
657
|
+
data_type: "String",
|
|
658
|
+
primary_key: false,
|
|
659
|
+
required: true,
|
|
660
|
+
unique: false,
|
|
661
|
+
default: null,
|
|
662
|
+
annotations: [],
|
|
663
|
+
ttl: null,
|
|
664
|
+
codec: null,
|
|
665
|
+
materialized: null,
|
|
666
|
+
comment: null
|
|
667
|
+
},
|
|
668
|
+
{
|
|
669
|
+
name: "errorType",
|
|
670
|
+
data_type: "String",
|
|
671
|
+
primary_key: false,
|
|
672
|
+
required: true,
|
|
673
|
+
unique: false,
|
|
674
|
+
default: null,
|
|
675
|
+
annotations: [],
|
|
676
|
+
ttl: null,
|
|
677
|
+
codec: null,
|
|
678
|
+
materialized: null,
|
|
679
|
+
comment: null
|
|
680
|
+
},
|
|
681
|
+
{
|
|
682
|
+
name: "failedAt",
|
|
683
|
+
data_type: "DateTime",
|
|
684
|
+
primary_key: false,
|
|
685
|
+
required: true,
|
|
686
|
+
unique: false,
|
|
687
|
+
default: null,
|
|
688
|
+
annotations: [],
|
|
689
|
+
ttl: null,
|
|
690
|
+
codec: null,
|
|
691
|
+
materialized: null,
|
|
692
|
+
comment: null
|
|
693
|
+
},
|
|
694
|
+
{
|
|
695
|
+
name: "source",
|
|
696
|
+
data_type: "String",
|
|
697
|
+
primary_key: false,
|
|
698
|
+
required: true,
|
|
699
|
+
unique: false,
|
|
700
|
+
default: null,
|
|
701
|
+
annotations: [],
|
|
702
|
+
ttl: null,
|
|
703
|
+
codec: null,
|
|
704
|
+
materialized: null,
|
|
705
|
+
comment: null
|
|
706
|
+
}
|
|
707
|
+
];
|
|
708
|
+
}
|
|
709
|
+
});
|
|
710
|
+
|
|
178
711
|
// src/config/configFile.ts
|
|
179
712
|
import path from "path";
|
|
180
713
|
import * as toml from "toml";
|
|
@@ -359,2129 +892,1849 @@ var init_runtime = __esm({
|
|
|
359
892
|
}
|
|
360
893
|
});
|
|
361
894
|
|
|
362
|
-
// src/dmv2/utils/stackTrace.ts
|
|
363
|
-
function shouldSkipStackLine(line) {
|
|
364
|
-
return line.includes("node_modules") || // Skip npm installed packages (prod)
|
|
365
|
-
line.includes("node:internal") || // Skip Node.js internals (modern format)
|
|
366
|
-
line.includes("internal/modules") || // Skip Node.js internals (older format)
|
|
367
|
-
line.includes("ts-node") || // Skip TypeScript execution
|
|
368
|
-
line.includes("/ts-moose-lib/src/") || // Skip dev/linked moose-lib src (Unix)
|
|
369
|
-
line.includes("\\ts-moose-lib\\src\\") || // Skip dev/linked moose-lib src (Windows)
|
|
370
|
-
line.includes("/ts-moose-lib/dist/") || // Skip dev/linked moose-lib dist (Unix)
|
|
371
|
-
line.includes("\\ts-moose-lib\\dist\\");
|
|
372
|
-
}
|
|
373
|
-
function parseStackLine(line) {
|
|
374
|
-
const match = line.match(/\((.*):(\d+):(\d+)\)/) || line.match(/at (.*):(\d+):(\d+)/);
|
|
375
|
-
if (match && match[1]) {
|
|
376
|
-
return {
|
|
377
|
-
file: match[1],
|
|
378
|
-
line: match[2]
|
|
379
|
-
};
|
|
380
|
-
}
|
|
381
|
-
return void 0;
|
|
382
|
-
}
|
|
383
|
-
function getSourceFileInfo(stack) {
|
|
384
|
-
if (!stack) return {};
|
|
385
|
-
const lines = stack.split("\n");
|
|
386
|
-
for (const line of lines) {
|
|
387
|
-
if (shouldSkipStackLine(line)) continue;
|
|
388
|
-
const info = parseStackLine(line);
|
|
389
|
-
if (info) return info;
|
|
390
|
-
}
|
|
391
|
-
return {};
|
|
392
|
-
}
|
|
393
|
-
function getSourceLocationFromStack(stack) {
|
|
394
|
-
if (!stack) return void 0;
|
|
395
|
-
const lines = stack.split("\n");
|
|
396
|
-
for (const line of lines.slice(1)) {
|
|
397
|
-
if (shouldSkipStackLine(line)) {
|
|
398
|
-
continue;
|
|
399
|
-
}
|
|
400
|
-
const v8Match = line.match(/at\s+(?:.*?\s+\()?(.+):(\d+):(\d+)\)?/);
|
|
401
|
-
if (v8Match) {
|
|
402
|
-
return {
|
|
403
|
-
file: v8Match[1],
|
|
404
|
-
line: parseInt(v8Match[2], 10),
|
|
405
|
-
column: parseInt(v8Match[3], 10)
|
|
406
|
-
};
|
|
407
|
-
}
|
|
408
|
-
const smMatch = line.match(/(?:.*@)?(.+):(\d+):(\d+)/);
|
|
409
|
-
if (smMatch) {
|
|
410
|
-
return {
|
|
411
|
-
file: smMatch[1],
|
|
412
|
-
line: parseInt(smMatch[2], 10),
|
|
413
|
-
column: parseInt(smMatch[3], 10)
|
|
414
|
-
};
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
return void 0;
|
|
418
|
-
}
|
|
419
|
-
function getSourceFileFromStack(stack) {
|
|
420
|
-
const location = getSourceLocationFromStack(stack);
|
|
421
|
-
return location?.file;
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
// src/dmv2/typedBase.ts
|
|
425
|
-
var TypedBase = class {
|
|
426
|
-
/** The JSON schema representation of type T. Injected by the compiler plugin. */
|
|
427
|
-
schema;
|
|
428
|
-
/** The name assigned to this resource instance. */
|
|
429
|
-
name;
|
|
430
|
-
/** A dictionary mapping column names (keys of T) to their Column definitions. */
|
|
431
|
-
columns;
|
|
432
|
-
/** An array containing the Column definitions for this resource. Injected by the compiler plugin. */
|
|
433
|
-
columnArray;
|
|
434
|
-
/** The configuration object specific to this resource type. */
|
|
435
|
-
config;
|
|
436
|
-
/** Typia validation functions for type T. Injected by the compiler plugin for OlapTable. */
|
|
437
|
-
validators;
|
|
438
|
-
/** Optional metadata for the resource, always present as an object. */
|
|
439
|
-
metadata;
|
|
440
|
-
/**
|
|
441
|
-
* Whether this resource allows extra fields beyond the defined columns.
|
|
442
|
-
* When true, extra fields in payloads are passed through to streaming functions.
|
|
443
|
-
* Injected by the compiler plugin when the type has an index signature.
|
|
444
|
-
*/
|
|
445
|
-
allowExtraFields;
|
|
446
|
-
/**
|
|
447
|
-
* @internal Constructor intended for internal use by subclasses and the compiler plugin.
|
|
448
|
-
* It expects the schema and columns to be provided, typically injected by the compiler.
|
|
449
|
-
*
|
|
450
|
-
* @param name The name for the resource instance.
|
|
451
|
-
* @param config The configuration object for the resource.
|
|
452
|
-
* @param schema The JSON schema for the resource's data type T (injected).
|
|
453
|
-
* @param columns The array of Column definitions for T (injected).
|
|
454
|
-
* @param allowExtraFields Whether extra fields are allowed (injected when type has index signature).
|
|
455
|
-
*/
|
|
456
|
-
constructor(name, config, schema, columns, validators, allowExtraFields) {
|
|
457
|
-
if (schema === void 0 || columns === void 0) {
|
|
458
|
-
throw new Error(
|
|
459
|
-
"Supply the type param T so that the schema is inserted by the compiler plugin."
|
|
460
|
-
);
|
|
461
|
-
}
|
|
462
|
-
this.schema = schema;
|
|
463
|
-
this.columnArray = columns;
|
|
464
|
-
const columnsObj = {};
|
|
465
|
-
columns.forEach((column) => {
|
|
466
|
-
columnsObj[column.name] = column;
|
|
467
|
-
});
|
|
468
|
-
this.columns = columnsObj;
|
|
469
|
-
this.name = name;
|
|
470
|
-
this.config = config;
|
|
471
|
-
this.validators = validators;
|
|
472
|
-
this.allowExtraFields = allowExtraFields ?? false;
|
|
473
|
-
this.metadata = config?.metadata ? { ...config.metadata } : {};
|
|
474
|
-
if (!this.metadata.source) {
|
|
475
|
-
const stack = new Error().stack;
|
|
476
|
-
if (stack) {
|
|
477
|
-
const info = getSourceFileInfo(stack);
|
|
478
|
-
this.metadata.source = { file: info.file, line: info.line };
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
};
|
|
483
|
-
|
|
484
|
-
// src/dataModels/dataModelTypes.ts
|
|
485
|
-
function isArrayNestedType(dt) {
|
|
486
|
-
return typeof dt === "object" && dt !== null && dt.elementType !== null && typeof dt.elementType === "object" && dt.elementType.hasOwnProperty("columns") && Array.isArray(dt.elementType.columns);
|
|
487
|
-
}
|
|
488
|
-
function isNestedType(dt) {
|
|
489
|
-
return typeof dt === "object" && dt !== null && Array.isArray(dt.columns);
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
// src/sqlHelpers.ts
|
|
493
|
-
var quoteIdentifier = (name) => {
|
|
494
|
-
return name.startsWith("`") && name.endsWith("`") ? name : `\`${name}\``;
|
|
495
|
-
};
|
|
496
|
-
var toStaticQuery = (sql3) => {
|
|
497
|
-
const [query, params] = toQuery(sql3);
|
|
498
|
-
if (Object.keys(params).length !== 0) {
|
|
499
|
-
throw new Error(
|
|
500
|
-
"Dynamic SQL is not allowed in the select statement in view creation."
|
|
501
|
-
);
|
|
502
|
-
}
|
|
503
|
-
return query;
|
|
504
|
-
};
|
|
505
|
-
var toQuery = (sql3) => {
|
|
506
|
-
const parameterizedStubs = sql3.values.map(
|
|
507
|
-
(v, i) => createClickhouseParameter(i, v)
|
|
508
|
-
);
|
|
509
|
-
const query = sql3.strings.map(
|
|
510
|
-
(s, i) => s != "" ? `${s}${emptyIfUndefined(parameterizedStubs[i])}` : ""
|
|
511
|
-
).join("");
|
|
512
|
-
const query_params = sql3.values.reduce(
|
|
513
|
-
(acc, v, i) => ({
|
|
514
|
-
...acc,
|
|
515
|
-
[`p${i}`]: getValueFromParameter(v)
|
|
516
|
-
}),
|
|
517
|
-
{}
|
|
518
|
-
);
|
|
519
|
-
return [query, query_params];
|
|
520
|
-
};
|
|
521
|
-
var getValueFromParameter = (value) => {
|
|
522
|
-
if (Array.isArray(value)) {
|
|
523
|
-
const [type, val] = value;
|
|
524
|
-
if (type === "Identifier") return val;
|
|
525
|
-
}
|
|
526
|
-
return value;
|
|
527
|
-
};
|
|
528
|
-
function createClickhouseParameter(parameterIndex, value) {
|
|
529
|
-
return `{p${parameterIndex}:${mapToClickHouseType(value)}}`;
|
|
530
|
-
}
|
|
531
|
-
var mapToClickHouseType = (value) => {
|
|
532
|
-
if (typeof value === "number") {
|
|
533
|
-
return Number.isInteger(value) ? "Int" : "Float";
|
|
534
|
-
}
|
|
535
|
-
if (typeof value === "boolean") return "Bool";
|
|
536
|
-
if (value instanceof Date) return "DateTime";
|
|
537
|
-
if (Array.isArray(value)) {
|
|
538
|
-
const [type, _] = value;
|
|
539
|
-
return type;
|
|
540
|
-
}
|
|
541
|
-
return "String";
|
|
542
|
-
};
|
|
543
|
-
function emptyIfUndefined(value) {
|
|
544
|
-
return value === void 0 ? "" : value;
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
// src/blocks/helpers.ts
|
|
548
|
-
function dropView(name) {
|
|
549
|
-
return `DROP VIEW IF EXISTS ${quoteIdentifier(name)}`.trim();
|
|
550
|
-
}
|
|
551
|
-
function createMaterializedView(options) {
|
|
552
|
-
return `CREATE MATERIALIZED VIEW IF NOT EXISTS ${quoteIdentifier(options.name)}
|
|
553
|
-
TO ${quoteIdentifier(options.destinationTable)}
|
|
554
|
-
AS ${options.select}`.trim();
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
// src/dmv2/internal.ts
|
|
558
|
-
import process2 from "process";
|
|
559
|
-
|
|
560
|
-
// src/index.ts
|
|
561
|
-
init_commons();
|
|
562
|
-
|
|
563
|
-
// src/consumption-apis/helpers.ts
|
|
564
|
-
import {
|
|
565
|
-
Client as TemporalClient,
|
|
566
|
-
Connection
|
|
567
|
-
} from "@temporalio/client";
|
|
568
|
-
import { createHash, randomUUID } from "crypto";
|
|
569
|
-
|
|
570
|
-
// src/consumption-apis/runner.ts
|
|
571
|
-
init_commons();
|
|
572
|
-
import * as jose from "jose";
|
|
573
|
-
|
|
574
|
-
// src/cluster-utils.ts
|
|
575
|
-
import cluster from "cluster";
|
|
576
|
-
import { availableParallelism } from "os";
|
|
577
|
-
import { exit } from "process";
|
|
578
|
-
|
|
579
|
-
// src/clients/redisClient.ts
|
|
580
|
-
import { createClient as createClient2 } from "redis";
|
|
581
|
-
|
|
582
|
-
// src/consumption-apis/standalone.ts
|
|
583
|
-
init_commons();
|
|
584
|
-
|
|
585
|
-
// src/utilities/dataParser.ts
|
|
586
|
-
import { parse } from "csv-parse";
|
|
587
|
-
var CSV_DELIMITERS = {
|
|
588
|
-
COMMA: ",",
|
|
589
|
-
TAB: " ",
|
|
590
|
-
SEMICOLON: ";",
|
|
591
|
-
PIPE: "|"
|
|
592
|
-
};
|
|
593
|
-
var DEFAULT_CSV_CONFIG = {
|
|
594
|
-
delimiter: CSV_DELIMITERS.COMMA,
|
|
595
|
-
columns: true,
|
|
596
|
-
skipEmptyLines: true,
|
|
597
|
-
trim: true
|
|
598
|
-
};
|
|
599
|
-
|
|
600
|
-
// src/dmv2/internal.ts
|
|
601
|
-
init_commons();
|
|
602
|
-
var isClientOnlyMode = () => process2.env.MOOSE_CLIENT_ONLY === "true";
|
|
603
|
-
var moose_internal = {
|
|
604
|
-
tables: /* @__PURE__ */ new Map(),
|
|
605
|
-
streams: /* @__PURE__ */ new Map(),
|
|
606
|
-
ingestApis: /* @__PURE__ */ new Map(),
|
|
607
|
-
apis: /* @__PURE__ */ new Map(),
|
|
608
|
-
sqlResources: /* @__PURE__ */ new Map(),
|
|
609
|
-
workflows: /* @__PURE__ */ new Map(),
|
|
610
|
-
webApps: /* @__PURE__ */ new Map()
|
|
611
|
-
};
|
|
612
|
-
var defaultRetentionPeriod = 60 * 60 * 24 * 7;
|
|
613
|
-
var getMooseInternal = () => globalThis.moose_internal;
|
|
614
|
-
if (getMooseInternal() === void 0) {
|
|
615
|
-
globalThis.moose_internal = moose_internal;
|
|
616
|
-
}
|
|
617
|
-
var dlqSchema = {
|
|
618
|
-
version: "3.1",
|
|
619
|
-
components: {
|
|
620
|
-
schemas: {
|
|
621
|
-
DeadLetterModel: {
|
|
622
|
-
type: "object",
|
|
623
|
-
properties: {
|
|
624
|
-
originalRecord: {
|
|
625
|
-
$ref: "#/components/schemas/Recordstringany"
|
|
626
|
-
},
|
|
627
|
-
errorMessage: {
|
|
628
|
-
type: "string"
|
|
629
|
-
},
|
|
630
|
-
errorType: {
|
|
631
|
-
type: "string"
|
|
632
|
-
},
|
|
633
|
-
failedAt: {
|
|
634
|
-
type: "string",
|
|
635
|
-
format: "date-time"
|
|
636
|
-
},
|
|
637
|
-
source: {
|
|
638
|
-
oneOf: [
|
|
639
|
-
{
|
|
640
|
-
const: "api"
|
|
641
|
-
},
|
|
642
|
-
{
|
|
643
|
-
const: "transform"
|
|
644
|
-
},
|
|
645
|
-
{
|
|
646
|
-
const: "table"
|
|
647
|
-
}
|
|
648
|
-
]
|
|
649
|
-
}
|
|
650
|
-
},
|
|
651
|
-
required: [
|
|
652
|
-
"originalRecord",
|
|
653
|
-
"errorMessage",
|
|
654
|
-
"errorType",
|
|
655
|
-
"failedAt",
|
|
656
|
-
"source"
|
|
657
|
-
]
|
|
658
|
-
},
|
|
659
|
-
Recordstringany: {
|
|
660
|
-
type: "object",
|
|
661
|
-
properties: {},
|
|
662
|
-
required: [],
|
|
663
|
-
description: "Construct a type with a set of properties K of type T",
|
|
664
|
-
additionalProperties: {}
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
},
|
|
668
|
-
schemas: [
|
|
669
|
-
{
|
|
670
|
-
$ref: "#/components/schemas/DeadLetterModel"
|
|
671
|
-
}
|
|
672
|
-
]
|
|
673
|
-
};
|
|
674
|
-
var dlqColumns = [
|
|
675
|
-
{
|
|
676
|
-
name: "originalRecord",
|
|
677
|
-
data_type: "Json",
|
|
678
|
-
primary_key: false,
|
|
679
|
-
required: true,
|
|
680
|
-
unique: false,
|
|
681
|
-
default: null,
|
|
682
|
-
annotations: [],
|
|
683
|
-
ttl: null,
|
|
684
|
-
codec: null,
|
|
685
|
-
materialized: null,
|
|
686
|
-
comment: null
|
|
687
|
-
},
|
|
688
|
-
{
|
|
689
|
-
name: "errorMessage",
|
|
690
|
-
data_type: "String",
|
|
691
|
-
primary_key: false,
|
|
692
|
-
required: true,
|
|
693
|
-
unique: false,
|
|
694
|
-
default: null,
|
|
695
|
-
annotations: [],
|
|
696
|
-
ttl: null,
|
|
697
|
-
codec: null,
|
|
698
|
-
materialized: null,
|
|
699
|
-
comment: null
|
|
700
|
-
},
|
|
701
|
-
{
|
|
702
|
-
name: "errorType",
|
|
703
|
-
data_type: "String",
|
|
704
|
-
primary_key: false,
|
|
705
|
-
required: true,
|
|
706
|
-
unique: false,
|
|
707
|
-
default: null,
|
|
708
|
-
annotations: [],
|
|
709
|
-
ttl: null,
|
|
710
|
-
codec: null,
|
|
711
|
-
materialized: null,
|
|
712
|
-
comment: null
|
|
713
|
-
},
|
|
714
|
-
{
|
|
715
|
-
name: "failedAt",
|
|
716
|
-
data_type: "DateTime",
|
|
717
|
-
primary_key: false,
|
|
718
|
-
required: true,
|
|
719
|
-
unique: false,
|
|
720
|
-
default: null,
|
|
721
|
-
annotations: [],
|
|
722
|
-
ttl: null,
|
|
723
|
-
codec: null,
|
|
724
|
-
materialized: null,
|
|
725
|
-
comment: null
|
|
726
|
-
},
|
|
727
|
-
{
|
|
728
|
-
name: "source",
|
|
729
|
-
data_type: "String",
|
|
730
|
-
primary_key: false,
|
|
731
|
-
required: true,
|
|
732
|
-
unique: false,
|
|
733
|
-
default: null,
|
|
734
|
-
annotations: [],
|
|
735
|
-
ttl: null,
|
|
736
|
-
codec: null,
|
|
737
|
-
materialized: null,
|
|
738
|
-
comment: null
|
|
739
|
-
}
|
|
740
|
-
];
|
|
741
|
-
|
|
742
895
|
// src/dmv2/sdk/olapTable.ts
|
|
743
896
|
import { Readable } from "stream";
|
|
744
897
|
import { createHash as createHash2 } from "crypto";
|
|
745
|
-
var OlapTable
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
const tableVersion = this.config.version;
|
|
792
|
-
if (!tableVersion) {
|
|
793
|
-
this._cachedTableName = this.name;
|
|
794
|
-
} else {
|
|
795
|
-
const versionSuffix = tableVersion.replace(/\./g, "_");
|
|
796
|
-
this._cachedTableName = `${this.name}_${versionSuffix}`;
|
|
797
|
-
}
|
|
798
|
-
return this._cachedTableName;
|
|
799
|
-
}
|
|
800
|
-
/**
|
|
801
|
-
* Creates a fast hash of the ClickHouse configuration.
|
|
802
|
-
* Uses crypto.createHash for better performance than JSON.stringify.
|
|
803
|
-
*
|
|
804
|
-
* @private
|
|
805
|
-
*/
|
|
806
|
-
createConfigHash(clickhouseConfig) {
|
|
807
|
-
const effectiveDatabase = this.config.database ?? clickhouseConfig.database;
|
|
808
|
-
const configString = `${clickhouseConfig.host}:${clickhouseConfig.port}:${clickhouseConfig.username}:${clickhouseConfig.password}:${effectiveDatabase}:${clickhouseConfig.useSSL}`;
|
|
809
|
-
return createHash2("sha256").update(configString).digest("hex").substring(0, 16);
|
|
810
|
-
}
|
|
811
|
-
/**
|
|
812
|
-
* Gets or creates a memoized ClickHouse client.
|
|
813
|
-
* The client is cached and reused across multiple insert calls for better performance.
|
|
814
|
-
* If the configuration changes, a new client will be created.
|
|
815
|
-
*
|
|
816
|
-
* @private
|
|
817
|
-
*/
|
|
818
|
-
async getMemoizedClient() {
|
|
819
|
-
await Promise.resolve().then(() => (init_runtime(), runtime_exports));
|
|
820
|
-
const configRegistry = globalThis._mooseConfigRegistry;
|
|
821
|
-
const { getClickhouseClient: getClickhouseClient2 } = await Promise.resolve().then(() => (init_commons(), commons_exports));
|
|
822
|
-
const clickhouseConfig = await configRegistry.getClickHouseConfig();
|
|
823
|
-
const currentConfigHash = this.createConfigHash(clickhouseConfig);
|
|
824
|
-
if (this._memoizedClient && this._configHash === currentConfigHash) {
|
|
825
|
-
return { client: this._memoizedClient, config: clickhouseConfig };
|
|
826
|
-
}
|
|
827
|
-
if (this._memoizedClient && this._configHash !== currentConfigHash) {
|
|
828
|
-
try {
|
|
829
|
-
await this._memoizedClient.close();
|
|
830
|
-
} catch (error) {
|
|
898
|
+
var OlapTable;
|
|
899
|
+
var init_olapTable = __esm({
|
|
900
|
+
"src/dmv2/sdk/olapTable.ts"() {
|
|
901
|
+
"use strict";
|
|
902
|
+
init_typedBase();
|
|
903
|
+
init_dataModelTypes();
|
|
904
|
+
init_helpers();
|
|
905
|
+
init_internal();
|
|
906
|
+
init_sqlHelpers();
|
|
907
|
+
OlapTable = class extends TypedBase {
|
|
908
|
+
name;
|
|
909
|
+
/** @internal */
|
|
910
|
+
kind = "OlapTable";
|
|
911
|
+
/** @internal Memoized ClickHouse client for reusing connections across insert calls */
|
|
912
|
+
_memoizedClient;
|
|
913
|
+
/** @internal Hash of the configuration used to create the memoized client */
|
|
914
|
+
_configHash;
|
|
915
|
+
/** @internal Cached table name to avoid repeated generation */
|
|
916
|
+
_cachedTableName;
|
|
917
|
+
constructor(name, config, schema, columns, validators) {
|
|
918
|
+
const resolvedConfig = config ? "engine" in config ? config : { ...config, engine: "MergeTree" /* MergeTree */ } : { engine: "MergeTree" /* MergeTree */ };
|
|
919
|
+
const hasFields = Array.isArray(resolvedConfig.orderByFields) && resolvedConfig.orderByFields.length > 0;
|
|
920
|
+
const hasExpr = typeof resolvedConfig.orderByExpression === "string" && resolvedConfig.orderByExpression.length > 0;
|
|
921
|
+
if (hasFields && hasExpr) {
|
|
922
|
+
throw new Error(
|
|
923
|
+
`OlapTable ${name}: Provide either orderByFields or orderByExpression, not both.`
|
|
924
|
+
);
|
|
925
|
+
}
|
|
926
|
+
const hasCluster = typeof resolvedConfig.cluster === "string";
|
|
927
|
+
const hasKeeperPath = typeof resolvedConfig.keeperPath === "string";
|
|
928
|
+
const hasReplicaName = typeof resolvedConfig.replicaName === "string";
|
|
929
|
+
if (hasCluster && (hasKeeperPath || hasReplicaName)) {
|
|
930
|
+
throw new Error(
|
|
931
|
+
`OlapTable ${name}: Cannot specify both 'cluster' and explicit replication params ('keeperPath' or 'replicaName'). Use 'cluster' for auto-injected params, or use explicit 'keeperPath' and 'replicaName' without 'cluster'.`
|
|
932
|
+
);
|
|
933
|
+
}
|
|
934
|
+
super(name, resolvedConfig, schema, columns, validators);
|
|
935
|
+
this.name = name;
|
|
936
|
+
const tables = getMooseInternal().tables;
|
|
937
|
+
const registryKey = this.config.version ? `${name}_${this.config.version}` : name;
|
|
938
|
+
if (!isClientOnlyMode() && tables.has(registryKey)) {
|
|
939
|
+
throw new Error(
|
|
940
|
+
`OlapTable with name ${name} and version ${config?.version ?? "unversioned"} already exists`
|
|
941
|
+
);
|
|
942
|
+
}
|
|
943
|
+
tables.set(registryKey, this);
|
|
831
944
|
}
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
* This is useful for cleaning up connections when the table instance is no longer needed.
|
|
849
|
-
* The client will be automatically recreated on the next insert call if needed.
|
|
850
|
-
*/
|
|
851
|
-
async closeClient() {
|
|
852
|
-
if (this._memoizedClient) {
|
|
853
|
-
try {
|
|
854
|
-
await this._memoizedClient.close();
|
|
855
|
-
} catch (error) {
|
|
856
|
-
} finally {
|
|
857
|
-
this._memoizedClient = void 0;
|
|
858
|
-
this._configHash = void 0;
|
|
945
|
+
/**
|
|
946
|
+
* Generates the versioned table name following Moose's naming convention
|
|
947
|
+
* Format: {tableName}_{version_with_dots_replaced_by_underscores}
|
|
948
|
+
*/
|
|
949
|
+
generateTableName() {
|
|
950
|
+
if (this._cachedTableName) {
|
|
951
|
+
return this._cachedTableName;
|
|
952
|
+
}
|
|
953
|
+
const tableVersion = this.config.version;
|
|
954
|
+
if (!tableVersion) {
|
|
955
|
+
this._cachedTableName = this.name;
|
|
956
|
+
} else {
|
|
957
|
+
const versionSuffix = tableVersion.replace(/\./g, "_");
|
|
958
|
+
this._cachedTableName = `${this.name}_${versionSuffix}`;
|
|
959
|
+
}
|
|
960
|
+
return this._cachedTableName;
|
|
859
961
|
}
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
if (this.validators?.validate) {
|
|
871
|
-
try {
|
|
872
|
-
const result = this.validators.validate(record);
|
|
873
|
-
return {
|
|
874
|
-
success: result.success,
|
|
875
|
-
data: result.data,
|
|
876
|
-
errors: result.errors?.map(
|
|
877
|
-
(err) => typeof err === "string" ? err : JSON.stringify(err)
|
|
878
|
-
)
|
|
879
|
-
};
|
|
880
|
-
} catch (error) {
|
|
881
|
-
return {
|
|
882
|
-
success: false,
|
|
883
|
-
errors: [error instanceof Error ? error.message : String(error)]
|
|
884
|
-
};
|
|
962
|
+
/**
|
|
963
|
+
* Creates a fast hash of the ClickHouse configuration.
|
|
964
|
+
* Uses crypto.createHash for better performance than JSON.stringify.
|
|
965
|
+
*
|
|
966
|
+
* @private
|
|
967
|
+
*/
|
|
968
|
+
createConfigHash(clickhouseConfig) {
|
|
969
|
+
const effectiveDatabase = this.config.database ?? clickhouseConfig.database;
|
|
970
|
+
const configString = `${clickhouseConfig.host}:${clickhouseConfig.port}:${clickhouseConfig.username}:${clickhouseConfig.password}:${effectiveDatabase}:${clickhouseConfig.useSSL}`;
|
|
971
|
+
return createHash2("sha256").update(configString).digest("hex").substring(0, 16);
|
|
885
972
|
}
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
973
|
+
/**
|
|
974
|
+
* Gets or creates a memoized ClickHouse client.
|
|
975
|
+
* The client is cached and reused across multiple insert calls for better performance.
|
|
976
|
+
* If the configuration changes, a new client will be created.
|
|
977
|
+
*
|
|
978
|
+
* @private
|
|
979
|
+
*/
|
|
980
|
+
async getMemoizedClient() {
|
|
981
|
+
await Promise.resolve().then(() => (init_runtime(), runtime_exports));
|
|
982
|
+
const configRegistry = globalThis._mooseConfigRegistry;
|
|
983
|
+
const { getClickhouseClient: getClickhouseClient2 } = await Promise.resolve().then(() => (init_commons(), commons_exports));
|
|
984
|
+
const clickhouseConfig = await configRegistry.getClickHouseConfig();
|
|
985
|
+
const currentConfigHash = this.createConfigHash(clickhouseConfig);
|
|
986
|
+
if (this._memoizedClient && this._configHash === currentConfigHash) {
|
|
987
|
+
return { client: this._memoizedClient, config: clickhouseConfig };
|
|
988
|
+
}
|
|
989
|
+
if (this._memoizedClient && this._configHash !== currentConfigHash) {
|
|
990
|
+
try {
|
|
991
|
+
await this._memoizedClient.close();
|
|
992
|
+
} catch (error) {
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
const effectiveDatabase = this.config.database ?? clickhouseConfig.database;
|
|
996
|
+
const client = getClickhouseClient2({
|
|
997
|
+
username: clickhouseConfig.username,
|
|
998
|
+
password: clickhouseConfig.password,
|
|
999
|
+
database: effectiveDatabase,
|
|
1000
|
+
useSSL: clickhouseConfig.useSSL ? "true" : "false",
|
|
1001
|
+
host: clickhouseConfig.host,
|
|
1002
|
+
port: clickhouseConfig.port
|
|
1003
|
+
});
|
|
1004
|
+
this._memoizedClient = client;
|
|
1005
|
+
this._configHash = currentConfigHash;
|
|
1006
|
+
return { client, config: clickhouseConfig };
|
|
1007
|
+
}
|
|
1008
|
+
/**
|
|
1009
|
+
* Closes the memoized ClickHouse client if it exists.
|
|
1010
|
+
* This is useful for cleaning up connections when the table instance is no longer needed.
|
|
1011
|
+
* The client will be automatically recreated on the next insert call if needed.
|
|
1012
|
+
*/
|
|
1013
|
+
async closeClient() {
|
|
1014
|
+
if (this._memoizedClient) {
|
|
1015
|
+
try {
|
|
1016
|
+
await this._memoizedClient.close();
|
|
1017
|
+
} catch (error) {
|
|
1018
|
+
} finally {
|
|
1019
|
+
this._memoizedClient = void 0;
|
|
1020
|
+
this._configHash = void 0;
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
/**
|
|
1025
|
+
* Validates a single record using typia's comprehensive type checking.
|
|
1026
|
+
* This provides the most accurate validation as it uses the exact TypeScript type information.
|
|
1027
|
+
*
|
|
1028
|
+
* @param record The record to validate
|
|
1029
|
+
* @returns Validation result with detailed error information
|
|
1030
|
+
*/
|
|
1031
|
+
validateRecord(record) {
|
|
1032
|
+
if (this.validators?.validate) {
|
|
1033
|
+
try {
|
|
1034
|
+
const result = this.validators.validate(record);
|
|
1035
|
+
return {
|
|
1036
|
+
success: result.success,
|
|
1037
|
+
data: result.data,
|
|
1038
|
+
errors: result.errors?.map(
|
|
1039
|
+
(err) => typeof err === "string" ? err : JSON.stringify(err)
|
|
1040
|
+
)
|
|
1041
|
+
};
|
|
1042
|
+
} catch (error) {
|
|
1043
|
+
return {
|
|
1044
|
+
success: false,
|
|
1045
|
+
errors: [error instanceof Error ? error.message : String(error)]
|
|
1046
|
+
};
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
throw new Error("No typia validator found");
|
|
1050
|
+
}
|
|
1051
|
+
/**
|
|
1052
|
+
* Type guard function using typia's is() function.
|
|
1053
|
+
* Provides compile-time type narrowing for TypeScript.
|
|
1054
|
+
*
|
|
1055
|
+
* @param record The record to check
|
|
1056
|
+
* @returns True if record matches type T, with type narrowing
|
|
1057
|
+
*/
|
|
1058
|
+
isValidRecord(record) {
|
|
1059
|
+
if (this.validators?.is) {
|
|
1060
|
+
return this.validators.is(record);
|
|
1061
|
+
}
|
|
1062
|
+
throw new Error("No typia validator found");
|
|
1063
|
+
}
|
|
1064
|
+
/**
|
|
1065
|
+
* Assert that a record matches type T, throwing detailed errors if not.
|
|
1066
|
+
* Uses typia's assert() function for the most detailed error reporting.
|
|
1067
|
+
*
|
|
1068
|
+
* @param record The record to assert
|
|
1069
|
+
* @returns The validated and typed record
|
|
1070
|
+
* @throws Detailed validation error if record doesn't match type T
|
|
1071
|
+
*/
|
|
1072
|
+
assertValidRecord(record) {
|
|
1073
|
+
if (this.validators?.assert) {
|
|
1074
|
+
return this.validators.assert(record);
|
|
1075
|
+
}
|
|
1076
|
+
throw new Error("No typia validator found");
|
|
1077
|
+
}
|
|
1078
|
+
/**
|
|
1079
|
+
* Validates an array of records with comprehensive error reporting.
|
|
1080
|
+
* Uses the most appropriate validation method available (typia or basic).
|
|
1081
|
+
*
|
|
1082
|
+
* @param data Array of records to validate
|
|
1083
|
+
* @returns Detailed validation results
|
|
1084
|
+
*/
|
|
1085
|
+
async validateRecords(data) {
|
|
1086
|
+
const valid = [];
|
|
1087
|
+
const invalid = [];
|
|
1088
|
+
valid.length = 0;
|
|
1089
|
+
invalid.length = 0;
|
|
1090
|
+
const dataLength = data.length;
|
|
1091
|
+
for (let i = 0; i < dataLength; i++) {
|
|
1092
|
+
const record = data[i];
|
|
1093
|
+
try {
|
|
1094
|
+
if (this.isValidRecord(record)) {
|
|
1095
|
+
valid.push(this.mapToClickhouseRecord(record));
|
|
1096
|
+
} else {
|
|
1097
|
+
const result = this.validateRecord(record);
|
|
1098
|
+
if (result.success) {
|
|
1099
|
+
valid.push(this.mapToClickhouseRecord(record));
|
|
1100
|
+
} else {
|
|
1101
|
+
invalid.push({
|
|
1102
|
+
record,
|
|
1103
|
+
error: result.errors?.join(", ") || "Validation failed",
|
|
1104
|
+
index: i,
|
|
1105
|
+
path: "root"
|
|
1106
|
+
});
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
} catch (error) {
|
|
939
1110
|
invalid.push({
|
|
940
1111
|
record,
|
|
941
|
-
error:
|
|
1112
|
+
error: error instanceof Error ? error.message : String(error),
|
|
942
1113
|
index: i,
|
|
943
1114
|
path: "root"
|
|
944
1115
|
});
|
|
945
1116
|
}
|
|
946
1117
|
}
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
path: "root"
|
|
953
|
-
});
|
|
1118
|
+
return {
|
|
1119
|
+
valid,
|
|
1120
|
+
invalid,
|
|
1121
|
+
total: dataLength
|
|
1122
|
+
};
|
|
954
1123
|
}
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
const successful = [];
|
|
970
|
-
const failed = [];
|
|
971
|
-
const RETRY_BATCH_SIZE = 10;
|
|
972
|
-
const totalRecords = records.length;
|
|
973
|
-
for (let i = 0; i < totalRecords; i += RETRY_BATCH_SIZE) {
|
|
974
|
-
const batchEnd = Math.min(i + RETRY_BATCH_SIZE, totalRecords);
|
|
975
|
-
const batch = records.slice(i, batchEnd);
|
|
976
|
-
try {
|
|
977
|
-
await client.insert({
|
|
978
|
-
table: quoteIdentifier(tableName),
|
|
979
|
-
values: batch,
|
|
980
|
-
format: "JSONEachRow",
|
|
981
|
-
clickhouse_settings: {
|
|
982
|
-
date_time_input_format: "best_effort",
|
|
983
|
-
// Add performance settings for retries
|
|
984
|
-
max_insert_block_size: RETRY_BATCH_SIZE,
|
|
985
|
-
max_block_size: RETRY_BATCH_SIZE
|
|
986
|
-
}
|
|
987
|
-
});
|
|
988
|
-
successful.push(...batch);
|
|
989
|
-
} catch (batchError) {
|
|
990
|
-
for (let j = 0; j < batch.length; j++) {
|
|
991
|
-
const record = batch[j];
|
|
1124
|
+
/**
|
|
1125
|
+
* Optimized batch retry that minimizes individual insert operations.
|
|
1126
|
+
* Groups records into smaller batches to reduce round trips while still isolating failures.
|
|
1127
|
+
*
|
|
1128
|
+
* @private
|
|
1129
|
+
*/
|
|
1130
|
+
async retryIndividualRecords(client, tableName, records) {
|
|
1131
|
+
const successful = [];
|
|
1132
|
+
const failed = [];
|
|
1133
|
+
const RETRY_BATCH_SIZE = 10;
|
|
1134
|
+
const totalRecords = records.length;
|
|
1135
|
+
for (let i = 0; i < totalRecords; i += RETRY_BATCH_SIZE) {
|
|
1136
|
+
const batchEnd = Math.min(i + RETRY_BATCH_SIZE, totalRecords);
|
|
1137
|
+
const batch = records.slice(i, batchEnd);
|
|
992
1138
|
try {
|
|
993
1139
|
await client.insert({
|
|
994
1140
|
table: quoteIdentifier(tableName),
|
|
995
|
-
values:
|
|
1141
|
+
values: batch,
|
|
996
1142
|
format: "JSONEachRow",
|
|
997
1143
|
clickhouse_settings: {
|
|
998
|
-
date_time_input_format: "best_effort"
|
|
1144
|
+
date_time_input_format: "best_effort",
|
|
1145
|
+
// Add performance settings for retries
|
|
1146
|
+
max_insert_block_size: RETRY_BATCH_SIZE,
|
|
1147
|
+
max_block_size: RETRY_BATCH_SIZE
|
|
999
1148
|
}
|
|
1000
1149
|
});
|
|
1001
|
-
successful.push(
|
|
1002
|
-
} catch (
|
|
1003
|
-
|
|
1004
|
-
record
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1150
|
+
successful.push(...batch);
|
|
1151
|
+
} catch (batchError) {
|
|
1152
|
+
for (let j = 0; j < batch.length; j++) {
|
|
1153
|
+
const record = batch[j];
|
|
1154
|
+
try {
|
|
1155
|
+
await client.insert({
|
|
1156
|
+
table: quoteIdentifier(tableName),
|
|
1157
|
+
values: [record],
|
|
1158
|
+
format: "JSONEachRow",
|
|
1159
|
+
clickhouse_settings: {
|
|
1160
|
+
date_time_input_format: "best_effort"
|
|
1161
|
+
}
|
|
1162
|
+
});
|
|
1163
|
+
successful.push(record);
|
|
1164
|
+
} catch (error) {
|
|
1165
|
+
failed.push({
|
|
1166
|
+
record,
|
|
1167
|
+
error: error instanceof Error ? error.message : String(error),
|
|
1168
|
+
index: i + j
|
|
1169
|
+
});
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1008
1172
|
}
|
|
1009
1173
|
}
|
|
1174
|
+
return { successful, failed };
|
|
1010
1175
|
}
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
}
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1176
|
+
/**
|
|
1177
|
+
* Validates input parameters and strategy compatibility
|
|
1178
|
+
* @private
|
|
1179
|
+
*/
|
|
1180
|
+
validateInsertParameters(data, options) {
|
|
1181
|
+
const isStream = data instanceof Readable;
|
|
1182
|
+
const strategy = options?.strategy || "fail-fast";
|
|
1183
|
+
const shouldValidate = options?.validate !== false;
|
|
1184
|
+
if (isStream && strategy === "isolate") {
|
|
1185
|
+
throw new Error(
|
|
1186
|
+
"The 'isolate' error strategy is not supported with stream input. Use 'fail-fast' or 'discard' instead."
|
|
1187
|
+
);
|
|
1188
|
+
}
|
|
1189
|
+
if (isStream && shouldValidate) {
|
|
1190
|
+
console.warn(
|
|
1191
|
+
"Validation is not supported with stream input. Validation will be skipped."
|
|
1192
|
+
);
|
|
1193
|
+
}
|
|
1194
|
+
return { isStream, strategy, shouldValidate };
|
|
1195
|
+
}
|
|
1196
|
+
/**
|
|
1197
|
+
* Handles early return cases for empty data
|
|
1198
|
+
* @private
|
|
1199
|
+
*/
|
|
1200
|
+
handleEmptyData(data, isStream) {
|
|
1201
|
+
if (isStream && !data) {
|
|
1202
|
+
return {
|
|
1203
|
+
successful: 0,
|
|
1204
|
+
failed: 0,
|
|
1205
|
+
total: 0
|
|
1206
|
+
};
|
|
1207
|
+
}
|
|
1208
|
+
if (!isStream && (!data || data.length === 0)) {
|
|
1209
|
+
return {
|
|
1210
|
+
successful: 0,
|
|
1211
|
+
failed: 0,
|
|
1212
|
+
total: 0
|
|
1213
|
+
};
|
|
1214
|
+
}
|
|
1215
|
+
return null;
|
|
1216
|
+
}
|
|
1217
|
+
/**
|
|
1218
|
+
* Performs pre-insertion validation for array data
|
|
1219
|
+
* @private
|
|
1220
|
+
*/
|
|
1221
|
+
async performPreInsertionValidation(data, shouldValidate, strategy, options) {
|
|
1222
|
+
if (!shouldValidate) {
|
|
1223
|
+
return { validatedData: data, validationErrors: [] };
|
|
1224
|
+
}
|
|
1225
|
+
try {
|
|
1226
|
+
const validationResult = await this.validateRecords(data);
|
|
1227
|
+
const validatedData = validationResult.valid;
|
|
1228
|
+
const validationErrors = validationResult.invalid;
|
|
1229
|
+
if (validationErrors.length > 0) {
|
|
1230
|
+
this.handleValidationErrors(validationErrors, strategy, data, options);
|
|
1231
|
+
switch (strategy) {
|
|
1232
|
+
case "discard":
|
|
1233
|
+
return { validatedData, validationErrors };
|
|
1234
|
+
case "isolate":
|
|
1235
|
+
return { validatedData: data, validationErrors };
|
|
1236
|
+
default:
|
|
1237
|
+
return { validatedData, validationErrors };
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
return { validatedData, validationErrors };
|
|
1241
|
+
} catch (validationError) {
|
|
1242
|
+
if (strategy === "fail-fast") {
|
|
1243
|
+
throw validationError;
|
|
1244
|
+
}
|
|
1245
|
+
console.warn("Validation error:", validationError);
|
|
1246
|
+
return { validatedData: data, validationErrors: [] };
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
/**
|
|
1250
|
+
* Handles validation errors based on the specified strategy
|
|
1251
|
+
* @private
|
|
1252
|
+
*/
|
|
1253
|
+
handleValidationErrors(validationErrors, strategy, data, options) {
|
|
1069
1254
|
switch (strategy) {
|
|
1255
|
+
case "fail-fast":
|
|
1256
|
+
const firstError = validationErrors[0];
|
|
1257
|
+
throw new Error(
|
|
1258
|
+
`Validation failed for record at index ${firstError.index}: ${firstError.error}`
|
|
1259
|
+
);
|
|
1070
1260
|
case "discard":
|
|
1071
|
-
|
|
1261
|
+
this.checkValidationThresholds(validationErrors, data.length, options);
|
|
1262
|
+
break;
|
|
1072
1263
|
case "isolate":
|
|
1073
|
-
|
|
1074
|
-
default:
|
|
1075
|
-
return { validatedData, validationErrors };
|
|
1264
|
+
break;
|
|
1076
1265
|
}
|
|
1077
1266
|
}
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1267
|
+
/**
|
|
1268
|
+
* Checks if validation errors exceed configured thresholds
|
|
1269
|
+
* @private
|
|
1270
|
+
*/
|
|
1271
|
+
checkValidationThresholds(validationErrors, totalRecords, options) {
|
|
1272
|
+
const validationFailedCount = validationErrors.length;
|
|
1273
|
+
const validationFailedRatio = validationFailedCount / totalRecords;
|
|
1274
|
+
if (options?.allowErrors !== void 0 && validationFailedCount > options.allowErrors) {
|
|
1275
|
+
throw new Error(
|
|
1276
|
+
`Too many validation failures: ${validationFailedCount} > ${options.allowErrors}. Errors: ${validationErrors.map((e) => e.error).join(", ")}`
|
|
1277
|
+
);
|
|
1278
|
+
}
|
|
1279
|
+
if (options?.allowErrorsRatio !== void 0 && validationFailedRatio > options.allowErrorsRatio) {
|
|
1280
|
+
throw new Error(
|
|
1281
|
+
`Validation failure ratio too high: ${validationFailedRatio.toFixed(3)} > ${options.allowErrorsRatio}. Errors: ${validationErrors.map((e) => e.error).join(", ")}`
|
|
1282
|
+
);
|
|
1283
|
+
}
|
|
1082
1284
|
}
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
throw new Error(
|
|
1119
|
-
`Validation failure ratio too high: ${validationFailedRatio.toFixed(3)} > ${options.allowErrorsRatio}. Errors: ${validationErrors.map((e) => e.error).join(", ")}`
|
|
1120
|
-
);
|
|
1121
|
-
}
|
|
1122
|
-
}
|
|
1123
|
-
/**
|
|
1124
|
-
* Optimized insert options preparation with better memory management
|
|
1125
|
-
* @private
|
|
1126
|
-
*/
|
|
1127
|
-
prepareInsertOptions(tableName, data, validatedData, isStream, strategy, options) {
|
|
1128
|
-
const insertOptions = {
|
|
1129
|
-
table: quoteIdentifier(tableName),
|
|
1130
|
-
format: "JSONEachRow",
|
|
1131
|
-
clickhouse_settings: {
|
|
1132
|
-
date_time_input_format: "best_effort",
|
|
1133
|
-
wait_end_of_query: 1,
|
|
1134
|
-
// Ensure at least once delivery for INSERT operations
|
|
1135
|
-
// Performance optimizations
|
|
1136
|
-
max_insert_block_size: isStream ? 1e5 : Math.min(validatedData.length, 1e5),
|
|
1137
|
-
max_block_size: 65536,
|
|
1138
|
-
// Use async inserts for better performance with large datasets
|
|
1139
|
-
async_insert: validatedData.length > 1e3 ? 1 : 0,
|
|
1140
|
-
wait_for_async_insert: 1
|
|
1141
|
-
// For at least once delivery
|
|
1285
|
+
/**
|
|
1286
|
+
* Optimized insert options preparation with better memory management
|
|
1287
|
+
* @private
|
|
1288
|
+
*/
|
|
1289
|
+
prepareInsertOptions(tableName, data, validatedData, isStream, strategy, options) {
|
|
1290
|
+
const insertOptions = {
|
|
1291
|
+
table: quoteIdentifier(tableName),
|
|
1292
|
+
format: "JSONEachRow",
|
|
1293
|
+
clickhouse_settings: {
|
|
1294
|
+
date_time_input_format: "best_effort",
|
|
1295
|
+
wait_end_of_query: 1,
|
|
1296
|
+
// Ensure at least once delivery for INSERT operations
|
|
1297
|
+
// Performance optimizations
|
|
1298
|
+
max_insert_block_size: isStream ? 1e5 : Math.min(validatedData.length, 1e5),
|
|
1299
|
+
max_block_size: 65536,
|
|
1300
|
+
// Use async inserts for better performance with large datasets
|
|
1301
|
+
async_insert: validatedData.length > 1e3 ? 1 : 0,
|
|
1302
|
+
wait_for_async_insert: 1
|
|
1303
|
+
// For at least once delivery
|
|
1304
|
+
}
|
|
1305
|
+
};
|
|
1306
|
+
if (isStream) {
|
|
1307
|
+
insertOptions.values = data;
|
|
1308
|
+
} else {
|
|
1309
|
+
insertOptions.values = validatedData;
|
|
1310
|
+
}
|
|
1311
|
+
if (strategy === "discard" && (options?.allowErrors !== void 0 || options?.allowErrorsRatio !== void 0)) {
|
|
1312
|
+
if (options.allowErrors !== void 0) {
|
|
1313
|
+
insertOptions.clickhouse_settings.input_format_allow_errors_num = options.allowErrors;
|
|
1314
|
+
}
|
|
1315
|
+
if (options.allowErrorsRatio !== void 0) {
|
|
1316
|
+
insertOptions.clickhouse_settings.input_format_allow_errors_ratio = options.allowErrorsRatio;
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
return insertOptions;
|
|
1142
1320
|
}
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1321
|
+
/**
|
|
1322
|
+
* Creates success result for completed insertions
|
|
1323
|
+
* @private
|
|
1324
|
+
*/
|
|
1325
|
+
createSuccessResult(data, validatedData, validationErrors, isStream, shouldValidate, strategy) {
|
|
1326
|
+
if (isStream) {
|
|
1327
|
+
return {
|
|
1328
|
+
successful: -1,
|
|
1329
|
+
// -1 indicates stream mode where count is unknown
|
|
1330
|
+
failed: 0,
|
|
1331
|
+
total: -1
|
|
1332
|
+
};
|
|
1333
|
+
}
|
|
1334
|
+
const insertedCount = validatedData.length;
|
|
1335
|
+
const totalProcessed = shouldValidate ? data.length : insertedCount;
|
|
1336
|
+
const result = {
|
|
1337
|
+
successful: insertedCount,
|
|
1338
|
+
failed: shouldValidate ? validationErrors.length : 0,
|
|
1339
|
+
total: totalProcessed
|
|
1340
|
+
};
|
|
1341
|
+
if (shouldValidate && validationErrors.length > 0 && strategy === "discard") {
|
|
1342
|
+
result.failedRecords = validationErrors.map((ve) => ({
|
|
1343
|
+
record: ve.record,
|
|
1344
|
+
error: `Validation error: ${ve.error}`,
|
|
1345
|
+
index: ve.index
|
|
1346
|
+
}));
|
|
1347
|
+
}
|
|
1348
|
+
return result;
|
|
1152
1349
|
}
|
|
1153
|
-
|
|
1154
|
-
|
|
1350
|
+
/**
|
|
1351
|
+
* Handles insertion errors based on the specified strategy
|
|
1352
|
+
* @private
|
|
1353
|
+
*/
|
|
1354
|
+
async handleInsertionError(batchError, strategy, tableName, data, validatedData, validationErrors, isStream, shouldValidate, options) {
|
|
1355
|
+
switch (strategy) {
|
|
1356
|
+
case "fail-fast":
|
|
1357
|
+
throw new Error(
|
|
1358
|
+
`Failed to insert data into table ${tableName}: ${batchError}`
|
|
1359
|
+
);
|
|
1360
|
+
case "discard":
|
|
1361
|
+
throw new Error(
|
|
1362
|
+
`Too many errors during insert into table ${tableName}. Error threshold exceeded: ${batchError}`
|
|
1363
|
+
);
|
|
1364
|
+
case "isolate":
|
|
1365
|
+
return await this.handleIsolateStrategy(
|
|
1366
|
+
batchError,
|
|
1367
|
+
tableName,
|
|
1368
|
+
data,
|
|
1369
|
+
validatedData,
|
|
1370
|
+
validationErrors,
|
|
1371
|
+
isStream,
|
|
1372
|
+
shouldValidate,
|
|
1373
|
+
options
|
|
1374
|
+
);
|
|
1375
|
+
default:
|
|
1376
|
+
throw new Error(`Unknown error strategy: ${strategy}`);
|
|
1377
|
+
}
|
|
1155
1378
|
}
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
return {
|
|
1166
|
-
successful: -1,
|
|
1167
|
-
// -1 indicates stream mode where count is unknown
|
|
1168
|
-
failed: 0,
|
|
1169
|
-
total: -1
|
|
1170
|
-
};
|
|
1171
|
-
}
|
|
1172
|
-
const insertedCount = validatedData.length;
|
|
1173
|
-
const totalProcessed = shouldValidate ? data.length : insertedCount;
|
|
1174
|
-
const result = {
|
|
1175
|
-
successful: insertedCount,
|
|
1176
|
-
failed: shouldValidate ? validationErrors.length : 0,
|
|
1177
|
-
total: totalProcessed
|
|
1178
|
-
};
|
|
1179
|
-
if (shouldValidate && validationErrors.length > 0 && strategy === "discard") {
|
|
1180
|
-
result.failedRecords = validationErrors.map((ve) => ({
|
|
1181
|
-
record: ve.record,
|
|
1182
|
-
error: `Validation error: ${ve.error}`,
|
|
1183
|
-
index: ve.index
|
|
1184
|
-
}));
|
|
1185
|
-
}
|
|
1186
|
-
return result;
|
|
1187
|
-
}
|
|
1188
|
-
/**
|
|
1189
|
-
* Handles insertion errors based on the specified strategy
|
|
1190
|
-
* @private
|
|
1191
|
-
*/
|
|
1192
|
-
async handleInsertionError(batchError, strategy, tableName, data, validatedData, validationErrors, isStream, shouldValidate, options) {
|
|
1193
|
-
switch (strategy) {
|
|
1194
|
-
case "fail-fast":
|
|
1195
|
-
throw new Error(
|
|
1196
|
-
`Failed to insert data into table ${tableName}: ${batchError}`
|
|
1197
|
-
);
|
|
1198
|
-
case "discard":
|
|
1199
|
-
throw new Error(
|
|
1200
|
-
`Too many errors during insert into table ${tableName}. Error threshold exceeded: ${batchError}`
|
|
1201
|
-
);
|
|
1202
|
-
case "isolate":
|
|
1203
|
-
return await this.handleIsolateStrategy(
|
|
1204
|
-
batchError,
|
|
1205
|
-
tableName,
|
|
1206
|
-
data,
|
|
1207
|
-
validatedData,
|
|
1208
|
-
validationErrors,
|
|
1209
|
-
isStream,
|
|
1210
|
-
shouldValidate,
|
|
1211
|
-
options
|
|
1212
|
-
);
|
|
1213
|
-
default:
|
|
1214
|
-
throw new Error(`Unknown error strategy: ${strategy}`);
|
|
1215
|
-
}
|
|
1216
|
-
}
|
|
1217
|
-
/**
|
|
1218
|
-
* Handles the isolate strategy for insertion errors
|
|
1219
|
-
* @private
|
|
1220
|
-
*/
|
|
1221
|
-
async handleIsolateStrategy(batchError, tableName, data, validatedData, validationErrors, isStream, shouldValidate, options) {
|
|
1222
|
-
if (isStream) {
|
|
1223
|
-
throw new Error(
|
|
1224
|
-
`Isolate strategy is not supported with stream input: ${batchError}`
|
|
1225
|
-
);
|
|
1226
|
-
}
|
|
1227
|
-
try {
|
|
1228
|
-
const { client } = await this.getMemoizedClient();
|
|
1229
|
-
const skipValidationOnRetry = options?.skipValidationOnRetry || false;
|
|
1230
|
-
const retryData = skipValidationOnRetry ? data : validatedData;
|
|
1231
|
-
const { successful, failed } = await this.retryIndividualRecords(
|
|
1232
|
-
client,
|
|
1233
|
-
tableName,
|
|
1234
|
-
retryData
|
|
1235
|
-
);
|
|
1236
|
-
const allFailedRecords = [
|
|
1237
|
-
// Validation errors (if any and not skipping validation on retry)
|
|
1238
|
-
...shouldValidate && !skipValidationOnRetry ? validationErrors.map((ve) => ({
|
|
1239
|
-
record: ve.record,
|
|
1240
|
-
error: `Validation error: ${ve.error}`,
|
|
1241
|
-
index: ve.index
|
|
1242
|
-
})) : [],
|
|
1243
|
-
// Insertion errors
|
|
1244
|
-
...failed
|
|
1245
|
-
];
|
|
1246
|
-
this.checkInsertionThresholds(
|
|
1247
|
-
allFailedRecords,
|
|
1248
|
-
data.length,
|
|
1249
|
-
options
|
|
1250
|
-
);
|
|
1251
|
-
return {
|
|
1252
|
-
successful: successful.length,
|
|
1253
|
-
failed: allFailedRecords.length,
|
|
1254
|
-
total: data.length,
|
|
1255
|
-
failedRecords: allFailedRecords
|
|
1256
|
-
};
|
|
1257
|
-
} catch (isolationError) {
|
|
1258
|
-
throw new Error(
|
|
1259
|
-
`Failed to insert data into table ${tableName} during record isolation: ${isolationError}`
|
|
1260
|
-
);
|
|
1261
|
-
}
|
|
1262
|
-
}
|
|
1263
|
-
/**
|
|
1264
|
-
* Checks if insertion errors exceed configured thresholds
|
|
1265
|
-
* @private
|
|
1266
|
-
*/
|
|
1267
|
-
checkInsertionThresholds(failedRecords, totalRecords, options) {
|
|
1268
|
-
const totalFailed = failedRecords.length;
|
|
1269
|
-
const failedRatio = totalFailed / totalRecords;
|
|
1270
|
-
if (options?.allowErrors !== void 0 && totalFailed > options.allowErrors) {
|
|
1271
|
-
throw new Error(
|
|
1272
|
-
`Too many failed records: ${totalFailed} > ${options.allowErrors}. Failed records: ${failedRecords.map((f) => f.error).join(", ")}`
|
|
1273
|
-
);
|
|
1274
|
-
}
|
|
1275
|
-
if (options?.allowErrorsRatio !== void 0 && failedRatio > options.allowErrorsRatio) {
|
|
1276
|
-
throw new Error(
|
|
1277
|
-
`Failed record ratio too high: ${failedRatio.toFixed(3)} > ${options.allowErrorsRatio}. Failed records: ${failedRecords.map((f) => f.error).join(", ")}`
|
|
1278
|
-
);
|
|
1279
|
-
}
|
|
1280
|
-
}
|
|
1281
|
-
/**
|
|
1282
|
-
* Recursively transforms a record to match ClickHouse's JSONEachRow requirements
|
|
1283
|
-
*
|
|
1284
|
-
* - For every Array(Nested(...)) field at any depth, each item is wrapped in its own array and recursively processed.
|
|
1285
|
-
* - For every Nested struct (not array), it recurses into the struct.
|
|
1286
|
-
* - This ensures compatibility with kafka_clickhouse_sync
|
|
1287
|
-
*
|
|
1288
|
-
* @param record The input record to transform (may be deeply nested)
|
|
1289
|
-
* @param columns The schema columns for this level (defaults to this.columnArray at the top level)
|
|
1290
|
-
* @returns The transformed record, ready for ClickHouse JSONEachRow insertion
|
|
1291
|
-
*/
|
|
1292
|
-
mapToClickhouseRecord(record, columns = this.columnArray) {
|
|
1293
|
-
const result = { ...record };
|
|
1294
|
-
for (const col of columns) {
|
|
1295
|
-
const value = record[col.name];
|
|
1296
|
-
const dt = col.data_type;
|
|
1297
|
-
if (isArrayNestedType(dt)) {
|
|
1298
|
-
if (Array.isArray(value) && (value.length === 0 || typeof value[0] === "object")) {
|
|
1299
|
-
result[col.name] = value.map((item) => [
|
|
1300
|
-
this.mapToClickhouseRecord(item, dt.elementType.columns)
|
|
1301
|
-
]);
|
|
1379
|
+
/**
|
|
1380
|
+
* Handles the isolate strategy for insertion errors
|
|
1381
|
+
* @private
|
|
1382
|
+
*/
|
|
1383
|
+
async handleIsolateStrategy(batchError, tableName, data, validatedData, validationErrors, isStream, shouldValidate, options) {
|
|
1384
|
+
if (isStream) {
|
|
1385
|
+
throw new Error(
|
|
1386
|
+
`Isolate strategy is not supported with stream input: ${batchError}`
|
|
1387
|
+
);
|
|
1302
1388
|
}
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1389
|
+
try {
|
|
1390
|
+
const { client } = await this.getMemoizedClient();
|
|
1391
|
+
const skipValidationOnRetry = options?.skipValidationOnRetry || false;
|
|
1392
|
+
const retryData = skipValidationOnRetry ? data : validatedData;
|
|
1393
|
+
const { successful, failed } = await this.retryIndividualRecords(
|
|
1394
|
+
client,
|
|
1395
|
+
tableName,
|
|
1396
|
+
retryData
|
|
1397
|
+
);
|
|
1398
|
+
const allFailedRecords = [
|
|
1399
|
+
// Validation errors (if any and not skipping validation on retry)
|
|
1400
|
+
...shouldValidate && !skipValidationOnRetry ? validationErrors.map((ve) => ({
|
|
1401
|
+
record: ve.record,
|
|
1402
|
+
error: `Validation error: ${ve.error}`,
|
|
1403
|
+
index: ve.index
|
|
1404
|
+
})) : [],
|
|
1405
|
+
// Insertion errors
|
|
1406
|
+
...failed
|
|
1407
|
+
];
|
|
1408
|
+
this.checkInsertionThresholds(
|
|
1409
|
+
allFailedRecords,
|
|
1410
|
+
data.length,
|
|
1411
|
+
options
|
|
1412
|
+
);
|
|
1413
|
+
return {
|
|
1414
|
+
successful: successful.length,
|
|
1415
|
+
failed: allFailedRecords.length,
|
|
1416
|
+
total: data.length,
|
|
1417
|
+
failedRecords: allFailedRecords
|
|
1418
|
+
};
|
|
1419
|
+
} catch (isolationError) {
|
|
1420
|
+
throw new Error(
|
|
1421
|
+
`Failed to insert data into table ${tableName} during record isolation: ${isolationError}`
|
|
1422
|
+
);
|
|
1306
1423
|
}
|
|
1307
1424
|
}
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1425
|
+
/**
|
|
1426
|
+
* Checks if insertion errors exceed configured thresholds
|
|
1427
|
+
* @private
|
|
1428
|
+
*/
|
|
1429
|
+
checkInsertionThresholds(failedRecords, totalRecords, options) {
|
|
1430
|
+
const totalFailed = failedRecords.length;
|
|
1431
|
+
const failedRatio = totalFailed / totalRecords;
|
|
1432
|
+
if (options?.allowErrors !== void 0 && totalFailed > options.allowErrors) {
|
|
1433
|
+
throw new Error(
|
|
1434
|
+
`Too many failed records: ${totalFailed} > ${options.allowErrors}. Failed records: ${failedRecords.map((f) => f.error).join(", ")}`
|
|
1435
|
+
);
|
|
1436
|
+
}
|
|
1437
|
+
if (options?.allowErrorsRatio !== void 0 && failedRatio > options.allowErrorsRatio) {
|
|
1438
|
+
throw new Error(
|
|
1439
|
+
`Failed record ratio too high: ${failedRatio.toFixed(3)} > ${options.allowErrorsRatio}. Failed records: ${failedRecords.map((f) => f.error).join(", ")}`
|
|
1440
|
+
);
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
/**
|
|
1444
|
+
* Recursively transforms a record to match ClickHouse's JSONEachRow requirements
|
|
1445
|
+
*
|
|
1446
|
+
* - For every Array(Nested(...)) field at any depth, each item is wrapped in its own array and recursively processed.
|
|
1447
|
+
* - For every Nested struct (not array), it recurses into the struct.
|
|
1448
|
+
* - This ensures compatibility with kafka_clickhouse_sync
|
|
1449
|
+
*
|
|
1450
|
+
* @param record The input record to transform (may be deeply nested)
|
|
1451
|
+
* @param columns The schema columns for this level (defaults to this.columnArray at the top level)
|
|
1452
|
+
* @returns The transformed record, ready for ClickHouse JSONEachRow insertion
|
|
1453
|
+
*/
|
|
1454
|
+
mapToClickhouseRecord(record, columns = this.columnArray) {
|
|
1455
|
+
const result = { ...record };
|
|
1456
|
+
for (const col of columns) {
|
|
1457
|
+
const value = record[col.name];
|
|
1458
|
+
const dt = col.data_type;
|
|
1459
|
+
if (isArrayNestedType(dt)) {
|
|
1460
|
+
if (Array.isArray(value) && (value.length === 0 || typeof value[0] === "object")) {
|
|
1461
|
+
result[col.name] = value.map((item) => [
|
|
1462
|
+
this.mapToClickhouseRecord(item, dt.elementType.columns)
|
|
1463
|
+
]);
|
|
1464
|
+
}
|
|
1465
|
+
} else if (isNestedType(dt)) {
|
|
1466
|
+
if (value && typeof value === "object") {
|
|
1467
|
+
result[col.name] = this.mapToClickhouseRecord(value, dt.columns);
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
return result;
|
|
1472
|
+
}
|
|
1473
|
+
/**
|
|
1474
|
+
* Inserts data directly into the ClickHouse table with enhanced error handling and validation.
|
|
1475
|
+
* This method establishes a direct connection to ClickHouse using the project configuration
|
|
1476
|
+
* and inserts the provided data into the versioned table.
|
|
1477
|
+
*
|
|
1478
|
+
* PERFORMANCE OPTIMIZATIONS:
|
|
1479
|
+
* - Memoized client connections with fast config hashing
|
|
1480
|
+
* - Single-pass validation with pre-allocated arrays
|
|
1481
|
+
* - Batch-optimized retry strategy (batches of 10, then individual)
|
|
1482
|
+
* - Optimized ClickHouse settings for large datasets
|
|
1483
|
+
* - Reduced memory allocations and object creation
|
|
1484
|
+
*
|
|
1485
|
+
* Uses advanced typia validation when available for comprehensive type checking,
|
|
1486
|
+
* with fallback to basic validation for compatibility.
|
|
1487
|
+
*
|
|
1488
|
+
* The ClickHouse client is memoized and reused across multiple insert calls for better performance.
|
|
1489
|
+
* If the configuration changes, a new client will be automatically created.
|
|
1490
|
+
*
|
|
1491
|
+
* @param data Array of objects conforming to the table schema, or a Node.js Readable stream
|
|
1492
|
+
* @param options Optional configuration for error handling, validation, and insertion behavior
|
|
1493
|
+
* @returns Promise resolving to detailed insertion results
|
|
1494
|
+
* @throws {ConfigError} When configuration cannot be read or parsed
|
|
1495
|
+
* @throws {ClickHouseError} When insertion fails based on the error strategy
|
|
1496
|
+
* @throws {ValidationError} When validation fails and strategy is 'fail-fast'
|
|
1497
|
+
*
|
|
1498
|
+
* @example
|
|
1499
|
+
* ```typescript
|
|
1500
|
+
* // Create an OlapTable instance (typia validators auto-injected)
|
|
1501
|
+
* const userTable = new OlapTable<User>('users');
|
|
1502
|
+
*
|
|
1503
|
+
* // Insert with comprehensive typia validation
|
|
1504
|
+
* const result1 = await userTable.insert([
|
|
1505
|
+
* { id: 1, name: 'John', email: 'john@example.com' },
|
|
1506
|
+
* { id: 2, name: 'Jane', email: 'jane@example.com' }
|
|
1507
|
+
* ]);
|
|
1508
|
+
*
|
|
1509
|
+
* // Insert data with stream input (validation not available for streams)
|
|
1510
|
+
* const dataStream = new Readable({
|
|
1511
|
+
* objectMode: true,
|
|
1512
|
+
* read() { // Stream implementation }
|
|
1513
|
+
* });
|
|
1514
|
+
* const result2 = await userTable.insert(dataStream, { strategy: 'fail-fast' });
|
|
1515
|
+
*
|
|
1516
|
+
* // Insert with validation disabled for performance
|
|
1517
|
+
* const result3 = await userTable.insert(data, { validate: false });
|
|
1518
|
+
*
|
|
1519
|
+
* // Insert with error handling strategies
|
|
1520
|
+
* const result4 = await userTable.insert(mixedData, {
|
|
1521
|
+
* strategy: 'isolate',
|
|
1522
|
+
* allowErrorsRatio: 0.1,
|
|
1523
|
+
* validate: true // Use typia validation (default)
|
|
1524
|
+
* });
|
|
1525
|
+
*
|
|
1526
|
+
* // Optional: Clean up connection when completely done
|
|
1527
|
+
* await userTable.closeClient();
|
|
1528
|
+
* ```
|
|
1529
|
+
*/
|
|
1530
|
+
async insert(data, options) {
|
|
1531
|
+
const { isStream, strategy, shouldValidate } = this.validateInsertParameters(data, options);
|
|
1532
|
+
const emptyResult = this.handleEmptyData(data, isStream);
|
|
1533
|
+
if (emptyResult) {
|
|
1534
|
+
return emptyResult;
|
|
1535
|
+
}
|
|
1536
|
+
let validatedData = [];
|
|
1537
|
+
let validationErrors = [];
|
|
1538
|
+
if (!isStream && shouldValidate) {
|
|
1539
|
+
const validationResult = await this.performPreInsertionValidation(
|
|
1540
|
+
data,
|
|
1541
|
+
shouldValidate,
|
|
1542
|
+
strategy,
|
|
1543
|
+
options
|
|
1544
|
+
);
|
|
1545
|
+
validatedData = validationResult.validatedData;
|
|
1546
|
+
validationErrors = validationResult.validationErrors;
|
|
1547
|
+
} else {
|
|
1548
|
+
validatedData = isStream ? [] : data;
|
|
1549
|
+
}
|
|
1550
|
+
const { client } = await this.getMemoizedClient();
|
|
1551
|
+
const tableName = this.generateTableName();
|
|
1552
|
+
try {
|
|
1553
|
+
const insertOptions = this.prepareInsertOptions(
|
|
1554
|
+
tableName,
|
|
1555
|
+
data,
|
|
1556
|
+
validatedData,
|
|
1557
|
+
isStream,
|
|
1558
|
+
strategy,
|
|
1559
|
+
options
|
|
1560
|
+
);
|
|
1561
|
+
await client.insert(insertOptions);
|
|
1562
|
+
return this.createSuccessResult(
|
|
1563
|
+
data,
|
|
1564
|
+
validatedData,
|
|
1565
|
+
validationErrors,
|
|
1566
|
+
isStream,
|
|
1567
|
+
shouldValidate,
|
|
1568
|
+
strategy
|
|
1569
|
+
);
|
|
1570
|
+
} catch (batchError) {
|
|
1571
|
+
return await this.handleInsertionError(
|
|
1572
|
+
batchError,
|
|
1573
|
+
strategy,
|
|
1574
|
+
tableName,
|
|
1575
|
+
data,
|
|
1576
|
+
validatedData,
|
|
1577
|
+
validationErrors,
|
|
1578
|
+
isStream,
|
|
1579
|
+
shouldValidate,
|
|
1580
|
+
options
|
|
1581
|
+
);
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
// Note: Static factory methods (withS3Queue, withReplacingMergeTree, withMergeTree)
|
|
1585
|
+
// were removed in ENG-856. Use direct configuration instead, e.g.:
|
|
1586
|
+
// new OlapTable(name, { engine: ClickHouseEngines.ReplacingMergeTree, orderByFields: ["id"], ver: "updated_at" })
|
|
1587
|
+
};
|
|
1421
1588
|
}
|
|
1422
|
-
|
|
1423
|
-
// were removed in ENG-856. Use direct configuration instead, e.g.:
|
|
1424
|
-
// new OlapTable(name, { engine: ClickHouseEngines.ReplacingMergeTree, orderByFields: ["id"], ver: "updated_at" })
|
|
1425
|
-
};
|
|
1589
|
+
});
|
|
1426
1590
|
|
|
1427
1591
|
// src/dmv2/sdk/stream.ts
|
|
1428
1592
|
import { createHash as createHash3 } from "crypto";
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
const streams = getMooseInternal().streams;
|
|
1454
|
-
if (streams.has(name)) {
|
|
1455
|
-
throw new Error(`Stream with name ${name} already exists`);
|
|
1456
|
-
}
|
|
1457
|
-
streams.set(name, this);
|
|
1458
|
-
this.defaultDeadLetterQueue = this.config.defaultDeadLetterQueue;
|
|
1459
|
-
}
|
|
1460
|
-
/**
|
|
1461
|
-
* Internal map storing transformation configurations.
|
|
1462
|
-
* Maps destination stream names to arrays of transformation functions and their configs.
|
|
1463
|
-
*
|
|
1464
|
-
* @internal
|
|
1465
|
-
*/
|
|
1466
|
-
_transformations = /* @__PURE__ */ new Map();
|
|
1467
|
-
/**
|
|
1468
|
-
* Internal function for multi-stream transformations.
|
|
1469
|
-
* Allows a single transformation to route messages to multiple destinations.
|
|
1470
|
-
*
|
|
1471
|
-
* @internal
|
|
1472
|
-
*/
|
|
1473
|
-
_multipleTransformations;
|
|
1474
|
-
/**
|
|
1475
|
-
* Internal array storing consumer configurations.
|
|
1476
|
-
*
|
|
1477
|
-
* @internal
|
|
1478
|
-
*/
|
|
1479
|
-
_consumers = new Array();
|
|
1480
|
-
/**
|
|
1481
|
-
* Builds the full Kafka topic name including optional namespace and version suffix.
|
|
1482
|
-
* Version suffix is appended as _x_y_z where dots in version are replaced with underscores.
|
|
1483
|
-
*/
|
|
1484
|
-
buildFullTopicName(namespace) {
|
|
1485
|
-
const versionSuffix = this.config.version ? `_${this.config.version.replace(/\./g, "_")}` : "";
|
|
1486
|
-
const base = `${this.name}${versionSuffix}`;
|
|
1487
|
-
return namespace !== void 0 && namespace.length > 0 ? `${namespace}.${base}` : base;
|
|
1488
|
-
}
|
|
1489
|
-
/**
|
|
1490
|
-
* Creates a fast hash string from relevant Kafka configuration fields.
|
|
1491
|
-
*/
|
|
1492
|
-
createConfigHash(kafkaConfig) {
|
|
1493
|
-
const configString = [
|
|
1494
|
-
kafkaConfig.broker,
|
|
1495
|
-
kafkaConfig.messageTimeoutMs,
|
|
1496
|
-
kafkaConfig.saslUsername,
|
|
1497
|
-
kafkaConfig.saslPassword,
|
|
1498
|
-
kafkaConfig.saslMechanism,
|
|
1499
|
-
kafkaConfig.securityProtocol,
|
|
1500
|
-
kafkaConfig.namespace
|
|
1501
|
-
].join(":");
|
|
1502
|
-
return createHash3("sha256").update(configString).digest("hex").substring(0, 16);
|
|
1503
|
-
}
|
|
1504
|
-
/**
|
|
1505
|
-
* Gets or creates a memoized KafkaJS producer using runtime configuration.
|
|
1506
|
-
*/
|
|
1507
|
-
async getMemoizedProducer() {
|
|
1508
|
-
await Promise.resolve().then(() => (init_runtime(), runtime_exports));
|
|
1509
|
-
const configRegistry = globalThis._mooseConfigRegistry;
|
|
1510
|
-
const { getKafkaProducer: getKafkaProducer2 } = await Promise.resolve().then(() => (init_commons(), commons_exports));
|
|
1511
|
-
const kafkaConfig = await configRegistry.getKafkaConfig();
|
|
1512
|
-
const currentHash = this.createConfigHash(kafkaConfig);
|
|
1513
|
-
if (this._memoizedProducer && this._kafkaConfigHash === currentHash) {
|
|
1514
|
-
return { producer: this._memoizedProducer, kafkaConfig };
|
|
1515
|
-
}
|
|
1516
|
-
if (this._memoizedProducer && this._kafkaConfigHash !== currentHash) {
|
|
1517
|
-
try {
|
|
1518
|
-
await this._memoizedProducer.disconnect();
|
|
1519
|
-
} catch {
|
|
1520
|
-
}
|
|
1521
|
-
this._memoizedProducer = void 0;
|
|
1522
|
-
}
|
|
1523
|
-
const clientId = `moose-sdk-stream-${this.name}`;
|
|
1524
|
-
const logger = {
|
|
1525
|
-
logPrefix: clientId,
|
|
1526
|
-
log: (message) => {
|
|
1527
|
-
console.log(`${clientId}: ${message}`);
|
|
1528
|
-
},
|
|
1529
|
-
error: (message) => {
|
|
1530
|
-
console.error(`${clientId}: ${message}`);
|
|
1531
|
-
},
|
|
1532
|
-
warn: (message) => {
|
|
1533
|
-
console.warn(`${clientId}: ${message}`);
|
|
1593
|
+
function attachTypeGuard(dl, typeGuard) {
|
|
1594
|
+
dl.asTyped = () => typeGuard(dl.originalRecord);
|
|
1595
|
+
}
|
|
1596
|
+
var RoutedMessage, Stream, DeadLetterQueue;
|
|
1597
|
+
var init_stream = __esm({
|
|
1598
|
+
"src/dmv2/sdk/stream.ts"() {
|
|
1599
|
+
"use strict";
|
|
1600
|
+
init_typedBase();
|
|
1601
|
+
init_internal();
|
|
1602
|
+
init_stackTrace();
|
|
1603
|
+
RoutedMessage = class {
|
|
1604
|
+
/** The destination stream for the message */
|
|
1605
|
+
destination;
|
|
1606
|
+
/** The message value(s) to send */
|
|
1607
|
+
values;
|
|
1608
|
+
/**
|
|
1609
|
+
* Creates a new routed message.
|
|
1610
|
+
*
|
|
1611
|
+
* @param destination The target stream
|
|
1612
|
+
* @param values The message(s) to route
|
|
1613
|
+
*/
|
|
1614
|
+
constructor(destination, values) {
|
|
1615
|
+
this.destination = destination;
|
|
1616
|
+
this.values = values;
|
|
1534
1617
|
}
|
|
1535
1618
|
};
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
}
|
|
1551
|
-
/**
|
|
1552
|
-
* Closes the memoized Kafka producer if it exists.
|
|
1553
|
-
*/
|
|
1554
|
-
async closeProducer() {
|
|
1555
|
-
if (this._memoizedProducer) {
|
|
1556
|
-
try {
|
|
1557
|
-
await this._memoizedProducer.disconnect();
|
|
1558
|
-
} catch {
|
|
1559
|
-
} finally {
|
|
1560
|
-
this._memoizedProducer = void 0;
|
|
1561
|
-
this._kafkaConfigHash = void 0;
|
|
1619
|
+
Stream = class extends TypedBase {
|
|
1620
|
+
defaultDeadLetterQueue;
|
|
1621
|
+
/** @internal Memoized KafkaJS producer for reusing connections across sends */
|
|
1622
|
+
_memoizedProducer;
|
|
1623
|
+
/** @internal Hash of the configuration used to create the memoized Kafka producer */
|
|
1624
|
+
_kafkaConfigHash;
|
|
1625
|
+
constructor(name, config, schema, columns, validators, allowExtraFields) {
|
|
1626
|
+
super(name, config ?? {}, schema, columns, void 0, allowExtraFields);
|
|
1627
|
+
const streams = getMooseInternal().streams;
|
|
1628
|
+
if (streams.has(name)) {
|
|
1629
|
+
throw new Error(`Stream with name ${name} already exists`);
|
|
1630
|
+
}
|
|
1631
|
+
streams.set(name, this);
|
|
1632
|
+
this.defaultDeadLetterQueue = this.config.defaultDeadLetterQueue;
|
|
1562
1633
|
}
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1634
|
+
/**
|
|
1635
|
+
* Internal map storing transformation configurations.
|
|
1636
|
+
* Maps destination stream names to arrays of transformation functions and their configs.
|
|
1637
|
+
*
|
|
1638
|
+
* @internal
|
|
1639
|
+
*/
|
|
1640
|
+
_transformations = /* @__PURE__ */ new Map();
|
|
1641
|
+
/**
|
|
1642
|
+
* Internal function for multi-stream transformations.
|
|
1643
|
+
* Allows a single transformation to route messages to multiple destinations.
|
|
1644
|
+
*
|
|
1645
|
+
* @internal
|
|
1646
|
+
*/
|
|
1647
|
+
_multipleTransformations;
|
|
1648
|
+
/**
|
|
1649
|
+
* Internal array storing consumer configurations.
|
|
1650
|
+
*
|
|
1651
|
+
* @internal
|
|
1652
|
+
*/
|
|
1653
|
+
_consumers = new Array();
|
|
1654
|
+
/**
|
|
1655
|
+
* Builds the full Kafka topic name including optional namespace and version suffix.
|
|
1656
|
+
* Version suffix is appended as _x_y_z where dots in version are replaced with underscores.
|
|
1657
|
+
*/
|
|
1658
|
+
buildFullTopicName(namespace) {
|
|
1659
|
+
const versionSuffix = this.config.version ? `_${this.config.version.replace(/\./g, "_")}` : "";
|
|
1660
|
+
const base = `${this.name}${versionSuffix}`;
|
|
1661
|
+
return namespace !== void 0 && namespace.length > 0 ? `${namespace}.${base}` : base;
|
|
1662
|
+
}
|
|
1663
|
+
/**
|
|
1664
|
+
* Creates a fast hash string from relevant Kafka configuration fields.
|
|
1665
|
+
*/
|
|
1666
|
+
createConfigHash(kafkaConfig) {
|
|
1667
|
+
const configString = [
|
|
1668
|
+
kafkaConfig.broker,
|
|
1669
|
+
kafkaConfig.messageTimeoutMs,
|
|
1670
|
+
kafkaConfig.saslUsername,
|
|
1671
|
+
kafkaConfig.saslPassword,
|
|
1672
|
+
kafkaConfig.saslMechanism,
|
|
1673
|
+
kafkaConfig.securityProtocol,
|
|
1674
|
+
kafkaConfig.namespace
|
|
1675
|
+
].join(":");
|
|
1676
|
+
return createHash3("sha256").update(configString).digest("hex").substring(0, 16);
|
|
1677
|
+
}
|
|
1678
|
+
/**
|
|
1679
|
+
* Gets or creates a memoized KafkaJS producer using runtime configuration.
|
|
1680
|
+
*/
|
|
1681
|
+
async getMemoizedProducer() {
|
|
1682
|
+
await Promise.resolve().then(() => (init_runtime(), runtime_exports));
|
|
1683
|
+
const configRegistry = globalThis._mooseConfigRegistry;
|
|
1684
|
+
const { getKafkaProducer: getKafkaProducer2 } = await Promise.resolve().then(() => (init_commons(), commons_exports));
|
|
1685
|
+
const kafkaConfig = await configRegistry.getKafkaConfig();
|
|
1686
|
+
const currentHash = this.createConfigHash(kafkaConfig);
|
|
1687
|
+
if (this._memoizedProducer && this._kafkaConfigHash === currentHash) {
|
|
1688
|
+
return { producer: this._memoizedProducer, kafkaConfig };
|
|
1689
|
+
}
|
|
1690
|
+
if (this._memoizedProducer && this._kafkaConfigHash !== currentHash) {
|
|
1691
|
+
try {
|
|
1692
|
+
await this._memoizedProducer.disconnect();
|
|
1693
|
+
} catch {
|
|
1694
|
+
}
|
|
1695
|
+
this._memoizedProducer = void 0;
|
|
1696
|
+
}
|
|
1697
|
+
const clientId = `moose-sdk-stream-${this.name}`;
|
|
1698
|
+
const logger = {
|
|
1699
|
+
logPrefix: clientId,
|
|
1700
|
+
log: (message) => {
|
|
1701
|
+
console.log(`${clientId}: ${message}`);
|
|
1702
|
+
},
|
|
1703
|
+
error: (message) => {
|
|
1704
|
+
console.error(`${clientId}: ${message}`);
|
|
1705
|
+
},
|
|
1706
|
+
warn: (message) => {
|
|
1707
|
+
console.warn(`${clientId}: ${message}`);
|
|
1708
|
+
}
|
|
1709
|
+
};
|
|
1710
|
+
const producer = await getKafkaProducer2(
|
|
1711
|
+
{
|
|
1712
|
+
clientId,
|
|
1713
|
+
broker: kafkaConfig.broker,
|
|
1714
|
+
securityProtocol: kafkaConfig.securityProtocol,
|
|
1715
|
+
saslUsername: kafkaConfig.saslUsername,
|
|
1716
|
+
saslPassword: kafkaConfig.saslPassword,
|
|
1717
|
+
saslMechanism: kafkaConfig.saslMechanism
|
|
1718
|
+
},
|
|
1719
|
+
logger
|
|
1593
1720
|
);
|
|
1721
|
+
this._memoizedProducer = producer;
|
|
1722
|
+
this._kafkaConfigHash = currentHash;
|
|
1723
|
+
return { producer, kafkaConfig };
|
|
1594
1724
|
}
|
|
1595
|
-
|
|
1596
|
-
|
|
1725
|
+
/**
|
|
1726
|
+
* Closes the memoized Kafka producer if it exists.
|
|
1727
|
+
*/
|
|
1728
|
+
async closeProducer() {
|
|
1729
|
+
if (this._memoizedProducer) {
|
|
1730
|
+
try {
|
|
1731
|
+
await this._memoizedProducer.disconnect();
|
|
1732
|
+
} catch {
|
|
1733
|
+
} finally {
|
|
1734
|
+
this._memoizedProducer = void 0;
|
|
1735
|
+
this._kafkaConfigHash = void 0;
|
|
1736
|
+
}
|
|
1737
|
+
}
|
|
1597
1738
|
}
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
)
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1739
|
+
/**
|
|
1740
|
+
* Sends one or more records to this stream's Kafka topic.
|
|
1741
|
+
* Values are JSON-serialized as message values.
|
|
1742
|
+
*/
|
|
1743
|
+
async send(values) {
|
|
1744
|
+
const flat = Array.isArray(values) ? values : values !== void 0 && values !== null ? [values] : [];
|
|
1745
|
+
if (flat.length === 0) return;
|
|
1746
|
+
const { producer, kafkaConfig } = await this.getMemoizedProducer();
|
|
1747
|
+
const topic = this.buildFullTopicName(kafkaConfig.namespace);
|
|
1748
|
+
const sr = this.config.schemaConfig;
|
|
1749
|
+
if (sr && sr.kind === "JSON") {
|
|
1750
|
+
const schemaRegistryUrl = kafkaConfig.schemaRegistryUrl;
|
|
1751
|
+
if (!schemaRegistryUrl) {
|
|
1752
|
+
throw new Error("Schema Registry URL not configured");
|
|
1753
|
+
}
|
|
1754
|
+
const {
|
|
1755
|
+
default: { SchemaRegistry }
|
|
1756
|
+
} = await import("@kafkajs/confluent-schema-registry");
|
|
1757
|
+
const registry = new SchemaRegistry({ host: schemaRegistryUrl });
|
|
1758
|
+
let schemaId = void 0;
|
|
1759
|
+
if ("id" in sr.reference) {
|
|
1760
|
+
schemaId = sr.reference.id;
|
|
1761
|
+
} else if ("subjectLatest" in sr.reference) {
|
|
1762
|
+
schemaId = await registry.getLatestSchemaId(sr.reference.subjectLatest);
|
|
1763
|
+
} else if ("subject" in sr.reference) {
|
|
1764
|
+
schemaId = await registry.getRegistryId(
|
|
1765
|
+
sr.reference.subject,
|
|
1766
|
+
sr.reference.version
|
|
1767
|
+
);
|
|
1768
|
+
}
|
|
1769
|
+
if (schemaId === void 0) {
|
|
1770
|
+
throw new Error("Malformed schema reference.");
|
|
1771
|
+
}
|
|
1772
|
+
const encoded = await Promise.all(
|
|
1773
|
+
flat.map(
|
|
1774
|
+
(v) => registry.encode(schemaId, v)
|
|
1775
|
+
)
|
|
1776
|
+
);
|
|
1777
|
+
await producer.send({
|
|
1778
|
+
topic,
|
|
1779
|
+
messages: encoded.map((value) => ({ value }))
|
|
1780
|
+
});
|
|
1781
|
+
return;
|
|
1782
|
+
} else if (sr !== void 0) {
|
|
1783
|
+
throw new Error("Currently only JSON Schema is supported.");
|
|
1784
|
+
}
|
|
1785
|
+
await producer.send({
|
|
1786
|
+
topic,
|
|
1787
|
+
messages: flat.map((v) => ({ value: JSON.stringify(v) }))
|
|
1788
|
+
});
|
|
1642
1789
|
}
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
* @returns A `RoutedMessage` object associating the values with this stream.
|
|
1676
|
-
*
|
|
1677
|
-
* @example
|
|
1678
|
-
* ```typescript
|
|
1679
|
-
* sourceStream.addMultiTransform((record) => [
|
|
1680
|
-
* destinationStream1.routed(transformedRecord1),
|
|
1681
|
-
* destinationStream2.routed([record2a, record2b])
|
|
1682
|
-
* ]);
|
|
1683
|
-
* ```
|
|
1684
|
-
*/
|
|
1685
|
-
routed = (values) => new RoutedMessage(this, values);
|
|
1686
|
-
/**
|
|
1687
|
-
* Adds a single transformation function that can route messages to multiple destination streams.
|
|
1688
|
-
* This is an alternative to adding multiple individual `addTransform` calls.
|
|
1689
|
-
* Only one multi-transform function can be added per stream.
|
|
1690
|
-
*
|
|
1691
|
-
* @param transformation A function that takes a message of type T and returns an array of `RoutedMessage` objects,
|
|
1692
|
-
* each specifying a destination stream and the message(s) to send to it.
|
|
1693
|
-
*/
|
|
1694
|
-
addMultiTransform(transformation) {
|
|
1695
|
-
this._multipleTransformations = transformation;
|
|
1696
|
-
}
|
|
1697
|
-
};
|
|
1698
|
-
function attachTypeGuard(dl, typeGuard) {
|
|
1699
|
-
dl.asTyped = () => typeGuard(dl.originalRecord);
|
|
1700
|
-
}
|
|
1701
|
-
var DeadLetterQueue = class extends Stream {
|
|
1702
|
-
constructor(name, config, typeGuard) {
|
|
1703
|
-
if (typeGuard === void 0) {
|
|
1704
|
-
throw new Error(
|
|
1705
|
-
"Supply the type param T so that the schema is inserted by the compiler plugin."
|
|
1706
|
-
);
|
|
1707
|
-
}
|
|
1708
|
-
super(name, config ?? {}, dlqSchema, dlqColumns, void 0, false);
|
|
1709
|
-
this.typeGuard = typeGuard;
|
|
1710
|
-
getMooseInternal().streams.set(name, this);
|
|
1711
|
-
}
|
|
1712
|
-
/**
|
|
1713
|
-
* Internal type guard function for validating and casting original records.
|
|
1714
|
-
*
|
|
1715
|
-
* @internal
|
|
1716
|
-
*/
|
|
1717
|
-
typeGuard;
|
|
1718
|
-
/**
|
|
1719
|
-
* Adds a transformation step for dead letter records.
|
|
1720
|
-
* The transformation function receives a DeadLetter<T> with type recovery capabilities.
|
|
1721
|
-
*
|
|
1722
|
-
* @template U The output type for the transformation
|
|
1723
|
-
* @param destination The destination stream for transformed messages
|
|
1724
|
-
* @param transformation Function to transform dead letter records
|
|
1725
|
-
* @param config Optional transformation configuration
|
|
1726
|
-
*/
|
|
1727
|
-
addTransform(destination, transformation, config) {
|
|
1728
|
-
const withValidate = (deadLetter) => {
|
|
1729
|
-
attachTypeGuard(deadLetter, this.typeGuard);
|
|
1730
|
-
return transformation(deadLetter);
|
|
1731
|
-
};
|
|
1732
|
-
super.addTransform(destination, withValidate, config);
|
|
1733
|
-
}
|
|
1734
|
-
/**
|
|
1735
|
-
* Adds a consumer for dead letter records.
|
|
1736
|
-
* The consumer function receives a DeadLetter<T> with type recovery capabilities.
|
|
1737
|
-
*
|
|
1738
|
-
* @param consumer Function to process dead letter records
|
|
1739
|
-
* @param config Optional consumer configuration
|
|
1740
|
-
*/
|
|
1741
|
-
addConsumer(consumer, config) {
|
|
1742
|
-
const withValidate = (deadLetter) => {
|
|
1743
|
-
attachTypeGuard(deadLetter, this.typeGuard);
|
|
1744
|
-
return consumer(deadLetter);
|
|
1745
|
-
};
|
|
1746
|
-
super.addConsumer(withValidate, config);
|
|
1747
|
-
}
|
|
1748
|
-
/**
|
|
1749
|
-
* Adds a multi-stream transformation for dead letter records.
|
|
1750
|
-
* The transformation function receives a DeadLetter<T> with type recovery capabilities.
|
|
1751
|
-
*
|
|
1752
|
-
* @param transformation Function to route dead letter records to multiple destinations
|
|
1753
|
-
*/
|
|
1754
|
-
addMultiTransform(transformation) {
|
|
1755
|
-
const withValidate = (deadLetter) => {
|
|
1756
|
-
attachTypeGuard(deadLetter, this.typeGuard);
|
|
1757
|
-
return transformation(deadLetter);
|
|
1758
|
-
};
|
|
1759
|
-
super.addMultiTransform(withValidate);
|
|
1760
|
-
}
|
|
1761
|
-
};
|
|
1762
|
-
|
|
1763
|
-
// src/dmv2/sdk/workflow.ts
|
|
1764
|
-
var Task = class {
|
|
1765
|
-
/**
|
|
1766
|
-
* Creates a new Task instance.
|
|
1767
|
-
*
|
|
1768
|
-
* @param name - Unique identifier for the task
|
|
1769
|
-
* @param config - Configuration object defining the task behavior
|
|
1770
|
-
*
|
|
1771
|
-
* @example
|
|
1772
|
-
* ```typescript
|
|
1773
|
-
* // No input, no output
|
|
1774
|
-
* const task1 = new Task<null, void>("task1", {
|
|
1775
|
-
* run: async () => {
|
|
1776
|
-
* console.log("No input/output");
|
|
1777
|
-
* }
|
|
1778
|
-
* });
|
|
1779
|
-
*
|
|
1780
|
-
* // No input, but has output
|
|
1781
|
-
* const task2 = new Task<null, OutputType>("task2", {
|
|
1782
|
-
* run: async () => {
|
|
1783
|
-
* return someOutput;
|
|
1784
|
-
* }
|
|
1785
|
-
* });
|
|
1786
|
-
*
|
|
1787
|
-
* // Has input, no output
|
|
1788
|
-
* const task3 = new Task<InputType, void>("task3", {
|
|
1789
|
-
* run: async (input: InputType) => {
|
|
1790
|
-
* // process input but return nothing
|
|
1791
|
-
* }
|
|
1792
|
-
* });
|
|
1793
|
-
*
|
|
1794
|
-
* // Has both input and output
|
|
1795
|
-
* const task4 = new Task<InputType, OutputType>("task4", {
|
|
1796
|
-
* run: async (input: InputType) => {
|
|
1797
|
-
* return process(input);
|
|
1798
|
-
* }
|
|
1799
|
-
* });
|
|
1800
|
-
* ```
|
|
1801
|
-
*/
|
|
1802
|
-
constructor(name, config) {
|
|
1803
|
-
this.name = name;
|
|
1804
|
-
this.config = config;
|
|
1805
|
-
}
|
|
1806
|
-
};
|
|
1807
|
-
var Workflow = class {
|
|
1808
|
-
/**
|
|
1809
|
-
* Creates a new Workflow instance and registers it with the Moose system.
|
|
1810
|
-
*
|
|
1811
|
-
* @param name - Unique identifier for the workflow
|
|
1812
|
-
* @param config - Configuration object defining the workflow behavior and task orchestration
|
|
1813
|
-
* @throws {Error} When the workflow contains null/undefined tasks or infinite loops
|
|
1814
|
-
*/
|
|
1815
|
-
constructor(name, config) {
|
|
1816
|
-
this.name = name;
|
|
1817
|
-
this.config = config;
|
|
1818
|
-
const workflows = getMooseInternal().workflows;
|
|
1819
|
-
if (workflows.has(name)) {
|
|
1820
|
-
throw new Error(`Workflow with name ${name} already exists`);
|
|
1821
|
-
}
|
|
1822
|
-
this.validateTaskGraph(config.startingTask, name);
|
|
1823
|
-
workflows.set(name, this);
|
|
1824
|
-
}
|
|
1825
|
-
/**
|
|
1826
|
-
* Validates the task graph to ensure there are no null tasks or infinite loops.
|
|
1827
|
-
*
|
|
1828
|
-
* @private
|
|
1829
|
-
* @param startingTask - The starting task to begin validation from
|
|
1830
|
-
* @param workflowName - The name of the workflow being validated (for error messages)
|
|
1831
|
-
* @throws {Error} When null/undefined tasks are found or infinite loops are detected
|
|
1832
|
-
*/
|
|
1833
|
-
validateTaskGraph(startingTask, workflowName) {
|
|
1834
|
-
if (startingTask === null || startingTask === void 0) {
|
|
1835
|
-
throw new Error(
|
|
1836
|
-
`Workflow "${workflowName}" has a null or undefined starting task`
|
|
1837
|
-
);
|
|
1838
|
-
}
|
|
1839
|
-
const visited = /* @__PURE__ */ new Set();
|
|
1840
|
-
const recursionStack = /* @__PURE__ */ new Set();
|
|
1841
|
-
const validateTask = (task, currentPath) => {
|
|
1842
|
-
if (task === null || task === void 0) {
|
|
1843
|
-
const pathStr = currentPath.length > 0 ? currentPath.join(" -> ") + " -> " : "";
|
|
1844
|
-
throw new Error(
|
|
1845
|
-
`Workflow "${workflowName}" contains a null or undefined task in the task chain: ${pathStr}null`
|
|
1846
|
-
);
|
|
1790
|
+
/**
|
|
1791
|
+
* Adds a transformation step that processes messages from this stream and sends the results to a destination stream.
|
|
1792
|
+
* Multiple transformations to the same destination stream can be added if they have distinct `version` identifiers in their config.
|
|
1793
|
+
*
|
|
1794
|
+
* @template U The data type of the messages in the destination stream.
|
|
1795
|
+
* @param destination The destination stream for the transformed messages.
|
|
1796
|
+
* @param transformation A function that takes a message of type T and returns zero or more messages of type U (or a Promise thereof).
|
|
1797
|
+
* Return `null` or `undefined` or an empty array `[]` to filter out a message. Return an array to emit multiple messages.
|
|
1798
|
+
* @param config Optional configuration for this specific transformation step, like a version.
|
|
1799
|
+
*/
|
|
1800
|
+
addTransform(destination, transformation, config) {
|
|
1801
|
+
const sourceFile = getSourceFileFromStack(new Error().stack);
|
|
1802
|
+
const transformConfig = {
|
|
1803
|
+
...config ?? {},
|
|
1804
|
+
sourceFile
|
|
1805
|
+
};
|
|
1806
|
+
if (transformConfig.deadLetterQueue === void 0) {
|
|
1807
|
+
transformConfig.deadLetterQueue = this.defaultDeadLetterQueue;
|
|
1808
|
+
}
|
|
1809
|
+
if (this._transformations.has(destination.name)) {
|
|
1810
|
+
const existingTransforms = this._transformations.get(destination.name);
|
|
1811
|
+
const hasVersion = existingTransforms.some(
|
|
1812
|
+
([_, __, cfg]) => cfg.version === transformConfig.version
|
|
1813
|
+
);
|
|
1814
|
+
if (!hasVersion) {
|
|
1815
|
+
existingTransforms.push([destination, transformation, transformConfig]);
|
|
1816
|
+
}
|
|
1817
|
+
} else {
|
|
1818
|
+
this._transformations.set(destination.name, [
|
|
1819
|
+
[destination, transformation, transformConfig]
|
|
1820
|
+
]);
|
|
1821
|
+
}
|
|
1847
1822
|
}
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1823
|
+
/**
|
|
1824
|
+
* Adds a consumer function that processes messages from this stream.
|
|
1825
|
+
* Multiple consumers can be added if they have distinct `version` identifiers in their config.
|
|
1826
|
+
*
|
|
1827
|
+
* @param consumer A function that takes a message of type T and performs an action (e.g., side effect, logging). Should return void or Promise<void>.
|
|
1828
|
+
* @param config Optional configuration for this specific consumer, like a version.
|
|
1829
|
+
*/
|
|
1830
|
+
addConsumer(consumer, config) {
|
|
1831
|
+
const sourceFile = getSourceFileFromStack(new Error().stack);
|
|
1832
|
+
const consumerConfig = {
|
|
1833
|
+
...config ?? {},
|
|
1834
|
+
sourceFile
|
|
1835
|
+
};
|
|
1836
|
+
if (consumerConfig.deadLetterQueue === void 0) {
|
|
1837
|
+
consumerConfig.deadLetterQueue = this.defaultDeadLetterQueue;
|
|
1838
|
+
}
|
|
1839
|
+
const hasVersion = this._consumers.some(
|
|
1840
|
+
(existing) => existing.config.version === consumerConfig.version
|
|
1854
1841
|
);
|
|
1842
|
+
if (!hasVersion) {
|
|
1843
|
+
this._consumers.push({ consumer, config: consumerConfig });
|
|
1844
|
+
}
|
|
1855
1845
|
}
|
|
1856
|
-
|
|
1857
|
-
|
|
1846
|
+
/**
|
|
1847
|
+
* Helper method for `addMultiTransform` to specify the destination and values for a routed message.
|
|
1848
|
+
* @param values The value or values to send to this stream.
|
|
1849
|
+
* @returns A `RoutedMessage` object associating the values with this stream.
|
|
1850
|
+
*
|
|
1851
|
+
* @example
|
|
1852
|
+
* ```typescript
|
|
1853
|
+
* sourceStream.addMultiTransform((record) => [
|
|
1854
|
+
* destinationStream1.routed(transformedRecord1),
|
|
1855
|
+
* destinationStream2.routed([record2a, record2b])
|
|
1856
|
+
* ]);
|
|
1857
|
+
* ```
|
|
1858
|
+
*/
|
|
1859
|
+
routed = (values) => new RoutedMessage(this, values);
|
|
1860
|
+
/**
|
|
1861
|
+
* Adds a single transformation function that can route messages to multiple destination streams.
|
|
1862
|
+
* This is an alternative to adding multiple individual `addTransform` calls.
|
|
1863
|
+
* Only one multi-transform function can be added per stream.
|
|
1864
|
+
*
|
|
1865
|
+
* @param transformation A function that takes a message of type T and returns an array of `RoutedMessage` objects,
|
|
1866
|
+
* each specifying a destination stream and the message(s) to send to it.
|
|
1867
|
+
*/
|
|
1868
|
+
addMultiTransform(transformation) {
|
|
1869
|
+
this._multipleTransformations = transformation;
|
|
1858
1870
|
}
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1871
|
+
};
|
|
1872
|
+
DeadLetterQueue = class extends Stream {
|
|
1873
|
+
constructor(name, config, typeGuard) {
|
|
1874
|
+
if (typeGuard === void 0) {
|
|
1875
|
+
throw new Error(
|
|
1876
|
+
"Supply the type param T so that the schema is inserted by the compiler plugin."
|
|
1877
|
+
);
|
|
1864
1878
|
}
|
|
1879
|
+
super(name, config ?? {}, dlqSchema, dlqColumns, void 0, false);
|
|
1880
|
+
this.typeGuard = typeGuard;
|
|
1881
|
+
getMooseInternal().streams.set(name, this);
|
|
1882
|
+
}
|
|
1883
|
+
/**
|
|
1884
|
+
* Internal type guard function for validating and casting original records.
|
|
1885
|
+
*
|
|
1886
|
+
* @internal
|
|
1887
|
+
*/
|
|
1888
|
+
typeGuard;
|
|
1889
|
+
/**
|
|
1890
|
+
* Adds a transformation step for dead letter records.
|
|
1891
|
+
* The transformation function receives a DeadLetter<T> with type recovery capabilities.
|
|
1892
|
+
*
|
|
1893
|
+
* @template U The output type for the transformation
|
|
1894
|
+
* @param destination The destination stream for transformed messages
|
|
1895
|
+
* @param transformation Function to transform dead letter records
|
|
1896
|
+
* @param config Optional transformation configuration
|
|
1897
|
+
*/
|
|
1898
|
+
addTransform(destination, transformation, config) {
|
|
1899
|
+
const withValidate = (deadLetter) => {
|
|
1900
|
+
attachTypeGuard(deadLetter, this.typeGuard);
|
|
1901
|
+
return transformation(deadLetter);
|
|
1902
|
+
};
|
|
1903
|
+
super.addTransform(destination, withValidate, config);
|
|
1904
|
+
}
|
|
1905
|
+
/**
|
|
1906
|
+
* Adds a consumer for dead letter records.
|
|
1907
|
+
* The consumer function receives a DeadLetter<T> with type recovery capabilities.
|
|
1908
|
+
*
|
|
1909
|
+
* @param consumer Function to process dead letter records
|
|
1910
|
+
* @param config Optional consumer configuration
|
|
1911
|
+
*/
|
|
1912
|
+
addConsumer(consumer, config) {
|
|
1913
|
+
const withValidate = (deadLetter) => {
|
|
1914
|
+
attachTypeGuard(deadLetter, this.typeGuard);
|
|
1915
|
+
return consumer(deadLetter);
|
|
1916
|
+
};
|
|
1917
|
+
super.addConsumer(withValidate, config);
|
|
1918
|
+
}
|
|
1919
|
+
/**
|
|
1920
|
+
* Adds a multi-stream transformation for dead letter records.
|
|
1921
|
+
* The transformation function receives a DeadLetter<T> with type recovery capabilities.
|
|
1922
|
+
*
|
|
1923
|
+
* @param transformation Function to route dead letter records to multiple destinations
|
|
1924
|
+
*/
|
|
1925
|
+
addMultiTransform(transformation) {
|
|
1926
|
+
const withValidate = (deadLetter) => {
|
|
1927
|
+
attachTypeGuard(deadLetter, this.typeGuard);
|
|
1928
|
+
return transformation(deadLetter);
|
|
1929
|
+
};
|
|
1930
|
+
super.addMultiTransform(withValidate);
|
|
1865
1931
|
}
|
|
1866
|
-
recursionStack.delete(taskName);
|
|
1867
1932
|
};
|
|
1868
|
-
validateTask(startingTask, []);
|
|
1869
1933
|
}
|
|
1870
|
-
};
|
|
1871
|
-
|
|
1872
|
-
// src/dmv2/sdk/ingestApi.ts
|
|
1873
|
-
var IngestApi = class extends TypedBase {
|
|
1874
|
-
constructor(name, config, schema, columns, validators, allowExtraFields) {
|
|
1875
|
-
super(name, config, schema, columns, void 0, allowExtraFields);
|
|
1876
|
-
const ingestApis = getMooseInternal().ingestApis;
|
|
1877
|
-
if (ingestApis.has(name)) {
|
|
1878
|
-
throw new Error(`Ingest API with name ${name} already exists`);
|
|
1879
|
-
}
|
|
1880
|
-
ingestApis.set(name, this);
|
|
1881
|
-
}
|
|
1882
|
-
};
|
|
1934
|
+
});
|
|
1883
1935
|
|
|
1884
|
-
// src/dmv2/sdk/
|
|
1885
|
-
var
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1936
|
+
// src/dmv2/sdk/workflow.ts
|
|
1937
|
+
var Task, Workflow;
|
|
1938
|
+
var init_workflow = __esm({
|
|
1939
|
+
"src/dmv2/sdk/workflow.ts"() {
|
|
1940
|
+
"use strict";
|
|
1941
|
+
init_internal();
|
|
1942
|
+
Task = class {
|
|
1943
|
+
/**
|
|
1944
|
+
* Creates a new Task instance.
|
|
1945
|
+
*
|
|
1946
|
+
* @param name - Unique identifier for the task
|
|
1947
|
+
* @param config - Configuration object defining the task behavior
|
|
1948
|
+
*
|
|
1949
|
+
* @example
|
|
1950
|
+
* ```typescript
|
|
1951
|
+
* // No input, no output
|
|
1952
|
+
* const task1 = new Task<null, void>("task1", {
|
|
1953
|
+
* run: async () => {
|
|
1954
|
+
* console.log("No input/output");
|
|
1955
|
+
* }
|
|
1956
|
+
* });
|
|
1957
|
+
*
|
|
1958
|
+
* // No input, but has output
|
|
1959
|
+
* const task2 = new Task<null, OutputType>("task2", {
|
|
1960
|
+
* run: async () => {
|
|
1961
|
+
* return someOutput;
|
|
1962
|
+
* }
|
|
1963
|
+
* });
|
|
1964
|
+
*
|
|
1965
|
+
* // Has input, no output
|
|
1966
|
+
* const task3 = new Task<InputType, void>("task3", {
|
|
1967
|
+
* run: async (input: InputType) => {
|
|
1968
|
+
* // process input but return nothing
|
|
1969
|
+
* }
|
|
1970
|
+
* });
|
|
1971
|
+
*
|
|
1972
|
+
* // Has both input and output
|
|
1973
|
+
* const task4 = new Task<InputType, OutputType>("task4", {
|
|
1974
|
+
* run: async (input: InputType) => {
|
|
1975
|
+
* return process(input);
|
|
1976
|
+
* }
|
|
1977
|
+
* });
|
|
1978
|
+
* ```
|
|
1979
|
+
*/
|
|
1980
|
+
constructor(name, config) {
|
|
1981
|
+
this.name = name;
|
|
1982
|
+
this.config = config;
|
|
1983
|
+
}
|
|
1897
1984
|
};
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1985
|
+
Workflow = class {
|
|
1986
|
+
/**
|
|
1987
|
+
* Creates a new Workflow instance and registers it with the Moose system.
|
|
1988
|
+
*
|
|
1989
|
+
* @param name - Unique identifier for the workflow
|
|
1990
|
+
* @param config - Configuration object defining the workflow behavior and task orchestration
|
|
1991
|
+
* @throws {Error} When the workflow contains null/undefined tasks or infinite loops
|
|
1992
|
+
*/
|
|
1993
|
+
constructor(name, config) {
|
|
1994
|
+
this.name = name;
|
|
1995
|
+
this.config = config;
|
|
1996
|
+
const workflows = getMooseInternal().workflows;
|
|
1997
|
+
if (workflows.has(name)) {
|
|
1998
|
+
throw new Error(`Workflow with name ${name} already exists`);
|
|
1999
|
+
}
|
|
2000
|
+
this.validateTaskGraph(config.startingTask, name);
|
|
2001
|
+
workflows.set(name, this);
|
|
2002
|
+
}
|
|
2003
|
+
/**
|
|
2004
|
+
* Validates the task graph to ensure there are no null tasks or infinite loops.
|
|
2005
|
+
*
|
|
2006
|
+
* @private
|
|
2007
|
+
* @param startingTask - The starting task to begin validation from
|
|
2008
|
+
* @param workflowName - The name of the workflow being validated (for error messages)
|
|
2009
|
+
* @throws {Error} When null/undefined tasks are found or infinite loops are detected
|
|
2010
|
+
*/
|
|
2011
|
+
validateTaskGraph(startingTask, workflowName) {
|
|
2012
|
+
if (startingTask === null || startingTask === void 0) {
|
|
2013
|
+
throw new Error(
|
|
2014
|
+
`Workflow "${workflowName}" has a null or undefined starting task`
|
|
2015
|
+
);
|
|
2016
|
+
}
|
|
2017
|
+
const visited = /* @__PURE__ */ new Set();
|
|
2018
|
+
const recursionStack = /* @__PURE__ */ new Set();
|
|
2019
|
+
const validateTask = (task, currentPath) => {
|
|
2020
|
+
if (task === null || task === void 0) {
|
|
2021
|
+
const pathStr = currentPath.length > 0 ? currentPath.join(" -> ") + " -> " : "";
|
|
1912
2022
|
throw new Error(
|
|
1913
|
-
`
|
|
2023
|
+
`Workflow "${workflowName}" contains a null or undefined task in the task chain: ${pathStr}null`
|
|
1914
2024
|
);
|
|
1915
2025
|
}
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
const existing = apis.get(versionedPath);
|
|
2026
|
+
const taskName = task.name;
|
|
2027
|
+
if (recursionStack.has(taskName)) {
|
|
2028
|
+
const cycleStartIndex = currentPath.indexOf(taskName);
|
|
2029
|
+
const cyclePath = cycleStartIndex >= 0 ? currentPath.slice(cycleStartIndex).concat(taskName) : currentPath.concat(taskName);
|
|
1921
2030
|
throw new Error(
|
|
1922
|
-
`
|
|
2031
|
+
`Workflow "${workflowName}" contains an infinite loop in task chain: ${cyclePath.join(" -> ")}`
|
|
1923
2032
|
);
|
|
1924
2033
|
}
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
2034
|
+
if (visited.has(taskName)) {
|
|
2035
|
+
return;
|
|
2036
|
+
}
|
|
2037
|
+
visited.add(taskName);
|
|
2038
|
+
recursionStack.add(taskName);
|
|
2039
|
+
if (task.config.onComplete) {
|
|
2040
|
+
for (const nextTask of task.config.onComplete) {
|
|
2041
|
+
validateTask(nextTask, [...currentPath, taskName]);
|
|
2042
|
+
}
|
|
1928
2043
|
}
|
|
2044
|
+
recursionStack.delete(taskName);
|
|
2045
|
+
};
|
|
2046
|
+
validateTask(startingTask, []);
|
|
2047
|
+
}
|
|
2048
|
+
};
|
|
2049
|
+
}
|
|
2050
|
+
});
|
|
2051
|
+
|
|
2052
|
+
// src/dmv2/sdk/ingestApi.ts
|
|
2053
|
+
var IngestApi;
|
|
2054
|
+
var init_ingestApi = __esm({
|
|
2055
|
+
"src/dmv2/sdk/ingestApi.ts"() {
|
|
2056
|
+
"use strict";
|
|
2057
|
+
init_typedBase();
|
|
2058
|
+
init_internal();
|
|
2059
|
+
IngestApi = class extends TypedBase {
|
|
2060
|
+
constructor(name, config, schema, columns, validators, allowExtraFields) {
|
|
2061
|
+
super(name, config, schema, columns, void 0, allowExtraFields);
|
|
2062
|
+
const ingestApis = getMooseInternal().ingestApis;
|
|
2063
|
+
if (ingestApis.has(name)) {
|
|
2064
|
+
throw new Error(`Ingest API with name ${name} already exists`);
|
|
1929
2065
|
}
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
2066
|
+
ingestApis.set(name, this);
|
|
2067
|
+
}
|
|
2068
|
+
};
|
|
2069
|
+
}
|
|
2070
|
+
});
|
|
2071
|
+
|
|
2072
|
+
// src/dmv2/sdk/consumptionApi.ts
|
|
2073
|
+
var Api, ConsumptionApi;
|
|
2074
|
+
var init_consumptionApi = __esm({
|
|
2075
|
+
"src/dmv2/sdk/consumptionApi.ts"() {
|
|
2076
|
+
"use strict";
|
|
2077
|
+
init_typedBase();
|
|
2078
|
+
init_internal();
|
|
2079
|
+
Api = class extends TypedBase {
|
|
2080
|
+
/** @internal The handler function that processes requests and generates responses. */
|
|
2081
|
+
_handler;
|
|
2082
|
+
/** @internal The JSON schema definition for the response type R. */
|
|
2083
|
+
responseSchema;
|
|
2084
|
+
constructor(name, handler, config, schema, columns, responseSchema) {
|
|
2085
|
+
super(name, config ?? {}, schema, columns);
|
|
2086
|
+
this._handler = handler;
|
|
2087
|
+
this.responseSchema = responseSchema ?? {
|
|
2088
|
+
version: "3.1",
|
|
2089
|
+
schemas: [{ type: "array", items: { type: "object" } }],
|
|
2090
|
+
components: { schemas: {} }
|
|
2091
|
+
};
|
|
2092
|
+
const apis = getMooseInternal().apis;
|
|
2093
|
+
const key = `${name}${config?.version ? `:${config.version}` : ""}`;
|
|
2094
|
+
if (apis.has(key)) {
|
|
1933
2095
|
throw new Error(
|
|
1934
|
-
`
|
|
2096
|
+
`Consumption API with name ${name} and version ${config?.version} already exists`
|
|
1935
2097
|
);
|
|
1936
2098
|
}
|
|
1937
|
-
apis.set(
|
|
2099
|
+
apis.set(key, this);
|
|
2100
|
+
if (config?.path) {
|
|
2101
|
+
if (config.version) {
|
|
2102
|
+
const pathEndsWithVersion = config.path.endsWith(`/${config.version}`) || config.path === config.version || config.path.endsWith(config.version) && config.path.length > config.version.length && config.path[config.path.length - config.version.length - 1] === "/";
|
|
2103
|
+
if (pathEndsWithVersion) {
|
|
2104
|
+
if (apis.has(config.path)) {
|
|
2105
|
+
const existing = apis.get(config.path);
|
|
2106
|
+
throw new Error(
|
|
2107
|
+
`Cannot register API "${name}" with path "${config.path}" - this path is already used by API "${existing.name}"`
|
|
2108
|
+
);
|
|
2109
|
+
}
|
|
2110
|
+
apis.set(config.path, this);
|
|
2111
|
+
} else {
|
|
2112
|
+
const versionedPath = `${config.path.replace(/\/$/, "")}/${config.version}`;
|
|
2113
|
+
if (apis.has(versionedPath)) {
|
|
2114
|
+
const existing = apis.get(versionedPath);
|
|
2115
|
+
throw new Error(
|
|
2116
|
+
`Cannot register API "${name}" with path "${versionedPath}" - this path is already used by API "${existing.name}"`
|
|
2117
|
+
);
|
|
2118
|
+
}
|
|
2119
|
+
apis.set(versionedPath, this);
|
|
2120
|
+
if (!apis.has(config.path)) {
|
|
2121
|
+
apis.set(config.path, this);
|
|
2122
|
+
}
|
|
2123
|
+
}
|
|
2124
|
+
} else {
|
|
2125
|
+
if (apis.has(config.path)) {
|
|
2126
|
+
const existing = apis.get(config.path);
|
|
2127
|
+
throw new Error(
|
|
2128
|
+
`Cannot register API "${name}" with custom path "${config.path}" - this path is already used by API "${existing.name}"`
|
|
2129
|
+
);
|
|
2130
|
+
}
|
|
2131
|
+
apis.set(config.path, this);
|
|
2132
|
+
}
|
|
2133
|
+
}
|
|
1938
2134
|
}
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
2135
|
+
/**
|
|
2136
|
+
* Retrieves the handler function associated with this Consumption API.
|
|
2137
|
+
* @returns The handler function.
|
|
2138
|
+
*/
|
|
2139
|
+
getHandler = () => {
|
|
2140
|
+
return this._handler;
|
|
2141
|
+
};
|
|
2142
|
+
async call(baseUrl, queryParams) {
|
|
2143
|
+
let path2;
|
|
2144
|
+
if (this.config?.path) {
|
|
2145
|
+
if (this.config.version) {
|
|
2146
|
+
const pathEndsWithVersion = this.config.path.endsWith(`/${this.config.version}`) || this.config.path === this.config.version || this.config.path.endsWith(this.config.version) && this.config.path.length > this.config.version.length && this.config.path[this.config.path.length - this.config.version.length - 1] === "/";
|
|
2147
|
+
if (pathEndsWithVersion) {
|
|
2148
|
+
path2 = this.config.path;
|
|
2149
|
+
} else {
|
|
2150
|
+
path2 = `${this.config.path.replace(/\/$/, "")}/${this.config.version}`;
|
|
2151
|
+
}
|
|
2152
|
+
} else {
|
|
2153
|
+
path2 = this.config.path;
|
|
2154
|
+
}
|
|
1955
2155
|
} else {
|
|
1956
|
-
path2 = `${this.
|
|
2156
|
+
path2 = this.config?.version ? `${this.name}/${this.config.version}` : this.name;
|
|
1957
2157
|
}
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
2158
|
+
const url = new URL(`${baseUrl.replace(/\/$/, "")}/api/${path2}`);
|
|
2159
|
+
const searchParams = url.searchParams;
|
|
2160
|
+
for (const [key, value] of Object.entries(queryParams)) {
|
|
2161
|
+
if (Array.isArray(value)) {
|
|
2162
|
+
for (const item of value) {
|
|
2163
|
+
if (item !== null && item !== void 0) {
|
|
2164
|
+
searchParams.append(key, String(item));
|
|
2165
|
+
}
|
|
2166
|
+
}
|
|
2167
|
+
} else if (value !== null && value !== void 0) {
|
|
2168
|
+
searchParams.append(key, String(value));
|
|
2169
|
+
}
|
|
2170
|
+
}
|
|
2171
|
+
const response = await fetch(url, {
|
|
2172
|
+
method: "GET",
|
|
2173
|
+
headers: {
|
|
2174
|
+
Accept: "application/json"
|
|
1971
2175
|
}
|
|
2176
|
+
});
|
|
2177
|
+
if (!response.ok) {
|
|
2178
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
1972
2179
|
}
|
|
1973
|
-
|
|
1974
|
-
|
|
2180
|
+
const data = await response.json();
|
|
2181
|
+
return data;
|
|
1975
2182
|
}
|
|
1976
|
-
}
|
|
1977
|
-
|
|
1978
|
-
method: "GET",
|
|
1979
|
-
headers: {
|
|
1980
|
-
Accept: "application/json"
|
|
1981
|
-
}
|
|
1982
|
-
});
|
|
1983
|
-
if (!response.ok) {
|
|
1984
|
-
throw new Error(`HTTP error! status: ${response.status}`);
|
|
1985
|
-
}
|
|
1986
|
-
const data = await response.json();
|
|
1987
|
-
return data;
|
|
2183
|
+
};
|
|
2184
|
+
ConsumptionApi = Api;
|
|
1988
2185
|
}
|
|
1989
|
-
};
|
|
1990
|
-
var ConsumptionApi = Api;
|
|
2186
|
+
});
|
|
1991
2187
|
|
|
1992
2188
|
// src/dmv2/sdk/ingestPipeline.ts
|
|
1993
|
-
var IngestPipeline
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2189
|
+
var IngestPipeline;
|
|
2190
|
+
var init_ingestPipeline = __esm({
|
|
2191
|
+
"src/dmv2/sdk/ingestPipeline.ts"() {
|
|
2192
|
+
"use strict";
|
|
2193
|
+
init_typedBase();
|
|
2194
|
+
init_stream();
|
|
2195
|
+
init_olapTable();
|
|
2196
|
+
init_ingestApi();
|
|
2197
|
+
init_helpers();
|
|
2198
|
+
IngestPipeline = class extends TypedBase {
|
|
2199
|
+
/**
|
|
2200
|
+
* The OLAP table component of the pipeline, if configured.
|
|
2201
|
+
* Provides analytical query capabilities for the ingested data.
|
|
2202
|
+
* Only present when `config.table` is not `false`.
|
|
2203
|
+
*/
|
|
2204
|
+
table;
|
|
2205
|
+
/**
|
|
2206
|
+
* The stream component of the pipeline, if configured.
|
|
2207
|
+
* Handles real-time data flow and processing between components.
|
|
2208
|
+
* Only present when `config.stream` is not `false`.
|
|
2209
|
+
*/
|
|
2210
|
+
stream;
|
|
2211
|
+
/**
|
|
2212
|
+
* The ingest API component of the pipeline, if configured.
|
|
2213
|
+
* Provides HTTP endpoints for data ingestion.
|
|
2214
|
+
* Only present when `config.ingestApi` is not `false`.
|
|
2215
|
+
*/
|
|
2216
|
+
ingestApi;
|
|
2217
|
+
/** The dead letter queue of the pipeline, if configured. */
|
|
2218
|
+
deadLetterQueue;
|
|
2219
|
+
constructor(name, config, schema, columns, validators, allowExtraFields) {
|
|
2220
|
+
super(name, config, schema, columns, validators, allowExtraFields);
|
|
2221
|
+
if (config.ingest !== void 0) {
|
|
2222
|
+
console.warn(
|
|
2223
|
+
"\u26A0\uFE0F DEPRECATION WARNING: The 'ingest' parameter is deprecated and will be removed in a future version. Please use 'ingestApi' instead."
|
|
2224
|
+
);
|
|
2225
|
+
if (config.ingestApi === void 0) {
|
|
2226
|
+
config.ingestApi = config.ingest;
|
|
2227
|
+
}
|
|
2228
|
+
}
|
|
2229
|
+
if (config.table) {
|
|
2230
|
+
const tableConfig = typeof config.table === "object" ? {
|
|
2231
|
+
...config.table,
|
|
2232
|
+
lifeCycle: config.table.lifeCycle ?? config.lifeCycle,
|
|
2233
|
+
...config.version && { version: config.version }
|
|
2234
|
+
} : {
|
|
2235
|
+
lifeCycle: config.lifeCycle,
|
|
2236
|
+
engine: "MergeTree" /* MergeTree */,
|
|
2237
|
+
...config.version && { version: config.version }
|
|
2238
|
+
};
|
|
2239
|
+
this.table = new OlapTable(
|
|
2240
|
+
name,
|
|
2241
|
+
tableConfig,
|
|
2242
|
+
this.schema,
|
|
2243
|
+
this.columnArray,
|
|
2244
|
+
this.validators
|
|
2245
|
+
);
|
|
2246
|
+
}
|
|
2247
|
+
if (config.deadLetterQueue) {
|
|
2248
|
+
const streamConfig = {
|
|
2249
|
+
destination: void 0,
|
|
2250
|
+
...typeof config.deadLetterQueue === "object" ? {
|
|
2251
|
+
...config.deadLetterQueue,
|
|
2252
|
+
lifeCycle: config.deadLetterQueue.lifeCycle ?? config.lifeCycle
|
|
2253
|
+
} : { lifeCycle: config.lifeCycle },
|
|
2254
|
+
...config.version && { version: config.version }
|
|
2255
|
+
};
|
|
2256
|
+
this.deadLetterQueue = new DeadLetterQueue(
|
|
2257
|
+
`${name}DeadLetterQueue`,
|
|
2258
|
+
streamConfig,
|
|
2259
|
+
validators.assert
|
|
2260
|
+
);
|
|
2261
|
+
}
|
|
2262
|
+
if (config.stream) {
|
|
2263
|
+
const streamConfig = {
|
|
2264
|
+
destination: this.table,
|
|
2265
|
+
defaultDeadLetterQueue: this.deadLetterQueue,
|
|
2266
|
+
...typeof config.stream === "object" ? {
|
|
2267
|
+
...config.stream,
|
|
2268
|
+
lifeCycle: config.stream.lifeCycle ?? config.lifeCycle
|
|
2269
|
+
} : { lifeCycle: config.lifeCycle },
|
|
2270
|
+
...config.version && { version: config.version }
|
|
2271
|
+
};
|
|
2272
|
+
this.stream = new Stream(
|
|
2273
|
+
name,
|
|
2274
|
+
streamConfig,
|
|
2275
|
+
this.schema,
|
|
2276
|
+
this.columnArray,
|
|
2277
|
+
void 0,
|
|
2278
|
+
this.allowExtraFields
|
|
2279
|
+
);
|
|
2280
|
+
this.stream.pipelineParent = this;
|
|
2281
|
+
}
|
|
2282
|
+
const effectiveIngestAPI = config.ingestApi !== void 0 ? config.ingestApi : config.ingest;
|
|
2283
|
+
if (effectiveIngestAPI) {
|
|
2284
|
+
if (!this.stream) {
|
|
2285
|
+
throw new Error("Ingest API needs a stream to write to.");
|
|
2286
|
+
}
|
|
2287
|
+
const ingestConfig = {
|
|
2288
|
+
destination: this.stream,
|
|
2289
|
+
deadLetterQueue: this.deadLetterQueue,
|
|
2290
|
+
...typeof effectiveIngestAPI === "object" ? effectiveIngestAPI : {},
|
|
2291
|
+
...config.version && { version: config.version },
|
|
2292
|
+
...config.path && { path: config.path }
|
|
2293
|
+
};
|
|
2294
|
+
this.ingestApi = new IngestApi(
|
|
2295
|
+
name,
|
|
2296
|
+
ingestConfig,
|
|
2297
|
+
this.schema,
|
|
2298
|
+
this.columnArray,
|
|
2299
|
+
void 0,
|
|
2300
|
+
this.allowExtraFields
|
|
2301
|
+
);
|
|
2302
|
+
this.ingestApi.pipelineParent = this;
|
|
2303
|
+
}
|
|
2022
2304
|
}
|
|
2023
|
-
}
|
|
2024
|
-
if (config.table) {
|
|
2025
|
-
const tableConfig = typeof config.table === "object" ? {
|
|
2026
|
-
...config.table,
|
|
2027
|
-
lifeCycle: config.table.lifeCycle ?? config.lifeCycle,
|
|
2028
|
-
...config.version && { version: config.version }
|
|
2029
|
-
} : {
|
|
2030
|
-
lifeCycle: config.lifeCycle,
|
|
2031
|
-
engine: "MergeTree" /* MergeTree */,
|
|
2032
|
-
...config.version && { version: config.version }
|
|
2033
|
-
};
|
|
2034
|
-
this.table = new OlapTable(
|
|
2035
|
-
name,
|
|
2036
|
-
tableConfig,
|
|
2037
|
-
this.schema,
|
|
2038
|
-
this.columnArray,
|
|
2039
|
-
this.validators
|
|
2040
|
-
);
|
|
2041
|
-
}
|
|
2042
|
-
if (config.deadLetterQueue) {
|
|
2043
|
-
const streamConfig = {
|
|
2044
|
-
destination: void 0,
|
|
2045
|
-
...typeof config.deadLetterQueue === "object" ? {
|
|
2046
|
-
...config.deadLetterQueue,
|
|
2047
|
-
lifeCycle: config.deadLetterQueue.lifeCycle ?? config.lifeCycle
|
|
2048
|
-
} : { lifeCycle: config.lifeCycle },
|
|
2049
|
-
...config.version && { version: config.version }
|
|
2050
|
-
};
|
|
2051
|
-
this.deadLetterQueue = new DeadLetterQueue(
|
|
2052
|
-
`${name}DeadLetterQueue`,
|
|
2053
|
-
streamConfig,
|
|
2054
|
-
validators.assert
|
|
2055
|
-
);
|
|
2056
|
-
}
|
|
2057
|
-
if (config.stream) {
|
|
2058
|
-
const streamConfig = {
|
|
2059
|
-
destination: this.table,
|
|
2060
|
-
defaultDeadLetterQueue: this.deadLetterQueue,
|
|
2061
|
-
...typeof config.stream === "object" ? {
|
|
2062
|
-
...config.stream,
|
|
2063
|
-
lifeCycle: config.stream.lifeCycle ?? config.lifeCycle
|
|
2064
|
-
} : { lifeCycle: config.lifeCycle },
|
|
2065
|
-
...config.version && { version: config.version }
|
|
2066
|
-
};
|
|
2067
|
-
this.stream = new Stream(
|
|
2068
|
-
name,
|
|
2069
|
-
streamConfig,
|
|
2070
|
-
this.schema,
|
|
2071
|
-
this.columnArray,
|
|
2072
|
-
void 0,
|
|
2073
|
-
this.allowExtraFields
|
|
2074
|
-
);
|
|
2075
|
-
this.stream.pipelineParent = this;
|
|
2076
|
-
}
|
|
2077
|
-
const effectiveIngestAPI = config.ingestApi !== void 0 ? config.ingestApi : config.ingest;
|
|
2078
|
-
if (effectiveIngestAPI) {
|
|
2079
|
-
if (!this.stream) {
|
|
2080
|
-
throw new Error("Ingest API needs a stream to write to.");
|
|
2081
|
-
}
|
|
2082
|
-
const ingestConfig = {
|
|
2083
|
-
destination: this.stream,
|
|
2084
|
-
deadLetterQueue: this.deadLetterQueue,
|
|
2085
|
-
...typeof effectiveIngestAPI === "object" ? effectiveIngestAPI : {},
|
|
2086
|
-
...config.version && { version: config.version },
|
|
2087
|
-
...config.path && { path: config.path }
|
|
2088
|
-
};
|
|
2089
|
-
this.ingestApi = new IngestApi(
|
|
2090
|
-
name,
|
|
2091
|
-
ingestConfig,
|
|
2092
|
-
this.schema,
|
|
2093
|
-
this.columnArray,
|
|
2094
|
-
void 0,
|
|
2095
|
-
this.allowExtraFields
|
|
2096
|
-
);
|
|
2097
|
-
this.ingestApi.pipelineParent = this;
|
|
2098
|
-
}
|
|
2305
|
+
};
|
|
2099
2306
|
}
|
|
2100
|
-
};
|
|
2307
|
+
});
|
|
2101
2308
|
|
|
2102
2309
|
// src/dmv2/sdk/etlPipeline.ts
|
|
2103
|
-
var InternalBatcher
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
}
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
this.setupPipeline();
|
|
2127
|
-
}
|
|
2128
|
-
batcher;
|
|
2129
|
-
setupPipeline() {
|
|
2130
|
-
this.batcher = this.createBatcher();
|
|
2131
|
-
const tasks = this.createAllTasks();
|
|
2132
|
-
tasks.extract.config.onComplete = [tasks.transform];
|
|
2133
|
-
tasks.transform.config.onComplete = [tasks.load];
|
|
2134
|
-
new Workflow(this.name, {
|
|
2135
|
-
startingTask: tasks.extract,
|
|
2136
|
-
retries: 1,
|
|
2137
|
-
timeout: "30m"
|
|
2138
|
-
});
|
|
2139
|
-
}
|
|
2140
|
-
createBatcher() {
|
|
2141
|
-
const iterable = typeof this.config.extract === "function" ? this.config.extract() : this.config.extract;
|
|
2142
|
-
return new InternalBatcher(iterable);
|
|
2143
|
-
}
|
|
2144
|
-
getDefaultTaskConfig() {
|
|
2145
|
-
return {
|
|
2146
|
-
retries: 1,
|
|
2147
|
-
timeout: "30m"
|
|
2148
|
-
};
|
|
2149
|
-
}
|
|
2150
|
-
createAllTasks() {
|
|
2151
|
-
const taskConfig = this.getDefaultTaskConfig();
|
|
2152
|
-
return {
|
|
2153
|
-
extract: this.createExtractTask(taskConfig),
|
|
2154
|
-
transform: this.createTransformTask(taskConfig),
|
|
2155
|
-
load: this.createLoadTask(taskConfig)
|
|
2310
|
+
var InternalBatcher, ETLPipeline;
|
|
2311
|
+
var init_etlPipeline = __esm({
|
|
2312
|
+
"src/dmv2/sdk/etlPipeline.ts"() {
|
|
2313
|
+
"use strict";
|
|
2314
|
+
init_workflow();
|
|
2315
|
+
InternalBatcher = class {
|
|
2316
|
+
iterator;
|
|
2317
|
+
batchSize;
|
|
2318
|
+
constructor(asyncIterable, batchSize = 20) {
|
|
2319
|
+
this.iterator = asyncIterable[Symbol.asyncIterator]();
|
|
2320
|
+
this.batchSize = batchSize;
|
|
2321
|
+
}
|
|
2322
|
+
async getNextBatch() {
|
|
2323
|
+
const items = [];
|
|
2324
|
+
for (let i = 0; i < this.batchSize; i++) {
|
|
2325
|
+
const { value, done } = await this.iterator.next();
|
|
2326
|
+
if (done) {
|
|
2327
|
+
return { items, hasMore: false };
|
|
2328
|
+
}
|
|
2329
|
+
items.push(value);
|
|
2330
|
+
}
|
|
2331
|
+
return { items, hasMore: true };
|
|
2332
|
+
}
|
|
2156
2333
|
};
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2334
|
+
ETLPipeline = class {
|
|
2335
|
+
constructor(name, config) {
|
|
2336
|
+
this.name = name;
|
|
2337
|
+
this.config = config;
|
|
2338
|
+
this.setupPipeline();
|
|
2339
|
+
}
|
|
2340
|
+
batcher;
|
|
2341
|
+
setupPipeline() {
|
|
2342
|
+
this.batcher = this.createBatcher();
|
|
2343
|
+
const tasks = this.createAllTasks();
|
|
2344
|
+
tasks.extract.config.onComplete = [tasks.transform];
|
|
2345
|
+
tasks.transform.config.onComplete = [tasks.load];
|
|
2346
|
+
new Workflow(this.name, {
|
|
2347
|
+
startingTask: tasks.extract,
|
|
2348
|
+
retries: 1,
|
|
2349
|
+
timeout: "30m"
|
|
2350
|
+
});
|
|
2351
|
+
}
|
|
2352
|
+
createBatcher() {
|
|
2353
|
+
const iterable = typeof this.config.extract === "function" ? this.config.extract() : this.config.extract;
|
|
2354
|
+
return new InternalBatcher(iterable);
|
|
2355
|
+
}
|
|
2356
|
+
getDefaultTaskConfig() {
|
|
2357
|
+
return {
|
|
2358
|
+
retries: 1,
|
|
2359
|
+
timeout: "30m"
|
|
2360
|
+
};
|
|
2361
|
+
}
|
|
2362
|
+
createAllTasks() {
|
|
2363
|
+
const taskConfig = this.getDefaultTaskConfig();
|
|
2364
|
+
return {
|
|
2365
|
+
extract: this.createExtractTask(taskConfig),
|
|
2366
|
+
transform: this.createTransformTask(taskConfig),
|
|
2367
|
+
load: this.createLoadTask(taskConfig)
|
|
2368
|
+
};
|
|
2369
|
+
}
|
|
2370
|
+
createExtractTask(taskConfig) {
|
|
2371
|
+
return new Task(`${this.name}_extract`, {
|
|
2372
|
+
run: async ({}) => {
|
|
2373
|
+
console.log(`Running extract task for ${this.name}...`);
|
|
2374
|
+
const batch = await this.batcher.getNextBatch();
|
|
2375
|
+
console.log(`Extract task completed with ${batch.items.length} items`);
|
|
2376
|
+
return batch;
|
|
2377
|
+
},
|
|
2378
|
+
retries: taskConfig.retries,
|
|
2379
|
+
timeout: taskConfig.timeout
|
|
2380
|
+
});
|
|
2381
|
+
}
|
|
2382
|
+
createTransformTask(taskConfig) {
|
|
2383
|
+
return new Task(
|
|
2384
|
+
`${this.name}_transform`,
|
|
2385
|
+
{
|
|
2386
|
+
// Use new single-parameter context API for handlers
|
|
2387
|
+
run: async ({ input }) => {
|
|
2388
|
+
const batch = input;
|
|
2389
|
+
console.log(
|
|
2390
|
+
`Running transform task for ${this.name} with ${batch.items.length} items...`
|
|
2391
|
+
);
|
|
2392
|
+
const transformedItems = [];
|
|
2393
|
+
for (const item of batch.items) {
|
|
2394
|
+
const transformed = await this.config.transform(item);
|
|
2395
|
+
transformedItems.push(transformed);
|
|
2396
|
+
}
|
|
2397
|
+
console.log(
|
|
2398
|
+
`Transform task completed with ${transformedItems.length} items`
|
|
2399
|
+
);
|
|
2400
|
+
return { items: transformedItems };
|
|
2401
|
+
},
|
|
2402
|
+
retries: taskConfig.retries,
|
|
2403
|
+
timeout: taskConfig.timeout
|
|
2404
|
+
}
|
|
2405
|
+
);
|
|
2406
|
+
}
|
|
2407
|
+
createLoadTask(taskConfig) {
|
|
2408
|
+
return new Task(`${this.name}_load`, {
|
|
2409
|
+
run: async ({ input: transformedItems }) => {
|
|
2410
|
+
console.log(
|
|
2411
|
+
`Running load task for ${this.name} with ${transformedItems.items.length} items...`
|
|
2412
|
+
);
|
|
2413
|
+
if ("insert" in this.config.load) {
|
|
2414
|
+
await this.config.load.insert(transformedItems.items);
|
|
2415
|
+
} else {
|
|
2416
|
+
await this.config.load(transformedItems.items);
|
|
2417
|
+
}
|
|
2418
|
+
console.log(`Load task completed`);
|
|
2419
|
+
},
|
|
2420
|
+
retries: taskConfig.retries,
|
|
2421
|
+
timeout: taskConfig.timeout
|
|
2422
|
+
});
|
|
2423
|
+
}
|
|
2424
|
+
// Execute the entire ETL pipeline
|
|
2425
|
+
async run() {
|
|
2426
|
+
console.log(`Starting ETL Pipeline: ${this.name}`);
|
|
2427
|
+
let batchNumber = 1;
|
|
2428
|
+
do {
|
|
2429
|
+
console.log(`Processing batch ${batchNumber}...`);
|
|
2430
|
+
const batch = await this.batcher.getNextBatch();
|
|
2431
|
+
if (batch.items.length === 0) {
|
|
2432
|
+
break;
|
|
2433
|
+
}
|
|
2180
2434
|
const transformedItems = [];
|
|
2181
|
-
for (const
|
|
2182
|
-
const
|
|
2183
|
-
transformedItems.push(
|
|
2435
|
+
for (const extractedData of batch.items) {
|
|
2436
|
+
const transformedData = await this.config.transform(extractedData);
|
|
2437
|
+
transformedItems.push(transformedData);
|
|
2438
|
+
}
|
|
2439
|
+
if ("insert" in this.config.load) {
|
|
2440
|
+
await this.config.load.insert(transformedItems);
|
|
2441
|
+
} else {
|
|
2442
|
+
await this.config.load(transformedItems);
|
|
2184
2443
|
}
|
|
2185
2444
|
console.log(
|
|
2186
|
-
`
|
|
2445
|
+
`Completed batch ${batchNumber} with ${batch.items.length} items`
|
|
2187
2446
|
);
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2447
|
+
batchNumber++;
|
|
2448
|
+
if (!batch.hasMore) {
|
|
2449
|
+
break;
|
|
2450
|
+
}
|
|
2451
|
+
} while (true);
|
|
2452
|
+
console.log(`Completed ETL Pipeline: ${this.name}`);
|
|
2192
2453
|
}
|
|
2193
|
-
|
|
2454
|
+
};
|
|
2194
2455
|
}
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2456
|
+
});
|
|
2457
|
+
|
|
2458
|
+
// src/dmv2/sdk/sqlResource.ts
|
|
2459
|
+
var SqlResource;
|
|
2460
|
+
var init_sqlResource = __esm({
|
|
2461
|
+
"src/dmv2/sdk/sqlResource.ts"() {
|
|
2462
|
+
"use strict";
|
|
2463
|
+
init_internal();
|
|
2464
|
+
init_sqlHelpers();
|
|
2465
|
+
init_stackTrace();
|
|
2466
|
+
SqlResource = class {
|
|
2467
|
+
/** @internal */
|
|
2468
|
+
kind = "SqlResource";
|
|
2469
|
+
/** Array of SQL statements to execute for setting up the resource. */
|
|
2470
|
+
setup;
|
|
2471
|
+
/** Array of SQL statements to execute for tearing down the resource. */
|
|
2472
|
+
teardown;
|
|
2473
|
+
/** The name of the SQL resource (e.g., view name, materialized view name). */
|
|
2474
|
+
name;
|
|
2475
|
+
/** List of OlapTables or Views that this resource reads data from. */
|
|
2476
|
+
pullsDataFrom;
|
|
2477
|
+
/** List of OlapTables or Views that this resource writes data to. */
|
|
2478
|
+
pushesDataTo;
|
|
2479
|
+
/** @internal Source file path where this resource was defined */
|
|
2480
|
+
sourceFile;
|
|
2481
|
+
/** @internal Source line number where this resource was defined */
|
|
2482
|
+
sourceLine;
|
|
2483
|
+
/** @internal Source column number where this resource was defined */
|
|
2484
|
+
sourceColumn;
|
|
2485
|
+
/**
|
|
2486
|
+
* Creates a new SqlResource instance.
|
|
2487
|
+
* @param name The name of the resource.
|
|
2488
|
+
* @param setup An array of SQL DDL statements to create the resource.
|
|
2489
|
+
* @param teardown An array of SQL DDL statements to drop the resource.
|
|
2490
|
+
* @param options Optional configuration for specifying data dependencies.
|
|
2491
|
+
* @param options.pullsDataFrom Tables/Views this resource reads from.
|
|
2492
|
+
* @param options.pushesDataTo Tables/Views this resource writes to.
|
|
2493
|
+
*/
|
|
2494
|
+
constructor(name, setup, teardown, options) {
|
|
2495
|
+
const sqlResources = getMooseInternal().sqlResources;
|
|
2496
|
+
if (!isClientOnlyMode() && sqlResources.has(name)) {
|
|
2497
|
+
throw new Error(`SqlResource with name ${name} already exists`);
|
|
2498
|
+
}
|
|
2499
|
+
sqlResources.set(name, this);
|
|
2500
|
+
this.name = name;
|
|
2501
|
+
this.setup = setup.map(
|
|
2502
|
+
(sql3) => typeof sql3 === "string" ? sql3 : toStaticQuery(sql3)
|
|
2200
2503
|
);
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2504
|
+
this.teardown = teardown.map(
|
|
2505
|
+
(sql3) => typeof sql3 === "string" ? sql3 : toStaticQuery(sql3)
|
|
2506
|
+
);
|
|
2507
|
+
this.pullsDataFrom = options?.pullsDataFrom ?? [];
|
|
2508
|
+
this.pushesDataTo = options?.pushesDataTo ?? [];
|
|
2509
|
+
const stack = new Error().stack;
|
|
2510
|
+
const location = getSourceLocationFromStack(stack);
|
|
2511
|
+
if (location) {
|
|
2512
|
+
this.sourceFile = location.file;
|
|
2513
|
+
this.sourceLine = location.line;
|
|
2514
|
+
this.sourceColumn = location.column;
|
|
2205
2515
|
}
|
|
2206
|
-
console.log(`Load task completed`);
|
|
2207
|
-
},
|
|
2208
|
-
retries: taskConfig.retries,
|
|
2209
|
-
timeout: taskConfig.timeout
|
|
2210
|
-
});
|
|
2211
|
-
}
|
|
2212
|
-
// Execute the entire ETL pipeline
|
|
2213
|
-
async run() {
|
|
2214
|
-
console.log(`Starting ETL Pipeline: ${this.name}`);
|
|
2215
|
-
let batchNumber = 1;
|
|
2216
|
-
do {
|
|
2217
|
-
console.log(`Processing batch ${batchNumber}...`);
|
|
2218
|
-
const batch = await this.batcher.getNextBatch();
|
|
2219
|
-
if (batch.items.length === 0) {
|
|
2220
|
-
break;
|
|
2221
|
-
}
|
|
2222
|
-
const transformedItems = [];
|
|
2223
|
-
for (const extractedData of batch.items) {
|
|
2224
|
-
const transformedData = await this.config.transform(extractedData);
|
|
2225
|
-
transformedItems.push(transformedData);
|
|
2226
|
-
}
|
|
2227
|
-
if ("insert" in this.config.load) {
|
|
2228
|
-
await this.config.load.insert(transformedItems);
|
|
2229
|
-
} else {
|
|
2230
|
-
await this.config.load(transformedItems);
|
|
2231
|
-
}
|
|
2232
|
-
console.log(
|
|
2233
|
-
`Completed batch ${batchNumber} with ${batch.items.length} items`
|
|
2234
|
-
);
|
|
2235
|
-
batchNumber++;
|
|
2236
|
-
if (!batch.hasMore) {
|
|
2237
|
-
break;
|
|
2238
2516
|
}
|
|
2239
|
-
}
|
|
2240
|
-
console.log(`Completed ETL Pipeline: ${this.name}`);
|
|
2241
|
-
}
|
|
2242
|
-
};
|
|
2243
|
-
|
|
2244
|
-
// src/dmv2/sdk/sqlResource.ts
|
|
2245
|
-
var SqlResource = class {
|
|
2246
|
-
/** @internal */
|
|
2247
|
-
kind = "SqlResource";
|
|
2248
|
-
/** Array of SQL statements to execute for setting up the resource. */
|
|
2249
|
-
setup;
|
|
2250
|
-
/** Array of SQL statements to execute for tearing down the resource. */
|
|
2251
|
-
teardown;
|
|
2252
|
-
/** The name of the SQL resource (e.g., view name, materialized view name). */
|
|
2253
|
-
name;
|
|
2254
|
-
/** List of OlapTables or Views that this resource reads data from. */
|
|
2255
|
-
pullsDataFrom;
|
|
2256
|
-
/** List of OlapTables or Views that this resource writes data to. */
|
|
2257
|
-
pushesDataTo;
|
|
2258
|
-
/** @internal Source file path where this resource was defined */
|
|
2259
|
-
sourceFile;
|
|
2260
|
-
/** @internal Source line number where this resource was defined */
|
|
2261
|
-
sourceLine;
|
|
2262
|
-
/** @internal Source column number where this resource was defined */
|
|
2263
|
-
sourceColumn;
|
|
2264
|
-
/**
|
|
2265
|
-
* Creates a new SqlResource instance.
|
|
2266
|
-
* @param name The name of the resource.
|
|
2267
|
-
* @param setup An array of SQL DDL statements to create the resource.
|
|
2268
|
-
* @param teardown An array of SQL DDL statements to drop the resource.
|
|
2269
|
-
* @param options Optional configuration for specifying data dependencies.
|
|
2270
|
-
* @param options.pullsDataFrom Tables/Views this resource reads from.
|
|
2271
|
-
* @param options.pushesDataTo Tables/Views this resource writes to.
|
|
2272
|
-
*/
|
|
2273
|
-
constructor(name, setup, teardown, options) {
|
|
2274
|
-
const sqlResources = getMooseInternal().sqlResources;
|
|
2275
|
-
if (!isClientOnlyMode() && sqlResources.has(name)) {
|
|
2276
|
-
throw new Error(`SqlResource with name ${name} already exists`);
|
|
2277
|
-
}
|
|
2278
|
-
sqlResources.set(name, this);
|
|
2279
|
-
this.name = name;
|
|
2280
|
-
this.setup = setup.map(
|
|
2281
|
-
(sql3) => typeof sql3 === "string" ? sql3 : toStaticQuery(sql3)
|
|
2282
|
-
);
|
|
2283
|
-
this.teardown = teardown.map(
|
|
2284
|
-
(sql3) => typeof sql3 === "string" ? sql3 : toStaticQuery(sql3)
|
|
2285
|
-
);
|
|
2286
|
-
this.pullsDataFrom = options?.pullsDataFrom ?? [];
|
|
2287
|
-
this.pushesDataTo = options?.pushesDataTo ?? [];
|
|
2288
|
-
const stack = new Error().stack;
|
|
2289
|
-
const location = getSourceLocationFromStack(stack);
|
|
2290
|
-
if (location) {
|
|
2291
|
-
this.sourceFile = location.file;
|
|
2292
|
-
this.sourceLine = location.line;
|
|
2293
|
-
this.sourceColumn = location.column;
|
|
2294
|
-
}
|
|
2517
|
+
};
|
|
2295
2518
|
}
|
|
2296
|
-
};
|
|
2519
|
+
});
|
|
2297
2520
|
|
|
2298
2521
|
// src/dmv2/sdk/materializedView.ts
|
|
2299
|
-
var requireTargetTableName
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
selectStatement = toStaticQuery(selectStatement);
|
|
2313
|
-
}
|
|
2314
|
-
if (targetSchema === void 0 || targetColumns === void 0) {
|
|
2315
|
-
throw new Error(
|
|
2316
|
-
"Supply the type param T so that the schema is inserted by the compiler plugin."
|
|
2317
|
-
);
|
|
2318
|
-
}
|
|
2319
|
-
const targetTable = options.targetTable instanceof OlapTable ? options.targetTable : new OlapTable(
|
|
2320
|
-
requireTargetTableName(
|
|
2321
|
-
options.targetTable?.name ?? options.tableName
|
|
2322
|
-
),
|
|
2323
|
-
{
|
|
2324
|
-
orderByFields: options.targetTable?.orderByFields ?? options.orderByFields,
|
|
2325
|
-
engine: options.targetTable?.engine ?? options.engine ?? "MergeTree" /* MergeTree */
|
|
2326
|
-
},
|
|
2327
|
-
targetSchema,
|
|
2328
|
-
targetColumns
|
|
2329
|
-
);
|
|
2330
|
-
if (targetTable.name === options.materializedViewName) {
|
|
2331
|
-
throw new Error(
|
|
2332
|
-
"Materialized view name cannot be the same as the target table name."
|
|
2333
|
-
);
|
|
2334
|
-
}
|
|
2335
|
-
super(
|
|
2336
|
-
options.materializedViewName,
|
|
2337
|
-
[
|
|
2338
|
-
createMaterializedView({
|
|
2339
|
-
name: options.materializedViewName,
|
|
2340
|
-
destinationTable: targetTable.name,
|
|
2341
|
-
select: selectStatement
|
|
2342
|
-
})
|
|
2343
|
-
// Population is now handled automatically by Rust infrastructure
|
|
2344
|
-
// based on table engine type and whether this is a new or updated view
|
|
2345
|
-
],
|
|
2346
|
-
[dropView(options.materializedViewName)],
|
|
2347
|
-
{
|
|
2348
|
-
pullsDataFrom: options.selectTables,
|
|
2349
|
-
pushesDataTo: [targetTable]
|
|
2522
|
+
var requireTargetTableName, MaterializedView;
|
|
2523
|
+
var init_materializedView = __esm({
|
|
2524
|
+
"src/dmv2/sdk/materializedView.ts"() {
|
|
2525
|
+
"use strict";
|
|
2526
|
+
init_helpers();
|
|
2527
|
+
init_sqlHelpers();
|
|
2528
|
+
init_olapTable();
|
|
2529
|
+
init_sqlResource();
|
|
2530
|
+
requireTargetTableName = (tableName) => {
|
|
2531
|
+
if (typeof tableName === "string") {
|
|
2532
|
+
return tableName;
|
|
2533
|
+
} else {
|
|
2534
|
+
throw new Error("Name of targetTable is not specified.");
|
|
2350
2535
|
}
|
|
2351
|
-
|
|
2352
|
-
|
|
2536
|
+
};
|
|
2537
|
+
MaterializedView = class extends SqlResource {
|
|
2538
|
+
/** The target OlapTable instance where the materialized data is stored. */
|
|
2539
|
+
targetTable;
|
|
2540
|
+
constructor(options, targetSchema, targetColumns) {
|
|
2541
|
+
let selectStatement = options.selectStatement;
|
|
2542
|
+
if (typeof selectStatement !== "string") {
|
|
2543
|
+
selectStatement = toStaticQuery(selectStatement);
|
|
2544
|
+
}
|
|
2545
|
+
if (targetSchema === void 0 || targetColumns === void 0) {
|
|
2546
|
+
throw new Error(
|
|
2547
|
+
"Supply the type param T so that the schema is inserted by the compiler plugin."
|
|
2548
|
+
);
|
|
2549
|
+
}
|
|
2550
|
+
const targetTable = options.targetTable instanceof OlapTable ? options.targetTable : new OlapTable(
|
|
2551
|
+
requireTargetTableName(
|
|
2552
|
+
options.targetTable?.name ?? options.tableName
|
|
2553
|
+
),
|
|
2554
|
+
{
|
|
2555
|
+
orderByFields: options.targetTable?.orderByFields ?? options.orderByFields,
|
|
2556
|
+
engine: options.targetTable?.engine ?? options.engine ?? "MergeTree" /* MergeTree */
|
|
2557
|
+
},
|
|
2558
|
+
targetSchema,
|
|
2559
|
+
targetColumns
|
|
2560
|
+
);
|
|
2561
|
+
if (targetTable.name === options.materializedViewName) {
|
|
2562
|
+
throw new Error(
|
|
2563
|
+
"Materialized view name cannot be the same as the target table name."
|
|
2564
|
+
);
|
|
2565
|
+
}
|
|
2566
|
+
super(
|
|
2567
|
+
options.materializedViewName,
|
|
2568
|
+
[
|
|
2569
|
+
createMaterializedView({
|
|
2570
|
+
name: options.materializedViewName,
|
|
2571
|
+
destinationTable: targetTable.name,
|
|
2572
|
+
select: selectStatement
|
|
2573
|
+
})
|
|
2574
|
+
// Population is now handled automatically by Rust infrastructure
|
|
2575
|
+
// based on table engine type and whether this is a new or updated view
|
|
2576
|
+
],
|
|
2577
|
+
[dropView(options.materializedViewName)],
|
|
2578
|
+
{
|
|
2579
|
+
pullsDataFrom: options.selectTables,
|
|
2580
|
+
pushesDataTo: [targetTable]
|
|
2581
|
+
}
|
|
2582
|
+
);
|
|
2583
|
+
this.targetTable = targetTable;
|
|
2584
|
+
}
|
|
2585
|
+
};
|
|
2353
2586
|
}
|
|
2354
|
-
};
|
|
2587
|
+
});
|
|
2355
2588
|
|
|
2356
2589
|
// src/dmv2/sdk/view.ts
|
|
2357
|
-
var View
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2590
|
+
var View;
|
|
2591
|
+
var init_view = __esm({
|
|
2592
|
+
"src/dmv2/sdk/view.ts"() {
|
|
2593
|
+
"use strict";
|
|
2594
|
+
init_helpers();
|
|
2595
|
+
init_sqlHelpers();
|
|
2596
|
+
init_sqlResource();
|
|
2597
|
+
View = class extends SqlResource {
|
|
2598
|
+
/**
|
|
2599
|
+
* Creates a new View instance.
|
|
2600
|
+
* @param name The name of the view to be created.
|
|
2601
|
+
* @param selectStatement The SQL SELECT statement that defines the view's logic.
|
|
2602
|
+
* @param baseTables An array of OlapTable or View objects that the `selectStatement` reads from. Used for dependency tracking.
|
|
2603
|
+
*/
|
|
2604
|
+
constructor(name, selectStatement, baseTables) {
|
|
2605
|
+
if (typeof selectStatement !== "string") {
|
|
2606
|
+
selectStatement = toStaticQuery(selectStatement);
|
|
2607
|
+
}
|
|
2608
|
+
super(
|
|
2609
|
+
name,
|
|
2610
|
+
[
|
|
2611
|
+
`CREATE VIEW IF NOT EXISTS ${name}
|
|
2372
2612
|
AS ${selectStatement}`.trim()
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2613
|
+
],
|
|
2614
|
+
[dropView(name)],
|
|
2615
|
+
{
|
|
2616
|
+
pullsDataFrom: baseTables
|
|
2617
|
+
}
|
|
2618
|
+
);
|
|
2377
2619
|
}
|
|
2378
|
-
|
|
2620
|
+
};
|
|
2379
2621
|
}
|
|
2380
|
-
};
|
|
2622
|
+
});
|
|
2381
2623
|
|
|
2382
2624
|
// src/dmv2/sdk/lifeCycle.ts
|
|
2383
|
-
var LifeCycle
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2625
|
+
var LifeCycle;
|
|
2626
|
+
var init_lifeCycle = __esm({
|
|
2627
|
+
"src/dmv2/sdk/lifeCycle.ts"() {
|
|
2628
|
+
"use strict";
|
|
2629
|
+
LifeCycle = /* @__PURE__ */ ((LifeCycle2) => {
|
|
2630
|
+
LifeCycle2["FULLY_MANAGED"] = "FULLY_MANAGED";
|
|
2631
|
+
LifeCycle2["DELETION_PROTECTED"] = "DELETION_PROTECTED";
|
|
2632
|
+
LifeCycle2["EXTERNALLY_MANAGED"] = "EXTERNALLY_MANAGED";
|
|
2633
|
+
return LifeCycle2;
|
|
2634
|
+
})(LifeCycle || {});
|
|
2635
|
+
}
|
|
2636
|
+
});
|
|
2389
2637
|
|
|
2390
2638
|
// src/dmv2/sdk/webApp.ts
|
|
2391
|
-
var RESERVED_MOUNT_PATHS
|
|
2392
|
-
|
|
2393
|
-
"/
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
if (mountPath === "/") {
|
|
2417
|
-
throw new Error(
|
|
2418
|
-
`mountPath cannot be "/" as it would allow routes to overlap with reserved paths: ${RESERVED_MOUNT_PATHS.join(", ")}`
|
|
2419
|
-
);
|
|
2420
|
-
}
|
|
2421
|
-
if (mountPath.endsWith("/")) {
|
|
2422
|
-
throw new Error(
|
|
2423
|
-
`mountPath cannot end with a trailing slash. Remove the '/' from: "${mountPath}"`
|
|
2424
|
-
);
|
|
2425
|
-
}
|
|
2426
|
-
for (const reserved of RESERVED_MOUNT_PATHS) {
|
|
2427
|
-
if (mountPath === reserved || mountPath.startsWith(`${reserved}/`)) {
|
|
2428
|
-
throw new Error(
|
|
2429
|
-
`mountPath cannot begin with a reserved path: ${RESERVED_MOUNT_PATHS.join(", ")}. Got: "${mountPath}"`
|
|
2430
|
-
);
|
|
2431
|
-
}
|
|
2432
|
-
}
|
|
2433
|
-
this.handler = this.toHandler(appOrHandler);
|
|
2434
|
-
this._rawApp = typeof appOrHandler === "function" ? void 0 : appOrHandler;
|
|
2435
|
-
const webApps = getMooseInternal().webApps;
|
|
2436
|
-
if (webApps.has(name)) {
|
|
2437
|
-
throw new Error(`WebApp with name ${name} already exists`);
|
|
2438
|
-
}
|
|
2439
|
-
if (this.config.mountPath) {
|
|
2440
|
-
for (const [existingName, existingApp] of webApps) {
|
|
2441
|
-
if (existingApp.config.mountPath === this.config.mountPath) {
|
|
2639
|
+
var RESERVED_MOUNT_PATHS, WebApp;
|
|
2640
|
+
var init_webApp = __esm({
|
|
2641
|
+
"src/dmv2/sdk/webApp.ts"() {
|
|
2642
|
+
"use strict";
|
|
2643
|
+
init_internal();
|
|
2644
|
+
RESERVED_MOUNT_PATHS = [
|
|
2645
|
+
"/admin",
|
|
2646
|
+
"/api",
|
|
2647
|
+
"/consumption",
|
|
2648
|
+
"/health",
|
|
2649
|
+
"/ingest",
|
|
2650
|
+
"/moose",
|
|
2651
|
+
// reserved for future use
|
|
2652
|
+
"/ready",
|
|
2653
|
+
"/workflows"
|
|
2654
|
+
];
|
|
2655
|
+
WebApp = class {
|
|
2656
|
+
name;
|
|
2657
|
+
handler;
|
|
2658
|
+
config;
|
|
2659
|
+
_rawApp;
|
|
2660
|
+
constructor(name, appOrHandler, config) {
|
|
2661
|
+
this.name = name;
|
|
2662
|
+
this.config = config;
|
|
2663
|
+
if (!this.config.mountPath) {
|
|
2442
2664
|
throw new Error(
|
|
2443
|
-
`
|
|
2665
|
+
`mountPath is required. Please specify a mount path for your WebApp (e.g., "/myapi").`
|
|
2444
2666
|
);
|
|
2445
2667
|
}
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
if (
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2668
|
+
const mountPath = this.config.mountPath;
|
|
2669
|
+
if (mountPath === "/") {
|
|
2670
|
+
throw new Error(
|
|
2671
|
+
`mountPath cannot be "/" as it would allow routes to overlap with reserved paths: ${RESERVED_MOUNT_PATHS.join(", ")}`
|
|
2672
|
+
);
|
|
2673
|
+
}
|
|
2674
|
+
if (mountPath.endsWith("/")) {
|
|
2675
|
+
throw new Error(
|
|
2676
|
+
`mountPath cannot end with a trailing slash. Remove the '/' from: "${mountPath}"`
|
|
2677
|
+
);
|
|
2678
|
+
}
|
|
2679
|
+
for (const reserved of RESERVED_MOUNT_PATHS) {
|
|
2680
|
+
if (mountPath === reserved || mountPath.startsWith(`${reserved}/`)) {
|
|
2681
|
+
throw new Error(
|
|
2682
|
+
`mountPath cannot begin with a reserved path: ${RESERVED_MOUNT_PATHS.join(", ")}. Got: "${mountPath}"`
|
|
2683
|
+
);
|
|
2684
|
+
}
|
|
2685
|
+
}
|
|
2686
|
+
this.handler = this.toHandler(appOrHandler);
|
|
2687
|
+
this._rawApp = typeof appOrHandler === "function" ? void 0 : appOrHandler;
|
|
2688
|
+
const webApps = getMooseInternal().webApps;
|
|
2689
|
+
if (webApps.has(name)) {
|
|
2690
|
+
throw new Error(`WebApp with name ${name} already exists`);
|
|
2691
|
+
}
|
|
2692
|
+
if (this.config.mountPath) {
|
|
2693
|
+
for (const [existingName, existingApp] of webApps) {
|
|
2694
|
+
if (existingApp.config.mountPath === this.config.mountPath) {
|
|
2695
|
+
throw new Error(
|
|
2696
|
+
`WebApp with mountPath "${this.config.mountPath}" already exists (used by WebApp "${existingName}")`
|
|
2697
|
+
);
|
|
2463
2698
|
}
|
|
2464
2699
|
}
|
|
2465
|
-
}
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2700
|
+
}
|
|
2701
|
+
webApps.set(name, this);
|
|
2702
|
+
}
|
|
2703
|
+
toHandler(appOrHandler) {
|
|
2704
|
+
if (typeof appOrHandler === "function") {
|
|
2705
|
+
return appOrHandler;
|
|
2706
|
+
}
|
|
2707
|
+
const app = appOrHandler;
|
|
2708
|
+
if (typeof app.handle === "function") {
|
|
2709
|
+
return (req, res) => {
|
|
2710
|
+
app.handle(req, res, (err) => {
|
|
2711
|
+
if (err) {
|
|
2712
|
+
console.error("WebApp handler error:", err);
|
|
2713
|
+
if (!res.headersSent) {
|
|
2714
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
2715
|
+
res.end(JSON.stringify({ error: "Internal Server Error" }));
|
|
2716
|
+
}
|
|
2717
|
+
}
|
|
2718
|
+
});
|
|
2719
|
+
};
|
|
2720
|
+
}
|
|
2721
|
+
if (typeof app.callback === "function") {
|
|
2722
|
+
return app.callback();
|
|
2723
|
+
}
|
|
2724
|
+
if (typeof app.routing === "function") {
|
|
2725
|
+
const routing = app.routing;
|
|
2726
|
+
const appWithReady = app;
|
|
2727
|
+
let readyPromise = null;
|
|
2728
|
+
return async (req, res) => {
|
|
2729
|
+
if (readyPromise === null) {
|
|
2730
|
+
readyPromise = typeof appWithReady.ready === "function" ? appWithReady.ready() : Promise.resolve();
|
|
2731
|
+
}
|
|
2732
|
+
await readyPromise;
|
|
2733
|
+
routing(req, res);
|
|
2734
|
+
};
|
|
2735
|
+
}
|
|
2736
|
+
throw new Error(
|
|
2737
|
+
`Unable to convert app to handler. The provided object must be:
|
|
2485
2738
|
- A function (raw Node.js handler)
|
|
2486
2739
|
- An object with .handle() method (Express, Connect)
|
|
2487
2740
|
- An object with .callback() method (Koa)
|
|
@@ -2493,12 +2746,14 @@ Examples:
|
|
|
2493
2746
|
Fastify: new WebApp("name", fastifyApp)
|
|
2494
2747
|
Raw: new WebApp("name", (req, res) => { ... })
|
|
2495
2748
|
`
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2749
|
+
);
|
|
2750
|
+
}
|
|
2751
|
+
getRawApp() {
|
|
2752
|
+
return this._rawApp;
|
|
2753
|
+
}
|
|
2754
|
+
};
|
|
2500
2755
|
}
|
|
2501
|
-
};
|
|
2756
|
+
});
|
|
2502
2757
|
|
|
2503
2758
|
// src/dmv2/registry.ts
|
|
2504
2759
|
function getTables() {
|
|
@@ -2564,6 +2819,32 @@ function getWebApps() {
|
|
|
2564
2819
|
function getWebApp(name) {
|
|
2565
2820
|
return getMooseInternal().webApps.get(name);
|
|
2566
2821
|
}
|
|
2822
|
+
var init_registry = __esm({
|
|
2823
|
+
"src/dmv2/registry.ts"() {
|
|
2824
|
+
"use strict";
|
|
2825
|
+
init_internal();
|
|
2826
|
+
}
|
|
2827
|
+
});
|
|
2828
|
+
|
|
2829
|
+
// src/dmv2/index.ts
|
|
2830
|
+
var init_dmv2 = __esm({
|
|
2831
|
+
"src/dmv2/index.ts"() {
|
|
2832
|
+
init_olapTable();
|
|
2833
|
+
init_stream();
|
|
2834
|
+
init_workflow();
|
|
2835
|
+
init_ingestApi();
|
|
2836
|
+
init_consumptionApi();
|
|
2837
|
+
init_ingestPipeline();
|
|
2838
|
+
init_etlPipeline();
|
|
2839
|
+
init_materializedView();
|
|
2840
|
+
init_sqlResource();
|
|
2841
|
+
init_view();
|
|
2842
|
+
init_lifeCycle();
|
|
2843
|
+
init_webApp();
|
|
2844
|
+
init_registry();
|
|
2845
|
+
}
|
|
2846
|
+
});
|
|
2847
|
+
init_dmv2();
|
|
2567
2848
|
export {
|
|
2568
2849
|
Api,
|
|
2569
2850
|
ConsumptionApi,
|