0.8.5 • Published 6 months ago

@flowblade/source-duckdb v0.8.5

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

@flowblade/source-duckdb

npm changelog bundles node downloads license

Install

yarn add @flowblade/source-duckdb @flowblade/core @duckdb/node-api

Note that at this time @duckdb/node-api is still in alpha. To install use the latest tag, ie: @duckdb/node-api@1.3.0-alpha.21

Query the database

import { DuckdbDatasource, sql } from '@flowblade/source-duckdb';

// See setup below
import { ds } from "./config.ts";

const params = {
    min: 10,
    max: 99,
    name: 'test',
    createdAt: new Date().toISOString(),
};

type Row = { id: number; name: 'test'; createdAt: Date };

const rawSql = sql<Row>`

      WITH products(productId, createdAt)
          AS MATERIALIZED (
               FROM RANGE(1,100) SELECT 
               range::INT,
               TIMESTAMPTZ '2025-01-01 12:30:00.123456789+01:00'
          )
      
      SELECT productId, 
             ${params.name} as name,
             createdAt
             
      FROM products 
      WHERE productId BETWEEN ${params.min}::INTEGER AND ${params.max}::INTEGER
      AND createdAt < ${params.createdAt}::TIMESTAMPTZ
    `;

const result = await ds.query(rawSql);

const { data, meta, error } = result;

if (data) {
    // Typed as Row[]
    console.log(data);
}
if (error) {
    // Typed as QError
    console.log(error);
}

// Optionally: map over the data to transform it
const { data: mappedData } = result.map((row) => {
    return {
        id: row.productId,
        key: `key-${row.productId}`
    }
});

Create a duckdb instance

import os from 'node:os';

import { type DuckDBConnection, DuckDBInstance } from '@duckdb/node-api';
import { DuckdbDatasource } from '@flowblade/source-duckdb';

// Create a connection to a DuckDB instance
const createConnection = async (
  maxThreads = 4
): Promise<DuckDBConnection> => {
  const availableThreads = os.availableParallelism();
  const maxParallelism = Math.min(maxThreads, availableThreads - 1);
  const threads = availableThreads > 1 ? maxParallelism : undefined;

  const instance = await DuckDBInstance.create(':memory:', {
    // Choose between READ_ONLY or READ_WRITE
    // Note that in READ_WRITE mode concurrency is limited to 1
    // See: https://duckdb.org/docs/connect/concurrency.html
    access_mode: 'READ_WRITE',
    max_memory: '64MB',
    // Using more threads may require additional memory
    ...(threads ? { threads: threads.toString(10) } : {}),
  });
  return await instance.connect();
};

const duckdb = await createConnection();

// Create a duckdb datasource
export const ds = new DuckdbDatasource({ connection: duckdb });

Compatibility

LevelCIDescription
NodeCI for 18.x, 20.x & 22.x.
CloudflareEnsured with @cloudflare/vitest-pool-workers (see wrangler.toml
Browserslist> 95% on 01/2025. Chrome 96+, Firefox 90+, Edge 19+, ios 15+, Safari 15+ and Opera 77+
TypescriptTS 5.0 + / are-the-type-wrong checks on CI.
ES2022Dist files checked with es-check
PerformanceMonitored with codspeed.io

Contributors

Contributions are welcome. Have a look to the CONTRIBUTING document.

Sponsors

Sponsor, coffee, or star – All is spent for quality time with loved ones. Thanks ! 🙏❤️

Special thanks to

License

MIT © Sébastien Vanvelthem and contributors.

0.8.5

6 months ago

0.8.4

6 months ago

0.8.3

6 months ago

0.8.2

6 months ago

0.8.1

6 months ago

0.8.0

6 months ago

0.7.2

6 months ago

0.7.1

7 months ago

0.7.0

8 months ago

0.6.0

8 months ago

0.5.0

9 months ago

0.4.0

9 months ago

0.3.2

9 months ago

0.3.1

10 months ago

0.3.0

10 months ago

0.2.1

10 months ago

0.2.0

10 months ago

0.1.13

10 months ago

0.1.12

10 months ago

0.1.11

10 months ago

0.1.10

10 months ago

0.1.9

10 months ago

0.1.8

11 months ago

0.1.7

11 months ago

0.1.6

11 months ago

0.1.5

11 months ago

0.1.4

11 months ago

0.1.3

11 months ago

0.1.2

11 months ago

0.1.1

11 months ago

1.0.0

11 months ago