The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
import type { QueryQuestionResult } from "embedding-sdk-bundle/lib/query-question";
import type {
FieldSchema,
MeasureSchema,
MetricSchema,
QuestionSchema,
SchemaColumn,
SchemaJavaScriptType,
SegmentSchema,
TableFieldSchema,
TableSchema,
} from "embedding-sdk-shared/lib/create-metabase-query/schema";
import type { DatasetColumn, RowValues } from "metabase-types/api";
export type {
FieldSchema,
MeasureSchema,
MetricSchema,
QuestionSchema,
SchemaColumn,
SchemaJavaScriptType,
SegmentSchema,
TableFieldSchema,
TableSchema,
};
/**
* A single value returned by Metabase query results or action responses.
*/
export type RowValue = string | number | null | boolean | object;
export type SchemaValue<TColumn extends SchemaColumn> =
TColumn["jsType"] extends "string"
? string | null
: TColumn["jsType"] extends "number"
? number | null
: TColumn["jsType"] extends "boolean"
? boolean | null
: TColumn["jsType"] extends "Date"
? string | null
: RowValue | null;
/** @notExported SchemaValue */
export type SchemaRow<TSchema extends { columns: readonly SchemaColumn[] }> = {
[TColumn in TSchema["columns"][number] as TColumn["name"]]: SchemaValue<TColumn>;
};
/**
* @notExported DatasetColumn
* @notExported RowValues
*/
export type QueryData<TRow> = {
id?: QueryQuestionResult["id"] | null;
name?: QueryQuestionResult["name"] | null;
description?: QueryQuestionResult["description"];
entityId?: QueryQuestionResult["entityId"] | null;
rowCount: number | null;
runningTime: number | null;
columns: DatasetColumn[];
rows: TRow[];
rawRows: RowValues[];
};
export type InferSchema<TSchema, TFallback> = TSchema extends {
columns: readonly SchemaColumn[];
}
? SchemaRow<TSchema>
: TFallback;
export function getSchemaId<TEntity extends { id: unknown }>(
entity: TEntity | TEntity["id"] | null,
): TEntity["id"] | null {
if (entity == null) {
return null;
}
if (typeof entity === "object" && "id" in entity) {
// `TEntity["id"]` is unconstrained, so it may itself be an object with an
// `id`: the guard cannot narrow the `TEntity | TEntity["id"]` union for TS.
return entity.id as TEntity["id"];
}
// Not an entity object, so it is already the id — but TS still cannot rule
// `TEntity` out of the union at the type level.
return entity as TEntity["id"];
}
export function mapRowsToObjects<TRow>(
columns: DatasetColumn[],
rows: RowValues[],
): TRow[] {
return rows.map((row) => {
// Rows are keyed by column names known only at runtime; `TRow` is the
// schema-derived shape the caller expects, so the two can't be tied in TS.
return columns.reduce<Record<string, RowValue>>((acc, column, index) => {
const key = column.name || column.display_name || `column_${index}`;
acc[key] = row[index];
return acc;
}, {}) as TRow;
});
}