@514labs/moose-lib 0.6.321-ci-5-ga23d35fe → 0.6.322

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -161,11 +161,184 @@ var init_dataModelTypes = __esm({
161
161
  }
162
162
  });
163
163
 
164
- // src/dataModels/types.ts
164
+ // src/sqlHelpers.ts
165
+ function sql(strings, ...values) {
166
+ return new Sql(strings, values);
167
+ }
168
+ function createClickhouseParameter(parameterIndex, value) {
169
+ return `{p${parameterIndex}:${mapToClickHouseType(value)}}`;
170
+ }
171
+ function emptyIfUndefined(value) {
172
+ return value === void 0 ? "" : value;
173
+ }
174
+ var quoteIdentifier, isTable, isView, isColumn, instanceofSql, Sql, toStaticQuery, toQuery, toQueryPreview, getValueFromParameter, mapToClickHouseType;
175
+ var init_sqlHelpers = __esm({
176
+ "src/sqlHelpers.ts"() {
177
+ "use strict";
178
+ quoteIdentifier = (name) => {
179
+ return name.startsWith("`") && name.endsWith("`") ? name : `\`${name}\``;
180
+ };
181
+ isTable = (value) => typeof value === "object" && value !== null && "kind" in value && value.kind === "OlapTable";
182
+ isView = (value) => typeof value === "object" && value !== null && "kind" in value && value.kind === "View";
183
+ isColumn = (value) => typeof value === "object" && value !== null && !("kind" in value) && "name" in value && "annotations" in value;
184
+ instanceofSql = (value) => typeof value === "object" && "values" in value && "strings" in value;
185
+ Sql = class {
186
+ values;
187
+ strings;
188
+ constructor(rawStrings, rawValues) {
189
+ if (rawStrings.length - 1 !== rawValues.length) {
190
+ if (rawStrings.length === 0) {
191
+ throw new TypeError("Expected at least 1 string");
192
+ }
193
+ throw new TypeError(
194
+ `Expected ${rawStrings.length} strings to have ${rawStrings.length - 1} values`
195
+ );
196
+ }
197
+ const valuesLength = rawValues.reduce(
198
+ (len, value) => len + (instanceofSql(value) ? value.values.length : isColumn(value) || isTable(value) || isView(value) ? 0 : 1),
199
+ 0
200
+ );
201
+ this.values = new Array(valuesLength);
202
+ this.strings = new Array(valuesLength + 1);
203
+ this.strings[0] = rawStrings[0];
204
+ let i = 0, pos = 0;
205
+ while (i < rawValues.length) {
206
+ const child = rawValues[i++];
207
+ const rawString = rawStrings[i];
208
+ if (instanceofSql(child)) {
209
+ this.strings[pos] += child.strings[0];
210
+ let childIndex = 0;
211
+ while (childIndex < child.values.length) {
212
+ this.values[pos++] = child.values[childIndex++];
213
+ this.strings[pos] = child.strings[childIndex];
214
+ }
215
+ this.strings[pos] += rawString;
216
+ } else if (isColumn(child)) {
217
+ const aggregationFunction = child.annotations.find(
218
+ ([k, _]) => k === "aggregationFunction"
219
+ );
220
+ if (aggregationFunction !== void 0) {
221
+ this.strings[pos] += `${aggregationFunction[1].functionName}Merge(\`${child.name}\`)`;
222
+ } else {
223
+ this.strings[pos] += `\`${child.name}\``;
224
+ }
225
+ this.strings[pos] += rawString;
226
+ } else if (isTable(child)) {
227
+ if (child.config.database) {
228
+ this.strings[pos] += `\`${child.config.database}\`.\`${child.name}\``;
229
+ } else {
230
+ this.strings[pos] += `\`${child.name}\``;
231
+ }
232
+ this.strings[pos] += rawString;
233
+ } else if (isView(child)) {
234
+ this.strings[pos] += `\`${child.name}\``;
235
+ this.strings[pos] += rawString;
236
+ } else {
237
+ this.values[pos++] = child;
238
+ this.strings[pos] = rawString;
239
+ }
240
+ }
241
+ }
242
+ };
243
+ toStaticQuery = (sql3) => {
244
+ const [query, params] = toQuery(sql3);
245
+ if (Object.keys(params).length !== 0) {
246
+ throw new Error(
247
+ "Dynamic SQL is not allowed in the select statement in view creation."
248
+ );
249
+ }
250
+ return query;
251
+ };
252
+ toQuery = (sql3) => {
253
+ const parameterizedStubs = sql3.values.map(
254
+ (v, i) => createClickhouseParameter(i, v)
255
+ );
256
+ const query = sql3.strings.map(
257
+ (s, i) => s != "" ? `${s}${emptyIfUndefined(parameterizedStubs[i])}` : ""
258
+ ).join("");
259
+ const query_params = sql3.values.reduce(
260
+ (acc, v, i) => ({
261
+ ...acc,
262
+ [`p${i}`]: getValueFromParameter(v)
263
+ }),
264
+ {}
265
+ );
266
+ return [query, query_params];
267
+ };
268
+ toQueryPreview = (sql3) => {
269
+ try {
270
+ const formatValue = (v) => {
271
+ if (Array.isArray(v)) {
272
+ const [type, val] = v;
273
+ if (type === "Identifier") {
274
+ return `\`${String(val)}\``;
275
+ }
276
+ return `[${v.map((x) => formatValue(x)).join(", ")}]`;
277
+ }
278
+ if (v === null || v === void 0) return "NULL";
279
+ if (typeof v === "string") return `'${v.replace(/'/g, "''")}'`;
280
+ if (typeof v === "number") return String(v);
281
+ if (typeof v === "boolean") return v ? "true" : "false";
282
+ if (v instanceof Date)
283
+ return `'${v.toISOString().replace("T", " ").slice(0, 19)}'`;
284
+ try {
285
+ return JSON.stringify(v);
286
+ } catch {
287
+ return String(v);
288
+ }
289
+ };
290
+ let out = sql3.strings[0] ?? "";
291
+ for (let i = 0; i < sql3.values.length; i++) {
292
+ const val = getValueFromParameter(sql3.values[i]);
293
+ out += formatValue(val);
294
+ out += sql3.strings[i + 1] ?? "";
295
+ }
296
+ return out.replace(/\s+/g, " ").trim();
297
+ } catch (error) {
298
+ console.log(`toQueryPreview error: ${error}`);
299
+ return "/* query preview unavailable */";
300
+ }
301
+ };
302
+ getValueFromParameter = (value) => {
303
+ if (Array.isArray(value)) {
304
+ const [type, val] = value;
305
+ if (type === "Identifier") return val;
306
+ }
307
+ return value;
308
+ };
309
+ mapToClickHouseType = (value) => {
310
+ if (typeof value === "number") {
311
+ return Number.isInteger(value) ? "Int" : "Float";
312
+ }
313
+ if (typeof value === "boolean") return "Bool";
314
+ if (value instanceof Date) return "DateTime";
315
+ if (Array.isArray(value)) {
316
+ const [type, _] = value;
317
+ return type;
318
+ }
319
+ return "String";
320
+ };
321
+ }
322
+ });
323
+
324
+ // src/blocks/helpers.ts
325
+ function dropView(name) {
326
+ return `DROP VIEW IF EXISTS ${quoteIdentifier(name)}`.trim();
327
+ }
328
+ function createMaterializedView(options) {
329
+ return `CREATE MATERIALIZED VIEW IF NOT EXISTS ${quoteIdentifier(options.name)}
330
+ TO ${quoteIdentifier(options.destinationTable)}
331
+ AS ${options.select}`.trim();
332
+ }
333
+ function populateTable(options) {
334
+ return `INSERT INTO ${quoteIdentifier(options.destinationTable)}
335
+ ${options.select}`.trim();
336
+ }
165
337
  var ClickHouseEngines;
166
- var init_types = __esm({
167
- "src/dataModels/types.ts"() {
338
+ var init_helpers = __esm({
339
+ "src/blocks/helpers.ts"() {
168
340
  "use strict";
341
+ init_sqlHelpers();
169
342
  ClickHouseEngines = /* @__PURE__ */ ((ClickHouseEngines2) => {
170
343
  ClickHouseEngines2["MergeTree"] = "MergeTree";
171
344
  ClickHouseEngines2["ReplacingMergeTree"] = "ReplacingMergeTree";
@@ -556,166 +729,6 @@ var init_internal = __esm({
556
729
  }
557
730
  });
558
731
 
559
- // src/sqlHelpers.ts
560
- function sql(strings, ...values) {
561
- return new Sql(strings, values);
562
- }
563
- function createClickhouseParameter(parameterIndex, value) {
564
- return `{p${parameterIndex}:${mapToClickHouseType(value)}}`;
565
- }
566
- function emptyIfUndefined(value) {
567
- return value === void 0 ? "" : value;
568
- }
569
- var quoteIdentifier, isTable, isView, isColumn, instanceofSql, Sql, toStaticQuery, toQuery, toQueryPreview, getValueFromParameter, mapToClickHouseType;
570
- var init_sqlHelpers = __esm({
571
- "src/sqlHelpers.ts"() {
572
- "use strict";
573
- quoteIdentifier = (name) => {
574
- return name.startsWith("`") && name.endsWith("`") ? name : `\`${name}\``;
575
- };
576
- isTable = (value) => typeof value === "object" && value !== null && "kind" in value && value.kind === "OlapTable";
577
- isView = (value) => typeof value === "object" && value !== null && "kind" in value && value.kind === "View";
578
- isColumn = (value) => typeof value === "object" && value !== null && !("kind" in value) && "name" in value && "annotations" in value;
579
- instanceofSql = (value) => typeof value === "object" && "values" in value && "strings" in value;
580
- Sql = class {
581
- values;
582
- strings;
583
- constructor(rawStrings, rawValues) {
584
- if (rawStrings.length - 1 !== rawValues.length) {
585
- if (rawStrings.length === 0) {
586
- throw new TypeError("Expected at least 1 string");
587
- }
588
- throw new TypeError(
589
- `Expected ${rawStrings.length} strings to have ${rawStrings.length - 1} values`
590
- );
591
- }
592
- const valuesLength = rawValues.reduce(
593
- (len, value) => len + (instanceofSql(value) ? value.values.length : isColumn(value) || isTable(value) || isView(value) ? 0 : 1),
594
- 0
595
- );
596
- this.values = new Array(valuesLength);
597
- this.strings = new Array(valuesLength + 1);
598
- this.strings[0] = rawStrings[0];
599
- let i = 0, pos = 0;
600
- while (i < rawValues.length) {
601
- const child = rawValues[i++];
602
- const rawString = rawStrings[i];
603
- if (instanceofSql(child)) {
604
- this.strings[pos] += child.strings[0];
605
- let childIndex = 0;
606
- while (childIndex < child.values.length) {
607
- this.values[pos++] = child.values[childIndex++];
608
- this.strings[pos] = child.strings[childIndex];
609
- }
610
- this.strings[pos] += rawString;
611
- } else if (isColumn(child)) {
612
- const aggregationFunction = child.annotations.find(
613
- ([k, _]) => k === "aggregationFunction"
614
- );
615
- if (aggregationFunction !== void 0) {
616
- this.strings[pos] += `${aggregationFunction[1].functionName}Merge(\`${child.name}\`)`;
617
- } else {
618
- this.strings[pos] += `\`${child.name}\``;
619
- }
620
- this.strings[pos] += rawString;
621
- } else if (isTable(child)) {
622
- if (child.config.database) {
623
- this.strings[pos] += `\`${child.config.database}\`.\`${child.name}\``;
624
- } else {
625
- this.strings[pos] += `\`${child.name}\``;
626
- }
627
- this.strings[pos] += rawString;
628
- } else if (isView(child)) {
629
- this.strings[pos] += `\`${child.name}\``;
630
- this.strings[pos] += rawString;
631
- } else {
632
- this.values[pos++] = child;
633
- this.strings[pos] = rawString;
634
- }
635
- }
636
- }
637
- };
638
- toStaticQuery = (sql3) => {
639
- const [query, params] = toQuery(sql3);
640
- if (Object.keys(params).length !== 0) {
641
- throw new Error(
642
- "Dynamic SQL is not allowed in the select statement in view creation."
643
- );
644
- }
645
- return query;
646
- };
647
- toQuery = (sql3) => {
648
- const parameterizedStubs = sql3.values.map(
649
- (v, i) => createClickhouseParameter(i, v)
650
- );
651
- const query = sql3.strings.map(
652
- (s, i) => s != "" ? `${s}${emptyIfUndefined(parameterizedStubs[i])}` : ""
653
- ).join("");
654
- const query_params = sql3.values.reduce(
655
- (acc, v, i) => ({
656
- ...acc,
657
- [`p${i}`]: getValueFromParameter(v)
658
- }),
659
- {}
660
- );
661
- return [query, query_params];
662
- };
663
- toQueryPreview = (sql3) => {
664
- try {
665
- const formatValue = (v) => {
666
- if (Array.isArray(v)) {
667
- const [type, val] = v;
668
- if (type === "Identifier") {
669
- return `\`${String(val)}\``;
670
- }
671
- return `[${v.map((x) => formatValue(x)).join(", ")}]`;
672
- }
673
- if (v === null || v === void 0) return "NULL";
674
- if (typeof v === "string") return `'${v.replace(/'/g, "''")}'`;
675
- if (typeof v === "number") return String(v);
676
- if (typeof v === "boolean") return v ? "true" : "false";
677
- if (v instanceof Date)
678
- return `'${v.toISOString().replace("T", " ").slice(0, 19)}'`;
679
- try {
680
- return JSON.stringify(v);
681
- } catch {
682
- return String(v);
683
- }
684
- };
685
- let out = sql3.strings[0] ?? "";
686
- for (let i = 0; i < sql3.values.length; i++) {
687
- const val = getValueFromParameter(sql3.values[i]);
688
- out += formatValue(val);
689
- out += sql3.strings[i + 1] ?? "";
690
- }
691
- return out.replace(/\s+/g, " ").trim();
692
- } catch (error) {
693
- console.log(`toQueryPreview error: ${error}`);
694
- return "/* query preview unavailable */";
695
- }
696
- };
697
- getValueFromParameter = (value) => {
698
- if (Array.isArray(value)) {
699
- const [type, val] = value;
700
- if (type === "Identifier") return val;
701
- }
702
- return value;
703
- };
704
- mapToClickHouseType = (value) => {
705
- if (typeof value === "number") {
706
- return Number.isInteger(value) ? "Int" : "Float";
707
- }
708
- if (typeof value === "boolean") return "Bool";
709
- if (value instanceof Date) return "DateTime";
710
- if (Array.isArray(value)) {
711
- const [type, _] = value;
712
- return type;
713
- }
714
- return "String";
715
- };
716
- }
717
- });
718
-
719
732
  // src/config/configFile.ts
720
733
  import path from "path";
721
734
  import * as toml from "toml";
@@ -909,7 +922,7 @@ var init_olapTable = __esm({
909
922
  "use strict";
910
923
  init_typedBase();
911
924
  init_dataModelTypes();
912
- init_types();
925
+ init_helpers();
913
926
  init_internal();
914
927
  init_sqlHelpers();
915
928
  OlapTable = class extends TypedBase {
@@ -2202,7 +2215,7 @@ var init_ingestPipeline = __esm({
2202
2215
  init_stream();
2203
2216
  init_olapTable();
2204
2217
  init_ingestApi();
2205
- init_types();
2218
+ init_helpers();
2206
2219
  IngestPipeline = class extends TypedBase {
2207
2220
  /**
2208
2221
  * The OLAP table component of the pipeline, if configured.
@@ -2475,7 +2488,7 @@ var requireTargetTableName, MaterializedView;
2475
2488
  var init_materializedView = __esm({
2476
2489
  "src/dmv2/sdk/materializedView.ts"() {
2477
2490
  "use strict";
2478
- init_types();
2491
+ init_helpers();
2479
2492
  init_sqlHelpers();
2480
2493
  init_olapTable();
2481
2494
  init_internal();
@@ -2894,7 +2907,6 @@ var init_dmv2 = __esm({
2894
2907
  "src/dmv2/index.ts"() {
2895
2908
  "use strict";
2896
2909
  init_olapTable();
2897
- init_types();
2898
2910
  init_stream();
2899
2911
  init_workflow();
2900
2912
  init_ingestApi();
@@ -2910,6 +2922,13 @@ var init_dmv2 = __esm({
2910
2922
  }
2911
2923
  });
2912
2924
 
2925
+ // src/dataModels/types.ts
2926
+ var init_types = __esm({
2927
+ "src/dataModels/types.ts"() {
2928
+ "use strict";
2929
+ }
2930
+ });
2931
+
2913
2932
  // src/browserCompatible.ts
2914
2933
  var init_browserCompatible = __esm({
2915
2934
  "src/browserCompatible.ts"() {
@@ -3045,7 +3064,7 @@ function joinQueries({
3045
3064
  );
3046
3065
  }
3047
3066
  var MooseClient, QueryClient, WorkflowClient, ApiHelpers, ConsumptionHelpers;
3048
- var init_helpers = __esm({
3067
+ var init_helpers2 = __esm({
3049
3068
  "src/consumption-apis/helpers.ts"() {
3050
3069
  "use strict";
3051
3070
  init_internal();
@@ -3168,11 +3187,11 @@ var init_helpers = __esm({
3168
3187
  }
3169
3188
  async getWorkflowConfig(name) {
3170
3189
  const workflows = await getWorkflows();
3171
- const workflow = workflows.get(name);
3172
- if (workflow) {
3190
+ const dmv2Workflow = workflows.get(name);
3191
+ if (dmv2Workflow) {
3173
3192
  return {
3174
- retries: workflow.config.retries || 3,
3175
- timeout: workflow.config.timeout || "1h"
3193
+ retries: dmv2Workflow.config.retries || 3,
3194
+ timeout: dmv2Workflow.config.timeout || "1h"
3176
3195
  };
3177
3196
  }
3178
3197
  throw new Error(`Workflow config not found for ${name}`);
@@ -3249,7 +3268,7 @@ var init_runner = __esm({
3249
3268
  "src/consumption-apis/runner.ts"() {
3250
3269
  "use strict";
3251
3270
  init_commons();
3252
- init_helpers();
3271
+ init_helpers2();
3253
3272
  init_cluster_utils();
3254
3273
  init_sqlHelpers();
3255
3274
  init_internal();
@@ -3598,7 +3617,7 @@ var standaloneUtils, initPromise2, toClientConfig;
3598
3617
  var init_standalone = __esm({
3599
3618
  "src/consumption-apis/standalone.ts"() {
3600
3619
  "use strict";
3601
- init_helpers();
3620
+ init_helpers2();
3602
3621
  init_commons();
3603
3622
  init_sqlHelpers();
3604
3623
  standaloneUtils = null;
@@ -3714,14 +3733,15 @@ var init_dataSource = __esm({
3714
3733
  var init_index = __esm({
3715
3734
  "src/index.ts"() {
3716
3735
  init_browserCompatible();
3736
+ init_helpers();
3717
3737
  init_commons();
3718
3738
  init_secrets();
3719
- init_helpers();
3739
+ init_helpers2();
3720
3740
  init_webAppHelpers();
3721
3741
  init_task();
3722
3742
  init_runner();
3723
3743
  init_redisClient();
3724
- init_helpers();
3744
+ init_helpers2();
3725
3745
  init_standalone();
3726
3746
  init_sqlHelpers();
3727
3747
  init_utilities();
@@ -3771,7 +3791,9 @@ export {
3771
3791
  createApi,
3772
3792
  createClickhouseParameter,
3773
3793
  createConsumptionApi,
3794
+ createMaterializedView,
3774
3795
  createProducerConfig,
3796
+ dropView,
3775
3797
  expressMiddleware,
3776
3798
  getApi,
3777
3799
  getApis,
@@ -3811,6 +3833,7 @@ export {
3811
3833
  parseCSV,
3812
3834
  parseJSON,
3813
3835
  parseJSONWithDates,
3836
+ populateTable,
3814
3837
  quoteIdentifier,
3815
3838
  sql,
3816
3839
  toQuery,