drizzle-cube
Version:
Drizzle ORM-first semantic layer with Cube.js compatibility. Type-safe analytics and dashboards with SQL injection protection.
2 lines (1 loc) • 6 kB
JavaScript
"use strict";var w=Object.create;var q=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var $=Object.getPrototypeOf,P=Object.prototype.hasOwnProperty;var L=(i,s,p,g)=>{if(s&&typeof s=="object"||typeof s=="function")for(let y of j(s))!P.call(i,y)&&y!==p&&q(i,y,{get:()=>s[y],enumerable:!(g=S(s,y))||g.enumerable});return i};var M=(i,s,p)=>(p=i!=null?w($(i)):{},L(s||!i||!i.__esModule?q(p,"default",{value:i,enumerable:!0}):p,i));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("../compiler-Du1HVm5_.cjs"),h=function(s,p,g){const{cubes:y,drizzle:E,schema:v,extractSecurityContext:m,engineType:x,cors:R,basePath:l="/cubejs-api/v1",bodyLimit:b=10485760,cache:Q}=p;if(!y||y.length===0)return g(new Error("At least one cube must be provided in the cubes array"));R&&s.register(import("@fastify/cors"),R),s.addHook("onRequest",async(e,t)=>{e.method==="POST"&&(e.body=void 0)});const n=new a.SemanticLayerCompiler({drizzle:E,schema:v,engineType:x,cache:Q});y.forEach(e=>{n.registerCube(e)}),s.post(`${l}/load`,{bodyLimit:b,schema:{body:{type:"object",additionalProperties:!0}}},async(e,t)=>{try{const r=e.body,o=r.query||r,u=await m(e),c=n.validateQuery(o);if(!c.isValid)return t.status(400).send(a.formatErrorResponse(`Query validation failed: ${c.errors.join(", ")}`,400));const d=await n.executeMultiCubeQuery(o,u);return a.formatCubeResponse(o,d,n)}catch(r){return e.log.error(r,"Query execution error"),t.status(500).send(a.formatErrorResponse(r instanceof Error?r.message:"Query execution failed",500))}}),s.get(`${l}/load`,{schema:{querystring:{type:"object",properties:{query:{type:"string"}},required:["query"]}}},async(e,t)=>{try{const{query:r}=e.query;let o;try{o=JSON.parse(r)}catch{return t.status(400).send(a.formatErrorResponse("Invalid JSON in query parameter",400))}const u=await m(e),c=n.validateQuery(o);if(!c.isValid)return t.status(400).send(a.formatErrorResponse(`Query validation failed: ${c.errors.join(", ")}`,400));const d=await n.executeMultiCubeQuery(o,u);return a.formatCubeResponse(o,d,n)}catch(r){return e.log.error(r,"Query execution error"),t.status(500).send(a.formatErrorResponse(r instanceof Error?r.message:"Query execution failed",500))}}),s.post(`${l}/batch`,{bodyLimit:b,schema:{body:{type:"object",required:["queries"],properties:{queries:{type:"array",items:{type:"object"}}}}}},async(e,t)=>{try{const{queries:r}=e.body;if(!r||!Array.isArray(r))return t.status(400).send(a.formatErrorResponse('Request body must contain a "queries" array',400));if(r.length===0)return t.status(400).send(a.formatErrorResponse("Queries array cannot be empty",400));const o=await m(e);return await a.handleBatchRequest(r,o,n)}catch(r){return e.log.error(r,"Batch execution error"),t.status(500).send(a.formatErrorResponse(r instanceof Error?r.message:"Batch execution failed",500))}}),s.get(`${l}/meta`,async(e,t)=>{try{const r=n.getMetadata();return a.formatMetaResponse(r)}catch(r){return e.log.error(r,"Metadata error"),t.status(500).send(a.formatErrorResponse(r instanceof Error?r.message:"Failed to fetch metadata",500))}}),s.post(`${l}/sql`,{bodyLimit:b,schema:{body:{type:"object",additionalProperties:!0}}},async(e,t)=>{try{const r=e.body,o=await m(e),u=n.validateQuery(r);if(!u.isValid)return t.status(400).send(a.formatErrorResponse(`Query validation failed: ${u.errors.join(", ")}`,400));const c=r.measures?.[0]||r.dimensions?.[0];if(!c)return t.status(400).send(a.formatErrorResponse("No measures or dimensions specified",400));const d=c.split(".")[0],f=await n.generateSQL(d,r,o);return a.formatSqlResponse(r,f)}catch(r){return e.log.error(r,"SQL generation error"),t.status(500).send(a.formatErrorResponse(r instanceof Error?r.message:"SQL generation failed",500))}}),s.get(`${l}/sql`,{schema:{querystring:{type:"object",properties:{query:{type:"string"}},required:["query"]}}},async(e,t)=>{try{const{query:r}=e.query,o=JSON.parse(r),u=await m(e),c=n.validateQuery(o);if(!c.isValid)return t.status(400).send(a.formatErrorResponse(`Query validation failed: ${c.errors.join(", ")}`,400));const d=o.measures?.[0]||o.dimensions?.[0];if(!d)return t.status(400).send(a.formatErrorResponse("No measures or dimensions specified",400));const f=d.split(".")[0],C=await n.generateSQL(f,o,u);return a.formatSqlResponse(o,C)}catch(r){return e.log.error(r,"SQL generation error"),t.status(500).send(a.formatErrorResponse(r instanceof Error?r.message:"SQL generation failed",500))}}),s.post(`${l}/dry-run`,{bodyLimit:b,schema:{body:{type:"object",additionalProperties:!0}}},async(e,t)=>{try{const r=e.body,o=r.query||r,u=await m(e);return await a.handleDryRun(o,u,n)}catch(r){return e.log.error(r,"Dry-run error"),t.status(400).send({error:r instanceof Error?r.message:"Dry-run validation failed",valid:!1})}}),s.get(`${l}/dry-run`,{schema:{querystring:{type:"object",properties:{query:{type:"string"}},required:["query"]}}},async(e,t)=>{try{const{query:r}=e.query,o=JSON.parse(r),u=await m(e);return await a.handleDryRun(o,u,n)}catch(r){return e.log.error(r,"Dry-run error"),t.status(400).send({error:r instanceof Error?r.message:"Dry-run validation failed",valid:!1})}}),s.post(`${l}/explain`,{bodyLimit:b,schema:{body:{type:"object",additionalProperties:!0}}},async(e,t)=>{try{const r=e.body,o=r.query||r,u=r.options||{},c=await m(e),d=n.validateQuery(o);return d.isValid?await n.explainQuery(o,c,u):t.status(400).send({error:`Query validation failed: ${d.errors.join(", ")}`})}catch(r){return e.log.error(r,"Explain error"),t.status(500).send({error:r instanceof Error?r.message:"Explain query failed"})}}),s.setErrorHandler(async(e,t,r)=>{t.log.error(e,"Fastify cube adapter error"),r.statusCode<400&&r.status(500);const o=e instanceof Error?e:String(e);return a.formatErrorResponse(o,r.statusCode)}),g()};async function N(i,s){await i.register(h,s)}function A(i){const s=require("fastify")({logger:!0});return s.register(h,i),s}exports.createCubeApp=A;exports.cubePlugin=h;exports.registerCubeRoutes=N;