@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.js CHANGED
@@ -177,11 +177,184 @@ var init_dataModelTypes = __esm({
177
177
  }
178
178
  });
179
179
 
180
- // src/dataModels/types.ts
180
+ // src/sqlHelpers.ts
181
+ function sql(strings, ...values) {
182
+ return new Sql(strings, values);
183
+ }
184
+ function createClickhouseParameter(parameterIndex, value) {
185
+ return `{p${parameterIndex}:${mapToClickHouseType(value)}}`;
186
+ }
187
+ function emptyIfUndefined(value) {
188
+ return value === void 0 ? "" : value;
189
+ }
190
+ var quoteIdentifier, isTable, isView, isColumn, instanceofSql, Sql, toStaticQuery, toQuery, toQueryPreview, getValueFromParameter, mapToClickHouseType;
191
+ var init_sqlHelpers = __esm({
192
+ "src/sqlHelpers.ts"() {
193
+ "use strict";
194
+ quoteIdentifier = (name) => {
195
+ return name.startsWith("`") && name.endsWith("`") ? name : `\`${name}\``;
196
+ };
197
+ isTable = (value) => typeof value === "object" && value !== null && "kind" in value && value.kind === "OlapTable";
198
+ isView = (value) => typeof value === "object" && value !== null && "kind" in value && value.kind === "View";
199
+ isColumn = (value) => typeof value === "object" && value !== null && !("kind" in value) && "name" in value && "annotations" in value;
200
+ instanceofSql = (value) => typeof value === "object" && "values" in value && "strings" in value;
201
+ Sql = class {
202
+ values;
203
+ strings;
204
+ constructor(rawStrings, rawValues) {
205
+ if (rawStrings.length - 1 !== rawValues.length) {
206
+ if (rawStrings.length === 0) {
207
+ throw new TypeError("Expected at least 1 string");
208
+ }
209
+ throw new TypeError(
210
+ `Expected ${rawStrings.length} strings to have ${rawStrings.length - 1} values`
211
+ );
212
+ }
213
+ const valuesLength = rawValues.reduce(
214
+ (len, value) => len + (instanceofSql(value) ? value.values.length : isColumn(value) || isTable(value) || isView(value) ? 0 : 1),
215
+ 0
216
+ );
217
+ this.values = new Array(valuesLength);
218
+ this.strings = new Array(valuesLength + 1);
219
+ this.strings[0] = rawStrings[0];
220
+ let i = 0, pos = 0;
221
+ while (i < rawValues.length) {
222
+ const child = rawValues[i++];
223
+ const rawString = rawStrings[i];
224
+ if (instanceofSql(child)) {
225
+ this.strings[pos] += child.strings[0];
226
+ let childIndex = 0;
227
+ while (childIndex < child.values.length) {
228
+ this.values[pos++] = child.values[childIndex++];
229
+ this.strings[pos] = child.strings[childIndex];
230
+ }
231
+ this.strings[pos] += rawString;
232
+ } else if (isColumn(child)) {
233
+ const aggregationFunction = child.annotations.find(
234
+ ([k, _]) => k === "aggregationFunction"
235
+ );
236
+ if (aggregationFunction !== void 0) {
237
+ this.strings[pos] += `${aggregationFunction[1].functionName}Merge(\`${child.name}\`)`;
238
+ } else {
239
+ this.strings[pos] += `\`${child.name}\``;
240
+ }
241
+ this.strings[pos] += rawString;
242
+ } else if (isTable(child)) {
243
+ if (child.config.database) {
244
+ this.strings[pos] += `\`${child.config.database}\`.\`${child.name}\``;
245
+ } else {
246
+ this.strings[pos] += `\`${child.name}\``;
247
+ }
248
+ this.strings[pos] += rawString;
249
+ } else if (isView(child)) {
250
+ this.strings[pos] += `\`${child.name}\``;
251
+ this.strings[pos] += rawString;
252
+ } else {
253
+ this.values[pos++] = child;
254
+ this.strings[pos] = rawString;
255
+ }
256
+ }
257
+ }
258
+ };
259
+ toStaticQuery = (sql3) => {
260
+ const [query, params] = toQuery(sql3);
261
+ if (Object.keys(params).length !== 0) {
262
+ throw new Error(
263
+ "Dynamic SQL is not allowed in the select statement in view creation."
264
+ );
265
+ }
266
+ return query;
267
+ };
268
+ toQuery = (sql3) => {
269
+ const parameterizedStubs = sql3.values.map(
270
+ (v, i) => createClickhouseParameter(i, v)
271
+ );
272
+ const query = sql3.strings.map(
273
+ (s, i) => s != "" ? `${s}${emptyIfUndefined(parameterizedStubs[i])}` : ""
274
+ ).join("");
275
+ const query_params = sql3.values.reduce(
276
+ (acc, v, i) => ({
277
+ ...acc,
278
+ [`p${i}`]: getValueFromParameter(v)
279
+ }),
280
+ {}
281
+ );
282
+ return [query, query_params];
283
+ };
284
+ toQueryPreview = (sql3) => {
285
+ try {
286
+ const formatValue = (v) => {
287
+ if (Array.isArray(v)) {
288
+ const [type, val] = v;
289
+ if (type === "Identifier") {
290
+ return `\`${String(val)}\``;
291
+ }
292
+ return `[${v.map((x) => formatValue(x)).join(", ")}]`;
293
+ }
294
+ if (v === null || v === void 0) return "NULL";
295
+ if (typeof v === "string") return `'${v.replace(/'/g, "''")}'`;
296
+ if (typeof v === "number") return String(v);
297
+ if (typeof v === "boolean") return v ? "true" : "false";
298
+ if (v instanceof Date)
299
+ return `'${v.toISOString().replace("T", " ").slice(0, 19)}'`;
300
+ try {
301
+ return JSON.stringify(v);
302
+ } catch {
303
+ return String(v);
304
+ }
305
+ };
306
+ let out = sql3.strings[0] ?? "";
307
+ for (let i = 0; i < sql3.values.length; i++) {
308
+ const val = getValueFromParameter(sql3.values[i]);
309
+ out += formatValue(val);
310
+ out += sql3.strings[i + 1] ?? "";
311
+ }
312
+ return out.replace(/\s+/g, " ").trim();
313
+ } catch (error) {
314
+ console.log(`toQueryPreview error: ${error}`);
315
+ return "/* query preview unavailable */";
316
+ }
317
+ };
318
+ getValueFromParameter = (value) => {
319
+ if (Array.isArray(value)) {
320
+ const [type, val] = value;
321
+ if (type === "Identifier") return val;
322
+ }
323
+ return value;
324
+ };
325
+ mapToClickHouseType = (value) => {
326
+ if (typeof value === "number") {
327
+ return Number.isInteger(value) ? "Int" : "Float";
328
+ }
329
+ if (typeof value === "boolean") return "Bool";
330
+ if (value instanceof Date) return "DateTime";
331
+ if (Array.isArray(value)) {
332
+ const [type, _] = value;
333
+ return type;
334
+ }
335
+ return "String";
336
+ };
337
+ }
338
+ });
339
+
340
+ // src/blocks/helpers.ts
341
+ function dropView(name) {
342
+ return `DROP VIEW IF EXISTS ${quoteIdentifier(name)}`.trim();
343
+ }
344
+ function createMaterializedView(options) {
345
+ return `CREATE MATERIALIZED VIEW IF NOT EXISTS ${quoteIdentifier(options.name)}
346
+ TO ${quoteIdentifier(options.destinationTable)}
347
+ AS ${options.select}`.trim();
348
+ }
349
+ function populateTable(options) {
350
+ return `INSERT INTO ${quoteIdentifier(options.destinationTable)}
351
+ ${options.select}`.trim();
352
+ }
181
353
  var ClickHouseEngines;
182
- var init_types = __esm({
183
- "src/dataModels/types.ts"() {
354
+ var init_helpers = __esm({
355
+ "src/blocks/helpers.ts"() {
184
356
  "use strict";
357
+ init_sqlHelpers();
185
358
  ClickHouseEngines = /* @__PURE__ */ ((ClickHouseEngines2) => {
186
359
  ClickHouseEngines2["MergeTree"] = "MergeTree";
187
360
  ClickHouseEngines2["ReplacingMergeTree"] = "ReplacingMergeTree";
@@ -572,166 +745,6 @@ var init_internal = __esm({
572
745
  }
573
746
  });
574
747
 
575
- // src/sqlHelpers.ts
576
- function sql(strings, ...values) {
577
- return new Sql(strings, values);
578
- }
579
- function createClickhouseParameter(parameterIndex, value) {
580
- return `{p${parameterIndex}:${mapToClickHouseType(value)}}`;
581
- }
582
- function emptyIfUndefined(value) {
583
- return value === void 0 ? "" : value;
584
- }
585
- var quoteIdentifier, isTable, isView, isColumn, instanceofSql, Sql, toStaticQuery, toQuery, toQueryPreview, getValueFromParameter, mapToClickHouseType;
586
- var init_sqlHelpers = __esm({
587
- "src/sqlHelpers.ts"() {
588
- "use strict";
589
- quoteIdentifier = (name) => {
590
- return name.startsWith("`") && name.endsWith("`") ? name : `\`${name}\``;
591
- };
592
- isTable = (value) => typeof value === "object" && value !== null && "kind" in value && value.kind === "OlapTable";
593
- isView = (value) => typeof value === "object" && value !== null && "kind" in value && value.kind === "View";
594
- isColumn = (value) => typeof value === "object" && value !== null && !("kind" in value) && "name" in value && "annotations" in value;
595
- instanceofSql = (value) => typeof value === "object" && "values" in value && "strings" in value;
596
- Sql = class {
597
- values;
598
- strings;
599
- constructor(rawStrings, rawValues) {
600
- if (rawStrings.length - 1 !== rawValues.length) {
601
- if (rawStrings.length === 0) {
602
- throw new TypeError("Expected at least 1 string");
603
- }
604
- throw new TypeError(
605
- `Expected ${rawStrings.length} strings to have ${rawStrings.length - 1} values`
606
- );
607
- }
608
- const valuesLength = rawValues.reduce(
609
- (len, value) => len + (instanceofSql(value) ? value.values.length : isColumn(value) || isTable(value) || isView(value) ? 0 : 1),
610
- 0
611
- );
612
- this.values = new Array(valuesLength);
613
- this.strings = new Array(valuesLength + 1);
614
- this.strings[0] = rawStrings[0];
615
- let i = 0, pos = 0;
616
- while (i < rawValues.length) {
617
- const child = rawValues[i++];
618
- const rawString = rawStrings[i];
619
- if (instanceofSql(child)) {
620
- this.strings[pos] += child.strings[0];
621
- let childIndex = 0;
622
- while (childIndex < child.values.length) {
623
- this.values[pos++] = child.values[childIndex++];
624
- this.strings[pos] = child.strings[childIndex];
625
- }
626
- this.strings[pos] += rawString;
627
- } else if (isColumn(child)) {
628
- const aggregationFunction = child.annotations.find(
629
- ([k, _]) => k === "aggregationFunction"
630
- );
631
- if (aggregationFunction !== void 0) {
632
- this.strings[pos] += `${aggregationFunction[1].functionName}Merge(\`${child.name}\`)`;
633
- } else {
634
- this.strings[pos] += `\`${child.name}\``;
635
- }
636
- this.strings[pos] += rawString;
637
- } else if (isTable(child)) {
638
- if (child.config.database) {
639
- this.strings[pos] += `\`${child.config.database}\`.\`${child.name}\``;
640
- } else {
641
- this.strings[pos] += `\`${child.name}\``;
642
- }
643
- this.strings[pos] += rawString;
644
- } else if (isView(child)) {
645
- this.strings[pos] += `\`${child.name}\``;
646
- this.strings[pos] += rawString;
647
- } else {
648
- this.values[pos++] = child;
649
- this.strings[pos] = rawString;
650
- }
651
- }
652
- }
653
- };
654
- toStaticQuery = (sql3) => {
655
- const [query, params] = toQuery(sql3);
656
- if (Object.keys(params).length !== 0) {
657
- throw new Error(
658
- "Dynamic SQL is not allowed in the select statement in view creation."
659
- );
660
- }
661
- return query;
662
- };
663
- toQuery = (sql3) => {
664
- const parameterizedStubs = sql3.values.map(
665
- (v, i) => createClickhouseParameter(i, v)
666
- );
667
- const query = sql3.strings.map(
668
- (s, i) => s != "" ? `${s}${emptyIfUndefined(parameterizedStubs[i])}` : ""
669
- ).join("");
670
- const query_params = sql3.values.reduce(
671
- (acc, v, i) => ({
672
- ...acc,
673
- [`p${i}`]: getValueFromParameter(v)
674
- }),
675
- {}
676
- );
677
- return [query, query_params];
678
- };
679
- toQueryPreview = (sql3) => {
680
- try {
681
- const formatValue = (v) => {
682
- if (Array.isArray(v)) {
683
- const [type, val] = v;
684
- if (type === "Identifier") {
685
- return `\`${String(val)}\``;
686
- }
687
- return `[${v.map((x) => formatValue(x)).join(", ")}]`;
688
- }
689
- if (v === null || v === void 0) return "NULL";
690
- if (typeof v === "string") return `'${v.replace(/'/g, "''")}'`;
691
- if (typeof v === "number") return String(v);
692
- if (typeof v === "boolean") return v ? "true" : "false";
693
- if (v instanceof Date)
694
- return `'${v.toISOString().replace("T", " ").slice(0, 19)}'`;
695
- try {
696
- return JSON.stringify(v);
697
- } catch {
698
- return String(v);
699
- }
700
- };
701
- let out = sql3.strings[0] ?? "";
702
- for (let i = 0; i < sql3.values.length; i++) {
703
- const val = getValueFromParameter(sql3.values[i]);
704
- out += formatValue(val);
705
- out += sql3.strings[i + 1] ?? "";
706
- }
707
- return out.replace(/\s+/g, " ").trim();
708
- } catch (error) {
709
- console.log(`toQueryPreview error: ${error}`);
710
- return "/* query preview unavailable */";
711
- }
712
- };
713
- getValueFromParameter = (value) => {
714
- if (Array.isArray(value)) {
715
- const [type, val] = value;
716
- if (type === "Identifier") return val;
717
- }
718
- return value;
719
- };
720
- mapToClickHouseType = (value) => {
721
- if (typeof value === "number") {
722
- return Number.isInteger(value) ? "Int" : "Float";
723
- }
724
- if (typeof value === "boolean") return "Bool";
725
- if (value instanceof Date) return "DateTime";
726
- if (Array.isArray(value)) {
727
- const [type, _] = value;
728
- return type;
729
- }
730
- return "String";
731
- };
732
- }
733
- });
734
-
735
748
  // src/config/configFile.ts
736
749
  async function findConfigFile(startDir = process.cwd()) {
737
750
  const fs2 = await import("fs");
@@ -923,7 +936,7 @@ var init_olapTable = __esm({
923
936
  "use strict";
924
937
  init_typedBase();
925
938
  init_dataModelTypes();
926
- init_types();
939
+ init_helpers();
927
940
  init_internal();
928
941
  import_node_stream = require("stream");
929
942
  import_node_crypto = require("crypto");
@@ -2218,7 +2231,7 @@ var init_ingestPipeline = __esm({
2218
2231
  init_stream();
2219
2232
  init_olapTable();
2220
2233
  init_ingestApi();
2221
- init_types();
2234
+ init_helpers();
2222
2235
  IngestPipeline = class extends TypedBase {
2223
2236
  /**
2224
2237
  * The OLAP table component of the pipeline, if configured.
@@ -2491,7 +2504,7 @@ var requireTargetTableName, MaterializedView;
2491
2504
  var init_materializedView = __esm({
2492
2505
  "src/dmv2/sdk/materializedView.ts"() {
2493
2506
  "use strict";
2494
- init_types();
2507
+ init_helpers();
2495
2508
  init_sqlHelpers();
2496
2509
  init_olapTable();
2497
2510
  init_internal();
@@ -2910,7 +2923,6 @@ var init_dmv2 = __esm({
2910
2923
  "src/dmv2/index.ts"() {
2911
2924
  "use strict";
2912
2925
  init_olapTable();
2913
- init_types();
2914
2926
  init_stream();
2915
2927
  init_workflow();
2916
2928
  init_ingestApi();
@@ -2926,6 +2938,13 @@ var init_dmv2 = __esm({
2926
2938
  }
2927
2939
  });
2928
2940
 
2941
+ // src/dataModels/types.ts
2942
+ var init_types = __esm({
2943
+ "src/dataModels/types.ts"() {
2944
+ "use strict";
2945
+ }
2946
+ });
2947
+
2929
2948
  // src/browserCompatible.ts
2930
2949
  var init_browserCompatible = __esm({
2931
2950
  "src/browserCompatible.ts"() {
@@ -3054,7 +3073,7 @@ function joinQueries({
3054
3073
  );
3055
3074
  }
3056
3075
  var import_client2, import_node_crypto3, import_perf_hooks, fs, MooseClient, QueryClient, WorkflowClient, ApiHelpers, ConsumptionHelpers;
3057
- var init_helpers = __esm({
3076
+ var init_helpers2 = __esm({
3058
3077
  "src/consumption-apis/helpers.ts"() {
3059
3078
  "use strict";
3060
3079
  import_client2 = require("@temporalio/client");
@@ -3181,11 +3200,11 @@ var init_helpers = __esm({
3181
3200
  }
3182
3201
  async getWorkflowConfig(name) {
3183
3202
  const workflows = await getWorkflows();
3184
- const workflow = workflows.get(name);
3185
- if (workflow) {
3203
+ const dmv2Workflow = workflows.get(name);
3204
+ if (dmv2Workflow) {
3186
3205
  return {
3187
- retries: workflow.config.retries || 3,
3188
- timeout: workflow.config.timeout || "1h"
3206
+ retries: dmv2Workflow.config.retries || 3,
3207
+ timeout: dmv2Workflow.config.timeout || "1h"
3189
3208
  };
3190
3209
  }
3191
3210
  throw new Error(`Workflow config not found for ${name}`);
@@ -3262,7 +3281,7 @@ var init_runner = __esm({
3262
3281
  "src/consumption-apis/runner.ts"() {
3263
3282
  "use strict";
3264
3283
  init_commons();
3265
- init_helpers();
3284
+ init_helpers2();
3266
3285
  jose = __toESM(require("jose"));
3267
3286
  init_cluster_utils();
3268
3287
  init_sqlHelpers();
@@ -3612,7 +3631,7 @@ var standaloneUtils, initPromise2, toClientConfig;
3612
3631
  var init_standalone = __esm({
3613
3632
  "src/consumption-apis/standalone.ts"() {
3614
3633
  "use strict";
3615
- init_helpers();
3634
+ init_helpers2();
3616
3635
  init_commons();
3617
3636
  init_sqlHelpers();
3618
3637
  standaloneUtils = null;
@@ -3767,7 +3786,9 @@ __export(index_exports, {
3767
3786
  createApi: () => createApi,
3768
3787
  createClickhouseParameter: () => createClickhouseParameter,
3769
3788
  createConsumptionApi: () => createConsumptionApi,
3789
+ createMaterializedView: () => createMaterializedView,
3770
3790
  createProducerConfig: () => createProducerConfig,
3791
+ dropView: () => dropView,
3771
3792
  expressMiddleware: () => expressMiddleware,
3772
3793
  getApi: () => getApi,
3773
3794
  getApis: () => getApis,
@@ -3807,6 +3828,7 @@ __export(index_exports, {
3807
3828
  parseCSV: () => parseCSV,
3808
3829
  parseJSON: () => parseJSON,
3809
3830
  parseJSONWithDates: () => parseJSONWithDates,
3831
+ populateTable: () => populateTable,
3810
3832
  quoteIdentifier: () => quoteIdentifier,
3811
3833
  sql: () => sql,
3812
3834
  toQuery: () => toQuery,
@@ -3817,14 +3839,15 @@ module.exports = __toCommonJS(index_exports);
3817
3839
  var init_index = __esm({
3818
3840
  "src/index.ts"() {
3819
3841
  init_browserCompatible();
3842
+ init_helpers();
3820
3843
  init_commons();
3821
3844
  init_secrets();
3822
- init_helpers();
3845
+ init_helpers2();
3823
3846
  init_webAppHelpers();
3824
3847
  init_task();
3825
3848
  init_runner();
3826
3849
  init_redisClient();
3827
- init_helpers();
3850
+ init_helpers2();
3828
3851
  init_standalone();
3829
3852
  init_sqlHelpers();
3830
3853
  init_utilities();
@@ -3875,7 +3898,9 @@ init_index();
3875
3898
  createApi,
3876
3899
  createClickhouseParameter,
3877
3900
  createConsumptionApi,
3901
+ createMaterializedView,
3878
3902
  createProducerConfig,
3903
+ dropView,
3879
3904
  expressMiddleware,
3880
3905
  getApi,
3881
3906
  getApis,
@@ -3915,6 +3940,7 @@ init_index();
3915
3940
  parseCSV,
3916
3941
  parseJSON,
3917
3942
  parseJSONWithDates,
3943
+ populateTable,
3918
3944
  quoteIdentifier,
3919
3945
  sql,
3920
3946
  toQuery,