UNPKG

drizzle-cube

Version:

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

214 lines (213 loc) 5.86 kB
import { S as P, c as u, f as v, a as L, b as Q, h as C } from "../compiler-Dec55JjO.js"; const w = function(o, x, q) { const { cubes: b, drizzle: S, schema: E, extractSecurityContext: y, engineType: j, cors: h, basePath: d = "/cubejs-api/v1", bodyLimit: p = 10485760 // 10MB } = x; if (!b || b.length === 0) return q(new Error("At least one cube must be provided in the cubes array")); h && o.register(import("@fastify/cors"), h), o.addHook("onRequest", async (r, t) => { r.method === "POST" && (r.body = void 0); }); const i = new P({ drizzle: S, schema: E, engineType: j }); b.forEach((r) => { i.registerCube(r); }), o.post(`${d}/load`, { bodyLimit: p, schema: { body: { type: "object", additionalProperties: !0 } } }, async (r, t) => { try { const e = r.body, a = e.query || e, s = await y(r), n = i.validateQuery(a); if (!n.isValid) return t.status(400).send(u( `Query validation failed: ${n.errors.join(", ")}`, 400 )); const c = await i.executeMultiCubeQuery(a, s); return v(a, c, i); } catch (e) { return r.log.error(e, "Query execution error"), t.status(500).send(u( e instanceof Error ? e.message : "Query execution failed", 500 )); } }), o.get(`${d}/load`, { schema: { querystring: { type: "object", properties: { query: { type: "string" } }, required: ["query"] } } }, async (r, t) => { try { const { query: e } = r.query; let a; try { a = JSON.parse(e); } catch { return t.status(400).send(u( "Invalid JSON in query parameter", 400 )); } const s = await y(r), n = i.validateQuery(a); if (!n.isValid) return t.status(400).send(u( `Query validation failed: ${n.errors.join(", ")}`, 400 )); const c = await i.executeMultiCubeQuery(a, s); return v(a, c, i); } catch (e) { return r.log.error(e, "Query execution error"), t.status(500).send(u( e instanceof Error ? e.message : "Query execution failed", 500 )); } }), o.get(`${d}/meta`, async (r, t) => { try { const e = i.getMetadata(); return L(e); } catch (e) { return r.log.error(e, "Metadata error"), t.status(500).send(u( e instanceof Error ? e.message : "Failed to fetch metadata", 500 )); } }), o.post(`${d}/sql`, { bodyLimit: p, schema: { body: { type: "object", additionalProperties: !0 } } }, async (r, t) => { var e, a; try { const s = r.body, n = await y(r), c = i.validateQuery(s); if (!c.isValid) return t.status(400).send(u( `Query validation failed: ${c.errors.join(", ")}`, 400 )); const l = ((e = s.measures) == null ? void 0 : e[0]) || ((a = s.dimensions) == null ? void 0 : a[0]); if (!l) return t.status(400).send(u( "No measures or dimensions specified", 400 )); const g = l.split(".")[0], f = await i.generateSQL(g, s, n); return Q(s, f); } catch (s) { return r.log.error(s, "SQL generation error"), t.status(500).send(u( s instanceof Error ? s.message : "SQL generation failed", 500 )); } }), o.get(`${d}/sql`, { schema: { querystring: { type: "object", properties: { query: { type: "string" } }, required: ["query"] } } }, async (r, t) => { var e, a; try { const { query: s } = r.query, n = JSON.parse(s), c = await y(r), l = i.validateQuery(n); if (!l.isValid) return t.status(400).send(u( `Query validation failed: ${l.errors.join(", ")}`, 400 )); const g = ((e = n.measures) == null ? void 0 : e[0]) || ((a = n.dimensions) == null ? void 0 : a[0]); if (!g) return t.status(400).send(u( "No measures or dimensions specified", 400 )); const f = g.split(".")[0], $ = await i.generateSQL(f, n, c); return Q(n, $); } catch (s) { return r.log.error(s, "SQL generation error"), t.status(500).send(u( s instanceof Error ? s.message : "SQL generation failed", 500 )); } }), o.post(`${d}/dry-run`, { bodyLimit: p, schema: { body: { type: "object", additionalProperties: !0 } } }, async (r, t) => { try { const e = r.body, a = e.query || e, s = await y(r); return await C(a, s, i); } catch (e) { return r.log.error(e, "Dry-run error"), t.status(400).send({ error: e instanceof Error ? e.message : "Dry-run validation failed", valid: !1 }); } }), o.get(`${d}/dry-run`, { schema: { querystring: { type: "object", properties: { query: { type: "string" } }, required: ["query"] } } }, async (r, t) => { try { const { query: e } = r.query, a = JSON.parse(e), s = await y(r); return await C(a, s, i); } catch (e) { return r.log.error(e, "Dry-run error"), t.status(400).send({ error: e instanceof Error ? e.message : "Dry-run validation failed", valid: !1 }); } }), o.setErrorHandler(async (r, t, e) => (t.log.error(r, "Fastify cube adapter error"), e.statusCode < 400 && e.status(500), u(r, e.statusCode))), q(); }; async function N(m, o) { await m.register(w, o); } function M(m) { const o = require("fastify")({ logger: !0 }); return o.register(w, m), o; } export { M as createCubeApp, w as cubePlugin, N as registerCubeRoutes };