@dringtech/lume-duck@0.2.1Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
latest
dringtech/lume-duckHelpers for working with DuckDB data in the Lume SSG
This package works with DenoIt is unknown whether this package works with Cloudflare Workers, Node.js, Bun
JSR Score
94%
Published
a month ago (0.2.1)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273import { open } from "../deps/duckdb.ts"; import { Query } from "./query.ts"; import type { columnTypes } from "./types.d.ts"; /** DuckDB Loader configuration. */ interface DuckDbLoaderConfig { /** Database path. Defaults to ":memory:" */ dbPath: string; } /** DuckDB Loader options - all parameters optional version of {@linkcode DuckDbLoaderConfig}. */ export type DuckDbLoaderOptions = Partial<DuckDbLoaderConfig>; type DuckData = ( ...params: columnTypes[] ) => Record<string, columnTypes>[]; export type Loader = ( path: string, ) => Promise<DuckData>; /** * Factory which generates a DuckDb loader. * * The loader generated by this factory loads SQL files from disk into * a {@linkcode Query} object. This is exposed in Lume as a function. * * @example Load SQL as DuckDB query * ```ts * site.loadData(['.sql'], duckLoader()) * ``` * * An SQL called `_data/simple.sql` file with the following content * * ```sql * select 1 as number; * ``` * * Can be used in Lume pages as follows (assuming a Vento template). * * ```vento * {{ simple() |> JSON.stringify }} * ``` * * @param options Configuration for factory * @returns DuckDB Loader */ export function duckDbLoader(options: DuckDbLoaderOptions = {}): Loader { const config: DuckDbLoaderConfig = { dbPath: ":memory:", ...options, }; const db = _internals.open(config.dbPath); const loader = async (path: string) => { const query = new _internals.Query(db); await query.loadFile(path); const runner = (...params: columnTypes[]) => query.run(...params); runner.toString = () => { console.warn("DuckDB query called as string, not function."); return query.sql; }; return runner; }; return loader; } export const _internals = { open, Query, };