@lightdash/query-sdk 0.2787.0 → 0.2789.0
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/drillDown.d.ts +36 -0
- package/dist/drillDown.js +54 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drill-down helper — builds a new query from a clicked row.
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* const dq = drillDown({
|
|
6
|
+
* sourceQuery: revenueBySegment,
|
|
7
|
+
* metric: 'total_revenue',
|
|
8
|
+
* dimension: 'order_date',
|
|
9
|
+
* row: clickedRow,
|
|
10
|
+
* });
|
|
11
|
+
* // Pass dq to useLightdash() to execute
|
|
12
|
+
*/
|
|
13
|
+
import { QueryBuilder } from './query';
|
|
14
|
+
import type { Row } from './types';
|
|
15
|
+
export type DrillDownOptions = {
|
|
16
|
+
/** The original query builder that produced the data */
|
|
17
|
+
sourceQuery: QueryBuilder;
|
|
18
|
+
/** The metric to drill into */
|
|
19
|
+
metric: string;
|
|
20
|
+
/** The dimension to drill by */
|
|
21
|
+
dimension: string;
|
|
22
|
+
/** The clicked row — its dimension values become equality filters */
|
|
23
|
+
row: Row;
|
|
24
|
+
/** Optional label for the drill query (shown in query inspector) */
|
|
25
|
+
label?: string;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Build a drill-down query from a clicked row.
|
|
29
|
+
*
|
|
30
|
+
* Takes the source query's dimension values from the clicked row and turns
|
|
31
|
+
* them into equality filters, then queries the same metric grouped by the
|
|
32
|
+
* chosen drill-by dimension.
|
|
33
|
+
*
|
|
34
|
+
* Returns a new QueryBuilder that can be passed to useLightdash().
|
|
35
|
+
*/
|
|
36
|
+
export declare function drillDown(options: DrillDownOptions): QueryBuilder;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drill-down helper — builds a new query from a clicked row.
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* const dq = drillDown({
|
|
6
|
+
* sourceQuery: revenueBySegment,
|
|
7
|
+
* metric: 'total_revenue',
|
|
8
|
+
* dimension: 'order_date',
|
|
9
|
+
* row: clickedRow,
|
|
10
|
+
* });
|
|
11
|
+
* // Pass dq to useLightdash() to execute
|
|
12
|
+
*/
|
|
13
|
+
import { QueryBuilder } from './query';
|
|
14
|
+
/**
|
|
15
|
+
* Build a drill-down query from a clicked row.
|
|
16
|
+
*
|
|
17
|
+
* Takes the source query's dimension values from the clicked row and turns
|
|
18
|
+
* them into equality filters, then queries the same metric grouped by the
|
|
19
|
+
* chosen drill-by dimension.
|
|
20
|
+
*
|
|
21
|
+
* Returns a new QueryBuilder that can be passed to useLightdash().
|
|
22
|
+
*/
|
|
23
|
+
export function drillDown(options) {
|
|
24
|
+
const { sourceQuery, metric, dimension, row, label } = options;
|
|
25
|
+
const sourceDef = sourceQuery.build();
|
|
26
|
+
// Build equality filters from the clicked row's dimension values
|
|
27
|
+
const rowFilters = sourceDef.dimensions.map((dimFieldId) => {
|
|
28
|
+
const value = row[dimFieldId];
|
|
29
|
+
if (value === null || value === undefined) {
|
|
30
|
+
return { field: dimFieldId, operator: 'isNull' };
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
field: dimFieldId,
|
|
34
|
+
operator: 'equals',
|
|
35
|
+
value: value,
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
// Carry forward existing filters from the source query
|
|
39
|
+
const sourceFilters = sourceDef.filters.map((f) => ({
|
|
40
|
+
field: f.fieldId,
|
|
41
|
+
operator: f.operator,
|
|
42
|
+
...(f.values.length > 0
|
|
43
|
+
? { value: f.values.length === 1 ? f.values[0] : f.values }
|
|
44
|
+
: {}),
|
|
45
|
+
...(f.settings?.unitOfTime ? { unit: f.settings.unitOfTime } : {}),
|
|
46
|
+
}));
|
|
47
|
+
return new QueryBuilder(sourceDef.exploreName)
|
|
48
|
+
.label(label ?? `[Drill down] ${metric} by ${dimension}`)
|
|
49
|
+
.dimensions([dimension])
|
|
50
|
+
.metrics([metric])
|
|
51
|
+
.filters([...sourceFilters, ...rowFilters])
|
|
52
|
+
.sorts([{ field: dimension, direction: 'asc' }])
|
|
53
|
+
.limit(500);
|
|
54
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED