Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | /**
* Cube, dimension, measure, and join definitions
* Core semantic layer building blocks
*/
import type { SQL, AnyColumn, Table, Subquery, View } from 'drizzle-orm'
import type {
SecurityContext,
DrizzleDatabase,
QueryResult,
MeasureType,
DimensionType
} from './core'
import type { SemanticQuery } from './query'
/**
* Any queryable relation that can be used in FROM/JOIN clauses
* Supports tables, views, subqueries, and raw SQL expressions
*/
export type QueryableRelation = Table | View | Subquery | SQL
/**
* Base query definition that can be extended dynamically
* Returns just the FROM/JOIN/WHERE setup, not a complete SELECT
*/
export interface BaseQueryDefinition {
/** Main table to query from */
from: QueryableRelation
/** Optional joins to other tables */
joins?: Array<{
table: QueryableRelation
on: SQL
type?: 'left' | 'right' | 'inner' | 'full'
}>
/** Base WHERE conditions (typically security context filtering) */
where?: SQL
}
/**
* Query context passed to cube SQL functions
* Provides access to database, schema, and security context
*/
export interface QueryContext {
/** Drizzle database instance */
db: DrizzleDatabase
/** Database schema (tables, columns, etc.) */
schema?: any
/** Security context for filtering */
securityContext: SecurityContext
/** The semantic query being executed */
query?: SemanticQuery
/** The compiled cube being queried */
cube?: Cube
}
/**
* Multi-cube query context for cross-cube operations
*/
export interface MultiCubeQueryContext
extends QueryContext {
/** Available cubes for cross-cube operations */
cubes: Map<string, Cube>
/** Current cube being processed */
currentCube: Cube
}
/**
* Cube definition focused on Drizzle query building
*/
export interface Cube {
name: string
title?: string
description?: string
/**
* Base query setup - returns the foundation that can be extended
* Should return FROM/JOIN/WHERE setup, NOT a complete SELECT
*/
sql: (ctx: QueryContext) => BaseQueryDefinition
/** Cube dimensions using direct column references */
dimensions: Record<string, Dimension>
/** Cube measures using direct column references */
measures: Record<string, Measure>
/** Optional joins to other cubes for multi-cube queries */
joins?: Record<string, CubeJoin>
/** Whether cube is publicly accessible */
public?: boolean
/** SQL alias for the cube */
sqlAlias?: string
/** Data source identifier */
dataSource?: string
/** Additional metadata */
meta?: Record<string, any>
}
/**
* Dimension definition
*/
export interface Dimension {
name: string
title?: string
description?: string
type: DimensionType
/** Direct column reference or SQL expression */
sql: AnyColumn | SQL | ((ctx: QueryContext) => AnyColumn | SQL)
/** Whether this is a primary key */
primaryKey?: boolean
/** Whether to show in UI */
shown?: boolean
/** Display format */
format?: string
/** Additional metadata */
meta?: Record<string, any>
}
/**
* Measure definition
*/
export interface Measure {
name: string
title?: string
description?: string
type: MeasureType
/**
* Column to aggregate or SQL expression
* Optional for calculated measures (type: 'calculated') which use calculatedSql instead
*/
sql?: AnyColumn | SQL | ((ctx: QueryContext) => AnyColumn | SQL)
/** Display format */
format?: string
/** Whether to show in UI */
shown?: boolean
/** Filters applied to this measure */
filters?: Array<(ctx: QueryContext) => SQL>
/** Rolling window configuration */
rollingWindow?: {
trailing?: string
leading?: string
offset?: string
}
/**
* Calculated measure template with {member} references
* Only used when type === 'calculated'
* Example: "1.0 * {completed} / NULLIF({total}, 0)"
*/
calculatedSql?: string
/**
* List of measure dependencies for calculated measures
* Auto-detected from calculatedSql if not provided
* Example: ['completed', 'total']
*/
dependencies?: string[]
/** Additional metadata */
meta?: Record<string, any>
}
/**
* Relationship types supported by cube joins
*/
export type CubeRelationship = 'belongsTo' | 'hasOne' | 'hasMany' | 'belongsToMany'
/**
* Type-safe cube join definition with lazy loading support
*/
export interface CubeJoin {
/** Target cube reference - lazy loaded to avoid circular dependencies */
targetCube: Cube | (() => Cube)
/** Semantic relationship - determines join behavior */
relationship: CubeRelationship
/** Array of join conditions - supports multi-column joins */
on: Array<{
/** Column from source cube */
source: AnyColumn
/** Column from target cube */
target: AnyColumn
/** Comparison operator - defaults to eq */
as?: (source: AnyColumn, target: AnyColumn) => SQL
}>
/** Override default SQL join type (derived from relationship) */
sqlJoinType?: 'inner' | 'left' | 'right' | 'full'
/**
* Many-to-many relationship configuration through a junction table
* Only used when relationship is 'belongsToMany'
*/
through?: {
/** Junction/join table (Drizzle table reference) */
table: Table
/** Join conditions from source cube to junction table */
sourceKey: Array<{
source: AnyColumn
target: AnyColumn
as?: (source: AnyColumn, target: AnyColumn) => SQL
}>
/** Join conditions from junction table to target cube */
targetKey: Array<{
source: AnyColumn
target: AnyColumn
as?: (source: AnyColumn, target: AnyColumn) => SQL
}>
/** Optional security context SQL for junction table */
securitySql?: (securityContext: SecurityContext) => SQL | SQL[]
}
}
/**
* Compiled cube with execution function
*/
export interface CompiledCube
extends Cube {
/** Execute a query against this cube */
queryFn: (query: SemanticQuery, securityContext: SecurityContext) => Promise<QueryResult>
}
/**
* Join key information for CTE joins
* Describes how a CTE should be joined to the main query
*/
export interface JoinKeyInfo {
/** Column name in the source table */
sourceColumn: string
/** Column name in the target table (CTE) */
targetColumn: string
/** Optional Drizzle column object for source */
sourceColumnObj?: AnyColumn
/** Optional Drizzle column object for target */
targetColumnObj?: AnyColumn
}
/**
* Pre-aggregation CTE information
* Describes a Common Table Expression used for pre-aggregating hasMany relationships
*/
export interface PreAggregationCTEInfo {
/** The cube being pre-aggregated */
cube: Cube
/** Table alias for this cube in the main query */
alias: string
/** CTE alias (WITH clause name) */
cteAlias: string
/** Join keys to connect CTE back to main query */
joinKeys: JoinKeyInfo[]
/** List of measure names included in this CTE */
measures: string[]
}
/**
* Unified Query Plan for both single and multi-cube queries
* - For single-cube queries: joinCubes array is empty
* - For multi-cube queries: joinCubes contains the additional cubes to join
* - selections, whereConditions, and groupByFields are populated by QueryBuilder
*/
export interface QueryPlan {
/** Primary cube that drives the query */
primaryCube: Cube
/** Additional cubes to join (empty for single-cube queries) */
joinCubes: Array<{
cube: Cube
alias: string
joinType: 'inner' | 'left' | 'right' | 'full'
joinCondition: SQL
/** Junction table information for belongsToMany relationships */
junctionTable?: {
table: Table
alias: string
joinType: 'inner' | 'left' | 'right' | 'full'
joinCondition: SQL
/** Optional security SQL function to apply to junction table */
securitySql?: (securityContext: SecurityContext) => SQL | SQL[]
}
}>
/** Combined field selections across all cubes (built by QueryBuilder) */
selections: Record<string, SQL | AnyColumn>
/** WHERE conditions for the entire query (built by QueryBuilder) */
whereConditions: SQL[]
/** GROUP BY fields if aggregations are present (built by QueryBuilder) */
groupByFields: (SQL | AnyColumn)[]
/** Pre-aggregation CTEs for hasMany relationships to prevent fan-out */
preAggregationCTEs?: PreAggregationCTEInfo[]
}
/**
* Utility type for cube definition with schema inference
*/
export type CubeDefinition = Omit<Cube, 'name'> & {
name?: string
}
/**
* Helper type for creating type-safe cubes
*/
export interface CubeDefiner {
<TName extends string>(
name: TName,
definition: CubeDefinition
): Cube & { name: TName }
} |