UNPKG

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) 5.5 kB
"use strict";var w=Object.create;var q=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var j=Object.getPrototypeOf,P=Object.prototype.hasOwnProperty;var $=(n,s,m,g)=>{if(s&&typeof s=="object"||typeof s=="function")for(let d of S(s))!P.call(n,d)&&d!==m&&q(n,d,{get:()=>s[d],enumerable:!(g=x(s,d))||g.enumerable});return n};var L=(n,s,m)=>(m=n!=null?w(j(n)):{},$(s||!n||!n.__esModule?q(m,"default",{value:n,enumerable:!0}):m,n));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("../compiler-Ld2iml85.cjs"),h=function(s,m,g){const{cubes:d,drizzle:E,schema:v,extractSecurityContext:p,engineType:C,cors:R,basePath:y="/cubejs-api/v1",bodyLimit:b=10485760}=m;if(!d||d.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 i=new o.SemanticLayerCompiler({drizzle:E,schema:v,engineType:C});d.forEach(e=>{i.registerCube(e)}),s.post(`${y}/load`,{bodyLimit:b,schema:{body:{type:"object",additionalProperties:!0}}},async(e,t)=>{try{const r=e.body,a=r.query||r,u=await p(e),c=i.validateQuery(a);if(!c.isValid)return t.status(400).send(o.formatErrorResponse(`Query validation failed: ${c.errors.join(", ")}`,400));const l=await i.executeMultiCubeQuery(a,u);return o.formatCubeResponse(a,l,i)}catch(r){return e.log.error(r,"Query execution error"),t.status(500).send(o.formatErrorResponse(r instanceof Error?r.message:"Query execution failed",500))}}),s.get(`${y}/load`,{schema:{querystring:{type:"object",properties:{query:{type:"string"}},required:["query"]}}},async(e,t)=>{try{const{query:r}=e.query;let a;try{a=JSON.parse(r)}catch{return t.status(400).send(o.formatErrorResponse("Invalid JSON in query parameter",400))}const u=await p(e),c=i.validateQuery(a);if(!c.isValid)return t.status(400).send(o.formatErrorResponse(`Query validation failed: ${c.errors.join(", ")}`,400));const l=await i.executeMultiCubeQuery(a,u);return o.formatCubeResponse(a,l,i)}catch(r){return e.log.error(r,"Query execution error"),t.status(500).send(o.formatErrorResponse(r instanceof Error?r.message:"Query execution failed",500))}}),s.post(`${y}/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(o.formatErrorResponse('Request body must contain a "queries" array',400));if(r.length===0)return t.status(400).send(o.formatErrorResponse("Queries array cannot be empty",400));const a=await p(e);return await o.handleBatchRequest(r,a,i)}catch(r){return e.log.error(r,"Batch execution error"),t.status(500).send(o.formatErrorResponse(r instanceof Error?r.message:"Batch execution failed",500))}}),s.get(`${y}/meta`,async(e,t)=>{try{const r=i.getMetadata();return o.formatMetaResponse(r)}catch(r){return e.log.error(r,"Metadata error"),t.status(500).send(o.formatErrorResponse(r instanceof Error?r.message:"Failed to fetch metadata",500))}}),s.post(`${y}/sql`,{bodyLimit:b,schema:{body:{type:"object",additionalProperties:!0}}},async(e,t)=>{try{const r=e.body,a=await p(e),u=i.validateQuery(r);if(!u.isValid)return t.status(400).send(o.formatErrorResponse(`Query validation failed: ${u.errors.join(", ")}`,400));const c=r.measures?.[0]||r.dimensions?.[0];if(!c)return t.status(400).send(o.formatErrorResponse("No measures or dimensions specified",400));const l=c.split(".")[0],f=await i.generateSQL(l,r,a);return o.formatSqlResponse(r,f)}catch(r){return e.log.error(r,"SQL generation error"),t.status(500).send(o.formatErrorResponse(r instanceof Error?r.message:"SQL generation failed",500))}}),s.get(`${y}/sql`,{schema:{querystring:{type:"object",properties:{query:{type:"string"}},required:["query"]}}},async(e,t)=>{try{const{query:r}=e.query,a=JSON.parse(r),u=await p(e),c=i.validateQuery(a);if(!c.isValid)return t.status(400).send(o.formatErrorResponse(`Query validation failed: ${c.errors.join(", ")}`,400));const l=a.measures?.[0]||a.dimensions?.[0];if(!l)return t.status(400).send(o.formatErrorResponse("No measures or dimensions specified",400));const f=l.split(".")[0],Q=await i.generateSQL(f,a,u);return o.formatSqlResponse(a,Q)}catch(r){return e.log.error(r,"SQL generation error"),t.status(500).send(o.formatErrorResponse(r instanceof Error?r.message:"SQL generation failed",500))}}),s.post(`${y}/dry-run`,{bodyLimit:b,schema:{body:{type:"object",additionalProperties:!0}}},async(e,t)=>{try{const r=e.body,a=r.query||r,u=await p(e);return await o.handleDryRun(a,u,i)}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(`${y}/dry-run`,{schema:{querystring:{type:"object",properties:{query:{type:"string"}},required:["query"]}}},async(e,t)=>{try{const{query:r}=e.query,a=JSON.parse(r),u=await p(e);return await o.handleDryRun(a,u,i)}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.setErrorHandler(async(e,t,r)=>(t.log.error(e,"Fastify cube adapter error"),r.statusCode<400&&r.status(500),o.formatErrorResponse(e,r.statusCode))),g()};async function M(n,s){await n.register(h,s)}function N(n){const s=require("fastify")({logger:!0});return s.register(h,n),s}exports.createCubeApp=N;exports.cubePlugin=h;exports.registerCubeRoutes=M;