UNPKG

knex-dialect-athena

Version:
75 lines (73 loc) 2 kB
import { AthenaConnection } from "./chunk-AJJNS3GG.mjs"; import { assert } from "./chunk-54RCQBX4.mjs"; import { QueryCompiler_Athena } from "./chunk-VVHT72BM.mjs"; import "./chunk-X5PQ2CFY.mjs"; import { __async } from "./chunk-SGFGNEI2.mjs"; // src/index.ts import Knex from "knex"; var noOp = () => { }; function createAthenaDialect(config) { return class Client_Athena extends Knex.Client { constructor() { super(...arguments); this.dialect = "athena"; this.driverName = "athena"; // TODO: add query cancelling behavior this.canCancelQuery = false; this.releaseConnection = noOp; this.queryCompiler = (builder, bindings) => new QueryCompiler_Athena( this, builder, bindings ); } acquireConnection() { return new AthenaConnection(config); } processBinding(binding) { if (binding === null) return "null"; if (binding instanceof Date) throw new Error("Date bindings are not (yet) supported"); if (Array.isArray(binding)) throw new Error("Array bindings are not supported"); return String(binding); } _query(connection, obj) { return __async(this, null, function* () { if (!obj.sql) throw new Error("The query is empty"); const response = yield connection.query( obj.sql, obj.bindings.flatMap((binding) => this.processBinding(binding)) ); obj.response = response; return obj; }); } processResponse(obj) { if (obj.method === "raw") return obj.response; if (obj.method === "first") { if (!obj.response[0]) throw new Error("Called `.first` but no rows were returned"); return obj.response[0]; } if (obj.method === "pluck") return obj.response.map((row) => { assert(!!obj.pluck, obj); return row[obj.pluck]; }); return obj.response; } }; } export { createAthenaDialect };