@514labs/moose-lib 0.6.295-ci-17-gc22400d0 → 0.6.295
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-CMEunMFq.d.ts → browserCompatible-B8CAYjv5.d.ts} +1 -1
- package/dist/{browserCompatible-FzU17dxm.d.mts → browserCompatible-ChWHzgtb.d.mts} +1 -1
- package/dist/browserCompatible.d.mts +2 -2
- package/dist/browserCompatible.d.ts +2 -2
- package/dist/browserCompatible.js +2161 -2444
- package/dist/browserCompatible.js.map +1 -1
- package/dist/browserCompatible.mjs +2165 -2446
- 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 +2058 -2341
- package/dist/dmv2/index.js.map +1 -1
- package/dist/dmv2/index.mjs +2020 -2301
- package/dist/dmv2/index.mjs.map +1 -1
- package/dist/{index-CcHF2cVT.d.mts → index-rQOQo9sv.d.mts} +5 -16
- package/dist/{index-CcHF2cVT.d.ts → index-rQOQo9sv.d.ts} +5 -16
- package/dist/index.d.mts +6 -76
- package/dist/index.d.ts +6 -76
- package/dist/index.js +2737 -3081
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2630 -2973
- package/dist/index.mjs.map +1 -1
- package/dist/moose-runner.js +1136 -1715
- package/dist/moose-runner.js.map +1 -1
- package/dist/moose-runner.mjs +1127 -1704
- package/dist/moose-runner.mjs.map +1 -1
- package/package.json +1 -1
package/dist/dmv2/index.mjs
CHANGED
|
@@ -8,247 +8,6 @@ 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
|
-
|
|
252
11
|
// src/commons.ts
|
|
253
12
|
var commons_exports = {};
|
|
254
13
|
__export(commons_exports, {
|
|
@@ -416,298 +175,6 @@ var init_commons = __esm({
|
|
|
416
175
|
}
|
|
417
176
|
});
|
|
418
177
|
|
|
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
|
-
|
|
711
178
|
// src/config/configFile.ts
|
|
712
179
|
import path from "path";
|
|
713
180
|
import * as toml from "toml";
|
|
@@ -892,1849 +359,2129 @@ var init_runtime = __esm({
|
|
|
892
359
|
}
|
|
893
360
|
});
|
|
894
361
|
|
|
895
|
-
// src/dmv2/
|
|
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
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
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 };
|
|
1007
479
|
}
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
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
|
+
]
|
|
1021
649
|
}
|
|
1022
|
-
}
|
|
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: {}
|
|
1023
665
|
}
|
|
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
|
-
|
|
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
|
+
// src/dmv2/sdk/olapTable.ts
|
|
743
|
+
import { Readable } from "stream";
|
|
744
|
+
import { createHash as createHash2 } from "crypto";
|
|
745
|
+
var OlapTable = class extends TypedBase {
|
|
746
|
+
name;
|
|
747
|
+
/** @internal */
|
|
748
|
+
kind = "OlapTable";
|
|
749
|
+
/** @internal Memoized ClickHouse client for reusing connections across insert calls */
|
|
750
|
+
_memoizedClient;
|
|
751
|
+
/** @internal Hash of the configuration used to create the memoized client */
|
|
752
|
+
_configHash;
|
|
753
|
+
/** @internal Cached table name to avoid repeated generation */
|
|
754
|
+
_cachedTableName;
|
|
755
|
+
constructor(name, config, schema, columns, validators) {
|
|
756
|
+
const resolvedConfig = config ? "engine" in config ? config : { ...config, engine: "MergeTree" /* MergeTree */ } : { engine: "MergeTree" /* MergeTree */ };
|
|
757
|
+
const hasFields = Array.isArray(resolvedConfig.orderByFields) && resolvedConfig.orderByFields.length > 0;
|
|
758
|
+
const hasExpr = typeof resolvedConfig.orderByExpression === "string" && resolvedConfig.orderByExpression.length > 0;
|
|
759
|
+
if (hasFields && hasExpr) {
|
|
760
|
+
throw new Error(
|
|
761
|
+
`OlapTable ${name}: Provide either orderByFields or orderByExpression, not both.`
|
|
762
|
+
);
|
|
763
|
+
}
|
|
764
|
+
const hasCluster = typeof resolvedConfig.cluster === "string";
|
|
765
|
+
const hasKeeperPath = typeof resolvedConfig.keeperPath === "string";
|
|
766
|
+
const hasReplicaName = typeof resolvedConfig.replicaName === "string";
|
|
767
|
+
if (hasCluster && (hasKeeperPath || hasReplicaName)) {
|
|
768
|
+
throw new Error(
|
|
769
|
+
`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'.`
|
|
770
|
+
);
|
|
771
|
+
}
|
|
772
|
+
super(name, resolvedConfig, schema, columns, validators);
|
|
773
|
+
this.name = name;
|
|
774
|
+
const tables = getMooseInternal().tables;
|
|
775
|
+
const registryKey = this.config.version ? `${name}_${this.config.version}` : name;
|
|
776
|
+
if (!isClientOnlyMode() && tables.has(registryKey)) {
|
|
777
|
+
throw new Error(
|
|
778
|
+
`OlapTable with name ${name} and version ${config?.version ?? "unversioned"} already exists`
|
|
779
|
+
);
|
|
780
|
+
}
|
|
781
|
+
tables.set(registryKey, this);
|
|
782
|
+
}
|
|
783
|
+
/**
|
|
784
|
+
* Generates the versioned table name following Moose's naming convention
|
|
785
|
+
* Format: {tableName}_{version_with_dots_replaced_by_underscores}
|
|
786
|
+
*/
|
|
787
|
+
generateTableName() {
|
|
788
|
+
if (this._cachedTableName) {
|
|
789
|
+
return this._cachedTableName;
|
|
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) {
|
|
1050
831
|
}
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
832
|
+
}
|
|
833
|
+
const effectiveDatabase = this.config.database ?? clickhouseConfig.database;
|
|
834
|
+
const client = getClickhouseClient2({
|
|
835
|
+
username: clickhouseConfig.username,
|
|
836
|
+
password: clickhouseConfig.password,
|
|
837
|
+
database: effectiveDatabase,
|
|
838
|
+
useSSL: clickhouseConfig.useSSL ? "true" : "false",
|
|
839
|
+
host: clickhouseConfig.host,
|
|
840
|
+
port: clickhouseConfig.port
|
|
841
|
+
});
|
|
842
|
+
this._memoizedClient = client;
|
|
843
|
+
this._configHash = currentConfigHash;
|
|
844
|
+
return { client, config: clickhouseConfig };
|
|
845
|
+
}
|
|
846
|
+
/**
|
|
847
|
+
* Closes the memoized ClickHouse client if it exists.
|
|
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;
|
|
1063
859
|
}
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
/**
|
|
863
|
+
* Validates a single record using typia's comprehensive type checking.
|
|
864
|
+
* This provides the most accurate validation as it uses the exact TypeScript type information.
|
|
865
|
+
*
|
|
866
|
+
* @param record The record to validate
|
|
867
|
+
* @returns Validation result with detailed error information
|
|
868
|
+
*/
|
|
869
|
+
validateRecord(record) {
|
|
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
|
+
};
|
|
1077
885
|
}
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
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
|
-
|
|
886
|
+
}
|
|
887
|
+
throw new Error("No typia validator found");
|
|
888
|
+
}
|
|
889
|
+
/**
|
|
890
|
+
* Type guard function using typia's is() function.
|
|
891
|
+
* Provides compile-time type narrowing for TypeScript.
|
|
892
|
+
*
|
|
893
|
+
* @param record The record to check
|
|
894
|
+
* @returns True if record matches type T, with type narrowing
|
|
895
|
+
*/
|
|
896
|
+
isValidRecord(record) {
|
|
897
|
+
if (this.validators?.is) {
|
|
898
|
+
return this.validators.is(record);
|
|
899
|
+
}
|
|
900
|
+
throw new Error("No typia validator found");
|
|
901
|
+
}
|
|
902
|
+
/**
|
|
903
|
+
* Assert that a record matches type T, throwing detailed errors if not.
|
|
904
|
+
* Uses typia's assert() function for the most detailed error reporting.
|
|
905
|
+
*
|
|
906
|
+
* @param record The record to assert
|
|
907
|
+
* @returns The validated and typed record
|
|
908
|
+
* @throws Detailed validation error if record doesn't match type T
|
|
909
|
+
*/
|
|
910
|
+
assertValidRecord(record) {
|
|
911
|
+
if (this.validators?.assert) {
|
|
912
|
+
return this.validators.assert(record);
|
|
913
|
+
}
|
|
914
|
+
throw new Error("No typia validator found");
|
|
915
|
+
}
|
|
916
|
+
/**
|
|
917
|
+
* Validates an array of records with comprehensive error reporting.
|
|
918
|
+
* Uses the most appropriate validation method available (typia or basic).
|
|
919
|
+
*
|
|
920
|
+
* @param data Array of records to validate
|
|
921
|
+
* @returns Detailed validation results
|
|
922
|
+
*/
|
|
923
|
+
async validateRecords(data) {
|
|
924
|
+
const valid = [];
|
|
925
|
+
const invalid = [];
|
|
926
|
+
valid.length = 0;
|
|
927
|
+
invalid.length = 0;
|
|
928
|
+
const dataLength = data.length;
|
|
929
|
+
for (let i = 0; i < dataLength; i++) {
|
|
930
|
+
const record = data[i];
|
|
931
|
+
try {
|
|
932
|
+
if (this.isValidRecord(record)) {
|
|
933
|
+
valid.push(this.mapToClickhouseRecord(record));
|
|
934
|
+
} else {
|
|
935
|
+
const result = this.validateRecord(record);
|
|
936
|
+
if (result.success) {
|
|
937
|
+
valid.push(this.mapToClickhouseRecord(record));
|
|
938
|
+
} else {
|
|
1110
939
|
invalid.push({
|
|
1111
940
|
record,
|
|
1112
|
-
error:
|
|
941
|
+
error: result.errors?.join(", ") || "Validation failed",
|
|
1113
942
|
index: i,
|
|
1114
943
|
path: "root"
|
|
1115
944
|
});
|
|
1116
945
|
}
|
|
1117
946
|
}
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
947
|
+
} catch (error) {
|
|
948
|
+
invalid.push({
|
|
949
|
+
record,
|
|
950
|
+
error: error instanceof Error ? error.message : String(error),
|
|
951
|
+
index: i,
|
|
952
|
+
path: "root"
|
|
953
|
+
});
|
|
1123
954
|
}
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
955
|
+
}
|
|
956
|
+
return {
|
|
957
|
+
valid,
|
|
958
|
+
invalid,
|
|
959
|
+
total: dataLength
|
|
960
|
+
};
|
|
961
|
+
}
|
|
962
|
+
/**
|
|
963
|
+
* Optimized batch retry that minimizes individual insert operations.
|
|
964
|
+
* Groups records into smaller batches to reduce round trips while still isolating failures.
|
|
965
|
+
*
|
|
966
|
+
* @private
|
|
967
|
+
*/
|
|
968
|
+
async retryIndividualRecords(client, tableName, records) {
|
|
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];
|
|
1138
992
|
try {
|
|
1139
993
|
await client.insert({
|
|
1140
994
|
table: quoteIdentifier(tableName),
|
|
1141
|
-
values:
|
|
995
|
+
values: [record],
|
|
1142
996
|
format: "JSONEachRow",
|
|
1143
997
|
clickhouse_settings: {
|
|
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
|
|
998
|
+
date_time_input_format: "best_effort"
|
|
1148
999
|
}
|
|
1149
1000
|
});
|
|
1150
|
-
successful.push(
|
|
1151
|
-
} catch (
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
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
|
-
}
|
|
1172
|
-
}
|
|
1173
|
-
}
|
|
1174
|
-
return { successful, failed };
|
|
1175
|
-
}
|
|
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) {
|
|
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
|
-
);
|
|
1260
|
-
case "discard":
|
|
1261
|
-
this.checkValidationThresholds(validationErrors, data.length, options);
|
|
1262
|
-
break;
|
|
1263
|
-
case "isolate":
|
|
1264
|
-
break;
|
|
1265
|
-
}
|
|
1266
|
-
}
|
|
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
|
-
}
|
|
1284
|
-
}
|
|
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;
|
|
1001
|
+
successful.push(record);
|
|
1002
|
+
} catch (error) {
|
|
1003
|
+
failed.push({
|
|
1004
|
+
record,
|
|
1005
|
+
error: error instanceof Error ? error.message : String(error),
|
|
1006
|
+
index: i + j
|
|
1007
|
+
});
|
|
1317
1008
|
}
|
|
1318
1009
|
}
|
|
1319
|
-
return insertOptions;
|
|
1320
1010
|
}
|
|
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
|
-
|
|
1011
|
+
}
|
|
1012
|
+
return { successful, failed };
|
|
1013
|
+
}
|
|
1014
|
+
/**
|
|
1015
|
+
* Validates input parameters and strategy compatibility
|
|
1016
|
+
* @private
|
|
1017
|
+
*/
|
|
1018
|
+
validateInsertParameters(data, options) {
|
|
1019
|
+
const isStream = data instanceof Readable;
|
|
1020
|
+
const strategy = options?.strategy || "fail-fast";
|
|
1021
|
+
const shouldValidate = options?.validate !== false;
|
|
1022
|
+
if (isStream && strategy === "isolate") {
|
|
1023
|
+
throw new Error(
|
|
1024
|
+
"The 'isolate' error strategy is not supported with stream input. Use 'fail-fast' or 'discard' instead."
|
|
1025
|
+
);
|
|
1026
|
+
}
|
|
1027
|
+
if (isStream && shouldValidate) {
|
|
1028
|
+
console.warn(
|
|
1029
|
+
"Validation is not supported with stream input. Validation will be skipped."
|
|
1030
|
+
);
|
|
1031
|
+
}
|
|
1032
|
+
return { isStream, strategy, shouldValidate };
|
|
1033
|
+
}
|
|
1034
|
+
/**
|
|
1035
|
+
* Handles early return cases for empty data
|
|
1036
|
+
* @private
|
|
1037
|
+
*/
|
|
1038
|
+
handleEmptyData(data, isStream) {
|
|
1039
|
+
if (isStream && !data) {
|
|
1040
|
+
return {
|
|
1041
|
+
successful: 0,
|
|
1042
|
+
failed: 0,
|
|
1043
|
+
total: 0
|
|
1044
|
+
};
|
|
1045
|
+
}
|
|
1046
|
+
if (!isStream && (!data || data.length === 0)) {
|
|
1047
|
+
return {
|
|
1048
|
+
successful: 0,
|
|
1049
|
+
failed: 0,
|
|
1050
|
+
total: 0
|
|
1051
|
+
};
|
|
1052
|
+
}
|
|
1053
|
+
return null;
|
|
1054
|
+
}
|
|
1055
|
+
/**
|
|
1056
|
+
* Performs pre-insertion validation for array data
|
|
1057
|
+
* @private
|
|
1058
|
+
*/
|
|
1059
|
+
async performPreInsertionValidation(data, shouldValidate, strategy, options) {
|
|
1060
|
+
if (!shouldValidate) {
|
|
1061
|
+
return { validatedData: data, validationErrors: [] };
|
|
1062
|
+
}
|
|
1063
|
+
try {
|
|
1064
|
+
const validationResult = await this.validateRecords(data);
|
|
1065
|
+
const validatedData = validationResult.valid;
|
|
1066
|
+
const validationErrors = validationResult.invalid;
|
|
1067
|
+
if (validationErrors.length > 0) {
|
|
1068
|
+
this.handleValidationErrors(validationErrors, strategy, data, options);
|
|
1355
1069
|
switch (strategy) {
|
|
1356
|
-
case "fail-fast":
|
|
1357
|
-
throw new Error(
|
|
1358
|
-
`Failed to insert data into table ${tableName}: ${batchError}`
|
|
1359
|
-
);
|
|
1360
1070
|
case "discard":
|
|
1361
|
-
|
|
1362
|
-
`Too many errors during insert into table ${tableName}. Error threshold exceeded: ${batchError}`
|
|
1363
|
-
);
|
|
1071
|
+
return { validatedData, validationErrors };
|
|
1364
1072
|
case "isolate":
|
|
1365
|
-
return
|
|
1366
|
-
batchError,
|
|
1367
|
-
tableName,
|
|
1368
|
-
data,
|
|
1369
|
-
validatedData,
|
|
1370
|
-
validationErrors,
|
|
1371
|
-
isStream,
|
|
1372
|
-
shouldValidate,
|
|
1373
|
-
options
|
|
1374
|
-
);
|
|
1073
|
+
return { validatedData: data, validationErrors };
|
|
1375
1074
|
default:
|
|
1376
|
-
|
|
1075
|
+
return { validatedData, validationErrors };
|
|
1377
1076
|
}
|
|
1378
1077
|
}
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
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
|
-
);
|
|
1388
|
-
}
|
|
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
|
-
);
|
|
1423
|
-
}
|
|
1078
|
+
return { validatedData, validationErrors };
|
|
1079
|
+
} catch (validationError) {
|
|
1080
|
+
if (strategy === "fail-fast") {
|
|
1081
|
+
throw validationError;
|
|
1424
1082
|
}
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1083
|
+
console.warn("Validation error:", validationError);
|
|
1084
|
+
return { validatedData: data, validationErrors: [] };
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
/**
|
|
1088
|
+
* Handles validation errors based on the specified strategy
|
|
1089
|
+
* @private
|
|
1090
|
+
*/
|
|
1091
|
+
handleValidationErrors(validationErrors, strategy, data, options) {
|
|
1092
|
+
switch (strategy) {
|
|
1093
|
+
case "fail-fast":
|
|
1094
|
+
const firstError = validationErrors[0];
|
|
1095
|
+
throw new Error(
|
|
1096
|
+
`Validation failed for record at index ${firstError.index}: ${firstError.error}`
|
|
1097
|
+
);
|
|
1098
|
+
case "discard":
|
|
1099
|
+
this.checkValidationThresholds(validationErrors, data.length, options);
|
|
1100
|
+
break;
|
|
1101
|
+
case "isolate":
|
|
1102
|
+
break;
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
/**
|
|
1106
|
+
* Checks if validation errors exceed configured thresholds
|
|
1107
|
+
* @private
|
|
1108
|
+
*/
|
|
1109
|
+
checkValidationThresholds(validationErrors, totalRecords, options) {
|
|
1110
|
+
const validationFailedCount = validationErrors.length;
|
|
1111
|
+
const validationFailedRatio = validationFailedCount / totalRecords;
|
|
1112
|
+
if (options?.allowErrors !== void 0 && validationFailedCount > options.allowErrors) {
|
|
1113
|
+
throw new Error(
|
|
1114
|
+
`Too many validation failures: ${validationFailedCount} > ${options.allowErrors}. Errors: ${validationErrors.map((e) => e.error).join(", ")}`
|
|
1115
|
+
);
|
|
1116
|
+
}
|
|
1117
|
+
if (options?.allowErrorsRatio !== void 0 && validationFailedRatio > options.allowErrorsRatio) {
|
|
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
|
|
1442
1142
|
}
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
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;
|
|
1143
|
+
};
|
|
1144
|
+
if (isStream) {
|
|
1145
|
+
insertOptions.values = data;
|
|
1146
|
+
} else {
|
|
1147
|
+
insertOptions.values = validatedData;
|
|
1148
|
+
}
|
|
1149
|
+
if (strategy === "discard" && (options?.allowErrors !== void 0 || options?.allowErrorsRatio !== void 0)) {
|
|
1150
|
+
if (options.allowErrors !== void 0) {
|
|
1151
|
+
insertOptions.clickhouse_settings.input_format_allow_errors_num = options.allowErrors;
|
|
1472
1152
|
}
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1153
|
+
if (options.allowErrorsRatio !== void 0) {
|
|
1154
|
+
insertOptions.clickhouse_settings.input_format_allow_errors_ratio = options.allowErrorsRatio;
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
return insertOptions;
|
|
1158
|
+
}
|
|
1159
|
+
/**
|
|
1160
|
+
* Creates success result for completed insertions
|
|
1161
|
+
* @private
|
|
1162
|
+
*/
|
|
1163
|
+
createSuccessResult(data, validatedData, validationErrors, isStream, shouldValidate, strategy) {
|
|
1164
|
+
if (isStream) {
|
|
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
|
+
]);
|
|
1549
1302
|
}
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
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
|
-
);
|
|
1303
|
+
} else if (isNestedType(dt)) {
|
|
1304
|
+
if (value && typeof value === "object") {
|
|
1305
|
+
result[col.name] = this.mapToClickhouseRecord(value, dt.columns);
|
|
1582
1306
|
}
|
|
1583
1307
|
}
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
// new OlapTable(name, { engine: ClickHouseEngines.ReplacingMergeTree, orderByFields: ["id"], ver: "updated_at" })
|
|
1587
|
-
};
|
|
1308
|
+
}
|
|
1309
|
+
return result;
|
|
1588
1310
|
}
|
|
1589
|
-
|
|
1311
|
+
/**
|
|
1312
|
+
* Inserts data directly into the ClickHouse table with enhanced error handling and validation.
|
|
1313
|
+
* This method establishes a direct connection to ClickHouse using the project configuration
|
|
1314
|
+
* and inserts the provided data into the versioned table.
|
|
1315
|
+
*
|
|
1316
|
+
* PERFORMANCE OPTIMIZATIONS:
|
|
1317
|
+
* - Memoized client connections with fast config hashing
|
|
1318
|
+
* - Single-pass validation with pre-allocated arrays
|
|
1319
|
+
* - Batch-optimized retry strategy (batches of 10, then individual)
|
|
1320
|
+
* - Optimized ClickHouse settings for large datasets
|
|
1321
|
+
* - Reduced memory allocations and object creation
|
|
1322
|
+
*
|
|
1323
|
+
* Uses advanced typia validation when available for comprehensive type checking,
|
|
1324
|
+
* with fallback to basic validation for compatibility.
|
|
1325
|
+
*
|
|
1326
|
+
* The ClickHouse client is memoized and reused across multiple insert calls for better performance.
|
|
1327
|
+
* If the configuration changes, a new client will be automatically created.
|
|
1328
|
+
*
|
|
1329
|
+
* @param data Array of objects conforming to the table schema, or a Node.js Readable stream
|
|
1330
|
+
* @param options Optional configuration for error handling, validation, and insertion behavior
|
|
1331
|
+
* @returns Promise resolving to detailed insertion results
|
|
1332
|
+
* @throws {ConfigError} When configuration cannot be read or parsed
|
|
1333
|
+
* @throws {ClickHouseError} When insertion fails based on the error strategy
|
|
1334
|
+
* @throws {ValidationError} When validation fails and strategy is 'fail-fast'
|
|
1335
|
+
*
|
|
1336
|
+
* @example
|
|
1337
|
+
* ```typescript
|
|
1338
|
+
* // Create an OlapTable instance (typia validators auto-injected)
|
|
1339
|
+
* const userTable = new OlapTable<User>('users');
|
|
1340
|
+
*
|
|
1341
|
+
* // Insert with comprehensive typia validation
|
|
1342
|
+
* const result1 = await userTable.insert([
|
|
1343
|
+
* { id: 1, name: 'John', email: 'john@example.com' },
|
|
1344
|
+
* { id: 2, name: 'Jane', email: 'jane@example.com' }
|
|
1345
|
+
* ]);
|
|
1346
|
+
*
|
|
1347
|
+
* // Insert data with stream input (validation not available for streams)
|
|
1348
|
+
* const dataStream = new Readable({
|
|
1349
|
+
* objectMode: true,
|
|
1350
|
+
* read() { // Stream implementation }
|
|
1351
|
+
* });
|
|
1352
|
+
* const result2 = await userTable.insert(dataStream, { strategy: 'fail-fast' });
|
|
1353
|
+
*
|
|
1354
|
+
* // Insert with validation disabled for performance
|
|
1355
|
+
* const result3 = await userTable.insert(data, { validate: false });
|
|
1356
|
+
*
|
|
1357
|
+
* // Insert with error handling strategies
|
|
1358
|
+
* const result4 = await userTable.insert(mixedData, {
|
|
1359
|
+
* strategy: 'isolate',
|
|
1360
|
+
* allowErrorsRatio: 0.1,
|
|
1361
|
+
* validate: true // Use typia validation (default)
|
|
1362
|
+
* });
|
|
1363
|
+
*
|
|
1364
|
+
* // Optional: Clean up connection when completely done
|
|
1365
|
+
* await userTable.closeClient();
|
|
1366
|
+
* ```
|
|
1367
|
+
*/
|
|
1368
|
+
async insert(data, options) {
|
|
1369
|
+
const { isStream, strategy, shouldValidate } = this.validateInsertParameters(data, options);
|
|
1370
|
+
const emptyResult = this.handleEmptyData(data, isStream);
|
|
1371
|
+
if (emptyResult) {
|
|
1372
|
+
return emptyResult;
|
|
1373
|
+
}
|
|
1374
|
+
let validatedData = [];
|
|
1375
|
+
let validationErrors = [];
|
|
1376
|
+
if (!isStream && shouldValidate) {
|
|
1377
|
+
const validationResult = await this.performPreInsertionValidation(
|
|
1378
|
+
data,
|
|
1379
|
+
shouldValidate,
|
|
1380
|
+
strategy,
|
|
1381
|
+
options
|
|
1382
|
+
);
|
|
1383
|
+
validatedData = validationResult.validatedData;
|
|
1384
|
+
validationErrors = validationResult.validationErrors;
|
|
1385
|
+
} else {
|
|
1386
|
+
validatedData = isStream ? [] : data;
|
|
1387
|
+
}
|
|
1388
|
+
const { client } = await this.getMemoizedClient();
|
|
1389
|
+
const tableName = this.generateTableName();
|
|
1390
|
+
try {
|
|
1391
|
+
const insertOptions = this.prepareInsertOptions(
|
|
1392
|
+
tableName,
|
|
1393
|
+
data,
|
|
1394
|
+
validatedData,
|
|
1395
|
+
isStream,
|
|
1396
|
+
strategy,
|
|
1397
|
+
options
|
|
1398
|
+
);
|
|
1399
|
+
await client.insert(insertOptions);
|
|
1400
|
+
return this.createSuccessResult(
|
|
1401
|
+
data,
|
|
1402
|
+
validatedData,
|
|
1403
|
+
validationErrors,
|
|
1404
|
+
isStream,
|
|
1405
|
+
shouldValidate,
|
|
1406
|
+
strategy
|
|
1407
|
+
);
|
|
1408
|
+
} catch (batchError) {
|
|
1409
|
+
return await this.handleInsertionError(
|
|
1410
|
+
batchError,
|
|
1411
|
+
strategy,
|
|
1412
|
+
tableName,
|
|
1413
|
+
data,
|
|
1414
|
+
validatedData,
|
|
1415
|
+
validationErrors,
|
|
1416
|
+
isStream,
|
|
1417
|
+
shouldValidate,
|
|
1418
|
+
options
|
|
1419
|
+
);
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
// Note: Static factory methods (withS3Queue, withReplacingMergeTree, withMergeTree)
|
|
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
|
+
};
|
|
1590
1426
|
|
|
1591
1427
|
// src/dmv2/sdk/stream.ts
|
|
1592
1428
|
import { createHash as createHash3 } from "crypto";
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
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
|
-
|
|
1642
|
-
|
|
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
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
await
|
|
1683
|
-
|
|
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
|
|
1720
|
-
);
|
|
1721
|
-
this._memoizedProducer = producer;
|
|
1722
|
-
this._kafkaConfigHash = currentHash;
|
|
1723
|
-
return { producer, kafkaConfig };
|
|
1724
|
-
}
|
|
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
|
-
}
|
|
1429
|
+
var RoutedMessage = class {
|
|
1430
|
+
/** The destination stream for the message */
|
|
1431
|
+
destination;
|
|
1432
|
+
/** The message value(s) to send */
|
|
1433
|
+
values;
|
|
1434
|
+
/**
|
|
1435
|
+
* Creates a new routed message.
|
|
1436
|
+
*
|
|
1437
|
+
* @param destination The target stream
|
|
1438
|
+
* @param values The message(s) to route
|
|
1439
|
+
*/
|
|
1440
|
+
constructor(destination, values) {
|
|
1441
|
+
this.destination = destination;
|
|
1442
|
+
this.values = values;
|
|
1443
|
+
}
|
|
1444
|
+
};
|
|
1445
|
+
var Stream = class extends TypedBase {
|
|
1446
|
+
defaultDeadLetterQueue;
|
|
1447
|
+
/** @internal Memoized KafkaJS producer for reusing connections across sends */
|
|
1448
|
+
_memoizedProducer;
|
|
1449
|
+
/** @internal Hash of the configuration used to create the memoized Kafka producer */
|
|
1450
|
+
_kafkaConfigHash;
|
|
1451
|
+
constructor(name, config, schema, columns, validators, allowExtraFields) {
|
|
1452
|
+
super(name, config ?? {}, schema, columns, void 0, allowExtraFields);
|
|
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 {
|
|
1738
1520
|
}
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
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
|
-
});
|
|
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}`);
|
|
1789
1534
|
}
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
} else {
|
|
1818
|
-
this._transformations.set(destination.name, [
|
|
1819
|
-
[destination, transformation, transformConfig]
|
|
1820
|
-
]);
|
|
1821
|
-
}
|
|
1535
|
+
};
|
|
1536
|
+
const producer = await getKafkaProducer2(
|
|
1537
|
+
{
|
|
1538
|
+
clientId,
|
|
1539
|
+
broker: kafkaConfig.broker,
|
|
1540
|
+
securityProtocol: kafkaConfig.securityProtocol,
|
|
1541
|
+
saslUsername: kafkaConfig.saslUsername,
|
|
1542
|
+
saslPassword: kafkaConfig.saslPassword,
|
|
1543
|
+
saslMechanism: kafkaConfig.saslMechanism
|
|
1544
|
+
},
|
|
1545
|
+
logger
|
|
1546
|
+
);
|
|
1547
|
+
this._memoizedProducer = producer;
|
|
1548
|
+
this._kafkaConfigHash = currentHash;
|
|
1549
|
+
return { producer, kafkaConfig };
|
|
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;
|
|
1822
1562
|
}
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
/**
|
|
1566
|
+
* Sends one or more records to this stream's Kafka topic.
|
|
1567
|
+
* Values are JSON-serialized as message values.
|
|
1568
|
+
*/
|
|
1569
|
+
async send(values) {
|
|
1570
|
+
const flat = Array.isArray(values) ? values : values !== void 0 && values !== null ? [values] : [];
|
|
1571
|
+
if (flat.length === 0) return;
|
|
1572
|
+
const { producer, kafkaConfig } = await this.getMemoizedProducer();
|
|
1573
|
+
const topic = this.buildFullTopicName(kafkaConfig.namespace);
|
|
1574
|
+
const sr = this.config.schemaConfig;
|
|
1575
|
+
if (sr && sr.kind === "JSON") {
|
|
1576
|
+
const schemaRegistryUrl = kafkaConfig.schemaRegistryUrl;
|
|
1577
|
+
if (!schemaRegistryUrl) {
|
|
1578
|
+
throw new Error("Schema Registry URL not configured");
|
|
1579
|
+
}
|
|
1580
|
+
const {
|
|
1581
|
+
default: { SchemaRegistry }
|
|
1582
|
+
} = await import("@kafkajs/confluent-schema-registry");
|
|
1583
|
+
const registry = new SchemaRegistry({ host: schemaRegistryUrl });
|
|
1584
|
+
let schemaId = void 0;
|
|
1585
|
+
if ("id" in sr.reference) {
|
|
1586
|
+
schemaId = sr.reference.id;
|
|
1587
|
+
} else if ("subjectLatest" in sr.reference) {
|
|
1588
|
+
schemaId = await registry.getLatestSchemaId(sr.reference.subjectLatest);
|
|
1589
|
+
} else if ("subject" in sr.reference) {
|
|
1590
|
+
schemaId = await registry.getRegistryId(
|
|
1591
|
+
sr.reference.subject,
|
|
1592
|
+
sr.reference.version
|
|
1841
1593
|
);
|
|
1842
|
-
if (!hasVersion) {
|
|
1843
|
-
this._consumers.push({ consumer, config: consumerConfig });
|
|
1844
|
-
}
|
|
1845
1594
|
}
|
|
1846
|
-
|
|
1847
|
-
|
|
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;
|
|
1595
|
+
if (schemaId === void 0) {
|
|
1596
|
+
throw new Error("Malformed schema reference.");
|
|
1870
1597
|
}
|
|
1598
|
+
const encoded = await Promise.all(
|
|
1599
|
+
flat.map(
|
|
1600
|
+
(v) => registry.encode(schemaId, v)
|
|
1601
|
+
)
|
|
1602
|
+
);
|
|
1603
|
+
await producer.send({
|
|
1604
|
+
topic,
|
|
1605
|
+
messages: encoded.map((value) => ({ value }))
|
|
1606
|
+
});
|
|
1607
|
+
return;
|
|
1608
|
+
} else if (sr !== void 0) {
|
|
1609
|
+
throw new Error("Currently only JSON Schema is supported.");
|
|
1610
|
+
}
|
|
1611
|
+
await producer.send({
|
|
1612
|
+
topic,
|
|
1613
|
+
messages: flat.map((v) => ({ value: JSON.stringify(v) }))
|
|
1614
|
+
});
|
|
1615
|
+
}
|
|
1616
|
+
/**
|
|
1617
|
+
* Adds a transformation step that processes messages from this stream and sends the results to a destination stream.
|
|
1618
|
+
* Multiple transformations to the same destination stream can be added if they have distinct `version` identifiers in their config.
|
|
1619
|
+
*
|
|
1620
|
+
* @template U The data type of the messages in the destination stream.
|
|
1621
|
+
* @param destination The destination stream for the transformed messages.
|
|
1622
|
+
* @param transformation A function that takes a message of type T and returns zero or more messages of type U (or a Promise thereof).
|
|
1623
|
+
* Return `null` or `undefined` or an empty array `[]` to filter out a message. Return an array to emit multiple messages.
|
|
1624
|
+
* @param config Optional configuration for this specific transformation step, like a version.
|
|
1625
|
+
*/
|
|
1626
|
+
addTransform(destination, transformation, config) {
|
|
1627
|
+
const sourceFile = getSourceFileFromStack(new Error().stack);
|
|
1628
|
+
const transformConfig = {
|
|
1629
|
+
...config ?? {},
|
|
1630
|
+
sourceFile
|
|
1871
1631
|
};
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
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);
|
|
1632
|
+
if (transformConfig.deadLetterQueue === void 0) {
|
|
1633
|
+
transformConfig.deadLetterQueue = this.defaultDeadLetterQueue;
|
|
1634
|
+
}
|
|
1635
|
+
if (this._transformations.has(destination.name)) {
|
|
1636
|
+
const existingTransforms = this._transformations.get(destination.name);
|
|
1637
|
+
const hasVersion = existingTransforms.some(
|
|
1638
|
+
([_, __, cfg]) => cfg.version === transformConfig.version
|
|
1639
|
+
);
|
|
1640
|
+
if (!hasVersion) {
|
|
1641
|
+
existingTransforms.push([destination, transformation, transformConfig]);
|
|
1931
1642
|
}
|
|
1643
|
+
} else {
|
|
1644
|
+
this._transformations.set(destination.name, [
|
|
1645
|
+
[destination, transformation, transformConfig]
|
|
1646
|
+
]);
|
|
1647
|
+
}
|
|
1648
|
+
}
|
|
1649
|
+
/**
|
|
1650
|
+
* Adds a consumer function that processes messages from this stream.
|
|
1651
|
+
* Multiple consumers can be added if they have distinct `version` identifiers in their config.
|
|
1652
|
+
*
|
|
1653
|
+
* @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>.
|
|
1654
|
+
* @param config Optional configuration for this specific consumer, like a version.
|
|
1655
|
+
*/
|
|
1656
|
+
addConsumer(consumer, config) {
|
|
1657
|
+
const sourceFile = getSourceFileFromStack(new Error().stack);
|
|
1658
|
+
const consumerConfig = {
|
|
1659
|
+
...config ?? {},
|
|
1660
|
+
sourceFile
|
|
1932
1661
|
};
|
|
1662
|
+
if (consumerConfig.deadLetterQueue === void 0) {
|
|
1663
|
+
consumerConfig.deadLetterQueue = this.defaultDeadLetterQueue;
|
|
1664
|
+
}
|
|
1665
|
+
const hasVersion = this._consumers.some(
|
|
1666
|
+
(existing) => existing.config.version === consumerConfig.version
|
|
1667
|
+
);
|
|
1668
|
+
if (!hasVersion) {
|
|
1669
|
+
this._consumers.push({ consumer, config: consumerConfig });
|
|
1670
|
+
}
|
|
1933
1671
|
}
|
|
1934
|
-
|
|
1672
|
+
/**
|
|
1673
|
+
* Helper method for `addMultiTransform` to specify the destination and values for a routed message.
|
|
1674
|
+
* @param values The value or values to send to this stream.
|
|
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
|
+
};
|
|
1935
1762
|
|
|
1936
1763
|
// src/dmv2/sdk/workflow.ts
|
|
1937
|
-
var Task
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
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
|
+
);
|
|
1983
1847
|
}
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
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);
|
|
1848
|
+
const taskName = task.name;
|
|
1849
|
+
if (recursionStack.has(taskName)) {
|
|
1850
|
+
const cycleStartIndex = currentPath.indexOf(taskName);
|
|
1851
|
+
const cyclePath = cycleStartIndex >= 0 ? currentPath.slice(cycleStartIndex).concat(taskName) : currentPath.concat(taskName);
|
|
1852
|
+
throw new Error(
|
|
1853
|
+
`Workflow "${workflowName}" contains an infinite loop in task chain: ${cyclePath.join(" -> ")}`
|
|
1854
|
+
);
|
|
2002
1855
|
}
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
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
|
-
);
|
|
1856
|
+
if (visited.has(taskName)) {
|
|
1857
|
+
return;
|
|
1858
|
+
}
|
|
1859
|
+
visited.add(taskName);
|
|
1860
|
+
recursionStack.add(taskName);
|
|
1861
|
+
if (task.config.onComplete) {
|
|
1862
|
+
for (const nextTask of task.config.onComplete) {
|
|
1863
|
+
validateTask(nextTask, [...currentPath, taskName]);
|
|
2016
1864
|
}
|
|
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(" -> ") + " -> " : "";
|
|
2022
|
-
throw new Error(
|
|
2023
|
-
`Workflow "${workflowName}" contains a null or undefined task in the task chain: ${pathStr}null`
|
|
2024
|
-
);
|
|
2025
|
-
}
|
|
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);
|
|
2030
|
-
throw new Error(
|
|
2031
|
-
`Workflow "${workflowName}" contains an infinite loop in task chain: ${cyclePath.join(" -> ")}`
|
|
2032
|
-
);
|
|
2033
|
-
}
|
|
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
|
-
}
|
|
2043
|
-
}
|
|
2044
|
-
recursionStack.delete(taskName);
|
|
2045
|
-
};
|
|
2046
|
-
validateTask(startingTask, []);
|
|
2047
1865
|
}
|
|
1866
|
+
recursionStack.delete(taskName);
|
|
2048
1867
|
};
|
|
1868
|
+
validateTask(startingTask, []);
|
|
2049
1869
|
}
|
|
2050
|
-
}
|
|
1870
|
+
};
|
|
2051
1871
|
|
|
2052
1872
|
// src/dmv2/sdk/ingestApi.ts
|
|
2053
|
-
var IngestApi
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
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`);
|
|
2065
|
-
}
|
|
2066
|
-
ingestApis.set(name, this);
|
|
2067
|
-
}
|
|
2068
|
-
};
|
|
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);
|
|
2069
1881
|
}
|
|
2070
|
-
}
|
|
1882
|
+
};
|
|
2071
1883
|
|
|
2072
1884
|
// src/dmv2/sdk/consumptionApi.ts
|
|
2073
|
-
var Api
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
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
|
-
}
|
|
1885
|
+
var Api = class extends TypedBase {
|
|
1886
|
+
/** @internal The handler function that processes requests and generates responses. */
|
|
1887
|
+
_handler;
|
|
1888
|
+
/** @internal The JSON schema definition for the response type R. */
|
|
1889
|
+
responseSchema;
|
|
1890
|
+
constructor(name, handler, config, schema, columns, responseSchema) {
|
|
1891
|
+
super(name, config ?? {}, schema, columns);
|
|
1892
|
+
this._handler = handler;
|
|
1893
|
+
this.responseSchema = responseSchema ?? {
|
|
1894
|
+
version: "3.1",
|
|
1895
|
+
schemas: [{ type: "array", items: { type: "object" } }],
|
|
1896
|
+
components: { schemas: {} }
|
|
1897
|
+
};
|
|
1898
|
+
const apis = getMooseInternal().apis;
|
|
1899
|
+
const key = `${name}${config?.version ? `:${config.version}` : ""}`;
|
|
1900
|
+
if (apis.has(key)) {
|
|
1901
|
+
throw new Error(
|
|
1902
|
+
`Consumption API with name ${name} and version ${config?.version} already exists`
|
|
1903
|
+
);
|
|
1904
|
+
}
|
|
1905
|
+
apis.set(key, this);
|
|
1906
|
+
if (config?.path) {
|
|
1907
|
+
if (config.version) {
|
|
1908
|
+
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] === "/";
|
|
1909
|
+
if (pathEndsWithVersion) {
|
|
1910
|
+
if (apis.has(config.path)) {
|
|
1911
|
+
const existing = apis.get(config.path);
|
|
1912
|
+
throw new Error(
|
|
1913
|
+
`Cannot register API "${name}" with path "${config.path}" - this path is already used by API "${existing.name}"`
|
|
1914
|
+
);
|
|
1915
|
+
}
|
|
1916
|
+
apis.set(config.path, this);
|
|
1917
|
+
} else {
|
|
1918
|
+
const versionedPath = `${config.path.replace(/\/$/, "")}/${config.version}`;
|
|
1919
|
+
if (apis.has(versionedPath)) {
|
|
1920
|
+
const existing = apis.get(versionedPath);
|
|
1921
|
+
throw new Error(
|
|
1922
|
+
`Cannot register API "${name}" with path "${versionedPath}" - this path is already used by API "${existing.name}"`
|
|
1923
|
+
);
|
|
1924
|
+
}
|
|
1925
|
+
apis.set(versionedPath, this);
|
|
1926
|
+
if (!apis.has(config.path)) {
|
|
2131
1927
|
apis.set(config.path, this);
|
|
2132
1928
|
}
|
|
2133
1929
|
}
|
|
1930
|
+
} else {
|
|
1931
|
+
if (apis.has(config.path)) {
|
|
1932
|
+
const existing = apis.get(config.path);
|
|
1933
|
+
throw new Error(
|
|
1934
|
+
`Cannot register API "${name}" with custom path "${config.path}" - this path is already used by API "${existing.name}"`
|
|
1935
|
+
);
|
|
1936
|
+
}
|
|
1937
|
+
apis.set(config.path, this);
|
|
2134
1938
|
}
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
}
|
|
2152
|
-
} else {
|
|
2153
|
-
path2 = this.config.path;
|
|
2154
|
-
}
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
/**
|
|
1942
|
+
* Retrieves the handler function associated with this Consumption API.
|
|
1943
|
+
* @returns The handler function.
|
|
1944
|
+
*/
|
|
1945
|
+
getHandler = () => {
|
|
1946
|
+
return this._handler;
|
|
1947
|
+
};
|
|
1948
|
+
async call(baseUrl, queryParams) {
|
|
1949
|
+
let path2;
|
|
1950
|
+
if (this.config?.path) {
|
|
1951
|
+
if (this.config.version) {
|
|
1952
|
+
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] === "/";
|
|
1953
|
+
if (pathEndsWithVersion) {
|
|
1954
|
+
path2 = this.config.path;
|
|
2155
1955
|
} else {
|
|
2156
|
-
path2 = this.config
|
|
2157
|
-
}
|
|
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
|
-
}
|
|
1956
|
+
path2 = `${this.config.path.replace(/\/$/, "")}/${this.config.version}`;
|
|
2170
1957
|
}
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
1958
|
+
} else {
|
|
1959
|
+
path2 = this.config.path;
|
|
1960
|
+
}
|
|
1961
|
+
} else {
|
|
1962
|
+
path2 = this.config?.version ? `${this.name}/${this.config.version}` : this.name;
|
|
1963
|
+
}
|
|
1964
|
+
const url = new URL(`${baseUrl.replace(/\/$/, "")}/api/${path2}`);
|
|
1965
|
+
const searchParams = url.searchParams;
|
|
1966
|
+
for (const [key, value] of Object.entries(queryParams)) {
|
|
1967
|
+
if (Array.isArray(value)) {
|
|
1968
|
+
for (const item of value) {
|
|
1969
|
+
if (item !== null && item !== void 0) {
|
|
1970
|
+
searchParams.append(key, String(item));
|
|
2175
1971
|
}
|
|
2176
|
-
});
|
|
2177
|
-
if (!response.ok) {
|
|
2178
|
-
throw new Error(`HTTP error! status: ${response.status}`);
|
|
2179
1972
|
}
|
|
2180
|
-
|
|
2181
|
-
|
|
1973
|
+
} else if (value !== null && value !== void 0) {
|
|
1974
|
+
searchParams.append(key, String(value));
|
|
2182
1975
|
}
|
|
2183
|
-
}
|
|
2184
|
-
|
|
1976
|
+
}
|
|
1977
|
+
const response = await fetch(url, {
|
|
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;
|
|
2185
1988
|
}
|
|
2186
|
-
}
|
|
1989
|
+
};
|
|
1990
|
+
var ConsumptionApi = Api;
|
|
2187
1991
|
|
|
2188
1992
|
// src/dmv2/sdk/ingestPipeline.ts
|
|
2189
|
-
var IngestPipeline
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
ingestApi
|
|
2217
|
-
|
|
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
|
-
}
|
|
1993
|
+
var IngestPipeline = class extends TypedBase {
|
|
1994
|
+
/**
|
|
1995
|
+
* The OLAP table component of the pipeline, if configured.
|
|
1996
|
+
* Provides analytical query capabilities for the ingested data.
|
|
1997
|
+
* Only present when `config.table` is not `false`.
|
|
1998
|
+
*/
|
|
1999
|
+
table;
|
|
2000
|
+
/**
|
|
2001
|
+
* The stream component of the pipeline, if configured.
|
|
2002
|
+
* Handles real-time data flow and processing between components.
|
|
2003
|
+
* Only present when `config.stream` is not `false`.
|
|
2004
|
+
*/
|
|
2005
|
+
stream;
|
|
2006
|
+
/**
|
|
2007
|
+
* The ingest API component of the pipeline, if configured.
|
|
2008
|
+
* Provides HTTP endpoints for data ingestion.
|
|
2009
|
+
* Only present when `config.ingestApi` is not `false`.
|
|
2010
|
+
*/
|
|
2011
|
+
ingestApi;
|
|
2012
|
+
/** The dead letter queue of the pipeline, if configured. */
|
|
2013
|
+
deadLetterQueue;
|
|
2014
|
+
constructor(name, config, schema, columns, validators, allowExtraFields) {
|
|
2015
|
+
super(name, config, schema, columns, validators, allowExtraFields);
|
|
2016
|
+
if (config.ingest !== void 0) {
|
|
2017
|
+
console.warn(
|
|
2018
|
+
"\u26A0\uFE0F DEPRECATION WARNING: The 'ingest' parameter is deprecated and will be removed in a future version. Please use 'ingestApi' instead."
|
|
2019
|
+
);
|
|
2020
|
+
if (config.ingestApi === void 0) {
|
|
2021
|
+
config.ingestApi = config.ingest;
|
|
2304
2022
|
}
|
|
2305
|
-
}
|
|
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
|
+
}
|
|
2306
2099
|
}
|
|
2307
|
-
}
|
|
2100
|
+
};
|
|
2308
2101
|
|
|
2309
2102
|
// src/dmv2/sdk/etlPipeline.ts
|
|
2310
|
-
var InternalBatcher
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2103
|
+
var InternalBatcher = class {
|
|
2104
|
+
iterator;
|
|
2105
|
+
batchSize;
|
|
2106
|
+
constructor(asyncIterable, batchSize = 20) {
|
|
2107
|
+
this.iterator = asyncIterable[Symbol.asyncIterator]();
|
|
2108
|
+
this.batchSize = batchSize;
|
|
2109
|
+
}
|
|
2110
|
+
async getNextBatch() {
|
|
2111
|
+
const items = [];
|
|
2112
|
+
for (let i = 0; i < this.batchSize; i++) {
|
|
2113
|
+
const { value, done } = await this.iterator.next();
|
|
2114
|
+
if (done) {
|
|
2115
|
+
return { items, hasMore: false };
|
|
2116
|
+
}
|
|
2117
|
+
items.push(value);
|
|
2118
|
+
}
|
|
2119
|
+
return { items, hasMore: true };
|
|
2120
|
+
}
|
|
2121
|
+
};
|
|
2122
|
+
var ETLPipeline = class {
|
|
2123
|
+
constructor(name, config) {
|
|
2124
|
+
this.name = name;
|
|
2125
|
+
this.config = config;
|
|
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"
|
|
2333
2148
|
};
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
}
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
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
|
-
}
|
|
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)
|
|
2156
|
+
};
|
|
2157
|
+
}
|
|
2158
|
+
createExtractTask(taskConfig) {
|
|
2159
|
+
return new Task(`${this.name}_extract`, {
|
|
2160
|
+
run: async ({}) => {
|
|
2161
|
+
console.log(`Running extract task for ${this.name}...`);
|
|
2162
|
+
const batch = await this.batcher.getNextBatch();
|
|
2163
|
+
console.log(`Extract task completed with ${batch.items.length} items`);
|
|
2164
|
+
return batch;
|
|
2165
|
+
},
|
|
2166
|
+
retries: taskConfig.retries,
|
|
2167
|
+
timeout: taskConfig.timeout
|
|
2168
|
+
});
|
|
2169
|
+
}
|
|
2170
|
+
createTransformTask(taskConfig) {
|
|
2171
|
+
return new Task(
|
|
2172
|
+
`${this.name}_transform`,
|
|
2173
|
+
{
|
|
2174
|
+
// Use new single-parameter context API for handlers
|
|
2175
|
+
run: async ({ input }) => {
|
|
2176
|
+
const batch = input;
|
|
2177
|
+
console.log(
|
|
2178
|
+
`Running transform task for ${this.name} with ${batch.items.length} items...`
|
|
2179
|
+
);
|
|
2434
2180
|
const transformedItems = [];
|
|
2435
|
-
for (const
|
|
2436
|
-
const
|
|
2437
|
-
transformedItems.push(
|
|
2438
|
-
}
|
|
2439
|
-
if ("insert" in this.config.load) {
|
|
2440
|
-
await this.config.load.insert(transformedItems);
|
|
2441
|
-
} else {
|
|
2442
|
-
await this.config.load(transformedItems);
|
|
2181
|
+
for (const item of batch.items) {
|
|
2182
|
+
const transformed = await this.config.transform(item);
|
|
2183
|
+
transformedItems.push(transformed);
|
|
2443
2184
|
}
|
|
2444
2185
|
console.log(
|
|
2445
|
-
`
|
|
2186
|
+
`Transform task completed with ${transformedItems.length} items`
|
|
2446
2187
|
);
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
} while (true);
|
|
2452
|
-
console.log(`Completed ETL Pipeline: ${this.name}`);
|
|
2188
|
+
return { items: transformedItems };
|
|
2189
|
+
},
|
|
2190
|
+
retries: taskConfig.retries,
|
|
2191
|
+
timeout: taskConfig.timeout
|
|
2453
2192
|
}
|
|
2454
|
-
|
|
2193
|
+
);
|
|
2455
2194
|
}
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
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)
|
|
2195
|
+
createLoadTask(taskConfig) {
|
|
2196
|
+
return new Task(`${this.name}_load`, {
|
|
2197
|
+
run: async ({ input: transformedItems }) => {
|
|
2198
|
+
console.log(
|
|
2199
|
+
`Running load task for ${this.name} with ${transformedItems.items.length} items...`
|
|
2503
2200
|
);
|
|
2504
|
-
this.
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
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;
|
|
2201
|
+
if ("insert" in this.config.load) {
|
|
2202
|
+
await this.config.load.insert(transformedItems.items);
|
|
2203
|
+
} else {
|
|
2204
|
+
await this.config.load(transformedItems.items);
|
|
2515
2205
|
}
|
|
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);
|
|
2516
2231
|
}
|
|
2517
|
-
|
|
2232
|
+
console.log(
|
|
2233
|
+
`Completed batch ${batchNumber} with ${batch.items.length} items`
|
|
2234
|
+
);
|
|
2235
|
+
batchNumber++;
|
|
2236
|
+
if (!batch.hasMore) {
|
|
2237
|
+
break;
|
|
2238
|
+
}
|
|
2239
|
+
} while (true);
|
|
2240
|
+
console.log(`Completed ETL Pipeline: ${this.name}`);
|
|
2518
2241
|
}
|
|
2519
|
-
}
|
|
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
|
+
}
|
|
2295
|
+
}
|
|
2296
|
+
};
|
|
2520
2297
|
|
|
2521
2298
|
// src/dmv2/sdk/materializedView.ts
|
|
2522
|
-
var requireTargetTableName
|
|
2523
|
-
|
|
2524
|
-
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
}
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
}
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
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;
|
|
2299
|
+
var requireTargetTableName = (tableName) => {
|
|
2300
|
+
if (typeof tableName === "string") {
|
|
2301
|
+
return tableName;
|
|
2302
|
+
} else {
|
|
2303
|
+
throw new Error("Name of targetTable is not specified.");
|
|
2304
|
+
}
|
|
2305
|
+
};
|
|
2306
|
+
var MaterializedView = class extends SqlResource {
|
|
2307
|
+
/** The target OlapTable instance where the materialized data is stored. */
|
|
2308
|
+
targetTable;
|
|
2309
|
+
constructor(options, targetSchema, targetColumns) {
|
|
2310
|
+
let selectStatement = options.selectStatement;
|
|
2311
|
+
if (typeof selectStatement !== "string") {
|
|
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]
|
|
2584
2350
|
}
|
|
2585
|
-
|
|
2351
|
+
);
|
|
2352
|
+
this.targetTable = targetTable;
|
|
2586
2353
|
}
|
|
2587
|
-
}
|
|
2354
|
+
};
|
|
2588
2355
|
|
|
2589
2356
|
// src/dmv2/sdk/view.ts
|
|
2590
|
-
var View
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
if (typeof selectStatement !== "string") {
|
|
2606
|
-
selectStatement = toStaticQuery(selectStatement);
|
|
2607
|
-
}
|
|
2608
|
-
super(
|
|
2609
|
-
name,
|
|
2610
|
-
[
|
|
2611
|
-
`CREATE VIEW IF NOT EXISTS ${name}
|
|
2357
|
+
var View = class extends SqlResource {
|
|
2358
|
+
/**
|
|
2359
|
+
* Creates a new View instance.
|
|
2360
|
+
* @param name The name of the view to be created.
|
|
2361
|
+
* @param selectStatement The SQL SELECT statement that defines the view's logic.
|
|
2362
|
+
* @param baseTables An array of OlapTable or View objects that the `selectStatement` reads from. Used for dependency tracking.
|
|
2363
|
+
*/
|
|
2364
|
+
constructor(name, selectStatement, baseTables) {
|
|
2365
|
+
if (typeof selectStatement !== "string") {
|
|
2366
|
+
selectStatement = toStaticQuery(selectStatement);
|
|
2367
|
+
}
|
|
2368
|
+
super(
|
|
2369
|
+
name,
|
|
2370
|
+
[
|
|
2371
|
+
`CREATE VIEW IF NOT EXISTS ${name}
|
|
2612
2372
|
AS ${selectStatement}`.trim()
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
}
|
|
2618
|
-
);
|
|
2373
|
+
],
|
|
2374
|
+
[dropView(name)],
|
|
2375
|
+
{
|
|
2376
|
+
pullsDataFrom: baseTables
|
|
2619
2377
|
}
|
|
2620
|
-
|
|
2378
|
+
);
|
|
2621
2379
|
}
|
|
2622
|
-
}
|
|
2380
|
+
};
|
|
2623
2381
|
|
|
2624
2382
|
// src/dmv2/sdk/lifeCycle.ts
|
|
2625
|
-
var LifeCycle
|
|
2626
|
-
|
|
2627
|
-
"
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
LifeCycle2["DELETION_PROTECTED"] = "DELETION_PROTECTED";
|
|
2632
|
-
LifeCycle2["EXTERNALLY_MANAGED"] = "EXTERNALLY_MANAGED";
|
|
2633
|
-
return LifeCycle2;
|
|
2634
|
-
})(LifeCycle || {});
|
|
2635
|
-
}
|
|
2636
|
-
});
|
|
2383
|
+
var LifeCycle = /* @__PURE__ */ ((LifeCycle2) => {
|
|
2384
|
+
LifeCycle2["FULLY_MANAGED"] = "FULLY_MANAGED";
|
|
2385
|
+
LifeCycle2["DELETION_PROTECTED"] = "DELETION_PROTECTED";
|
|
2386
|
+
LifeCycle2["EXTERNALLY_MANAGED"] = "EXTERNALLY_MANAGED";
|
|
2387
|
+
return LifeCycle2;
|
|
2388
|
+
})(LifeCycle || {});
|
|
2637
2389
|
|
|
2638
2390
|
// src/dmv2/sdk/webApp.ts
|
|
2639
|
-
var RESERVED_MOUNT_PATHS
|
|
2640
|
-
|
|
2641
|
-
"
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2391
|
+
var RESERVED_MOUNT_PATHS = [
|
|
2392
|
+
"/admin",
|
|
2393
|
+
"/api",
|
|
2394
|
+
"/consumption",
|
|
2395
|
+
"/health",
|
|
2396
|
+
"/ingest",
|
|
2397
|
+
"/moose",
|
|
2398
|
+
// reserved for future use
|
|
2399
|
+
"/ready",
|
|
2400
|
+
"/workflows"
|
|
2401
|
+
];
|
|
2402
|
+
var WebApp = class {
|
|
2403
|
+
name;
|
|
2404
|
+
handler;
|
|
2405
|
+
config;
|
|
2406
|
+
_rawApp;
|
|
2407
|
+
constructor(name, appOrHandler, config) {
|
|
2408
|
+
this.name = name;
|
|
2409
|
+
this.config = config;
|
|
2410
|
+
if (!this.config.mountPath) {
|
|
2411
|
+
throw new Error(
|
|
2412
|
+
`mountPath is required. Please specify a mount path for your WebApp (e.g., "/myapi").`
|
|
2413
|
+
);
|
|
2414
|
+
}
|
|
2415
|
+
const mountPath = this.config.mountPath;
|
|
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) {
|
|
2675
2442
|
throw new Error(
|
|
2676
|
-
`
|
|
2443
|
+
`WebApp with mountPath "${this.config.mountPath}" already exists (used by WebApp "${existingName}")`
|
|
2677
2444
|
);
|
|
2678
2445
|
}
|
|
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
|
-
);
|
|
2698
|
-
}
|
|
2699
|
-
}
|
|
2700
|
-
}
|
|
2701
|
-
webApps.set(name, this);
|
|
2702
2446
|
}
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
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();
|
|
2447
|
+
}
|
|
2448
|
+
webApps.set(name, this);
|
|
2449
|
+
}
|
|
2450
|
+
toHandler(appOrHandler) {
|
|
2451
|
+
if (typeof appOrHandler === "function") {
|
|
2452
|
+
return appOrHandler;
|
|
2453
|
+
}
|
|
2454
|
+
const app = appOrHandler;
|
|
2455
|
+
if (typeof app.handle === "function") {
|
|
2456
|
+
return (req, res) => {
|
|
2457
|
+
app.handle(req, res, (err) => {
|
|
2458
|
+
if (err) {
|
|
2459
|
+
console.error("WebApp handler error:", err);
|
|
2460
|
+
if (!res.headersSent) {
|
|
2461
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
2462
|
+
res.end(JSON.stringify({ error: "Internal Server Error" }));
|
|
2731
2463
|
}
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2464
|
+
}
|
|
2465
|
+
});
|
|
2466
|
+
};
|
|
2467
|
+
}
|
|
2468
|
+
if (typeof app.callback === "function") {
|
|
2469
|
+
return app.callback();
|
|
2470
|
+
}
|
|
2471
|
+
if (typeof app.routing === "function") {
|
|
2472
|
+
const routing = app.routing;
|
|
2473
|
+
const appWithReady = app;
|
|
2474
|
+
let readyPromise = null;
|
|
2475
|
+
return async (req, res) => {
|
|
2476
|
+
if (readyPromise === null) {
|
|
2477
|
+
readyPromise = typeof appWithReady.ready === "function" ? appWithReady.ready() : Promise.resolve();
|
|
2478
|
+
}
|
|
2479
|
+
await readyPromise;
|
|
2480
|
+
routing(req, res);
|
|
2481
|
+
};
|
|
2482
|
+
}
|
|
2483
|
+
throw new Error(
|
|
2484
|
+
`Unable to convert app to handler. The provided object must be:
|
|
2738
2485
|
- A function (raw Node.js handler)
|
|
2739
2486
|
- An object with .handle() method (Express, Connect)
|
|
2740
2487
|
- An object with .callback() method (Koa)
|
|
@@ -2746,14 +2493,12 @@ Examples:
|
|
|
2746
2493
|
Fastify: new WebApp("name", fastifyApp)
|
|
2747
2494
|
Raw: new WebApp("name", (req, res) => { ... })
|
|
2748
2495
|
`
|
|
2749
|
-
|
|
2750
|
-
}
|
|
2751
|
-
getRawApp() {
|
|
2752
|
-
return this._rawApp;
|
|
2753
|
-
}
|
|
2754
|
-
};
|
|
2496
|
+
);
|
|
2755
2497
|
}
|
|
2756
|
-
|
|
2498
|
+
getRawApp() {
|
|
2499
|
+
return this._rawApp;
|
|
2500
|
+
}
|
|
2501
|
+
};
|
|
2757
2502
|
|
|
2758
2503
|
// src/dmv2/registry.ts
|
|
2759
2504
|
function getTables() {
|
|
@@ -2819,32 +2564,6 @@ function getWebApps() {
|
|
|
2819
2564
|
function getWebApp(name) {
|
|
2820
2565
|
return getMooseInternal().webApps.get(name);
|
|
2821
2566
|
}
|
|
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();
|
|
2848
2567
|
export {
|
|
2849
2568
|
Api,
|
|
2850
2569
|
ConsumptionApi,
|