@duckdb/duckdb-wasm
TypeScript icon, indicating that this package has built-in type declarations

1.29.0 • Public • Published

Main Benchmarks duckdb npm JSDevlivr

DuckDB-Wasm

DuckDB-Wasm is an in-process analytical SQL database for the browser. It is powered by WebAssembly, speaks Arrow fluently, reads Parquet, CSV and JSON files backed by Filesystem APIs or HTTP requests and has been tested with Chrome, Firefox, Safari and Node.js. Read the launch blog post.

Try it out at shell.duckdb.org and on Observable and read the API documentation.

DuckDB-Wasm is fast! If you're here for performance numbers, head over to our benchmarks.

Instantiation

cdn(jsdelivr)

import * as duckdb from '@duckdb/duckdb-wasm';

const JSDELIVR_BUNDLES = duckdb.getJsDelivrBundles();

// Select a bundle based on browser checks
const bundle = await duckdb.selectBundle(JSDELIVR_BUNDLES);

const worker_url = URL.createObjectURL(
  new Blob([`importScripts("${bundle.mainWorker!}");`], {type: 'text/javascript'})
);

// Instantiate the asynchronus version of DuckDB-wasm
const worker = new Worker(worker_url);
const logger = new duckdb.ConsoleLogger();
const db = new duckdb.AsyncDuckDB(logger, worker);
await db.instantiate(bundle.mainModule, bundle.pthreadWorker);
URL.revokeObjectURL(worker_url);

webpack

import * as duckdb from '@duckdb/duckdb-wasm';
import duckdb_wasm from '@duckdb/duckdb-wasm/dist/duckdb-mvp.wasm';
import duckdb_wasm_next from '@duckdb/duckdb-wasm/dist/duckdb-eh.wasm';
const MANUAL_BUNDLES: duckdb.DuckDBBundles = {
    mvp: {
        mainModule: duckdb_wasm,
        mainWorker: new URL('@duckdb/duckdb-wasm/dist/duckdb-browser-mvp.worker.js', import.meta.url).toString(),
    },
    eh: {
        mainModule: duckdb_wasm_next,
        mainWorker: new URL('@duckdb/duckdb-wasm/dist/duckdb-browser-eh.worker.js', import.meta.url).toString(),
    },
};
// Select a bundle based on browser checks
const bundle = await duckdb.selectBundle(MANUAL_BUNDLES);
// Instantiate the asynchronus version of DuckDB-wasm
const worker = new Worker(bundle.mainWorker!);
const logger = new duckdb.ConsoleLogger();
const db = new duckdb.AsyncDuckDB(logger, worker);
await db.instantiate(bundle.mainModule, bundle.pthreadWorker);

vite

import * as duckdb from '@duckdb/duckdb-wasm';
import duckdb_wasm from '@duckdb/duckdb-wasm/dist/duckdb-mvp.wasm?url';
import mvp_worker from '@duckdb/duckdb-wasm/dist/duckdb-browser-mvp.worker.js?url';
import duckdb_wasm_eh from '@duckdb/duckdb-wasm/dist/duckdb-eh.wasm?url';
import eh_worker from '@duckdb/duckdb-wasm/dist/duckdb-browser-eh.worker.js?url';

const MANUAL_BUNDLES: duckdb.DuckDBBundles = {
    mvp: {
        mainModule: duckdb_wasm,
        mainWorker: mvp_worker,
    },
    eh: {
        mainModule: duckdb_wasm_eh,
        mainWorker: eh_worker,
    },
};
// Select a bundle based on browser checks
const bundle = await duckdb.selectBundle(MANUAL_BUNDLES);
// Instantiate the asynchronus version of DuckDB-wasm
const worker = new Worker(bundle.mainWorker!);
const logger = new duckdb.ConsoleLogger();
const db = new duckdb.AsyncDuckDB(logger, worker);
await db.instantiate(bundle.mainModule, bundle.pthreadWorker);

static served (manually download the files from https://cdn.jsdelivr.net/npm/@duckdb/duckdb-wasm/dist/)

import * as duckdb from '@duckdb/duckdb-wasm';

const MANUAL_BUNDLES: duckdb.DuckDBBundles = {
    mvp: {
        mainModule: 'change/me/../duckdb-mvp.wasm',
        mainWorker: 'change/me/../duckdb-browser-mvp.worker.js',
    },
    eh: {
        mainModule: 'change/m/../duckdb-eh.wasm',
        mainWorker: 'change/m/../duckdb-browser-eh.worker.js',
    },
};
// Select a bundle based on browser checks
const bundle = await duckdb.selectBundle(JSDELIVR_BUNDLES);
// Instantiate the asynchronus version of DuckDB-wasm
const worker = new Worker(bundle.mainWorker!);
const logger = new duckdb.ConsoleLogger();
const db = new duckdb.AsyncDuckDB(logger, worker);
await db.instantiate(bundle.mainModule, bundle.pthreadWorker);

Data Import

// Data can be inserted from an existing arrow.Table
await c.insertArrowTable(existingTable, { name: 'arrow_table' });
// ..., from a raw Arrow IPC stream
const c = await db.connect();
const streamResponse = await fetch(`someapi`);
const streamReader = streamResponse.body.getReader();
const streamInserts = [];
while (true) {
    const { value, done } = await streamReader.read();
    if (done) break;
    streamInserts.push(c.insertArrowFromIPCStream(value, { name: 'streamed' }));
}
await Promise.all(streamInserts);

// ..., from CSV files
// (interchangeable: registerFile{Text,Buffer,URL,Handle})
await db.registerFileText(`data.csv`, '1|foo\n2|bar\n');
// ... with typed insert options
await c.insertCSVFromPath('data.csv', {
    schema: 'main',
    name: 'foo',
    detect: false,
    header: false,
    delimiter: '|',
    columns: {
        col1: new arrow.Int32(),
        col2: new arrow.Utf8(),
    },
});

// ..., from JSON documents in row-major format
await db.registerFileText(
    'rows.json',
    `[
    { "col1": 1, "col2": "foo" },
    { "col1": 2, "col2": "bar" },
]`,
);
// ... or column-major format
await db.registerFileText(
    'columns.json',
    `{
    "col1": [1, 2],
    "col2": ["foo", "bar"]
}`,
);
// ... with typed insert options
await c.insertJSONFromPath('rows.json', { name: 'rows' });
await c.insertJSONFromPath('columns.json', { name: 'columns' });

// ... from a Javascript array
const myArray = [
  {
    name: 'Dan',
    age: 32,
    numberOfPets: 3
  }
];
const encoder = new TextEncoder();
const buffer = encoder.encode(myArray);
await db.registerFileBuffer(myTableName, buffer);
await c.insertJSONFromPath(myTableName, {
  schema: 'main',
  name: 'foo',
});

// ..., from Parquet files
const pickedFile: File = letUserPickFile();
await db.registerFileHandle('local.parquet', pickedFile, DuckDBDataProtocol.BROWSER_FILEREADER, true);
await db.registerFileURL('remote.parquet', 'https://origin/remote.parquet', DuckDBDataProtocol.HTTP, false);
const res = await fetch('https://origin/remote.parquet');
await db.registerFileBuffer('buffer.parquet', new Uint8Array(await res.arrayBuffer()));

// ..., by specifying URLs in the SQL text
await c.query(`
    CREATE TABLE direct AS
        SELECT * FROM "https://origin/remote.parquet"
`);
// ..., or by executing raw insert statements
await c.query(`INSERT INTO existing_table
    VALUES (1, "foo"), (2, "bar")`);

// Close the connection to release memory
await c.close();

Query Execution

// Either materialize the query result
await conn.query<{ v: arrow.Int }>(`
    SELECT * FROM generate_series(1, 100) t(v)
`);
// ..., or fetch the result chunks lazily
for await (const batch of await conn.send<{ v: arrow.Int }>(`
    SELECT * FROM generate_series(1, 100) t(v)
`)) {
    // ...
}
// Close the connection to release memory
await conn.close();

Prepared Statements

// Prepare query
const stmt = await conn.prepare(`SELECT v + ? FROM generate_series(0, 10000) as t(v);`);
// ... and run the query with materialized results
await stmt.query(234);
// ... or result chunks
for await (const batch of await stmt.send(234)) {
    // ...
}
// Close the statement to release memory
await stmt.close();
// Closing the connection will release statements as well
await conn.close();

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.29.03,610latest
1.29.1-dev12.047next

Version History

VersionDownloads (Last 7 Days)Published
1.29.1-dev12.047
1.29.1-dev9.0138
1.29.1-dev5.0452
1.29.1-dev3.01,545
1.29.03,610
1.28.1-dev297.010
1.28.1-dev295.08
1.28.1-dev296.08
1.28.1-dev294.08
1.28.1-dev293.024
1.28.1-dev292.037
1.28.1-dev290.08
1.28.1-dev288.010
1.28.1-dev287.06
1.28.1-dev286.01
1.28.1-dev283.01
1.28.1-dev279.02
1.28.1-dev273.01
1.28.1-dev270.03
1.28.1-dev278.0209
1.28.1-dev276.06
1.28.1-dev275.06
1.28.1-dev272.06
1.28.1-dev271.06
1.28.1-dev269.02
1.28.1-dev258.0976
1.28.1-dev257.042
1.28.1-dev255.01
1.28.1-dev254.01
1.28.1-dev252.01
1.28.1-dev249.01
1.28.1-dev250.03
1.28.1-dev248.030
1.28.1-dev246.02
1.28.1-dev245.01
1.28.1-dev242.0813
1.28.1-dev240.01
1.28.1-dev239.00
1.28.1-dev234.045
1.28.1-dev233.06
1.28.1-dev232.056
1.28.1-dev231.06
1.28.1-dev226.05
1.28.1-dev223.00
1.28.1-dev222.01
1.28.1-dev221.00
1.28.1-dev218.032
1.28.1-dev216.00
1.28.1-dev215.024
1.28.1-dev212.00
1.28.1-dev211.013
1.28.1-dev210.00
1.28.1-dev207.01
1.28.1-dev206.00
1.28.1-dev205.00
1.28.1-dev204.00
1.28.1-dev195.0497
1.28.1-dev194.05
1.28.1-dev193.05
1.28.1-dev192.05
1.28.1-dev191.05
1.28.1-dev190.00
1.28.1-dev189.0247
1.28.1-dev188.00
1.28.1-dev186.00
1.28.1-dev187.00
1.28.1-dev185.00
1.28.1-dev183.00
1.28.1-dev182.00
1.28.1-dev181.00
1.28.1-dev179.00
1.28.1-dev178.00
1.28.1-dev175.00
1.28.1-dev173.01
1.28.1-dev172.00
1.28.1-dev169.0236
1.28.1-dev166.00
1.28.1-dev162.00
1.28.1-dev159.0180
1.28.1-dev155.00
1.28.1-dev154.00
1.28.1-dev153.00
1.28.1-dev149.00
1.28.1-dev148.00
1.28.1-dev132.00
1.28.1-dev136.00
1.28.1-dev131.00
1.28.1-dev127.0301
1.28.1-dev112.00
1.28.1-dev111.00
1.28.1-dev110.00
1.28.1-dev106.04,420
1.28.1-dev109.00
1.28.1-dev105.00
1.28.1-dev104.03
1.28.1-dev99.02,717
1.28.1-dev91.031
1.28.1-dev89.03
1.28.1-dev87.00
1.28.1-dev82.00
1.28.1-dev81.00
1.28.1-dev80.00
1.28.1-dev78.00
1.28.1-dev75.00
1.28.1-dev74.00
1.28.1-dev73.00
1.28.1-dev71.00
1.28.1-dev66.00
1.28.1-dev65.00
1.28.08,805
1.27.1-dev125.03
1.27.1-dev124.00
1.27.1-dev123.00
1.27.1-dev120.02
1.27.1-dev119.00
1.27.1-dev118.00
1.27.1-dev115.00
1.27.1-dev114.00
1.27.0734
1.26.067
1.25.00
1.24.05
1.21.01
1.20.012
1.11.1-dev.03
1.17.07
1.16.13
1.16.0259
1.15.05
1.14.416
1.14.33
1.14.23
1.14.16
1.14.05
1.13.1-dev285.05
1.13.1-dev282.03
1.13.1-dev274.03
1.13.1-dev273.03
1.13.1-dev267.03
1.13.1-dev266.03
1.13.1-dev260.03
1.13.1-dev258.03
1.13.1-dev240.03
1.13.1-dev230.06
1.13.1-dev166.04
1.13.1-dev161.03
1.13.1-dev154.03
1.13.1-dev129.03
1.13.1-dev98.03
1.13.1-dev94.03
1.13.1-dev67.04
1.13.1-dev24.03
1.13.1-dev20.03
1.13.1-dev7.03
1.13.033
1.12.1-dev87.04
1.12.1-dev64.03
1.12.1-dev17.03
1.12.1-dev1.03
1.12.011
1.11.1-dev82.03
1.11.1-dev80.03
1.11.1-dev77.03
1.11.1-dev54.016
1.11.1-dev53.03
1.11.1-dev39.03
1.11.1-dev30.03
1.11.1-dev27.03
1.11.1-dev24.03
1.11.1-dev22.03
1.11.1-dev21.03
1.11.03
0.1.130
0.1.13-dev39.00
0.1.13-dev33.00
0.1.13-dev30.00
0.1.13-dev29.00
0.1.13-dev26.00
0.1.13-dev25.00
0.1.13-dev23.00
0.1.13-dev21.00
0.1.13-dev20.00
0.1.13-dev17.00
0.1.13-dev12.00
0.1.13-dev8.00
0.1.13-dev7.00
0.1.13-dev1.00
0.1.120
0.1.12-dev89.00
0.1.12-dev72.00
0.1.12-dev67.00
0.1.12-dev54.00
0.1.12-dev35.00
0.1.12-dev26.00
0.1.12-dev8.00
0.1.110
0.1.93
0.1.83
0.1.70
0.1.60
0.1.6-dev1.00
0.1.50
0.1.5-dev2.00
0.1.5-dev1.00
0.1.4-dev3.00
0.1.4-dev2.00
0.1.4-dev1.00

Package Sidebar

Install

npm i @duckdb/duckdb-wasm

Weekly Downloads

27,009

Version

1.29.0

License

MIT

Unpacked Size

147 MB

Total Files

84

Last publish

Collaborators

  • duckdb_admin
  • mytherin
  • ankoh