UNPKG

drizzle-cube

Version:

Drizzle ORM-first semantic layer with Cube.js compatibility. Type-safe analytics and dashboards with SQL injection protection.

89 lines (88 loc) 3.42 kB
import { Hono } from 'hono'; import { SemanticQuery, SecurityContext, DatabaseExecutor, DrizzleDatabase, Cube } from '../../server'; import { PostgresJsDatabase } from 'drizzle-orm/postgres-js'; import { MySql2Database } from 'drizzle-orm/mysql2'; import { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3'; export interface HonoAdapterOptions { /** * Array of cube definitions to register */ cubes: Cube[]; /** * Drizzle database instance (REQUIRED) * This is the core of drizzle-cube - Drizzle ORM integration * Accepts PostgreSQL, MySQL, or SQLite database instances */ drizzle: PostgresJsDatabase<any> | MySql2Database<any> | BetterSQLite3Database<any> | DrizzleDatabase; /** * Database schema for type inference (RECOMMENDED) * Provides full type safety for cube definitions */ schema?: any; /** * Extract security context from incoming HTTP request. * Called for EVERY API request to determine user permissions and multi-tenant isolation. * * This is your security boundary - ensure proper authentication and authorization here. * * @param c - Hono context containing the incoming HTTP request * @returns Security context with organisationId, userId, roles, etc. * * @example * extractSecurityContext: async (c) => { * // Extract JWT from Authorization header * const token = c.req.header('Authorization')?.replace('Bearer ', '') * const decoded = await verifyJWT(token) * * // Return context that will be available in all cube SQL functions * return { * organisationId: decoded.orgId, * userId: decoded.userId, * roles: decoded.roles * } * } */ extractSecurityContext: (c: any) => SecurityContext | Promise<SecurityContext>; /** * Database engine type (optional - auto-detected if not provided) */ engineType?: 'postgres' | 'mysql' | 'sqlite'; /** * CORS configuration (optional) */ cors?: { origin?: string | string[] | ((origin: string, c: any) => string | null | undefined); allowMethods?: string[]; allowHeaders?: string[]; credentials?: boolean; }; /** * API base path (default: '/cubejs-api/v1') */ basePath?: string; } /** * Create Hono routes for Cube.js-compatible API */ export declare function createCubeRoutes(options: HonoAdapterOptions): Hono<import('hono/types').BlankEnv, import('hono/types').BlankSchema, "/">; /** * Convenience function to create routes and mount them on an existing Hono app */ export declare function mountCubeRoutes(app: Hono, options: HonoAdapterOptions): Hono<import('hono/types').BlankEnv, import('hono/types').BlankSchema, "/">; /** * Create a complete Hono app with Cube.js routes * * @example * const app = createCubeApp({ * cubes: [salesCube, employeesCube], * drizzle: db, * schema, * extractSecurityContext: async (c) => { * const token = c.req.header('Authorization') * const decoded = await verifyJWT(token) * return { organisationId: decoded.orgId, userId: decoded.userId } * } * }) */ export declare function createCubeApp(options: HonoAdapterOptions): Hono<import('hono/types').BlankEnv, import('hono/types').BlankSchema, "/">; export type { SecurityContext, DatabaseExecutor, SemanticQuery, DrizzleDatabase };