@514labs/moose-lib 0.6.459 → 0.6.460

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.
@@ -1,2627 +0,0 @@
1
- import { IJsonSchemaCollection, tags } from 'typia';
2
- import { Pattern, TagBase } from 'typia/lib/tags';
3
- import { Readable } from 'node:stream';
4
- import { ClickHouseClient, ResultSet, CommandResult } from '@clickhouse/client';
5
- import { Client } from '@temporalio/client';
6
- import { JWTPayload } from 'jose';
7
- import http from 'http';
8
-
9
- /**
10
- * Quote a ClickHouse identifier with backticks if not already quoted.
11
- * Backticks allow special characters (e.g., hyphens) in identifiers.
12
- */
13
- declare const quoteIdentifier: (name: string) => string;
14
- type IdentifierBrandedString = string & {
15
- readonly __identifier_brand?: unique symbol;
16
- };
17
- type NonIdentifierBrandedString = string & {
18
- readonly __identifier_brand?: unique symbol;
19
- };
20
- /**
21
- * Values supported by SQL engine.
22
- */
23
- type Value = NonIdentifierBrandedString | number | boolean | Date | [string, string];
24
- /**
25
- * Supported value or SQL instance.
26
- */
27
- type RawValue = Value | Sql;
28
- /**
29
- * Sql template tag interface with attached helper methods.
30
- */
31
- interface SqlTemplateTag {
32
- /**
33
- * @deprecated Use `sql.statement` for full SQL statements or `sql.fragment` for SQL fragments.
34
- */
35
- (strings: readonly string[], ...values: readonly (RawValue | Column | OlapTable<any> | View)[]): Sql;
36
- /**
37
- * Template literal tag for complete SQL statements (e.g. SELECT, INSERT, CREATE).
38
- * Produces a Sql instance with `isFragment = false`.
39
- */
40
- statement(strings: readonly string[], ...values: readonly (RawValue | Column | OlapTable<any> | View)[]): Sql;
41
- /**
42
- * Template literal tag for SQL fragments (e.g. expressions, conditions, partial clauses).
43
- * Produces a Sql instance with `isFragment = true`.
44
- */
45
- fragment(strings: readonly string[], ...values: readonly (RawValue | Column | OlapTable<any> | View)[]): Sql;
46
- /**
47
- * Join an array of Sql fragments with a separator.
48
- * @param fragments - Array of Sql fragments to join
49
- * @param separator - Optional separator string (defaults to ", ")
50
- */
51
- join(fragments: Sql[], separator?: string): Sql;
52
- /**
53
- * Create raw SQL from a string without parameterization.
54
- * WARNING: SQL injection risk if used with untrusted input.
55
- */
56
- raw(text: string): Sql;
57
- }
58
- declare const sql: SqlTemplateTag;
59
- /**
60
- * A SQL instance can be nested within each other to build SQL strings.
61
- */
62
- declare class Sql {
63
- readonly values: Value[];
64
- readonly strings: string[];
65
- readonly isFragment: boolean | undefined;
66
- constructor(rawStrings: readonly string[], rawValues: readonly (RawValue | Column | OlapTable<any> | View | Sql)[], isFragment?: boolean);
67
- /**
68
- * Append another Sql fragment, returning a new Sql instance.
69
- */
70
- append(other: Sql): Sql;
71
- }
72
- declare const toStaticQuery: (sql: Sql) => string;
73
- declare const toQuery: (sql: Sql) => [string, {
74
- [pN: string]: any;
75
- }];
76
- /**
77
- * Build a display-only SQL string with values inlined for logging/debugging.
78
- * Does not alter execution behavior; use toQuery for actual execution.
79
- */
80
- declare const toQueryPreview: (sql: Sql) => string;
81
- declare const getValueFromParameter: (value: any) => any;
82
- declare function createClickhouseParameter(parameterIndex: number, value: Value): string;
83
- /**
84
- * Convert the JS type (source is JSON format by API query parameter) to the corresponding ClickHouse type for generating named placeholder of parameterized query.
85
- * Only support to convert number to Int or Float, boolean to Bool, string to String, other types will convert to String.
86
- * If exist complex type e.g: object, Array, null, undefined, Date, Record.. etc, just convert to string type by ClickHouse function in SQL.
87
- * ClickHouse support converting string to other types function.
88
- * Please see Each section of the https://clickhouse.com/docs/en/sql-reference/functions and https://clickhouse.com/docs/en/sql-reference/functions/type-conversion-functions
89
- * @param value
90
- * @returns 'Float', 'Int', 'Bool', 'String'
91
- */
92
- declare const mapToClickHouseType: (value: Value) => string;
93
-
94
- type EnumValues = {
95
- name: string;
96
- value: {
97
- Int: number;
98
- };
99
- }[] | {
100
- name: string;
101
- value: {
102
- String: string;
103
- };
104
- }[];
105
- type DataEnum = {
106
- name: string;
107
- values: EnumValues;
108
- };
109
- type Nested = {
110
- name: string;
111
- columns: Column[];
112
- jwt: boolean;
113
- };
114
- type ArrayType = {
115
- elementType: DataType;
116
- elementNullable: boolean;
117
- };
118
- type NamedTupleType = {
119
- fields: Array<[string, DataType]>;
120
- };
121
- type MapType = {
122
- keyType: DataType;
123
- valueType: DataType;
124
- };
125
- type JsonOptions = {
126
- max_dynamic_paths?: number;
127
- max_dynamic_types?: number;
128
- typed_paths?: Array<[string, DataType]>;
129
- skip_paths?: string[];
130
- skip_regexps?: string[];
131
- };
132
- type DataType = string | DataEnum | ArrayType | Nested | NamedTupleType | MapType | JsonOptions | {
133
- nullable: DataType;
134
- };
135
- interface Column {
136
- name: IdentifierBrandedString;
137
- data_type: DataType;
138
- required: boolean;
139
- unique: false;
140
- primary_key: boolean;
141
- default: string | null;
142
- materialized: string | null;
143
- alias: string | null;
144
- ttl: string | null;
145
- codec: string | null;
146
- annotations: [string, any][];
147
- comment: string | null;
148
- }
149
-
150
- /**
151
- * Type definition for typia validation functions
152
- */
153
- interface TypiaValidators<T> {
154
- /** Typia validator function: returns { success: boolean, data?: T, errors?: any[] } */
155
- validate?: (data: unknown) => {
156
- success: boolean;
157
- data?: T;
158
- errors?: any[];
159
- };
160
- /** Typia assert function: throws on validation failure, returns T on success */
161
- assert?: (data: unknown) => T;
162
- /** Typia is function: returns boolean indicating if data matches type T */
163
- is?: (data: unknown) => data is T;
164
- }
165
- /**
166
- * Base class for all typed Moose dmv2 resources (OlapTable, Stream, etc.).
167
- * Handles the storage and injection of schema information (JSON schema and Column array)
168
- * provided by the Moose compiler plugin.
169
- *
170
- * @template T The data type (interface or type alias) defining the schema of the resource.
171
- * @template C The specific configuration type for the resource (e.g., OlapConfig, StreamConfig).
172
- */
173
- declare class TypedBase<T, C> {
174
- /** The JSON schema representation of type T. Injected by the compiler plugin. */
175
- schema: IJsonSchemaCollection.IV3_1;
176
- /** The name assigned to this resource instance. */
177
- name: string;
178
- /** A dictionary mapping column names (keys of T) to their Column definitions. */
179
- columns: {
180
- [columnName in keyof Required<T>]: Column;
181
- };
182
- /** An array containing the Column definitions for this resource. Injected by the compiler plugin. */
183
- columnArray: Column[];
184
- /** The configuration object specific to this resource type. */
185
- config: C;
186
- /** Typia validation functions for type T. Injected by the compiler plugin for OlapTable. */
187
- validators?: TypiaValidators<T>;
188
- /** Optional metadata for the resource, always present as an object. */
189
- metadata: {
190
- [key: string]: any;
191
- };
192
- /**
193
- * Whether this resource allows extra fields beyond the defined columns.
194
- * When true, extra fields in payloads are passed through to streaming functions.
195
- * Injected by the compiler plugin when the type has an index signature.
196
- */
197
- allowExtraFields: boolean;
198
- /**
199
- * @internal Constructor intended for internal use by subclasses and the compiler plugin.
200
- * It expects the schema and columns to be provided, typically injected by the compiler.
201
- *
202
- * @param name The name for the resource instance.
203
- * @param config The configuration object for the resource.
204
- * @param schema The JSON schema for the resource's data type T (injected).
205
- * @param columns The array of Column definitions for T (injected).
206
- * @param allowExtraFields Whether extra fields are allowed (injected when type has index signature).
207
- */
208
- constructor(name: string, config: C, schema?: IJsonSchemaCollection.IV3_1, columns?: Column[], validators?: TypiaValidators<T>, allowExtraFields?: boolean);
209
- }
210
-
211
- type ClickHousePrecision<P extends number> = {
212
- _clickhouse_precision?: P;
213
- };
214
- declare const DecimalRegex: "^-?\\d+(\\.\\d+)?$";
215
- type ClickHouseDecimal<P extends number, S extends number> = {
216
- _clickhouse_precision?: P;
217
- _clickhouse_scale?: S;
218
- } & Pattern<typeof DecimalRegex>;
219
- type ClickHouseFixedStringSize<N extends number> = {
220
- _clickhouse_fixed_string_size?: N;
221
- };
222
- /**
223
- * FixedString(N) - Fixed-length string of exactly N bytes.
224
- *
225
- * ClickHouse stores exactly N bytes, padding shorter values with null bytes.
226
- * Values exceeding N bytes will throw an exception.
227
- *
228
- * Use for binary data: hashes, IP addresses, UUIDs, MAC addresses.
229
- *
230
- * @example
231
- * interface BinaryData {
232
- * md5_hash: string & FixedString<16>; // 16-byte MD5
233
- * sha256_hash: string & FixedString<32>; // 32-byte SHA256
234
- * }
235
- */
236
- type FixedString<N extends number> = string & ClickHouseFixedStringSize<N>;
237
- type ClickHouseByteSize<N extends number> = {
238
- _clickhouse_byte_size?: N;
239
- };
240
- type LowCardinality = {
241
- _LowCardinality?: true;
242
- };
243
- type DateTime = Date;
244
- type DateTime64<P extends number> = Date & ClickHousePrecision<P>;
245
- type DateTimeString = string & tags.Format<"date-time">;
246
- /**
247
- * JS Date objects cannot hold microsecond precision.
248
- * Use string as the runtime type to avoid losing information.
249
- */
250
- type DateTime64String<P extends number> = string & tags.Format<"date-time"> & ClickHousePrecision<P>;
251
- type Float32 = number & ClickHouseFloat<"float32">;
252
- type Float64 = number & ClickHouseFloat<"float64">;
253
- type Int8 = number & ClickHouseInt<"int8">;
254
- type Int16 = number & ClickHouseInt<"int16">;
255
- type Int32 = number & ClickHouseInt<"int32">;
256
- type Int64 = number & ClickHouseInt<"int64">;
257
- type UInt8 = number & ClickHouseInt<"uint8">;
258
- type UInt16 = number & ClickHouseInt<"uint16">;
259
- type UInt32 = number & ClickHouseInt<"uint32">;
260
- type UInt64 = number & ClickHouseInt<"uint64">;
261
- type Decimal<P extends number, S extends number> = string & ClickHouseDecimal<P, S>;
262
- /**
263
- * Attach compression codec to a column type.
264
- *
265
- * Any valid ClickHouse codec expression is allowed. ClickHouse validates the codec at runtime.
266
- *
267
- * @template T The base data type
268
- * @template CodecExpr The codec expression (single codec or chain)
269
- *
270
- * @example
271
- * interface Metrics {
272
- * // Single codec
273
- * log_blob: string & ClickHouseCodec<"ZSTD(3)">;
274
- *
275
- * // Codec chain (processed left-to-right)
276
- * timestamp: Date & ClickHouseCodec<"Delta, LZ4">;
277
- * temperature: number & ClickHouseCodec<"Gorilla, ZSTD">;
278
- *
279
- * // Specialized codecs
280
- * counter: number & ClickHouseCodec<"DoubleDelta">;
281
- *
282
- * // Can combine with other annotations
283
- * count: UInt64 & ClickHouseCodec<"DoubleDelta, LZ4">;
284
- * }
285
- */
286
- type ClickHouseCodec<CodecExpr extends string> = {
287
- _clickhouse_codec?: CodecExpr;
288
- };
289
- type ClickHouseFloat<Value extends "float32" | "float64"> = tags.Type<Value extends "float32" ? "float" : "double">;
290
- type ClickHouseInt<Value extends "int8" | "int16" | "int32" | "int64" | "uint8" | "uint16" | "uint32" | "uint64"> = Value extends "int32" | "int64" | "uint32" | "uint64" ? tags.Type<Value> : TagBase<{
291
- target: "number";
292
- kind: "type";
293
- value: Value;
294
- validate: Value extends "int8" ? "-128 <= $input && $input <= 127" : Value extends "int16" ? "-32768 <= $input && $input <= 32767" : Value extends "uint8" ? "0 <= $input && $input <= 255" : Value extends "uint16" ? "0 <= $input && $input <= 65535" : never;
295
- exclusive: true;
296
- schema: {
297
- type: "integer";
298
- };
299
- }>;
300
- /**
301
- * By default, nested objects map to the `Nested` type in clickhouse.
302
- * Write `nestedObject: AnotherInterfaceType & ClickHouseNamedTuple`
303
- * to map AnotherInterfaceType to the named tuple type.
304
- */
305
- type ClickHouseNamedTuple = {
306
- _clickhouse_mapped_type?: "namedTuple";
307
- };
308
- type ClickHouseJson<maxDynamicPaths extends number | undefined = undefined, maxDynamicTypes extends number | undefined = undefined, skipPaths extends string[] = [], skipRegexes extends string[] = []> = {
309
- _clickhouse_mapped_type?: "JSON";
310
- _clickhouse_json_settings?: {
311
- maxDynamicPaths?: maxDynamicPaths;
312
- maxDynamicTypes?: maxDynamicTypes;
313
- skipPaths?: skipPaths;
314
- skipRegexes?: skipRegexes;
315
- };
316
- };
317
- type ClickHousePoint = [number, number] & {
318
- _clickhouse_mapped_type?: "Point";
319
- };
320
- type ClickHouseRing = ClickHousePoint[] & {
321
- _clickhouse_mapped_type?: "Ring";
322
- };
323
- type ClickHouseLineString = ClickHousePoint[] & {
324
- _clickhouse_mapped_type?: "LineString";
325
- };
326
- type ClickHouseMultiLineString = ClickHouseLineString[] & {
327
- _clickhouse_mapped_type?: "MultiLineString";
328
- };
329
- type ClickHousePolygon = ClickHouseRing[] & {
330
- _clickhouse_mapped_type?: "Polygon";
331
- };
332
- type ClickHouseMultiPolygon = ClickHousePolygon[] & {
333
- _clickhouse_mapped_type?: "MultiPolygon";
334
- };
335
- /**
336
- * typia may have trouble handling this type.
337
- * In which case, use {@link WithDefault} as a workaround
338
- *
339
- * @example
340
- * { field: number & ClickHouseDefault<"0"> }
341
- */
342
- type ClickHouseDefault<SqlExpression extends string> = {
343
- _clickhouse_default?: SqlExpression;
344
- };
345
- /**
346
- * @example
347
- * {
348
- * ...
349
- * timestamp: Date;
350
- * debugMessage: string & ClickHouseTTL<"timestamp + INTERVAL 1 WEEK">;
351
- * }
352
- */
353
- type ClickHouseTTL<SqlExpression extends string> = {
354
- _clickhouse_ttl?: SqlExpression;
355
- };
356
- /**
357
- * ClickHouse MATERIALIZED column annotation.
358
- * The column value is computed at INSERT time and physically stored.
359
- * Cannot be explicitly inserted by users.
360
- *
361
- * @example
362
- * interface Events {
363
- * eventTime: DateTime;
364
- * // Extract date component - computed and stored at insert time
365
- * eventDate: Date & ClickHouseMaterialized<"toDate(event_time)">;
366
- *
367
- * userId: string;
368
- * // Precompute hash for fast lookups
369
- * userHash: UInt64 & ClickHouseMaterialized<"cityHash64(userId)">;
370
- * }
371
- *
372
- * @remarks
373
- * - MATERIALIZED and DEFAULT are mutually exclusive
374
- * - Can be combined with ClickHouseCodec for compression
375
- * - Changing the expression modifies the column in-place (existing values preserved)
376
- */
377
- type ClickHouseMaterialized<SqlExpression extends string> = {
378
- _clickhouse_materialized?: SqlExpression;
379
- };
380
- /**
381
- * ClickHouse ALIAS column annotation.
382
- * The column value is computed on-the-fly at SELECT time and NOT physically stored.
383
- * Cannot be explicitly inserted by users.
384
- *
385
- * @example
386
- * interface Events {
387
- * eventTime: DateTime;
388
- * // Computed at query time, not stored on disk
389
- * eventDate: Date & ClickHouseAlias<"toDate(event_time)">;
390
- *
391
- * firstName: string;
392
- * lastName: string;
393
- * // Virtual computed column
394
- * fullName: string & ClickHouseAlias<"concat(first_name, ' ', last_name)">;
395
- * }
396
- *
397
- * @remarks
398
- * - ALIAS, MATERIALIZED, and DEFAULT are mutually exclusive
399
- * - ALIAS columns are NOT stored on disk (saves storage, costs CPU at query time)
400
- * - Cannot be used in ORDER BY, PRIMARY KEY, or PARTITION BY
401
- * - Can be combined with ClickHouseCodec (though rarely useful since not stored)
402
- */
403
- type ClickHouseAlias<SqlExpression extends string> = {
404
- _clickhouse_alias?: SqlExpression;
405
- };
406
- /**
407
- * See also {@link ClickHouseDefault}
408
- *
409
- * @example{ updated_at: WithDefault<Date, "now()"> }
410
- */
411
- type WithDefault<T, _SqlExpression extends string> = T;
412
- type IsComputed<T> = "_clickhouse_alias" extends keyof T ? true : "_clickhouse_materialized" extends keyof T ? true : false;
413
- type HasDefault<T> = "_clickhouse_default" extends keyof T ? true : false;
414
- /** Keys whose columns are ALIAS or MATERIALIZED — excluded from inserts entirely. */
415
- type ComputedKeys<T> = {
416
- [K in keyof T]: IsComputed<T[K]> extends true ? K : never;
417
- }[keyof T];
418
- /** Keys whose columns carry a ClickHouseDefault expression — optional during inserts. */
419
- type DefaultKeys<T> = {
420
- [K in keyof T]: HasDefault<T[K]> extends true ? K : never;
421
- }[keyof T];
422
- /**
423
- * Derive the insert-safe shape of a model:
424
- * - ALIAS / MATERIALIZED columns are **omitted** (ClickHouse computes them).
425
- * - DEFAULT columns become **optional** (ClickHouse fills them when absent).
426
- * - All other columns remain **required**.
427
- *
428
- * @example
429
- * interface Events {
430
- * id: Key<string>;
431
- * timestamp: Date;
432
- * eventDate: Date & ClickHouseAlias<"toDate(timestamp)">;
433
- * createdAt: Date & ClickHouseDefault<"now()">;
434
- * }
435
- *
436
- * // Insertable<Events> ≡ { id: string; timestamp: Date; createdAt?: Date }
437
- * const table = new OlapTable<Events>("events");
438
- * await table.insert([{ id: "1", timestamp: new Date() }]);
439
- */
440
- type Insertable<T> = {
441
- [K in Exclude<keyof T, ComputedKeys<T> | DefaultKeys<T>>]: T[K];
442
- } & {
443
- [K in Exclude<DefaultKeys<T>, ComputedKeys<T>>]?: T[K];
444
- };
445
- /**
446
- * ClickHouse table engine types supported by Moose.
447
- */
448
- declare enum ClickHouseEngines {
449
- MergeTree = "MergeTree",
450
- ReplacingMergeTree = "ReplacingMergeTree",
451
- SummingMergeTree = "SummingMergeTree",
452
- AggregatingMergeTree = "AggregatingMergeTree",
453
- CollapsingMergeTree = "CollapsingMergeTree",
454
- VersionedCollapsingMergeTree = "VersionedCollapsingMergeTree",
455
- GraphiteMergeTree = "GraphiteMergeTree",
456
- S3Queue = "S3Queue",
457
- S3 = "S3",
458
- Buffer = "Buffer",
459
- Distributed = "Distributed",
460
- IcebergS3 = "IcebergS3",
461
- Kafka = "Kafka",
462
- Merge = "Merge",
463
- ReplicatedMergeTree = "ReplicatedMergeTree",
464
- ReplicatedReplacingMergeTree = "ReplicatedReplacingMergeTree",
465
- ReplicatedAggregatingMergeTree = "ReplicatedAggregatingMergeTree",
466
- ReplicatedSummingMergeTree = "ReplicatedSummingMergeTree",
467
- ReplicatedCollapsingMergeTree = "ReplicatedCollapsingMergeTree",
468
- ReplicatedVersionedCollapsingMergeTree = "ReplicatedVersionedCollapsingMergeTree"
469
- }
470
-
471
- /**
472
- * Defines how Moose manages the lifecycle of database resources when your code changes.
473
- *
474
- * This enum controls the behavior when there are differences between your code definitions
475
- * and the actual database schema or structure.
476
- */
477
- declare enum LifeCycle {
478
- /**
479
- * Full automatic management (default behavior).
480
- * Moose will automatically modify database resources to match your code definitions,
481
- * including potentially destructive operations like dropping columns or tables.
482
- */
483
- FULLY_MANAGED = "FULLY_MANAGED",
484
- /**
485
- * Deletion-protected automatic management.
486
- * Moose will modify resources to match your code but will avoid destructive actions
487
- * such as dropping columns, or tables. Only additive changes are applied.
488
- */
489
- DELETION_PROTECTED = "DELETION_PROTECTED",
490
- /**
491
- * External management - no automatic changes.
492
- * Moose will not modify the database resources. You are responsible for managing
493
- * the schema and ensuring it matches your code definitions manually.
494
- */
495
- EXTERNALLY_MANAGED = "EXTERNALLY_MANAGED"
496
- }
497
-
498
- interface TableIndex {
499
- name: string;
500
- expression: string;
501
- type: string;
502
- arguments?: string[];
503
- granularity?: number;
504
- }
505
- interface TableProjection {
506
- name: string;
507
- body: string;
508
- }
509
- /**
510
- * Represents a failed record during insertion with error details
511
- */
512
- interface FailedRecord<T> {
513
- /** The original record that failed to insert */
514
- record: T;
515
- /** The error message describing why the insertion failed */
516
- error: string;
517
- /** Optional: The index of this record in the original batch */
518
- index?: number;
519
- }
520
- /**
521
- * Result of an insert operation with detailed success/failure information
522
- */
523
- interface InsertResult<T> {
524
- /** Number of records successfully inserted */
525
- successful: number;
526
- /** Number of records that failed to insert */
527
- failed: number;
528
- /** Total number of records processed */
529
- total: number;
530
- /** Detailed information about failed records (if record isolation was used) */
531
- failedRecords?: FailedRecord<T>[];
532
- }
533
- /**
534
- * Error handling strategy for insert operations
535
- */
536
- type ErrorStrategy = "fail-fast" | "discard" | "isolate";
537
- /**
538
- * Options for insert operations
539
- */
540
- interface InsertOptions {
541
- /** Maximum number of bad records to tolerate before failing */
542
- allowErrors?: number;
543
- /** Maximum ratio of bad records to tolerate (0.0 to 1.0) before failing */
544
- allowErrorsRatio?: number;
545
- /** Error handling strategy */
546
- strategy?: ErrorStrategy;
547
- /** Whether to enable dead letter queue for failed records (future feature) */
548
- deadLetterQueue?: boolean;
549
- /** Whether to validate data against schema before insertion (default: true) */
550
- validate?: boolean;
551
- /** Whether to skip validation for individual records during 'isolate' strategy retries (default: false) */
552
- skipValidationOnRetry?: boolean;
553
- }
554
- /**
555
- * Validation result for a record with detailed error information
556
- */
557
- interface ValidationError {
558
- /** The original record that failed validation */
559
- record: any;
560
- /** Detailed validation error message */
561
- error: string;
562
- /** Optional: The index of this record in the original batch */
563
- index?: number;
564
- /** The path to the field that failed validation */
565
- path?: string;
566
- }
567
- /**
568
- * Result of data validation with success/failure breakdown
569
- */
570
- interface ValidationResult<T> {
571
- /** Records that passed validation */
572
- valid: T[];
573
- /** Records that failed validation with detailed error information */
574
- invalid: ValidationError[];
575
- /** Total number of records processed */
576
- total: number;
577
- }
578
- /**
579
- * S3Queue-specific table settings that can be modified with ALTER TABLE MODIFY SETTING
580
- * Note: Since ClickHouse 24.7, settings no longer require the 's3queue_' prefix
581
- */
582
- interface S3QueueTableSettings {
583
- /** Processing mode: "ordered" for sequential or "unordered" for parallel processing */
584
- mode?: "ordered" | "unordered";
585
- /** What to do with files after processing: 'keep' or 'delete' */
586
- after_processing?: "keep" | "delete";
587
- /** ZooKeeper/Keeper path for coordination between replicas */
588
- keeper_path?: string;
589
- /** Number of retry attempts for failed files */
590
- loading_retries?: string;
591
- /** Number of threads for parallel processing */
592
- processing_threads_num?: string;
593
- /** Enable parallel inserts */
594
- parallel_inserts?: string;
595
- /** Enable logging to system.s3queue_log table */
596
- enable_logging_to_queue_log?: string;
597
- /** Last processed file path (for ordered mode) */
598
- last_processed_path?: string;
599
- /** Maximum number of tracked files in ZooKeeper */
600
- tracked_files_limit?: string;
601
- /** TTL for tracked files in seconds */
602
- tracked_file_ttl_sec?: string;
603
- /** Minimum polling timeout in milliseconds */
604
- polling_min_timeout_ms?: string;
605
- /** Maximum polling timeout in milliseconds */
606
- polling_max_timeout_ms?: string;
607
- /** Polling backoff in milliseconds */
608
- polling_backoff_ms?: string;
609
- /** Minimum cleanup interval in milliseconds */
610
- cleanup_interval_min_ms?: string;
611
- /** Maximum cleanup interval in milliseconds */
612
- cleanup_interval_max_ms?: string;
613
- /** Number of buckets for sharding (0 = disabled) */
614
- buckets?: string;
615
- /** Batch size for listing objects */
616
- list_objects_batch_size?: string;
617
- /** Enable hash ring filtering for distributed processing */
618
- enable_hash_ring_filtering?: string;
619
- /** Maximum files to process before committing */
620
- max_processed_files_before_commit?: string;
621
- /** Maximum rows to process before committing */
622
- max_processed_rows_before_commit?: string;
623
- /** Maximum bytes to process before committing */
624
- max_processed_bytes_before_commit?: string;
625
- /** Maximum processing time in seconds before committing */
626
- max_processing_time_sec_before_commit?: string;
627
- /** Use persistent processing nodes (available from 25.8) */
628
- use_persistent_processing_nodes?: string;
629
- /** TTL for persistent processing nodes in seconds */
630
- persistent_processing_nodes_ttl_seconds?: string;
631
- /** Additional settings */
632
- [key: string]: string | undefined;
633
- }
634
- /**
635
- * Base configuration shared by all table engines
636
- * @template T The data type of the records stored in the table.
637
- */
638
- type BaseOlapConfig<T> = ({
639
- /**
640
- * Specifies the fields to use for ordering data within the ClickHouse table.
641
- * This is crucial for optimizing query performance.
642
- */
643
- orderByFields: (keyof T & string)[];
644
- orderByExpression?: undefined;
645
- } | {
646
- orderByFields?: undefined;
647
- /**
648
- * An arbitrary ClickHouse SQL expression for the order by clause.
649
- *
650
- * `orderByExpression: "(id, name)"` is equivalent to `orderByFields: ["id", "name"]`
651
- * `orderByExpression: "tuple()"` means no sorting
652
- */
653
- orderByExpression: string;
654
- } | {
655
- orderByFields?: undefined;
656
- orderByExpression?: undefined;
657
- }) & {
658
- partitionBy?: string;
659
- /**
660
- * SAMPLE BY expression for approximate query processing.
661
- *
662
- * Examples:
663
- * ```typescript
664
- * // Single unsigned integer field
665
- * sampleByExpression: "userId"
666
- *
667
- * // Hash function on any field type
668
- * sampleByExpression: "cityHash64(id)"
669
- *
670
- * // Multiple fields with hash
671
- * sampleByExpression: "cityHash64(userId, timestamp)"
672
- * ```
673
- *
674
- * Requirements:
675
- * - Expression must evaluate to an unsigned integer (UInt8/16/32/64)
676
- * - Expression must be present in the ORDER BY clause
677
- * - If using hash functions, the same expression must appear in orderByExpression
678
- */
679
- sampleByExpression?: string;
680
- /**
681
- * Optional PRIMARY KEY expression.
682
- * When specified, this overrides the primary key inferred from Key<T> column annotations.
683
- *
684
- * This allows for:
685
- * - Complex primary keys using functions (e.g., "cityHash64(id)")
686
- * - Different column ordering in primary key vs schema definition
687
- * - Primary keys that differ from ORDER BY
688
- *
689
- * Example: primaryKeyExpression: "(userId, cityHash64(eventId))"
690
- *
691
- * Note: When this is set, any Key<T> annotations on columns are ignored for PRIMARY KEY generation.
692
- */
693
- primaryKeyExpression?: string;
694
- version?: string;
695
- lifeCycle?: LifeCycle;
696
- settings?: {
697
- [key: string]: string;
698
- };
699
- /**
700
- * Optional TTL configuration for the table.
701
- * e.g., "TTL timestamp + INTERVAL 90 DAY DELETE"
702
- *
703
- * Use the {@link ClickHouseTTL} type to configure column level TTL
704
- */
705
- ttl?: string;
706
- /** Optional secondary/data-skipping indexes */
707
- indexes?: TableIndex[];
708
- /** Optional projections for alternative data ordering within parts */
709
- projections?: TableProjection[];
710
- /**
711
- * Optional database name for multi-database support.
712
- * When not specified, uses the global ClickHouse config database.
713
- */
714
- database?: string;
715
- /**
716
- * Optional cluster name for ON CLUSTER support.
717
- * Use this to enable replicated tables across ClickHouse clusters.
718
- * The cluster must be defined in config.toml (dev environment only).
719
- * Example: cluster: "prod_cluster"
720
- */
721
- cluster?: string;
722
- /**
723
- * Optional seed filter applied when `moose seed clickhouse` populates a
724
- * local/testing database from a remote source.
725
- *
726
- * Example:
727
- * ```typescript
728
- * seedFilter: { limit: 100, where: "user_id = 10" }
729
- * ```
730
- */
731
- seedFilter?: {
732
- /** Maximum number of rows to seed for this table. */
733
- limit?: number;
734
- /** ClickHouse SQL WHERE expression to filter seeded rows. */
735
- where?: string;
736
- };
737
- };
738
- /**
739
- * Configuration for MergeTree engine
740
- * @template T The data type of the records stored in the table.
741
- */
742
- type MergeTreeConfig<T> = BaseOlapConfig<T> & {
743
- engine: ClickHouseEngines.MergeTree;
744
- };
745
- /**
746
- * Configuration for ReplacingMergeTree engine (deduplication)
747
- * @template T The data type of the records stored in the table.
748
- */
749
- type ReplacingMergeTreeConfig<T> = BaseOlapConfig<T> & {
750
- engine: ClickHouseEngines.ReplacingMergeTree;
751
- ver?: keyof T & string;
752
- isDeleted?: keyof T & string;
753
- };
754
- /**
755
- * Configuration for AggregatingMergeTree engine
756
- * @template T The data type of the records stored in the table.
757
- */
758
- type AggregatingMergeTreeConfig<T> = BaseOlapConfig<T> & {
759
- engine: ClickHouseEngines.AggregatingMergeTree;
760
- };
761
- /**
762
- * Configuration for SummingMergeTree engine
763
- * @template T The data type of the records stored in the table.
764
- */
765
- type SummingMergeTreeConfig<T> = BaseOlapConfig<T> & {
766
- engine: ClickHouseEngines.SummingMergeTree;
767
- columns?: string[];
768
- };
769
- /**
770
- * Configuration for CollapsingMergeTree engine
771
- * @template T The data type of the records stored in the table.
772
- */
773
- type CollapsingMergeTreeConfig<T> = BaseOlapConfig<T> & {
774
- engine: ClickHouseEngines.CollapsingMergeTree;
775
- sign: keyof T & string;
776
- };
777
- /**
778
- * Configuration for VersionedCollapsingMergeTree engine
779
- * @template T The data type of the records stored in the table.
780
- */
781
- type VersionedCollapsingMergeTreeConfig<T> = BaseOlapConfig<T> & {
782
- engine: ClickHouseEngines.VersionedCollapsingMergeTree;
783
- sign: keyof T & string;
784
- ver: keyof T & string;
785
- };
786
- interface ReplicatedEngineProperties {
787
- keeperPath?: string;
788
- replicaName?: string;
789
- }
790
- /**
791
- * Configuration for ReplicatedMergeTree engine
792
- * @template T The data type of the records stored in the table.
793
- *
794
- * Note: keeperPath and replicaName are optional. Omit them for ClickHouse Cloud,
795
- * which manages replication automatically. For self-hosted with ClickHouse Keeper,
796
- * provide both parameters or neither (to use server defaults).
797
- */
798
- type ReplicatedMergeTreeConfig<T> = Omit<MergeTreeConfig<T>, "engine"> & ReplicatedEngineProperties & {
799
- engine: ClickHouseEngines.ReplicatedMergeTree;
800
- };
801
- /**
802
- * Configuration for ReplicatedReplacingMergeTree engine
803
- * @template T The data type of the records stored in the table.
804
- *
805
- * Note: keeperPath and replicaName are optional. Omit them for ClickHouse Cloud,
806
- * which manages replication automatically. For self-hosted with ClickHouse Keeper,
807
- * provide both parameters or neither (to use server defaults).
808
- */
809
- type ReplicatedReplacingMergeTreeConfig<T> = Omit<ReplacingMergeTreeConfig<T>, "engine"> & ReplicatedEngineProperties & {
810
- engine: ClickHouseEngines.ReplicatedReplacingMergeTree;
811
- };
812
- /**
813
- * Configuration for ReplicatedAggregatingMergeTree engine
814
- * @template T The data type of the records stored in the table.
815
- *
816
- * Note: keeperPath and replicaName are optional. Omit them for ClickHouse Cloud,
817
- * which manages replication automatically. For self-hosted with ClickHouse Keeper,
818
- * provide both parameters or neither (to use server defaults).
819
- */
820
- type ReplicatedAggregatingMergeTreeConfig<T> = Omit<AggregatingMergeTreeConfig<T>, "engine"> & ReplicatedEngineProperties & {
821
- engine: ClickHouseEngines.ReplicatedAggregatingMergeTree;
822
- };
823
- /**
824
- * Configuration for ReplicatedSummingMergeTree engine
825
- * @template T The data type of the records stored in the table.
826
- *
827
- * Note: keeperPath and replicaName are optional. Omit them for ClickHouse Cloud,
828
- * which manages replication automatically. For self-hosted with ClickHouse Keeper,
829
- * provide both parameters or neither (to use server defaults).
830
- */
831
- type ReplicatedSummingMergeTreeConfig<T> = Omit<SummingMergeTreeConfig<T>, "engine"> & ReplicatedEngineProperties & {
832
- engine: ClickHouseEngines.ReplicatedSummingMergeTree;
833
- };
834
- /**
835
- * Configuration for ReplicatedCollapsingMergeTree engine
836
- * @template T The data type of the records stored in the table.
837
- *
838
- * Note: keeperPath and replicaName are optional. Omit them for ClickHouse Cloud,
839
- * which manages replication automatically. For self-hosted with ClickHouse Keeper,
840
- * provide both parameters or neither (to use server defaults).
841
- */
842
- type ReplicatedCollapsingMergeTreeConfig<T> = Omit<CollapsingMergeTreeConfig<T>, "engine"> & ReplicatedEngineProperties & {
843
- engine: ClickHouseEngines.ReplicatedCollapsingMergeTree;
844
- };
845
- /**
846
- * Configuration for ReplicatedVersionedCollapsingMergeTree engine
847
- * @template T The data type of the records stored in the table.
848
- *
849
- * Note: keeperPath and replicaName are optional. Omit them for ClickHouse Cloud,
850
- * which manages replication automatically. For self-hosted with ClickHouse Keeper,
851
- * provide both parameters or neither (to use server defaults).
852
- */
853
- type ReplicatedVersionedCollapsingMergeTreeConfig<T> = Omit<VersionedCollapsingMergeTreeConfig<T>, "engine"> & ReplicatedEngineProperties & {
854
- engine: ClickHouseEngines.ReplicatedVersionedCollapsingMergeTree;
855
- };
856
- /**
857
- * Configuration for S3Queue engine - only non-alterable constructor parameters.
858
- * S3Queue-specific settings like 'mode', 'keeper_path', etc. should be specified
859
- * in the settings field, not here.
860
- * @template T The data type of the records stored in the table.
861
- */
862
- type S3QueueConfig<T> = Omit<BaseOlapConfig<T>, "settings" | "orderByFields" | "partitionBy" | "sampleByExpression" | "projections"> & {
863
- engine: ClickHouseEngines.S3Queue;
864
- /** S3 bucket path with wildcards (e.g., 's3://bucket/data/*.json') */
865
- s3Path: string;
866
- /** Data format (e.g., 'JSONEachRow', 'CSV', 'Parquet') */
867
- format: string;
868
- /** AWS access key ID (optional, omit for NOSIGN/public buckets) */
869
- awsAccessKeyId?: string;
870
- /** AWS secret access key */
871
- awsSecretAccessKey?: string;
872
- /** Compression type (e.g., 'gzip', 'zstd') */
873
- compression?: string;
874
- /** Custom HTTP headers */
875
- headers?: {
876
- [key: string]: string;
877
- };
878
- /**
879
- * S3Queue-specific table settings that can be modified with ALTER TABLE MODIFY SETTING.
880
- * These settings control the behavior of the S3Queue engine.
881
- */
882
- settings?: S3QueueTableSettings;
883
- };
884
- /**
885
- * Configuration for S3 engine
886
- * Note: S3 engine supports ORDER BY clause, unlike S3Queue, Buffer, and Distributed engines
887
- * @template T The data type of the records stored in the table.
888
- */
889
- type S3Config<T> = Omit<BaseOlapConfig<T>, "sampleByExpression" | "projections"> & {
890
- engine: ClickHouseEngines.S3;
891
- /** S3 path (e.g., 's3://bucket/path/file.json') */
892
- path: string;
893
- /** Data format (e.g., 'JSONEachRow', 'CSV', 'Parquet') */
894
- format: string;
895
- /** AWS access key ID (optional, omit for NOSIGN/public buckets) */
896
- awsAccessKeyId?: string;
897
- /** AWS secret access key */
898
- awsSecretAccessKey?: string;
899
- /** Compression type (e.g., 'gzip', 'zstd', 'auto') */
900
- compression?: string;
901
- /** Partition strategy (optional) */
902
- partitionStrategy?: string;
903
- /** Partition columns in data file (optional) */
904
- partitionColumnsInDataFile?: string;
905
- };
906
- /**
907
- * Configuration for Buffer engine
908
- * @template T The data type of the records stored in the table.
909
- */
910
- type BufferConfig<T> = Omit<BaseOlapConfig<T>, "orderByFields" | "orderByExpression" | "partitionBy" | "sampleByExpression" | "projections"> & {
911
- engine: ClickHouseEngines.Buffer;
912
- /** Target database name for the destination table */
913
- targetDatabase: string;
914
- /** Target table name where data will be flushed */
915
- targetTable: string;
916
- /** Number of buffer layers (typically 16) */
917
- numLayers: number;
918
- /** Minimum time in seconds before flushing */
919
- minTime: number;
920
- /** Maximum time in seconds before flushing */
921
- maxTime: number;
922
- /** Minimum number of rows before flushing */
923
- minRows: number;
924
- /** Maximum number of rows before flushing */
925
- maxRows: number;
926
- /** Minimum bytes before flushing */
927
- minBytes: number;
928
- /** Maximum bytes before flushing */
929
- maxBytes: number;
930
- /** Optional: Flush time in seconds */
931
- flushTime?: number;
932
- /** Optional: Flush number of rows */
933
- flushRows?: number;
934
- /** Optional: Flush number of bytes */
935
- flushBytes?: number;
936
- };
937
- /**
938
- * Configuration for Distributed engine
939
- * @template T The data type of the records stored in the table.
940
- */
941
- type DistributedConfig<T> = Omit<BaseOlapConfig<T>, "orderByFields" | "orderByExpression" | "partitionBy" | "sampleByExpression" | "projections"> & {
942
- engine: ClickHouseEngines.Distributed;
943
- /** Cluster name from the ClickHouse configuration */
944
- cluster: string;
945
- /** Database name on the cluster */
946
- targetDatabase: string;
947
- /** Table name on the cluster */
948
- targetTable: string;
949
- /** Optional: Sharding key expression for data distribution */
950
- shardingKey?: string;
951
- /** Optional: Policy name for data distribution */
952
- policyName?: string;
953
- };
954
- /** Kafka table settings. See: https://clickhouse.com/docs/engines/table-engines/integrations/kafka */
955
- interface KafkaTableSettings {
956
- kafka_security_protocol?: "PLAINTEXT" | "SSL" | "SASL_PLAINTEXT" | "SASL_SSL";
957
- kafka_sasl_mechanism?: "GSSAPI" | "PLAIN" | "SCRAM-SHA-256" | "SCRAM-SHA-512" | "OAUTHBEARER";
958
- kafka_sasl_username?: string;
959
- kafka_sasl_password?: string;
960
- kafka_schema?: string;
961
- kafka_num_consumers?: string;
962
- kafka_max_block_size?: string;
963
- kafka_skip_broken_messages?: string;
964
- kafka_commit_every_batch?: string;
965
- kafka_client_id?: string;
966
- kafka_poll_timeout_ms?: string;
967
- kafka_poll_max_batch_size?: string;
968
- kafka_flush_interval_ms?: string;
969
- kafka_consumer_reschedule_ms?: string;
970
- kafka_thread_per_consumer?: string;
971
- kafka_handle_error_mode?: "default" | "stream";
972
- kafka_commit_on_select?: string;
973
- kafka_max_rows_per_message?: string;
974
- kafka_compression_codec?: string;
975
- kafka_compression_level?: string;
976
- }
977
- /** Kafka engine for streaming data from Kafka topics. Additional settings go in `settings`. */
978
- type KafkaConfig<T> = Omit<BaseOlapConfig<T>, "orderByFields" | "orderByExpression" | "partitionBy" | "sampleByExpression" | "projections"> & {
979
- engine: ClickHouseEngines.Kafka;
980
- brokerList: string;
981
- topicList: string;
982
- groupName: string;
983
- format: string;
984
- settings?: KafkaTableSettings;
985
- };
986
- /**
987
- * Configuration for IcebergS3 engine - read-only Iceberg table access
988
- *
989
- * Provides direct querying of Apache Iceberg tables stored on S3.
990
- * Data is not copied; queries stream directly from Parquet/ORC files.
991
- *
992
- * @template T The data type of the records stored in the table.
993
- *
994
- * @example
995
- * ```typescript
996
- * const lakeEvents = new OlapTable<Event>("lake_events", {
997
- * engine: ClickHouseEngines.IcebergS3,
998
- * path: "s3://datalake/events/",
999
- * format: "Parquet",
1000
- * awsAccessKeyId: mooseRuntimeEnv.get("AWS_ACCESS_KEY_ID"),
1001
- * awsSecretAccessKey: mooseRuntimeEnv.get("AWS_SECRET_ACCESS_KEY")
1002
- * });
1003
- * ```
1004
- *
1005
- * @remarks
1006
- * - IcebergS3 engine is read-only
1007
- * - Does not support ORDER BY, PARTITION BY, or SAMPLE BY clauses
1008
- * - Queries always see the latest Iceberg snapshot (with metadata cache)
1009
- */
1010
- type IcebergS3Config<T> = Omit<BaseOlapConfig<T>, "orderByFields" | "orderByExpression" | "partitionBy" | "sampleByExpression" | "projections"> & {
1011
- engine: ClickHouseEngines.IcebergS3;
1012
- /** S3 path to Iceberg table root (e.g., 's3://bucket/warehouse/events/') */
1013
- path: string;
1014
- /** Data format - 'Parquet' or 'ORC' */
1015
- format: "Parquet" | "ORC";
1016
- /** AWS access key ID (optional, omit for NOSIGN/public buckets) */
1017
- awsAccessKeyId?: string;
1018
- /** AWS secret access key (optional) */
1019
- awsSecretAccessKey?: string;
1020
- /** Compression type (optional: 'gzip', 'zstd', 'auto') */
1021
- compression?: string;
1022
- };
1023
- /**
1024
- * Configuration for Merge engine - read-only view over multiple tables matching a regex pattern.
1025
- *
1026
- * @template T The data type of the records in the source tables.
1027
- *
1028
- * @example
1029
- * ```typescript
1030
- * const allEvents = new OlapTable<Event>("all_events", {
1031
- * engine: ClickHouseEngines.Merge,
1032
- * sourceDatabase: "currentDatabase()",
1033
- * tablesRegexp: "^events_\\d+$",
1034
- * });
1035
- * ```
1036
- *
1037
- * @remarks
1038
- * - Merge engine is read-only; INSERT operations are not supported
1039
- * - Cannot be used as a destination in IngestPipeline
1040
- * - Does not support ORDER BY, PARTITION BY, or SAMPLE BY clauses
1041
- */
1042
- type MergeConfig<T> = Omit<BaseOlapConfig<T>, "orderByFields" | "orderByExpression" | "partitionBy" | "sampleByExpression" | "projections"> & {
1043
- engine: ClickHouseEngines.Merge;
1044
- /** Database to scan for source tables (literal name, currentDatabase(), or REGEXP(...)) */
1045
- sourceDatabase: string;
1046
- /** Regex pattern to match table names in the source database */
1047
- tablesRegexp: string;
1048
- };
1049
- /**
1050
- * Legacy configuration (backward compatibility) - defaults to MergeTree engine
1051
- * @template T The data type of the records stored in the table.
1052
- */
1053
- type LegacyOlapConfig<T> = BaseOlapConfig<T>;
1054
- type EngineConfig<T> = MergeTreeConfig<T> | ReplacingMergeTreeConfig<T> | AggregatingMergeTreeConfig<T> | SummingMergeTreeConfig<T> | CollapsingMergeTreeConfig<T> | VersionedCollapsingMergeTreeConfig<T> | ReplicatedMergeTreeConfig<T> | ReplicatedReplacingMergeTreeConfig<T> | ReplicatedAggregatingMergeTreeConfig<T> | ReplicatedSummingMergeTreeConfig<T> | ReplicatedCollapsingMergeTreeConfig<T> | ReplicatedVersionedCollapsingMergeTreeConfig<T> | S3QueueConfig<T> | S3Config<T> | BufferConfig<T> | DistributedConfig<T> | IcebergS3Config<T> | KafkaConfig<T> | MergeConfig<T>;
1055
- /**
1056
- * Union of all engine-specific configurations (new API)
1057
- * @template T The data type of the records stored in the table.
1058
- */
1059
- type OlapConfig<T> = EngineConfig<T> | LegacyOlapConfig<T>;
1060
- /**
1061
- * Represents an OLAP (Online Analytical Processing) table, typically corresponding to a ClickHouse table.
1062
- * Provides a typed interface for interacting with the table.
1063
- *
1064
- * @template T The data type of the records stored in the table. The structure of T defines the table schema.
1065
- */
1066
- declare class OlapTable<T> extends TypedBase<T, OlapConfig<T>> {
1067
- name: IdentifierBrandedString;
1068
- /** @internal */
1069
- readonly kind = "OlapTable";
1070
- /** @internal Typia validators for Insertable<T> — used during insert validation */
1071
- private insertValidators?;
1072
- /** @internal Memoized ClickHouse client for reusing connections across insert calls */
1073
- private _memoizedClient?;
1074
- /** @internal Hash of the configuration used to create the memoized client */
1075
- private _configHash?;
1076
- /** @internal Cached table name to avoid repeated generation */
1077
- private _cachedTableName?;
1078
- /**
1079
- * Creates a new OlapTable instance.
1080
- * @param name The name of the table. This name is used for the underlying ClickHouse table.
1081
- * @param config Optional configuration for the OLAP table.
1082
- */
1083
- constructor(name: string, config?: OlapConfig<T>);
1084
- /** @internal **/
1085
- constructor(name: string, config: OlapConfig<T>, schema: IJsonSchemaCollection.IV3_1, columns: Column[], validators?: TypiaValidators<T>, insertValidators?: TypiaValidators<Insertable<T>>);
1086
- /** @internal Returns the versioned ClickHouse table name (e.g., "events_1_0_0") */
1087
- generateTableName(): string;
1088
- /**
1089
- * Creates a fast hash of the ClickHouse configuration.
1090
- * Uses crypto.createHash for better performance than JSON.stringify.
1091
- *
1092
- * @private
1093
- */
1094
- private createConfigHash;
1095
- /**
1096
- * Gets or creates a memoized ClickHouse client.
1097
- * The client is cached and reused across multiple insert calls for better performance.
1098
- * If the configuration changes, a new client will be created.
1099
- *
1100
- * @private
1101
- */
1102
- private getMemoizedClient;
1103
- /**
1104
- * Closes the memoized ClickHouse client if it exists.
1105
- * This is useful for cleaning up connections when the table instance is no longer needed.
1106
- * The client will be automatically recreated on the next insert call if needed.
1107
- */
1108
- closeClient(): Promise<void>;
1109
- /**
1110
- * Validates a single record using typia's comprehensive type checking.
1111
- * This provides the most accurate validation as it uses the exact TypeScript type information.
1112
- *
1113
- * @param record The record to validate
1114
- * @returns Validation result with detailed error information
1115
- */
1116
- validateRecord(record: unknown): {
1117
- success: boolean;
1118
- data?: T;
1119
- errors?: string[];
1120
- };
1121
- /**
1122
- * Type guard function using typia's is() function.
1123
- * Provides compile-time type narrowing for TypeScript.
1124
- *
1125
- * @param record The record to check
1126
- * @returns True if record matches type T, with type narrowing
1127
- */
1128
- isValidRecord(record: unknown): record is T;
1129
- /**
1130
- * Assert that a record matches type T, throwing detailed errors if not.
1131
- * Uses typia's assert() function for the most detailed error reporting.
1132
- *
1133
- * @param record The record to assert
1134
- * @returns The validated and typed record
1135
- * @throws Detailed validation error if record doesn't match type T
1136
- */
1137
- assertValidRecord(record: unknown): T;
1138
- /**
1139
- * Validates records for insert using Insertable<T> validators when available.
1140
- * Falls back to the full T validators if insert validators weren't generated.
1141
- * @private
1142
- */
1143
- private validateInsertRecords;
1144
- /**
1145
- * Validates an array of records with comprehensive error reporting.
1146
- * Uses the most appropriate validation method available (typia or basic).
1147
- *
1148
- * @param data Array of records to validate
1149
- * @returns Detailed validation results
1150
- */
1151
- validateRecords(data: unknown[]): Promise<ValidationResult<T>>;
1152
- /**
1153
- * Optimized batch retry that minimizes individual insert operations.
1154
- * Groups records into smaller batches to reduce round trips while still isolating failures.
1155
- *
1156
- * @private
1157
- */
1158
- private retryIndividualRecords;
1159
- /**
1160
- * Validates input parameters and strategy compatibility
1161
- * @private
1162
- */
1163
- private validateInsertParameters;
1164
- /**
1165
- * Handles early return cases for empty data
1166
- * @private
1167
- */
1168
- private handleEmptyData;
1169
- /**
1170
- * Performs pre-insertion validation for array data
1171
- * @private
1172
- */
1173
- private performPreInsertionValidation;
1174
- /**
1175
- * Handles validation errors based on the specified strategy
1176
- * @private
1177
- */
1178
- private handleValidationErrors;
1179
- /**
1180
- * Checks if validation errors exceed configured thresholds
1181
- * @private
1182
- */
1183
- private checkValidationThresholds;
1184
- /**
1185
- * Optimized insert options preparation with better memory management
1186
- * @private
1187
- */
1188
- private prepareInsertOptions;
1189
- /**
1190
- * Creates success result for completed insertions
1191
- * @private
1192
- */
1193
- private createSuccessResult;
1194
- /**
1195
- * Handles insertion errors based on the specified strategy
1196
- * @private
1197
- */
1198
- private handleInsertionError;
1199
- /**
1200
- * Handles the isolate strategy for insertion errors
1201
- * @private
1202
- */
1203
- private handleIsolateStrategy;
1204
- /**
1205
- * Checks if insertion errors exceed configured thresholds
1206
- * @private
1207
- */
1208
- private checkInsertionThresholds;
1209
- /**
1210
- * Recursively transforms a record to match ClickHouse's JSONEachRow requirements
1211
- *
1212
- * - For every Array(Nested(...)) field at any depth, each item is wrapped in its own array and recursively processed.
1213
- * - For every Nested struct (not array), it recurses into the struct.
1214
- * - This ensures compatibility with kafka_clickhouse_sync
1215
- *
1216
- * @param record The input record to transform (may be deeply nested)
1217
- * @param columns The schema columns for this level (defaults to this.columnArray at the top level)
1218
- * @returns The transformed record, ready for ClickHouse JSONEachRow insertion
1219
- */
1220
- private mapToClickhouseRecord;
1221
- /**
1222
- * Inserts data directly into the ClickHouse table with enhanced error handling and validation.
1223
- * This method establishes a direct connection to ClickHouse using the project configuration
1224
- * and inserts the provided data into the versioned table.
1225
- *
1226
- * PERFORMANCE OPTIMIZATIONS:
1227
- * - Memoized client connections with fast config hashing
1228
- * - Single-pass validation with pre-allocated arrays
1229
- * - Batch-optimized retry strategy (batches of 10, then individual)
1230
- * - Optimized ClickHouse settings for large datasets
1231
- * - Reduced memory allocations and object creation
1232
- *
1233
- * Uses advanced typia validation when available for comprehensive type checking,
1234
- * with fallback to basic validation for compatibility.
1235
- *
1236
- * The ClickHouse client is memoized and reused across multiple insert calls for better performance.
1237
- * If the configuration changes, a new client will be automatically created.
1238
- *
1239
- * @param data Array of objects conforming to the table schema, or a Node.js Readable stream
1240
- * @param options Optional configuration for error handling, validation, and insertion behavior
1241
- * @returns Promise resolving to detailed insertion results
1242
- * @throws {ConfigError} When configuration cannot be read or parsed
1243
- * @throws {ClickHouseError} When insertion fails based on the error strategy
1244
- * @throws {ValidationError} When validation fails and strategy is 'fail-fast'
1245
- *
1246
- * @example
1247
- * ```typescript
1248
- * // Create an OlapTable instance (typia validators auto-injected)
1249
- * const userTable = new OlapTable<User>('users');
1250
- *
1251
- * // Insert with comprehensive typia validation
1252
- * const result1 = await userTable.insert([
1253
- * { id: 1, name: 'John', email: 'john@example.com' },
1254
- * { id: 2, name: 'Jane', email: 'jane@example.com' }
1255
- * ]);
1256
- *
1257
- * // Insert data with stream input (validation not available for streams)
1258
- * const dataStream = new Readable({
1259
- * objectMode: true,
1260
- * read() { // Stream implementation }
1261
- * });
1262
- * const result2 = await userTable.insert(dataStream, { strategy: 'fail-fast' });
1263
- *
1264
- * // Insert with validation disabled for performance
1265
- * const result3 = await userTable.insert(data, { validate: false });
1266
- *
1267
- * // Insert with error handling strategies
1268
- * const result4 = await userTable.insert(mixedData, {
1269
- * strategy: 'isolate',
1270
- * allowErrorsRatio: 0.1,
1271
- * validate: true // Use typia validation (default)
1272
- * });
1273
- *
1274
- * // Optional: Clean up connection when completely done
1275
- * await userTable.closeClient();
1276
- * ```
1277
- */
1278
- insert(data: Insertable<T>[] | Readable, options?: InsertOptions): Promise<InsertResult<T>>;
1279
- }
1280
-
1281
- /**
1282
- * @fileoverview Stream SDK for data streaming operations in Moose.
1283
- *
1284
- * This module provides the core streaming functionality including:
1285
- * - Stream creation and configuration
1286
- * - Message transformations between streams
1287
- * - Consumer registration for message processing
1288
- * - Dead letter queue handling for error recovery
1289
- *
1290
- * @module Stream
1291
- */
1292
-
1293
- /**
1294
- * Represents zero, one, or many values of type T.
1295
- * Used for flexible return types in transformations where a single input
1296
- * can produce no output, one output, or multiple outputs.
1297
- *
1298
- * @template T The type of the value(s)
1299
- * @example
1300
- * ```typescript
1301
- * // Can return a single value
1302
- * const single: ZeroOrMany<string> = "hello";
1303
- *
1304
- * // Can return an array
1305
- * const multiple: ZeroOrMany<string> = ["hello", "world"];
1306
- *
1307
- * // Can return null/undefined to filter out
1308
- * const filtered: ZeroOrMany<string> = null;
1309
- * ```
1310
- */
1311
- type ZeroOrMany<T> = T | T[] | undefined | null;
1312
- /**
1313
- * Function type for transforming records from one type to another.
1314
- * Supports both synchronous and asynchronous transformations.
1315
- *
1316
- * @template T The input record type
1317
- * @template U The output record type
1318
- * @param record The input record to transform
1319
- * @returns The transformed record(s), or null/undefined to filter out
1320
- *
1321
- * @example
1322
- * ```typescript
1323
- * const transform: SyncOrAsyncTransform<InputType, OutputType> = (record) => {
1324
- * return { ...record, processed: true };
1325
- * };
1326
- * ```
1327
- */
1328
- type SyncOrAsyncTransform<T, U> = (record: T) => ZeroOrMany<U> | Promise<ZeroOrMany<U>>;
1329
- /**
1330
- * Function type for consuming records without producing output.
1331
- * Used for side effects like logging, external API calls, or database writes.
1332
- *
1333
- * @template T The record type to consume
1334
- * @param record The record to process
1335
- * @returns Promise<void> or void
1336
- *
1337
- * @example
1338
- * ```typescript
1339
- * const consumer: Consumer<UserEvent> = async (event) => {
1340
- * await sendToAnalytics(event);
1341
- * };
1342
- * ```
1343
- */
1344
- type Consumer<T> = (record: T) => Promise<void> | void;
1345
- /**
1346
- * Configuration options for stream transformations.
1347
- *
1348
- * @template T The type of records being transformed
1349
- */
1350
- interface TransformConfig<T> {
1351
- /**
1352
- * Optional version identifier for this transformation.
1353
- * Multiple transformations to the same destination can coexist with different versions.
1354
- */
1355
- version?: string;
1356
- /**
1357
- * Optional metadata for documentation and tracking purposes.
1358
- */
1359
- metadata?: {
1360
- description?: string;
1361
- };
1362
- /**
1363
- * Optional dead letter queue for handling transformation failures.
1364
- * Failed records will be sent to this queue for manual inspection or reprocessing.
1365
- * Uses {@link Stream.defaultDeadLetterQueue} by default
1366
- * unless a DeadLetterQueue is provided, or it is explicitly disabled with a null value
1367
- */
1368
- deadLetterQueue?: DeadLetterQueue<T> | null;
1369
- /**
1370
- * @internal Source file path where this transform was declared.
1371
- * Automatically captured from stack trace.
1372
- */
1373
- sourceFile?: string;
1374
- }
1375
- /**
1376
- * Configuration options for stream consumers.
1377
- *
1378
- * @template T The type of records being consumed
1379
- */
1380
- interface ConsumerConfig<T> {
1381
- /**
1382
- * Optional version identifier for this consumer.
1383
- * Multiple consumers can coexist with different versions.
1384
- */
1385
- version?: string;
1386
- /**
1387
- * Optional dead letter queue for handling consumer failures.
1388
- * Failed records will be sent to this queue for manual inspection or reprocessing.
1389
- * Uses {@link Stream.defaultDeadLetterQueue} by default
1390
- * unless a DeadLetterQueue is provided, or it is explicitly disabled with a null value
1391
- */
1392
- deadLetterQueue?: DeadLetterQueue<T> | null;
1393
- /**
1394
- * @internal Source file path where this consumer was declared.
1395
- * Automatically captured from stack trace.
1396
- */
1397
- sourceFile?: string;
1398
- }
1399
- type SchemaRegistryEncoding = "JSON" | "AVRO" | "PROTOBUF";
1400
- type SchemaRegistryReference = {
1401
- id: number;
1402
- } | {
1403
- subjectLatest: string;
1404
- } | {
1405
- subject: string;
1406
- version: number;
1407
- };
1408
- interface KafkaSchemaConfig {
1409
- kind: SchemaRegistryEncoding;
1410
- reference: SchemaRegistryReference;
1411
- }
1412
- /**
1413
- * Represents a message routed to a specific destination stream.
1414
- * Used internally by the multi-transform functionality to specify
1415
- * where transformed messages should be sent.
1416
- *
1417
- * @internal
1418
- */
1419
- declare class RoutedMessage {
1420
- /** The destination stream for the message */
1421
- destination: Stream<any>;
1422
- /** The message value(s) to send */
1423
- values: ZeroOrMany<any>;
1424
- /**
1425
- * Creates a new routed message.
1426
- *
1427
- * @param destination The target stream
1428
- * @param values The message(s) to route
1429
- */
1430
- constructor(destination: Stream<any>, values: ZeroOrMany<any>);
1431
- }
1432
- /**
1433
- * Configuration options for a data stream (e.g., a Redpanda topic).
1434
- * @template T The data type of the messages in the stream.
1435
- */
1436
- interface StreamConfig<T> {
1437
- /**
1438
- * Specifies the number of partitions for the stream. Affects parallelism and throughput.
1439
- */
1440
- parallelism?: number;
1441
- /**
1442
- * Specifies the data retention period for the stream in seconds. Messages older than this may be deleted.
1443
- */
1444
- retentionPeriod?: number;
1445
- /**
1446
- * An optional destination OLAP table where messages from this stream should be automatically ingested.
1447
- */
1448
- destination?: OlapTable<T>;
1449
- /**
1450
- * An optional version string for this configuration. Can be used for tracking changes or managing deployments.
1451
- */
1452
- version?: string;
1453
- metadata?: {
1454
- description?: string;
1455
- };
1456
- lifeCycle?: LifeCycle;
1457
- defaultDeadLetterQueue?: DeadLetterQueue<T>;
1458
- /** Optional Schema Registry configuration for this stream */
1459
- schemaConfig?: KafkaSchemaConfig;
1460
- }
1461
- /**
1462
- * Represents a data stream, typically corresponding to a Redpanda topic.
1463
- * Provides a typed interface for producing to and consuming from the stream, and defining transformations.
1464
- *
1465
- * @template T The data type of the messages flowing through the stream. The structure of T defines the message schema.
1466
- */
1467
- declare class Stream<T> extends TypedBase<T, StreamConfig<T>> {
1468
- defaultDeadLetterQueue?: DeadLetterQueue<T>;
1469
- /** @internal Memoized KafkaJS producer for reusing connections across sends */
1470
- private _memoizedProducer?;
1471
- /** @internal Hash of the configuration used to create the memoized Kafka producer */
1472
- private _kafkaConfigHash?;
1473
- /**
1474
- * Creates a new Stream instance.
1475
- * @param name The name of the stream. This name is used for the underlying Redpanda topic.
1476
- * @param config Optional configuration for the stream.
1477
- */
1478
- constructor(name: string, config?: StreamConfig<T>);
1479
- /**
1480
- * @internal
1481
- * Note: `validators` parameter is a positional placeholder (always undefined for Stream).
1482
- * It exists because TypedBase has validators as the 5th param, and we need to pass
1483
- * allowExtraFields as the 6th param. Stream doesn't use validators.
1484
- */
1485
- constructor(name: string, config: StreamConfig<T>, schema: IJsonSchemaCollection.IV3_1, columns: Column[], validators: undefined, allowExtraFields: boolean);
1486
- /**
1487
- * Internal map storing transformation configurations.
1488
- * Maps destination stream names to arrays of transformation functions and their configs.
1489
- *
1490
- * @internal
1491
- */
1492
- _transformations: Map<string, [Stream<any>, SyncOrAsyncTransform<T, any>, TransformConfig<T>][]>;
1493
- /**
1494
- * Internal function for multi-stream transformations.
1495
- * Allows a single transformation to route messages to multiple destinations.
1496
- *
1497
- * @internal
1498
- */
1499
- _multipleTransformations?: (record: T) => [RoutedMessage];
1500
- /**
1501
- * Internal array storing consumer configurations.
1502
- *
1503
- * @internal
1504
- */
1505
- _consumers: {
1506
- consumer: Consumer<T>;
1507
- config: ConsumerConfig<T>;
1508
- }[];
1509
- /**
1510
- * Builds the full Kafka topic name including optional namespace and version suffix.
1511
- * Version suffix is appended as _x_y_z where dots in version are replaced with underscores.
1512
- */
1513
- private buildFullTopicName;
1514
- /**
1515
- * Creates a fast hash string from relevant Kafka configuration fields.
1516
- */
1517
- private createConfigHash;
1518
- /**
1519
- * Gets or creates a memoized KafkaJS producer using runtime configuration.
1520
- */
1521
- private getMemoizedProducer;
1522
- /**
1523
- * Closes the memoized Kafka producer if it exists.
1524
- */
1525
- closeProducer(): Promise<void>;
1526
- /**
1527
- * Sends one or more records to this stream's Kafka topic.
1528
- * Values are JSON-serialized as message values.
1529
- */
1530
- send(values: ZeroOrMany<T>): Promise<void>;
1531
- /**
1532
- * Adds a transformation step that processes messages from this stream and sends the results to a destination stream.
1533
- * Multiple transformations to the same destination stream can be added if they have distinct `version` identifiers in their config.
1534
- *
1535
- * @template U The data type of the messages in the destination stream.
1536
- * @param destination The destination stream for the transformed messages.
1537
- * @param transformation A function that takes a message of type T and returns zero or more messages of type U (or a Promise thereof).
1538
- * Return `null` or `undefined` or an empty array `[]` to filter out a message. Return an array to emit multiple messages.
1539
- * @param config Optional configuration for this specific transformation step, like a version.
1540
- */
1541
- addTransform<U>(destination: Stream<U>, transformation: SyncOrAsyncTransform<T, U>, config?: TransformConfig<T>): void;
1542
- /**
1543
- * Adds a consumer function that processes messages from this stream.
1544
- * Multiple consumers can be added if they have distinct `version` identifiers in their config.
1545
- *
1546
- * @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>.
1547
- * @param config Optional configuration for this specific consumer, like a version.
1548
- */
1549
- addConsumer(consumer: Consumer<T>, config?: ConsumerConfig<T>): void;
1550
- /**
1551
- * Helper method for `addMultiTransform` to specify the destination and values for a routed message.
1552
- * @param values The value or values to send to this stream.
1553
- * @returns A `RoutedMessage` object associating the values with this stream.
1554
- *
1555
- * @example
1556
- * ```typescript
1557
- * sourceStream.addMultiTransform((record) => [
1558
- * destinationStream1.routed(transformedRecord1),
1559
- * destinationStream2.routed([record2a, record2b])
1560
- * ]);
1561
- * ```
1562
- */
1563
- routed: (values: ZeroOrMany<T>) => RoutedMessage;
1564
- /**
1565
- * Adds a single transformation function that can route messages to multiple destination streams.
1566
- * This is an alternative to adding multiple individual `addTransform` calls.
1567
- * Only one multi-transform function can be added per stream.
1568
- *
1569
- * @param transformation A function that takes a message of type T and returns an array of `RoutedMessage` objects,
1570
- * each specifying a destination stream and the message(s) to send to it.
1571
- */
1572
- addMultiTransform(transformation: (record: T) => [RoutedMessage]): void;
1573
- }
1574
- /**
1575
- * Base model for dead letter queue entries.
1576
- * Contains the original failed record along with error information.
1577
- */
1578
- interface DeadLetterModel {
1579
- /** The original record that failed processing */
1580
- originalRecord: Record<string, any>;
1581
- /** Human-readable error message describing the failure */
1582
- errorMessage: string;
1583
- /** Classification of the error type (e.g., "ValidationError", "TransformError") */
1584
- errorType: string;
1585
- /** Timestamp when the failure occurred */
1586
- failedAt: Date;
1587
- /** The source component where the failure occurred */
1588
- source: "api" | "transform" | "table";
1589
- }
1590
- /**
1591
- * Enhanced dead letter model with type recovery functionality.
1592
- * Extends the base model with the ability to recover the original typed record.
1593
- *
1594
- * @template T The original record type before failure
1595
- */
1596
- interface DeadLetter<T> extends DeadLetterModel {
1597
- /**
1598
- * Recovers the original record as its typed form.
1599
- * Useful for reprocessing failed records with proper type safety.
1600
- *
1601
- * @returns The original record cast to type T
1602
- */
1603
- asTyped: () => T;
1604
- }
1605
- /**
1606
- * Specialized stream for handling failed records (dead letters).
1607
- * Provides type-safe access to failed records for reprocessing or analysis.
1608
- *
1609
- * @template T The original record type that failed processing
1610
- *
1611
- * @example
1612
- * ```typescript
1613
- * const dlq = new DeadLetterQueue<UserEvent>("user-events-dlq");
1614
- *
1615
- * dlq.addConsumer(async (deadLetter) => {
1616
- * const originalEvent = deadLetter.asTyped();
1617
- * console.log(`Failed event: ${deadLetter.errorMessage}`);
1618
- * // Potentially reprocess or alert
1619
- * });
1620
- * ```
1621
- */
1622
- declare class DeadLetterQueue<T> extends Stream<DeadLetterModel> {
1623
- /**
1624
- * Creates a new DeadLetterQueue instance.
1625
- * @param name The name of the dead letter queue stream
1626
- * @param config Optional configuration for the stream. The metadata property is always present and includes stackTrace.
1627
- */
1628
- constructor(name: string, config?: StreamConfig<DeadLetterModel>);
1629
- /** @internal **/
1630
- constructor(name: string, config: StreamConfig<DeadLetterModel>, validate: (originalRecord: any) => T);
1631
- /**
1632
- * Internal type guard function for validating and casting original records.
1633
- *
1634
- * @internal
1635
- */
1636
- private typeGuard;
1637
- /**
1638
- * Adds a transformation step for dead letter records.
1639
- * The transformation function receives a DeadLetter<T> with type recovery capabilities.
1640
- *
1641
- * @template U The output type for the transformation
1642
- * @param destination The destination stream for transformed messages
1643
- * @param transformation Function to transform dead letter records
1644
- * @param config Optional transformation configuration
1645
- */
1646
- addTransform<U>(destination: Stream<U>, transformation: SyncOrAsyncTransform<DeadLetter<T>, U>, config?: TransformConfig<DeadLetterModel>): void;
1647
- /**
1648
- * Adds a consumer for dead letter records.
1649
- * The consumer function receives a DeadLetter<T> with type recovery capabilities.
1650
- *
1651
- * @param consumer Function to process dead letter records
1652
- * @param config Optional consumer configuration
1653
- */
1654
- addConsumer(consumer: Consumer<DeadLetter<T>>, config?: ConsumerConfig<DeadLetterModel>): void;
1655
- /**
1656
- * Adds a multi-stream transformation for dead letter records.
1657
- * The transformation function receives a DeadLetter<T> with type recovery capabilities.
1658
- *
1659
- * @param transformation Function to route dead letter records to multiple destinations
1660
- */
1661
- addMultiTransform(transformation: (record: DeadLetter<T>) => [RoutedMessage]): void;
1662
- }
1663
-
1664
- /**
1665
- * Context passed to task handlers. Single param to future-proof API changes.
1666
- *
1667
- * - state: shared mutable state for the task and its lifecycle hooks
1668
- * - input: optional typed input for the task (undefined when task has no input)
1669
- */
1670
- /**
1671
- * Task handler context. If the task declares an input type (T != null),
1672
- * `input` is required and strongly typed. For no-input tasks (T = null),
1673
- * `input` is omitted/optional.
1674
- */
1675
- type TaskContext<TInput> = TInput extends null ? {
1676
- state: any;
1677
- input?: null;
1678
- } : {
1679
- state: any;
1680
- input: TInput;
1681
- };
1682
- /**
1683
- * Configuration options for defining a task within a workflow.
1684
- *
1685
- * @template T - The input type for the task
1686
- * @template R - The return type for the task
1687
- */
1688
- interface TaskConfig<T, R> {
1689
- /** The main function that executes the task logic */
1690
- run: (context: TaskContext<T>) => Promise<R>;
1691
- /**
1692
- * Optional array of tasks to execute after this task completes successfully.
1693
- * Supports all combinations of input types (real type or null) and output types (real type or void).
1694
- * When this task returns void, onComplete tasks expect null as input.
1695
- * When this task returns a real type, onComplete tasks expect that type as input.
1696
- */
1697
- onComplete?: (Task<R extends void ? null : R, any> | Task<R extends void ? null : R, void>)[];
1698
- /**
1699
- * Optional function that is called when the task is cancelled.
1700
- */
1701
- /** Optional function that is called when the task is cancelled. */
1702
- onCancel?: (context: TaskContext<T>) => Promise<void>;
1703
- /** Optional timeout duration for the task execution (e.g., "30s", "5m") */
1704
- timeout?: string;
1705
- /** Optional number of retry attempts if the task fails */
1706
- retries?: number;
1707
- }
1708
- /**
1709
- * Represents a single task within a workflow system.
1710
- *
1711
- * A Task encapsulates the execution logic, completion handlers, and configuration
1712
- * for a unit of work that can be chained with other tasks in a workflow.
1713
- *
1714
- * @template T - The input type that this task expects
1715
- * @template R - The return type that this task produces
1716
- */
1717
- declare class Task<T, R> {
1718
- readonly name: string;
1719
- readonly config: TaskConfig<T, R>;
1720
- /**
1721
- * Creates a new Task instance.
1722
- *
1723
- * @param name - Unique identifier for the task
1724
- * @param config - Configuration object defining the task behavior
1725
- *
1726
- * @example
1727
- * ```typescript
1728
- * // No input, no output
1729
- * const task1 = new Task<null, void>("task1", {
1730
- * run: async () => {
1731
- * console.log("No input/output");
1732
- * }
1733
- * });
1734
- *
1735
- * // No input, but has output
1736
- * const task2 = new Task<null, OutputType>("task2", {
1737
- * run: async () => {
1738
- * return someOutput;
1739
- * }
1740
- * });
1741
- *
1742
- * // Has input, no output
1743
- * const task3 = new Task<InputType, void>("task3", {
1744
- * run: async (input: InputType) => {
1745
- * // process input but return nothing
1746
- * }
1747
- * });
1748
- *
1749
- * // Has both input and output
1750
- * const task4 = new Task<InputType, OutputType>("task4", {
1751
- * run: async (input: InputType) => {
1752
- * return process(input);
1753
- * }
1754
- * });
1755
- * ```
1756
- */
1757
- constructor(name: string, config: TaskConfig<T, R>);
1758
- }
1759
- /**
1760
- * Configuration options for defining a workflow.
1761
- *
1762
- * A workflow orchestrates the execution of multiple tasks in a defined sequence
1763
- * or pattern, with support for scheduling, retries, and timeouts.
1764
- */
1765
- interface WorkflowConfig {
1766
- /**
1767
- * The initial task that begins the workflow execution.
1768
- * Supports all combinations of input types (real type or null) and output types (real type or void):
1769
- * - Task<null, OutputType>: No input, returns a type
1770
- * - Task<null, void>: No input, returns nothing
1771
- * - Task<InputType, OutputType>: Has input, returns a type
1772
- * - Task<InputType, void>: Has input, returns nothing
1773
- */
1774
- startingTask: Task<null, any> | Task<null, void> | Task<any, any> | Task<any, void>;
1775
- /** Optional number of retry attempts if the entire workflow fails */
1776
- retries?: number;
1777
- /** Optional timeout duration for the entire workflow execution (e.g., "10m", "1h") */
1778
- timeout?: string;
1779
- /** Optional cron-style schedule string for automated workflow execution */
1780
- schedule?: string;
1781
- }
1782
- /**
1783
- * Represents a complete workflow composed of interconnected tasks.
1784
- *
1785
- * A Workflow manages the execution flow of multiple tasks, handling scheduling,
1786
- * error recovery, and task orchestration. Once created, workflows are automatically
1787
- * registered with the internal Moose system.
1788
- *
1789
- * @example
1790
- * ```typescript
1791
- * const dataProcessingWorkflow = new Workflow("dataProcessing", {
1792
- * startingTask: extractDataTask,
1793
- * schedule: "0 2 * * *", // Run daily at 2 AM
1794
- * timeout: "1h",
1795
- * retries: 2
1796
- * });
1797
- * ```
1798
- */
1799
- declare class Workflow {
1800
- readonly name: string;
1801
- readonly config: WorkflowConfig;
1802
- /** @internal Source file path where this workflow was declared */
1803
- sourceFile?: string;
1804
- /** @internal Source line number where this workflow was declared */
1805
- sourceLine?: number;
1806
- /** @internal Source column number where this workflow was declared */
1807
- sourceColumn?: number;
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: string, config: WorkflowConfig);
1816
- /**
1817
- * Validates the task graph to ensure there are no null tasks or infinite loops.
1818
- *
1819
- * @private
1820
- * @param startingTask - The starting task to begin validation from
1821
- * @param workflowName - The name of the workflow being validated (for error messages)
1822
- * @throws {Error} When null/undefined tasks are found or infinite loops are detected
1823
- */
1824
- private validateTaskGraph;
1825
- }
1826
-
1827
- /**
1828
- * @template T The data type of the messages expected by the destination stream.
1829
- */
1830
- interface IngestConfig<T> {
1831
- /**
1832
- * The destination stream where the ingested data should be sent.
1833
- */
1834
- destination: Stream<T>;
1835
- deadLetterQueue?: DeadLetterQueue<T>;
1836
- /**
1837
- * An optional version string for this configuration.
1838
- */
1839
- version?: string;
1840
- /**
1841
- * An optional custom path for the ingestion endpoint.
1842
- */
1843
- path?: string;
1844
- metadata?: {
1845
- description?: string;
1846
- };
1847
- }
1848
- /**
1849
- * Represents an Ingest API endpoint, used for sending data into a Moose system, typically writing to a Stream.
1850
- * Provides a typed interface for the expected data format.
1851
- *
1852
- * @template T The data type of the records that this API endpoint accepts. The structure of T defines the expected request body schema.
1853
- */
1854
- declare class IngestApi<T> extends TypedBase<T, IngestConfig<T>> {
1855
- /**
1856
- * Creates a new IngestApi instance.
1857
- * @param name The name of the ingest API endpoint.
1858
- * @param config Optional configuration for the ingest API.
1859
- */
1860
- constructor(name: string, config?: IngestConfig<T>);
1861
- /**
1862
- * @internal
1863
- * Note: `validators` parameter is a positional placeholder (always undefined for IngestApi).
1864
- * It exists because TypedBase has validators as the 5th param, and we need to pass
1865
- * allowExtraFields as the 6th param. IngestApi doesn't use validators.
1866
- */
1867
- constructor(name: string, config: IngestConfig<T>, schema: IJsonSchemaCollection.IV3_1, columns: Column[], validators: undefined, allowExtraFields: boolean);
1868
- }
1869
-
1870
- /**
1871
- * Utilities provided by getMooseUtils() for database access and SQL queries.
1872
- * Works in both Moose runtime and standalone contexts.
1873
- */
1874
- interface MooseUtils {
1875
- client: MooseClient;
1876
- sql: typeof sql;
1877
- jwt?: JWTPayload;
1878
- }
1879
- /**
1880
- * @deprecated Use MooseUtils instead. ApiUtil is now a type alias to MooseUtils
1881
- * and will be removed in a future version.
1882
- *
1883
- * Migration: Replace `ApiUtil` with `MooseUtils` in your type annotations.
1884
- */
1885
- type ApiUtil = MooseUtils;
1886
- /** @deprecated Use MooseUtils instead. */
1887
- type ConsumptionUtil = MooseUtils;
1888
- declare class MooseClient {
1889
- query: QueryClient;
1890
- workflow: WorkflowClient;
1891
- constructor(queryClient: QueryClient, temporalClient?: Client);
1892
- }
1893
- /**
1894
- * Options for per-query ClickHouse row policy enforcement.
1895
- * When provided, each query activates the specified role and injects
1896
- * custom settings (e.g., tenant ID) that row policies evaluate via getSetting().
1897
- */
1898
- interface RowPolicyOptions {
1899
- role: string;
1900
- clickhouse_settings: Record<string, string>;
1901
- }
1902
- /**
1903
- * Shared ClickHouse role name used by all row policies.
1904
- * IMPORTANT: Must match MOOSE_RLS_ROLE in apps/framework-cli/src/framework/core/infrastructure/select_row_policy.rs
1905
- */
1906
- declare const MOOSE_RLS_ROLE = "moose_rls_role";
1907
- /**
1908
- * Dedicated ClickHouse user for RLS queries.
1909
- * Created at DDL time with SELECT-only permissions and the RLS role granted.
1910
- * IMPORTANT: Must match MOOSE_RLS_USER in apps/framework-cli/src/framework/core/infrastructure/select_row_policy.rs
1911
- */
1912
- declare const MOOSE_RLS_USER = "moose_rls_user";
1913
- /**
1914
- * Prefix for ClickHouse custom settings used by row policies.
1915
- * Setting names are `{MOOSE_RLS_SETTING_PREFIX}{column}`.
1916
- * IMPORTANT: Must match the format in setting_name() in apps/framework-cli/src/framework/core/infrastructure/select_row_policy.rs
1917
- */
1918
- declare const MOOSE_RLS_SETTING_PREFIX = "SQL_moose_rls_";
1919
- /** Config mapping ClickHouse setting names to JWT claim names */
1920
- type RowPoliciesConfig = Record<string, string>;
1921
- /**
1922
- * Build RowPolicyOptions from a row policies config and a claim-value source.
1923
- * Only sets ClickHouse settings for claims that are present in the source.
1924
- *
1925
- * Missing claims are skipped — if a table's row policy calls getSetting()
1926
- * for a setting that wasn't set, ClickHouse will error. This is correct:
1927
- * it means the JWT is missing a claim that the queried table requires.
1928
- * Tables whose policies don't reference the missing setting are unaffected.
1929
- *
1930
- * @param config Maps ClickHouse setting name → claim name
1931
- * @param claims Maps claim name → claim value (e.g., JWT payload or rlsContext)
1932
- * @returns RowPolicyOptions with the shared RLS role and populated settings
1933
- */
1934
- declare function buildRowPolicyOptionsFromClaims(config: RowPoliciesConfig, claims: Record<string, unknown>): RowPolicyOptions;
1935
- declare class QueryClient {
1936
- client: ClickHouseClient;
1937
- query_id_prefix: string;
1938
- private rowPolicyOptions?;
1939
- constructor(client: ClickHouseClient, query_id_prefix: string, rowPolicyOptions?: RowPolicyOptions);
1940
- execute<T = any>(sql: Sql): Promise<ResultSet<"JSONEachRow"> & {
1941
- __query_result_t?: T[];
1942
- }>;
1943
- command(sql: Sql): Promise<CommandResult>;
1944
- }
1945
- declare class WorkflowClient {
1946
- client: Client | undefined;
1947
- constructor(temporalClient?: Client);
1948
- execute(name: string, input_data: any): Promise<{
1949
- status: number;
1950
- body: string;
1951
- }>;
1952
- terminate(workflowId: string): Promise<{
1953
- status: number;
1954
- body: string;
1955
- }>;
1956
- private getWorkflowConfig;
1957
- private processInputData;
1958
- }
1959
- /**
1960
- * This looks similar to the client in runner.ts which is a worker.
1961
- * Temporal SDK uses similar looking connection options & client,
1962
- * but there are different libraries for a worker & client like this one
1963
- * that triggers workflows.
1964
- */
1965
- declare function getTemporalClient(temporalUrl: string, namespace: string, clientCert: string, clientKey: string, apiKey: string): Promise<Client | undefined>;
1966
- declare const ApiHelpers: {
1967
- column: (value: string) => [string, string];
1968
- table: (value: string) => [string, string];
1969
- };
1970
- /** @deprecated Use ApiHelpers instead. */
1971
- declare const ConsumptionHelpers: {
1972
- column: (value: string) => [string, string];
1973
- table: (value: string) => [string, string];
1974
- };
1975
- declare function joinQueries({ values, separator, prefix, suffix, }: {
1976
- values: readonly RawValue[];
1977
- separator?: string;
1978
- prefix?: string;
1979
- suffix?: string;
1980
- }): Sql;
1981
-
1982
- /**
1983
- * Defines the signature for a handler function used by a Consumption API.
1984
- * @template T The expected type of the request parameters or query parameters.
1985
- * @template R The expected type of the response data.
1986
- * @param params An object containing the validated request parameters, matching the structure of T.
1987
- * @param utils Utility functions provided to the handler, e.g., for database access (`runSql`).
1988
- * @returns A Promise resolving to the response data of type R.
1989
- */
1990
- type ApiHandler<T, R> = (params: T, utils: ApiUtil) => Promise<R>;
1991
- /**
1992
- * @template T The data type of the request parameters.
1993
- */
1994
- interface ApiConfig<T> {
1995
- /**
1996
- * An optional version string for this configuration.
1997
- */
1998
- version?: string;
1999
- /**
2000
- * An optional custom path for the API endpoint.
2001
- * If not specified, defaults to the API name.
2002
- */
2003
- path?: string;
2004
- metadata?: {
2005
- description?: string;
2006
- };
2007
- }
2008
- /**
2009
- * Represents a Consumption API endpoint (API), used for querying data from a Moose system.
2010
- * Exposes data, often from an OlapTable or derived through a custom handler function.
2011
- *
2012
- * @template T The data type defining the expected structure of the API's query parameters.
2013
- * @template R The data type defining the expected structure of the API's response body. Defaults to `any`.
2014
- */
2015
- declare class Api<T, R = any> extends TypedBase<T, ApiConfig<T>> {
2016
- /** @internal The handler function that processes requests and generates responses. */
2017
- _handler: ApiHandler<T, R>;
2018
- /** @internal The JSON schema definition for the response type R. */
2019
- responseSchema: IJsonSchemaCollection.IV3_1;
2020
- /**
2021
- * Creates a new Api instance.
2022
- * @param name The name of the consumption API endpoint.
2023
- * @param handler The function to execute when the endpoint is called. It receives validated query parameters and utility functions.
2024
- * @param config Optional configuration for the consumption API.
2025
- */
2026
- constructor(name: string, handler: ApiHandler<T, R>, config?: {});
2027
- /** @internal **/
2028
- constructor(name: string, handler: ApiHandler<T, R>, config: ApiConfig<T>, schema: IJsonSchemaCollection.IV3_1, columns: Column[], responseSchema: IJsonSchemaCollection.IV3_1);
2029
- /**
2030
- * Retrieves the handler function associated with this Consumption API.
2031
- * @returns The handler function.
2032
- */
2033
- getHandler: () => ApiHandler<T, R>;
2034
- call(baseUrl: string, queryParams: T): Promise<R>;
2035
- }
2036
- /** @deprecated Use ApiConfig<T> directly instead. */
2037
- type EgressConfig<T> = ApiConfig<T>;
2038
- /** @deprecated Use Api directly instead. */
2039
- declare const ConsumptionApi: typeof Api;
2040
-
2041
- /**
2042
- * Configuration options for a complete ingestion pipeline, potentially including an Ingest API, a Stream, and an OLAP Table.
2043
- *
2044
- * @template T The data type of the records being ingested.
2045
- *
2046
- * @example
2047
- * ```typescript
2048
- * // Simple pipeline with all components enabled
2049
- * const pipelineConfig: IngestPipelineConfig<UserData> = {
2050
- * table: true,
2051
- * stream: true,
2052
- * ingestApi: true
2053
- * };
2054
- *
2055
- * // Advanced pipeline with custom configurations
2056
- * const advancedConfig: IngestPipelineConfig<UserData> = {
2057
- * table: { orderByFields: ['timestamp', 'userId'], engine: ClickHouseEngines.ReplacingMergeTree },
2058
- * stream: { parallelism: 4, retentionPeriod: 86400 },
2059
- * ingestApi: true,
2060
- * version: '1.2.0',
2061
- * metadata: { description: 'User data ingestion pipeline' }
2062
- * };
2063
- * ```
2064
- */
2065
- type IngestPipelineConfig<T> = {
2066
- /**
2067
- * Configuration for the OLAP table component of the pipeline.
2068
- *
2069
- * - If `true`, a table with default settings is created.
2070
- * - If an `OlapConfig` object is provided, it specifies the table's configuration.
2071
- * - If `false`, no OLAP table is created.
2072
- *
2073
- * @default false
2074
- */
2075
- table: boolean | OlapConfig<T>;
2076
- /**
2077
- * Configuration for the stream component of the pipeline.
2078
- *
2079
- * - If `true`, a stream with default settings is created.
2080
- * - Pass a config object to specify the stream's configuration.
2081
- * - The stream's destination will automatically be set to the pipeline's table if one exists.
2082
- * - If `false`, no stream is created.
2083
- *
2084
- * @default false
2085
- */
2086
- stream: boolean | Omit<StreamConfig<T>, "destination">;
2087
- /**
2088
- * Configuration for the ingest API component of the pipeline.
2089
- *
2090
- * - If `true`, an ingest API with default settings is created.
2091
- * - If a partial `IngestConfig` object (excluding `destination`) is provided, it specifies the API's configuration.
2092
- * - The API's destination will automatically be set to the pipeline's stream if one exists.
2093
- * - If `false`, no ingest API is created.
2094
- *
2095
- * **Note:** Requires a stream to be configured when enabled.
2096
- *
2097
- * @default false
2098
- */
2099
- ingestApi: boolean | Omit<IngestConfig<T>, "destination">;
2100
- /**
2101
- * @deprecated Use `ingestApi` instead. This parameter will be removed in a future version.
2102
- */
2103
- ingest?: boolean | Omit<IngestConfig<T>, "destination">;
2104
- /**
2105
- * Configuration for the dead letter queue of the pipeline.
2106
- * If `true`, a dead letter queue with default settings is created.
2107
- * If a partial `StreamConfig` object (excluding `destination`) is provided, it specifies the dead letter queue's configuration.
2108
- * The API's destination will automatically be set to the pipeline's stream if one exists.
2109
- * If `false` or `undefined`, no dead letter queue is created.
2110
- */
2111
- deadLetterQueue?: boolean | StreamConfig<DeadLetterModel>;
2112
- /**
2113
- * An optional version string applying to all components (table, stream, ingest) created by this pipeline configuration.
2114
- * This version will be used for schema versioning and component identification.
2115
- *
2116
- * @example "v1.0.0", "2023-12", "prod"
2117
- */
2118
- version?: string;
2119
- /**
2120
- * An optional custom path for the ingestion API endpoint.
2121
- * This will be used as the HTTP path for the ingest API if one is created.
2122
- *
2123
- * @example "pipelines/analytics", "data/events"
2124
- */
2125
- path?: string;
2126
- /**
2127
- * Optional metadata for the pipeline.
2128
- */
2129
- metadata?: {
2130
- /** Human-readable description of the pipeline's purpose */
2131
- description?: string;
2132
- };
2133
- /** Determines how changes in code will propagate to the resources. */
2134
- lifeCycle?: LifeCycle;
2135
- };
2136
- /**
2137
- * Represents a complete ingestion pipeline, potentially combining an Ingest API, a Stream, and an OLAP Table
2138
- * under a single name and configuration. Simplifies the setup of common ingestion patterns.
2139
- *
2140
- * This class provides a high-level abstraction for creating data ingestion workflows that can include:
2141
- * - An HTTP API endpoint for receiving data
2142
- * - A streaming component for real-time data processing
2143
- * - An OLAP table for analytical queries
2144
- *
2145
- * @template T The data type of the records flowing through the pipeline. This type defines the schema for the
2146
- * Ingest API input, the Stream messages, and the OLAP Table rows.
2147
- *
2148
- * @example
2149
- * ```typescript
2150
- * // Create a complete pipeline with all components
2151
- * const userDataPipeline = new IngestPipeline('userData', {
2152
- * table: true,
2153
- * stream: true,
2154
- * ingestApi: true,
2155
- * version: '1.0.0',
2156
- * metadata: { description: 'Pipeline for user registration data' }
2157
- * });
2158
- *
2159
- * // Create a pipeline with only table and stream
2160
- * const analyticsStream = new IngestPipeline('analytics', {
2161
- * table: { orderByFields: ['timestamp'], engine: ClickHouseEngines.ReplacingMergeTree },
2162
- * stream: { parallelism: 8, retentionPeriod: 604800 },
2163
- * ingestApi: false
2164
- * });
2165
- * ```
2166
- */
2167
- declare class IngestPipeline<T> extends TypedBase<T, IngestPipelineConfig<T>> {
2168
- /**
2169
- * The OLAP table component of the pipeline, if configured.
2170
- * Provides analytical query capabilities for the ingested data.
2171
- * Only present when `config.table` is not `false`.
2172
- */
2173
- table?: OlapTable<T>;
2174
- /**
2175
- * The stream component of the pipeline, if configured.
2176
- * Handles real-time data flow and processing between components.
2177
- * Only present when `config.stream` is not `false`.
2178
- */
2179
- stream?: Stream<T>;
2180
- /**
2181
- * The ingest API component of the pipeline, if configured.
2182
- * Provides HTTP endpoints for data ingestion.
2183
- * Only present when `config.ingestApi` is not `false`.
2184
- */
2185
- ingestApi?: IngestApi<T>;
2186
- /** The dead letter queue of the pipeline, if configured. */
2187
- deadLetterQueue?: DeadLetterQueue<T>;
2188
- /**
2189
- * Creates a new IngestPipeline instance.
2190
- * Based on the configuration, it automatically creates and links the IngestApi, Stream, and OlapTable components.
2191
- *
2192
- * @param name The base name for the pipeline components (e.g., "userData" could create "userData" table, "userData" stream, "userData" ingest API).
2193
- * @param config Optional configuration for the ingestion pipeline.
2194
- *
2195
- * @throws {Error} When ingest API is enabled but no stream is configured, since the API requires a stream destination.
2196
- *
2197
- * @example
2198
- * ```typescript
2199
- * const pipeline = new IngestPipeline('events', {
2200
- * table: { orderByFields: ['timestamp'], engine: ClickHouseEngines.ReplacingMergeTree },
2201
- * stream: { parallelism: 2 },
2202
- * ingestApi: true
2203
- * });
2204
- * ```
2205
- */
2206
- constructor(name: string, config: IngestPipelineConfig<T>);
2207
- /**
2208
- * Internal constructor used by the framework for advanced initialization.
2209
- *
2210
- * @internal
2211
- * @param name The base name for the pipeline components.
2212
- * @param config Configuration specifying which components to create and their settings.
2213
- * @param schema JSON schema collection for type validation.
2214
- * @param columns Column definitions for the data model.
2215
- * @param validators Typia validation functions.
2216
- * @param allowExtraFields Whether extra fields are allowed (injected when type has index signature).
2217
- */
2218
- constructor(name: string, config: IngestPipelineConfig<T>, schema: IJsonSchemaCollection.IV3_1, columns: Column[], validators: TypiaValidators<T>, allowExtraFields: boolean);
2219
- }
2220
-
2221
- interface ETLPipelineConfig<T, U> {
2222
- extract: AsyncIterable<T> | (() => AsyncIterable<T>);
2223
- transform: (sourceData: T) => Promise<U>;
2224
- load: ((data: U[]) => Promise<void>) | OlapTable<U>;
2225
- }
2226
- declare class ETLPipeline<T, U> {
2227
- readonly name: string;
2228
- readonly config: ETLPipelineConfig<T, U>;
2229
- private batcher;
2230
- constructor(name: string, config: ETLPipelineConfig<T, U>);
2231
- private setupPipeline;
2232
- private createBatcher;
2233
- private getDefaultTaskConfig;
2234
- private createAllTasks;
2235
- private createExtractTask;
2236
- private createTransformTask;
2237
- private createLoadTask;
2238
- run(): Promise<void>;
2239
- }
2240
-
2241
- /**
2242
- * Represents a database View, defined by a SQL SELECT statement based on one or more base tables or other views.
2243
- * Emits structured data for the Moose infrastructure system.
2244
- */
2245
- declare class View {
2246
- /** @internal */
2247
- readonly kind = "View";
2248
- /** The name of the view */
2249
- name: string;
2250
- /** The SELECT SQL statement that defines the view */
2251
- selectSql: string;
2252
- /** Names of source tables/views that the SELECT reads from */
2253
- sourceTables: string[];
2254
- /** Optional metadata for the view */
2255
- metadata: {
2256
- [key: string]: any;
2257
- };
2258
- /**
2259
- * Creates a new View instance.
2260
- * @param name The name of the view to be created.
2261
- * @param selectStatement The SQL SELECT statement that defines the view's logic.
2262
- * @param baseTables An array of OlapTable or View objects that the `selectStatement` reads from. Used for dependency tracking.
2263
- * @param metadata Optional metadata for the view (e.g., description, source file).
2264
- */
2265
- constructor(name: string, selectStatement: string | Sql, baseTables: (OlapTable<any> | View)[], metadata?: {
2266
- [key: string]: any;
2267
- });
2268
- }
2269
-
2270
- /**
2271
- * Configuration options for creating a Materialized View.
2272
- * @template T The data type of the records stored in the target table of the materialized view.
2273
- */
2274
- interface MaterializedViewConfig<T> {
2275
- /** The SQL SELECT statement or `Sql` object defining the data to be materialized. Dynamic SQL (with parameters) is not allowed here. */
2276
- selectStatement: string | Sql;
2277
- /** An array of OlapTable or View objects that the `selectStatement` reads from. */
2278
- selectTables: (OlapTable<any> | View)[];
2279
- /** @deprecated See {@link targetTable}
2280
- * The name for the underlying target OlapTable that stores the materialized data. */
2281
- tableName?: string;
2282
- /** The name for the ClickHouse MATERIALIZED VIEW object itself. */
2283
- materializedViewName: string;
2284
- /** @deprecated See {@link targetTable}
2285
- * Optional ClickHouse engine for the target table (e.g., ReplacingMergeTree). Defaults to MergeTree. */
2286
- engine?: ClickHouseEngines;
2287
- targetTable?: OlapTable<T> /** Target table if the OlapTable object is already constructed. */ | {
2288
- /** The name for the underlying target OlapTable that stores the materialized data. */
2289
- name: string;
2290
- /** Optional ClickHouse engine for the target table (e.g., ReplacingMergeTree). Defaults to MergeTree. */
2291
- engine?: ClickHouseEngines;
2292
- /** Optional ordering fields for the target table. Crucial if using ReplacingMergeTree. */
2293
- orderByFields?: (keyof T & string)[];
2294
- };
2295
- /** @deprecated See {@link targetTable}
2296
- * Optional ordering fields for the target table. Crucial if using ReplacingMergeTree. */
2297
- orderByFields?: (keyof T & string)[];
2298
- /** Optional metadata for the materialized view (e.g., description, source file). */
2299
- metadata?: {
2300
- [key: string]: any;
2301
- };
2302
- /** Optional lifecycle management policy for the materialized view.
2303
- * Controls whether Moose can drop or modify the MV automatically.
2304
- * Defaults to FULLY_MANAGED if not specified. */
2305
- lifeCycle?: LifeCycle;
2306
- }
2307
- /**
2308
- * Represents a Materialized View in ClickHouse.
2309
- * This encapsulates both the target OlapTable that stores the data and the MATERIALIZED VIEW definition
2310
- * that populates the table based on inserts into the source tables.
2311
- *
2312
- * @template TargetTable The data type of the records stored in the underlying target OlapTable. The structure of T defines the target table schema.
2313
- */
2314
- declare class MaterializedView<TargetTable> {
2315
- /** @internal */
2316
- readonly kind = "MaterializedView";
2317
- /** The name of the materialized view */
2318
- name: string;
2319
- /** The target OlapTable instance where the materialized data is stored. */
2320
- targetTable: OlapTable<TargetTable>;
2321
- /** The SELECT SQL statement */
2322
- selectSql: string;
2323
- /** Names of source tables that the SELECT reads from */
2324
- sourceTables: string[];
2325
- /** Optional metadata for the materialized view */
2326
- metadata: {
2327
- [key: string]: any;
2328
- };
2329
- /** Optional lifecycle management policy for the materialized view */
2330
- lifeCycle?: LifeCycle;
2331
- /**
2332
- * Creates a new MaterializedView instance.
2333
- * Requires the `TargetTable` type parameter to be explicitly provided or inferred,
2334
- * as it's needed to define the schema of the underlying target table.
2335
- *
2336
- * @param options Configuration options for the materialized view.
2337
- */
2338
- constructor(options: MaterializedViewConfig<TargetTable>);
2339
- /** @internal **/
2340
- constructor(options: MaterializedViewConfig<TargetTable>, targetSchema: IJsonSchemaCollection.IV3_1, targetColumns: Column[]);
2341
- }
2342
-
2343
- type SqlObject = OlapTable<any> | SqlResource | View | MaterializedView<any>;
2344
- /**
2345
- * Represents a generic SQL resource that requires setup and teardown commands.
2346
- * Base class for constructs like Views and Materialized Views. Tracks dependencies.
2347
- */
2348
- declare class SqlResource {
2349
- /** @internal */
2350
- readonly kind = "SqlResource";
2351
- /** Array of SQL statements to execute for setting up the resource. */
2352
- setup: readonly string[];
2353
- /** Array of SQL statements to execute for tearing down the resource. */
2354
- teardown: readonly string[];
2355
- /** The name of the SQL resource (e.g., view name, materialized view name). */
2356
- name: string;
2357
- /** List of OlapTables or Views that this resource reads data from. */
2358
- pullsDataFrom: SqlObject[];
2359
- /** List of OlapTables or Views that this resource writes data to. */
2360
- pushesDataTo: SqlObject[];
2361
- /** @internal Source file path where this resource was defined */
2362
- sourceFile?: string;
2363
- /** @internal Source line number where this resource was defined */
2364
- sourceLine?: number;
2365
- /** @internal Source column number where this resource was defined */
2366
- sourceColumn?: number;
2367
- /**
2368
- * Creates a new SqlResource instance.
2369
- * @param name The name of the resource.
2370
- * @param setup An array of SQL DDL statements to create the resource.
2371
- * @param teardown An array of SQL DDL statements to drop the resource.
2372
- * @param options Optional configuration for specifying data dependencies.
2373
- * @param options.pullsDataFrom Tables/Views this resource reads from.
2374
- * @param options.pushesDataTo Tables/Views this resource writes to.
2375
- */
2376
- constructor(name: string, setup: readonly (string | Sql)[], teardown: readonly (string | Sql)[], options?: {
2377
- pullsDataFrom?: SqlObject[];
2378
- pushesDataTo?: SqlObject[];
2379
- });
2380
- }
2381
-
2382
- /** Extract the row type from an OlapTable instance. */
2383
- type RowOf<T> = T extends OlapTable<infer R> ? R : never;
2384
- /**
2385
- * Configuration for a SelectRowPolicy.
2386
- *
2387
- * Defines a ClickHouse row policy that filters rows based on a column value
2388
- * matched against a JWT claim via `getSetting()`.
2389
- *
2390
- * The `column` field is type-checked against the columns shared by all
2391
- * tables — a typo will be caught at compile time.
2392
- */
2393
- interface SelectRowPolicyConfig<Tables extends readonly OlapTable<any>[] = readonly OlapTable<any>[]> {
2394
- /** Tables the policy applies to. Policies propagate through regular Views automatically. */
2395
- tables: readonly [...Tables];
2396
- /** Column to filter on (e.g., "org_id"). Must exist in every table. */
2397
- column: keyof RowOf<Tables[number]> & string;
2398
- /** JWT claim name that provides the filter value (e.g., "org_id") */
2399
- claim: string;
2400
- }
2401
- /**
2402
- * Represents a ClickHouse Row Policy as a first-class Moose primitive.
2403
- *
2404
- * When defined, Moose generates `CREATE ROW POLICY` DDL that uses
2405
- * `getSetting('SQL_moose_rls_{column}')` for dynamic per-query tenant scoping.
2406
- *
2407
- * @example
2408
- * ```typescript
2409
- * export const tenantIsolation = new SelectRowPolicy("tenant_isolation", {
2410
- * tables: [DataEventTable],
2411
- * column: "org_id",
2412
- * claim: "org_id",
2413
- * });
2414
- * ```
2415
- */
2416
- declare class SelectRowPolicy<Tables extends readonly OlapTable<any>[] = readonly OlapTable<any>[]> {
2417
- /** @internal */
2418
- readonly kind = "SelectRowPolicy";
2419
- /** The name of the row policy */
2420
- readonly name: string;
2421
- /** The policy configuration */
2422
- readonly config: Readonly<SelectRowPolicyConfig<Tables>>;
2423
- constructor(name: string, config: SelectRowPolicyConfig<Tables>);
2424
- /** Resolved table references for serialization */
2425
- get tableRefs(): {
2426
- name: string;
2427
- database?: string;
2428
- }[];
2429
- }
2430
-
2431
- type WebAppHandler = (req: http.IncomingMessage, res: http.ServerResponse) => void | Promise<void>;
2432
- interface FrameworkApp {
2433
- handle?: (req: http.IncomingMessage, res: http.ServerResponse, next?: (err?: any) => void) => void;
2434
- callback?: () => WebAppHandler;
2435
- routing?: (req: http.IncomingMessage, res: http.ServerResponse) => void;
2436
- ready?: () => PromiseLike<unknown>;
2437
- }
2438
- interface WebAppConfig {
2439
- mountPath: string;
2440
- metadata?: {
2441
- description?: string;
2442
- };
2443
- injectMooseUtils?: boolean;
2444
- }
2445
- declare class WebApp {
2446
- name: string;
2447
- handler: WebAppHandler;
2448
- config: WebAppConfig;
2449
- /** @internal Source file path where this web app was declared */
2450
- sourceFile?: string;
2451
- /** @internal Source line number where this web app was declared */
2452
- sourceLine?: number;
2453
- /** @internal Source column number where this web app was declared */
2454
- sourceColumn?: number;
2455
- private _rawApp?;
2456
- constructor(name: string, appOrHandler: FrameworkApp | WebAppHandler, config: WebAppConfig);
2457
- private toHandler;
2458
- getRawApp(): FrameworkApp | undefined;
2459
- }
2460
-
2461
- /**
2462
- * @module registry
2463
- * Public registry functions for accessing Moose Data Model v2 (dmv2) resources.
2464
- *
2465
- * This module provides functions to retrieve registered resources like tables, streams,
2466
- * APIs, and more. These functions are part of the public API and can be used by
2467
- * user applications to inspect and interact with registered Moose resources.
2468
- */
2469
-
2470
- /**
2471
- * Get all registered OLAP tables.
2472
- * @returns A Map of table name to OlapTable instance
2473
- */
2474
- declare function getTables(): Map<string, OlapTable<any>>;
2475
- /**
2476
- * Get a registered OLAP table by name.
2477
- * @param name - The name of the table
2478
- * @returns The OlapTable instance or undefined if not found
2479
- */
2480
- declare function getTable(name: string): OlapTable<any> | undefined;
2481
- /**
2482
- * Get all registered streams.
2483
- * @returns A Map of stream name to Stream instance
2484
- */
2485
- declare function getStreams(): Map<string, Stream<any>>;
2486
- /**
2487
- * Get a registered stream by name.
2488
- * @param name - The name of the stream
2489
- * @returns The Stream instance or undefined if not found
2490
- */
2491
- declare function getStream(name: string): Stream<any> | undefined;
2492
- /**
2493
- * Get all registered ingestion APIs.
2494
- * @returns A Map of API name to IngestApi instance
2495
- */
2496
- declare function getIngestApis(): Map<string, IngestApi<any>>;
2497
- /**
2498
- * Get a registered ingestion API by name.
2499
- * @param name - The name of the ingestion API
2500
- * @returns The IngestApi instance or undefined if not found
2501
- */
2502
- declare function getIngestApi(name: string): IngestApi<any> | undefined;
2503
- /**
2504
- * Get all registered APIs (consumption/egress APIs).
2505
- * @returns A Map of API key to Api instance
2506
- */
2507
- declare function getApis(): Map<string, Api<any>>;
2508
- /**
2509
- * Get a registered API by name, version, or path.
2510
- *
2511
- * Supports multiple lookup strategies:
2512
- * 1. Direct lookup by full key (name:version or name for unversioned)
2513
- * 2. Lookup by name with automatic version aliasing when only one versioned API exists
2514
- * 3. Lookup by custom path (if configured)
2515
- *
2516
- * @param nameOrPath - The name, name:version, or custom path of the API
2517
- * @returns The Api instance or undefined if not found
2518
- */
2519
- declare function getApi(nameOrPath: string): Api<any> | undefined;
2520
- /**
2521
- * Get all registered SQL resources.
2522
- * @returns A Map of resource name to SqlResource instance
2523
- */
2524
- declare function getSqlResources(): Map<string, SqlResource>;
2525
- /**
2526
- * Get a registered SQL resource by name.
2527
- * @param name - The name of the SQL resource
2528
- * @returns The SqlResource instance or undefined if not found
2529
- */
2530
- declare function getSqlResource(name: string): SqlResource | undefined;
2531
- /**
2532
- * Get all registered workflows.
2533
- * @returns A Map of workflow name to Workflow instance
2534
- */
2535
- declare function getWorkflows(): Map<string, Workflow>;
2536
- /**
2537
- * Get a registered workflow by name.
2538
- * @param name - The name of the workflow
2539
- * @returns The Workflow instance or undefined if not found
2540
- */
2541
- declare function getWorkflow(name: string): Workflow | undefined;
2542
- /**
2543
- * Get all registered web apps.
2544
- * @returns A Map of web app name to WebApp instance
2545
- */
2546
- declare function getWebApps(): Map<string, WebApp>;
2547
- /**
2548
- * Get a registered web app by name.
2549
- * @param name - The name of the web app
2550
- * @returns The WebApp instance or undefined if not found
2551
- */
2552
- declare function getWebApp(name: string): WebApp | undefined;
2553
- /**
2554
- * Get all registered materialized views.
2555
- * @returns A Map of MV name to MaterializedView instance
2556
- */
2557
- declare function getMaterializedViews(): Map<string, MaterializedView<any>>;
2558
- /**
2559
- * Get a registered materialized view by name.
2560
- * @param name - The name of the materialized view
2561
- * @returns The MaterializedView instance or undefined if not found
2562
- */
2563
- declare function getMaterializedView(name: string): MaterializedView<any> | undefined;
2564
- /**
2565
- * Get all registered views.
2566
- * @returns A Map of view name to View instance
2567
- */
2568
- declare function getViews(): Map<string, View>;
2569
- /**
2570
- * Get a registered view by name.
2571
- * @param name - The name of the view
2572
- * @returns The View instance or undefined if not found
2573
- */
2574
- declare function getView(name: string): View | undefined;
2575
- /**
2576
- * Get all registered row policies.
2577
- * @returns A Map of policy name to SelectRowPolicy instance
2578
- */
2579
- declare function getSelectRowPolicies(): Map<string, SelectRowPolicy>;
2580
- /**
2581
- * Get a registered row policy by name.
2582
- * @param name - The name of the row policy
2583
- * @returns The SelectRowPolicy instance or undefined if not found
2584
- */
2585
- declare function getSelectRowPolicy(name: string): SelectRowPolicy | undefined;
2586
-
2587
- /**
2588
- * @module dmv2
2589
- * This module defines the core Moose v2 data model constructs, including OlapTable, Stream, IngestApi, Api,
2590
- * IngestPipeline, View, and MaterializedView. These classes provide a typed interface for defining and managing
2591
- * data infrastructure components like ClickHouse tables, Redpanda streams, and data processing pipelines.
2592
- */
2593
- /**
2594
- * A helper type used potentially for indicating aggregated fields in query results or schemas.
2595
- * Captures the aggregation function name and argument types.
2596
- * (Usage context might be specific to query builders or ORM features).
2597
- *
2598
- * @template AggregationFunction The name of the aggregation function (e.g., 'sum', 'avg', 'count').
2599
- * @template ArgTypes An array type representing the types of the arguments passed to the aggregation function.
2600
- */
2601
- type Aggregated<AggregationFunction extends string, ArgTypes extends any[] = []> = {
2602
- _aggregationFunction?: AggregationFunction;
2603
- _argTypes?: ArgTypes;
2604
- };
2605
- /**
2606
- * A helper type for SimpleAggregateFunction in ClickHouse.
2607
- * SimpleAggregateFunction stores the aggregated value directly instead of intermediate states,
2608
- * offering better performance for functions like sum, max, min, any, anyLast, etc.
2609
- *
2610
- * @template AggregationFunction The name of the simple aggregation function (e.g., 'sum', 'max', 'anyLast').
2611
- * @template ArgType The type of the argument (and result) of the aggregation function.
2612
- *
2613
- * @example
2614
- * ```typescript
2615
- * interface Stats {
2616
- * rowCount: number & SimpleAggregated<'sum', number>;
2617
- * maxValue: number & SimpleAggregated<'max', number>;
2618
- * lastStatus: string & SimpleAggregated<'anyLast', string>;
2619
- * }
2620
- * ```
2621
- */
2622
- type SimpleAggregated<AggregationFunction extends string, ArgType = any> = {
2623
- _simpleAggregationFunction?: AggregationFunction;
2624
- _argType?: ArgType;
2625
- };
2626
-
2627
- export { type ClickHouseDecimal as $, type Aggregated as A, getApi as B, ClickHouseEngines as C, type DeadLetterModel as D, type EgressConfig as E, type FrameworkApp as F, getSqlResources as G, getSqlResource as H, IngestApi as I, getWorkflows as J, getWorkflow as K, LifeCycle as L, MaterializedView as M, getWebApps as N, OlapTable as O, getWebApp as P, getView as Q, getViews as R, type SimpleAggregated as S, Task as T, getMaterializedView as U, View as V, Workflow as W, getMaterializedViews as X, getSelectRowPolicies as Y, getSelectRowPolicy as Z, type ClickHousePrecision as _, type OlapConfig as a, WorkflowClient as a$, type ClickHouseByteSize as a0, type ClickHouseFixedStringSize as a1, type ClickHouseFloat as a2, type ClickHouseInt as a3, type ClickHouseJson as a4, type LowCardinality as a5, type ClickHouseNamedTuple as a6, type ClickHouseDefault as a7, type ClickHouseTTL as a8, type ClickHouseMaterialized as a9, type RawValue as aA, type SqlTemplateTag as aB, sql as aC, Sql as aD, toStaticQuery as aE, toQuery as aF, toQueryPreview as aG, getValueFromParameter as aH, createClickhouseParameter as aI, mapToClickHouseType as aJ, type MooseUtils as aK, MooseClient as aL, type Column as aM, QueryClient as aN, type DataType as aO, type ClickHousePoint as aP, type ClickHouseRing as aQ, type ClickHouseLineString as aR, type ClickHouseMultiLineString as aS, type ClickHousePolygon as aT, type ClickHouseMultiPolygon as aU, type RowPolicyOptions as aV, MOOSE_RLS_ROLE as aW, MOOSE_RLS_USER as aX, MOOSE_RLS_SETTING_PREFIX as aY, type RowPoliciesConfig as aZ, buildRowPolicyOptionsFromClaims as a_, type ClickHouseAlias as aa, type WithDefault as ab, type ClickHouseCodec as ac, type DateTime as ad, type DateTime64 as ae, type DateTimeString as af, type DateTime64String as ag, type FixedString as ah, type Float32 as ai, type Float64 as aj, type Int8 as ak, type Int16 as al, type Int32 as am, type Int64 as an, type UInt8 as ao, type UInt16 as ap, type UInt32 as aq, type UInt64 as ar, type Decimal as as, type Insertable as at, type ApiUtil as au, type ConsumptionUtil as av, quoteIdentifier as aw, type IdentifierBrandedString as ax, type NonIdentifierBrandedString as ay, type Value as az, type S3QueueTableSettings as b, getTemporalClient as b0, ApiHelpers as b1, ConsumptionHelpers as b2, joinQueries as b3, type ConsumerConfig as b4, type TransformConfig as b5, type TaskContext as b6, type TaskConfig as b7, type IngestPipelineConfig as b8, type MaterializedViewConfig as b9, Stream as c, type StreamConfig as d, type DeadLetter as e, DeadLetterQueue as f, type IngestConfig as g, Api as h, type ApiConfig as i, ConsumptionApi as j, IngestPipeline as k, SqlResource as l, SelectRowPolicy as m, type SelectRowPolicyConfig as n, ETLPipeline as o, type ETLPipelineConfig as p, WebApp as q, type WebAppConfig as r, type WebAppHandler as s, getTables as t, getTable as u, getStreams as v, getStream as w, getIngestApis as x, getIngestApi as y, getApis as z };