ga4-export-fixer 0.9.0-dev.5 → 0.9.0-dev.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ga4-export-fixer",
3
- "version": "0.9.0-dev.5",
3
+ "version": "0.9.0-dev.7",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -320,64 +320,10 @@ ${excludedEventsSQL}`,
320
320
  } : {};
321
321
  const itemListExcludedColumns = itemListSteps ? ['_item_row_id'] : [];
322
322
 
323
- // Build enrichment-source CTEs and gather event-level join/column data.
324
- // Item-level enrichments throw "not yet supported" — they will arrive in a later release.
325
- const enrichments = mergedConfig.enrichments ?? [];
326
- const enrichmentSteps = [];
327
- const enrichmentJoins = [];
328
- const enrichmentColumns = {}; // column name → SQL expression for select.columns
329
- const enrichmentColumnNames = new Set(); // column names for excludedColumns of wildcards
330
- const enrichmentColumnOwner = {}; // column name → { i, name } for collision errors
331
- for (const [i, e] of enrichments.entries()) {
332
- const level = e.level ?? 'event';
333
- if (level === 'item') {
334
- throw new Error(
335
- `config.enrichments[${i}] uses level: 'item', which is not yet supported in this version. ` +
336
- `Item-level enrichments will ship in a future release; see design_docs/planned/data-enrichments.md.`
337
- );
338
- }
339
- const joinKeys = Array.isArray(e.joinKey) ? e.joinKey : [e.joinKey];
340
- const cteName = `enrich_${e.name}`;
341
- // Source CTE selects joinKey columns plus the requested columns. key === value
342
- // shape skips the alias clause in queryBuilder's columnsToSQL.
343
- const cteCols = {};
344
- for (const k of joinKeys) cteCols[k] = k;
345
- for (const c of e.columns) cteCols[c] = c;
346
- const sourceStep = {
347
- name: cteName,
348
- select: { columns: cteCols },
349
- from: e.source,
350
- };
351
- // Opt-in dedupe: which row wins is non-deterministic — users with strict needs
352
- // pre-aggregate in their source SQL.
353
- if (e.dedupe) {
354
- sourceStep.qualify = `row_number() over (partition by ${joinKeys.join(', ')}) = 1`;
355
- }
356
- enrichmentSteps.push(sourceStep);
357
-
358
- enrichmentJoins.push({
359
- type: 'left',
360
- table: cteName,
361
- on: `using(${joinKeys.join(', ')})`,
362
- });
363
-
364
- // Replace-or-add: each enrichment column overrides explicit select columns via JS object
365
- // spread, AND joins the excludedColumns set so it suppresses overlap with the wildcard
366
- // event_data.* / session_data.* expansions below.
367
- for (const c of e.columns) {
368
- if (enrichmentColumnNames.has(c)) {
369
- const owner = enrichmentColumnOwner[c];
370
- throw new Error(
371
- `config.enrichments[${i}] (name: '${e.name}') and config.enrichments[${owner.i}] ` +
372
- `(name: '${owner.name}') both target column '${c}'. ` +
373
- `Two enrichments cannot write the same column; rename one in source SQL or pick a different name.`
374
- );
375
- }
376
- enrichmentColumns[c] = `${cteName}.${c}`;
377
- enrichmentColumnNames.add(c);
378
- enrichmentColumnOwner[c] = { i, name: e.name };
379
- }
380
- }
323
+ // Build enrichment-source CTEs and gather event-level join/column data. Item-level
324
+ // enrichments throw "not yet supported" inside the utility — they will arrive in a later release.
325
+ const { steps: enrichmentSteps, joins: enrichmentJoins, columns: enrichmentColumns,
326
+ columnNames: enrichmentColumnNames } = utils.buildEnrichments(mergedConfig.enrichments);
381
327
  const enrichmentExcludedColumns = [...enrichmentColumnNames];
382
328
 
383
329
  // Only forward enrichment columns to each wildcard's EXCEPT input if they actually exist
package/utils.js CHANGED
@@ -560,6 +560,87 @@ const buildPassThroughs = (explicitColumns, sourceColumns) => {
560
560
  };
561
561
 
562
562
 
563
+ /**
564
+ * Builds the per-enrichment CTE definitions, JOIN clauses, and column-name mappings for the
565
+ * declarative `enrichments` feature.
566
+ *
567
+ * Pure config-to-data mapping. No knowledge of downstream CTEs or specific table modules —
568
+ * intended to be called by any table module that exposes an `enrichments` config field.
569
+ *
570
+ * Encapsulates two generation-time throws:
571
+ * - level: 'item' (not yet supported; deferred per design_docs/planned/data-enrichments.md Q15).
572
+ * - Enrichment-vs-enrichment column collisions (two enrichments targeting the same column).
573
+ *
574
+ * @param {Array<Object>} enrichments - Validated enrichment entries. Each entry has fields:
575
+ * { name, level, source, joinKey, columns, dedupe? } per data-enrichments.md Q8.
576
+ * @returns {Object} A struct with five fields:
577
+ * - `steps` — array of queryBuilder source-CTE step definitions (one `enrich_<name>` per entry).
578
+ * - `joins` — array of LEFT JOIN clauses to attach downstream (one per entry).
579
+ * - `columns` — map of `{ <enrichmentColumn>: 'enrich_<name>.<col>' }` for spreading into a
580
+ * downstream SELECT's `select.columns`.
581
+ * - `columnNames` — Set of all enrichment column names (used by callers for overlap detection
582
+ * against downstream CTEs).
583
+ * - `columnOwner` — map of `{ <column>: { i, name } }` recording which enrichment owns each
584
+ * column; preserved for diagnostics.
585
+ *
586
+ * @throws {Error} If any entry has `level: 'item'` (with a pointer to data-enrichments.md).
587
+ * @throws {Error} If two enrichments target the same column name (with both enrichment names).
588
+ *
589
+ * @example
590
+ * const { steps, joins, columns, columnNames } = buildEnrichments(config.enrichments);
591
+ */
592
+ const buildEnrichments = (enrichments) => {
593
+ const steps = [];
594
+ const joins = [];
595
+ const columns = {};
596
+ const columnNames = new Set();
597
+ const columnOwner = {};
598
+
599
+ for (const [i, e] of (enrichments ?? []).entries()) {
600
+ const level = e.level ?? 'event';
601
+ if (level === 'item') {
602
+ throw new Error(
603
+ `config.enrichments[${i}] uses level: 'item', which is not yet supported in this version. ` +
604
+ `Item-level enrichments will ship in a future release; see design_docs/planned/data-enrichments.md.`
605
+ );
606
+ }
607
+ const joinKeys = Array.isArray(e.joinKey) ? e.joinKey : [e.joinKey];
608
+ const cteName = `enrich_${e.name}`;
609
+
610
+ // Source CTE selects joinKey columns plus the requested columns. key === value
611
+ // shape skips the alias clause in queryBuilder's columnsToSQL.
612
+ const cteCols = {};
613
+ for (const k of joinKeys) cteCols[k] = k;
614
+ for (const c of e.columns) cteCols[c] = c;
615
+ const sourceStep = { name: cteName, select: { columns: cteCols }, from: e.source };
616
+ // Opt-in dedupe: which row wins is non-deterministic — users with strict needs
617
+ // pre-aggregate in their source SQL.
618
+ if (e.dedupe) {
619
+ sourceStep.qualify = `row_number() over (partition by ${joinKeys.join(', ')}) = 1`;
620
+ }
621
+ steps.push(sourceStep);
622
+
623
+ joins.push({ type: 'left', table: cteName, on: `using(${joinKeys.join(', ')})` });
624
+
625
+ for (const c of e.columns) {
626
+ if (columnNames.has(c)) {
627
+ const owner = columnOwner[c];
628
+ throw new Error(
629
+ `config.enrichments[${i}] (name: '${e.name}') and config.enrichments[${owner.i}] ` +
630
+ `(name: '${owner.name}') both target column '${c}'. ` +
631
+ `Two enrichments cannot write the same column; rename one in source SQL or pick a different name.`
632
+ );
633
+ }
634
+ columns[c] = `${cteName}.${c}`;
635
+ columnNames.add(c);
636
+ columnOwner[c] = { i, name: e.name };
637
+ }
638
+ }
639
+
640
+ return { steps, joins, columns, columnNames, columnOwner };
641
+ };
642
+
643
+
563
644
  /**
564
645
  * Processes a date input string and returns a corresponding SQL date casting expression,
565
646
  * or passes through BigQuery SQL statements as-is.
@@ -636,6 +717,7 @@ module.exports = {
636
717
  setDataformContext,
637
718
  selectOtherColumns,
638
719
  buildPassThroughs,
720
+ buildEnrichments,
639
721
  processDate,
640
722
  getDatasetName
641
723
  };