@hpcc-js/wasm-duckdb 1.12.0 → 1.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +581 -16
- package/dist/index.js +13 -10
- package/dist/index.js.map +4 -4
- package/package.json +11 -13
- package/src/duckdb.ts +253 -46
- package/types/duckdb.d.ts +238 -0
- package/types/duckdblib.d.ts +170 -0
- package/types/build/duckdb-browser-eh.worker.d.ts +0 -2
- package/types/build/duckdb-eh.wasm.d.ts +0 -2
- package/types/src/duckdb.d.ts +0 -52
- /package/types/{src/__package__.d.ts → __package__.d.ts} +0 -0
- /package/types/{src/index.d.ts → index.d.ts} +0 -0
package/package.json
CHANGED
|
@@ -1,47 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hpcc-js/wasm-duckdb",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.13.1",
|
|
4
4
|
"description": "hpcc-js - WASM DuckDB",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": {
|
|
8
|
-
"types": "./types/
|
|
8
|
+
"types": "./types/index.d.ts",
|
|
9
9
|
"default": "./dist/index.js"
|
|
10
10
|
}
|
|
11
11
|
},
|
|
12
12
|
"main": "./dist/index.js",
|
|
13
|
-
"types": "./types/
|
|
13
|
+
"types": "./types/index.d.ts",
|
|
14
14
|
"files": [
|
|
15
15
|
"dist/**/*",
|
|
16
16
|
"src/**/*",
|
|
17
17
|
"types/**/*"
|
|
18
18
|
],
|
|
19
19
|
"scripts": {
|
|
20
|
-
"clean": "rimraf coverage
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"pack-duckdb-eh": "npx -y mkdirp build && node ./utils/sfx-wasm.js ../../node_modules/@duckdb/duckdb-wasm/dist/duckdb-eh.wasm > ./build/duckdb-eh.wasm.ts",
|
|
24
|
-
"pack-duckdb": "run-p pack-duckdb-eh pack-duckdb-eh-worker pack-duckdb-eh-worker-node",
|
|
20
|
+
"clean": "rimraf coverage dist dist-test types .nyc_output",
|
|
21
|
+
"build-cpp": "cmake --build ../../build --target duckdblib",
|
|
22
|
+
"build-cpp-watch": "chokidar 'src-cpp/**.*' -c 'npm run build-cpp'",
|
|
25
23
|
"gen-types": "tsc --project tsconfig.json --emitDeclarationOnly",
|
|
26
24
|
"gen-types-watch": "npm run gen-types -- --watch",
|
|
27
25
|
"bundle": "node esbuild.js",
|
|
28
26
|
"bundle-dev": "npm run bundle -- --development",
|
|
29
27
|
"bundle-watch": "npm run bundle-dev -- --watch",
|
|
30
28
|
"build-dev": "run-p gen-types bundle-dev",
|
|
31
|
-
"build": "
|
|
29
|
+
"build": "run-p gen-types bundle",
|
|
32
30
|
"lint-skypack": "npx -y @skypack/package-check",
|
|
33
|
-
"lint-eslint": "eslint src/**/*.ts tests/**/*.ts",
|
|
31
|
+
"lint-eslint": "eslint \"src/**/*.ts\" \"tests/**/*.ts\"",
|
|
34
32
|
"lint": "run-p lint-eslint",
|
|
35
33
|
"test-browser": "vitest run --project browser",
|
|
34
|
+
"test-node": "vitest run --project node",
|
|
36
35
|
"test": "vitest run",
|
|
37
36
|
"coverage": "vitest run --coverage",
|
|
38
37
|
"update": "npx -y npm-check-updates -u -t minor",
|
|
39
38
|
"update-major": "npx -y npm-check-updates -u"
|
|
40
39
|
},
|
|
41
40
|
"devDependencies": {
|
|
42
|
-
"@duckdb/duckdb-wasm": "1.31.0",
|
|
43
41
|
"@hpcc-js/esbuild-plugins": "1.7.0",
|
|
44
|
-
"
|
|
42
|
+
"@hpcc-js/wasm-util": "1.0.0"
|
|
45
43
|
},
|
|
46
44
|
"keywords": [
|
|
47
45
|
"DuckDB",
|
|
@@ -57,5 +55,5 @@
|
|
|
57
55
|
},
|
|
58
56
|
"homepage": "https://hpcc-systems.github.io/hpcc-js-wasm/",
|
|
59
57
|
"license": "Apache-2.0",
|
|
60
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "11395f5acfc865accde71905509bb45d5c79b11b"
|
|
61
59
|
}
|
package/src/duckdb.ts
CHANGED
|
@@ -1,81 +1,288 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
1
|
+
// @ts-expect-error importing from a wasm file is resolved via a custom esbuild plugin
|
|
2
|
+
import load, { reset } from "../../../build/packages/duckdb/duckdblib.wasm";
|
|
3
|
+
import type { MainModule, DuckDB as CPPDuckDB } from "../types/duckdblib.js";
|
|
4
|
+
import { MainModuleEx } from "@hpcc-js/wasm-util";
|
|
5
|
+
|
|
6
|
+
let g_duckdb: Promise<DuckDB>;
|
|
7
|
+
const textEncoder = new TextEncoder();
|
|
4
8
|
|
|
5
9
|
/**
|
|
6
|
-
* DuckDB WASM library
|
|
10
|
+
* DuckDB WASM library - an in-process SQL OLAP Database Management System.
|
|
11
|
+
*
|
|
12
|
+
* DuckDB is designed for OLAP (Online Analytical Processing) workloads and provides:
|
|
13
|
+
* - Full SQL support with rich analytical functions
|
|
14
|
+
* - In-process execution (no server required)
|
|
15
|
+
* - Efficient columnar storage
|
|
16
|
+
* - Support for reading CSV, JSON, and other formats
|
|
17
|
+
* - Transaction management
|
|
18
|
+
* - Prepared statements with parameter binding
|
|
7
19
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
20
|
+
* @example Basic Query
|
|
10
21
|
* ```ts
|
|
11
22
|
* import { DuckDB } from "@hpcc-js/wasm-duckdb";
|
|
12
23
|
*
|
|
13
|
-
*
|
|
14
|
-
* const
|
|
24
|
+
* const duckdb = await DuckDB.load();
|
|
25
|
+
* const connection = duckdb.connect();
|
|
26
|
+
*
|
|
27
|
+
* const result = connection.query("SELECT 'Hello, DuckDB!' AS message");
|
|
28
|
+
* console.log(result.getValue(0, 0)); // "Hello, DuckDB!"
|
|
29
|
+
*
|
|
30
|
+
* result.delete();
|
|
31
|
+
* connection.delete();
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @example Working with JSON Files
|
|
35
|
+
* ```ts
|
|
36
|
+
* const duckdb = await DuckDB.load();
|
|
37
|
+
* const connection = duckdb.connect();
|
|
15
38
|
*
|
|
16
39
|
* const data = [
|
|
17
|
-
* {
|
|
18
|
-
* {
|
|
40
|
+
* { id: 1, name: "Alice", age: 30 },
|
|
41
|
+
* { id: 2, name: "Bob", age: 25 }
|
|
19
42
|
* ];
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
43
|
+
* duckdb.registerFileString("users.json", JSON.stringify(data));
|
|
44
|
+
*
|
|
45
|
+
* const result = connection.query("SELECT * FROM read_json_auto('users.json') WHERE age > 26");
|
|
46
|
+
* console.log(result.toJSON()); // [{"id":1,"name":"Alice","age":30}]
|
|
47
|
+
*
|
|
48
|
+
* result.delete();
|
|
49
|
+
* connection.delete();
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @example Prepared Statements
|
|
53
|
+
* ```ts
|
|
54
|
+
* const connection = duckdb.connect();
|
|
55
|
+
* connection.query("CREATE TABLE users (id INTEGER, name VARCHAR, age INTEGER)").delete();
|
|
56
|
+
*
|
|
57
|
+
* const insertStmt = connection.prepare("INSERT INTO users VALUES (?, ?, ?)");
|
|
58
|
+
* insertStmt.execute([1, "Alice", 30]).delete();
|
|
59
|
+
* insertStmt.execute([2, "Bob", 25]).delete();
|
|
60
|
+
* insertStmt.delete();
|
|
61
|
+
*
|
|
62
|
+
* const queryStmt = connection.prepare("SELECT * FROM users WHERE age > ?");
|
|
63
|
+
* const result = queryStmt.execute([26]);
|
|
64
|
+
* console.log(result.toJSON());
|
|
65
|
+
*
|
|
66
|
+
* result.delete();
|
|
67
|
+
* queryStmt.delete();
|
|
68
|
+
* connection.delete();
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* @example Transaction Management
|
|
72
|
+
* ```ts
|
|
73
|
+
* const connection = duckdb.connect();
|
|
74
|
+
*
|
|
75
|
+
* connection.setAutoCommit(false);
|
|
76
|
+
* connection.beginTransaction();
|
|
77
|
+
*
|
|
78
|
+
* try {
|
|
79
|
+
* connection.query("UPDATE accounts SET balance = balance - 200 WHERE id = 1").delete();
|
|
80
|
+
* connection.query("UPDATE accounts SET balance = balance + 200 WHERE id = 2").delete();
|
|
81
|
+
* connection.commit();
|
|
82
|
+
* console.log("Transfer successful");
|
|
83
|
+
* } catch (error) {
|
|
84
|
+
* connection.rollback();
|
|
85
|
+
* console.error("Transfer failed");
|
|
28
86
|
* }
|
|
29
87
|
*
|
|
30
|
-
*
|
|
88
|
+
* connection.delete();
|
|
31
89
|
* ```
|
|
90
|
+
*
|
|
91
|
+
* @see [DuckDB Documentation](https://duckdb.org/docs/)
|
|
92
|
+
* @see [DuckDB GitHub](https://github.com/duckdb/duckdb)
|
|
32
93
|
*/
|
|
33
|
-
export class DuckDB {
|
|
34
94
|
|
|
35
|
-
db: AsyncDuckDB;
|
|
36
95
|
|
|
37
|
-
|
|
38
|
-
|
|
96
|
+
|
|
97
|
+
export class DuckDB extends MainModuleEx<MainModule> {
|
|
98
|
+
|
|
99
|
+
db: CPPDuckDB
|
|
100
|
+
|
|
101
|
+
private constructor(_module: MainModule) {
|
|
102
|
+
super(_module);
|
|
103
|
+
this.db = this._module.create()!;
|
|
104
|
+
const { FS_createPath } = this._module;
|
|
105
|
+
FS_createPath("/", "home/web_user", true, true);
|
|
39
106
|
}
|
|
40
107
|
|
|
41
108
|
/**
|
|
42
|
-
*
|
|
109
|
+
* Loads and initializes the DuckDB WASM module.
|
|
110
|
+
*
|
|
111
|
+
* This method compiles and instantiates the WebAssembly module. It returns a singleton
|
|
112
|
+
* instance, so subsequent calls will return the same instance.
|
|
43
113
|
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* :::
|
|
114
|
+
* **Note:** WebAssembly compilation is disallowed on the main thread if the buffer size
|
|
115
|
+
* is larger than 4KB, which is why this method is asynchronous.
|
|
47
116
|
*
|
|
48
|
-
* @returns A promise
|
|
117
|
+
* @returns A promise that resolves to the DuckDB instance
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* const duckdb = await DuckDB.load();
|
|
122
|
+
* console.log(duckdb.version()); // e.g., "v1.4.3"
|
|
123
|
+
* ```
|
|
49
124
|
*/
|
|
50
125
|
static load(): Promise<DuckDB> {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const db = new AsyncDuckDB(logger, worker);
|
|
58
|
-
const wasmUrl = URL.createObjectURL(
|
|
59
|
-
new Blob([new Uint8Array(load())], { "type": "application/wasm" })
|
|
60
|
-
);
|
|
61
|
-
return db.instantiate(wasmUrl, null).then(async () => {
|
|
62
|
-
URL.revokeObjectURL(wasmUrl);
|
|
63
|
-
return new DuckDB(db, await db.getVersion());
|
|
64
|
-
});
|
|
126
|
+
if (!g_duckdb) {
|
|
127
|
+
g_duckdb = load().then((module: any) => {
|
|
128
|
+
return new DuckDB(module)
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
return g_duckdb;
|
|
65
132
|
}
|
|
66
133
|
|
|
67
134
|
/**
|
|
68
|
-
*
|
|
135
|
+
* Unloads the compiled WASM instance.
|
|
136
|
+
*
|
|
137
|
+
* This is primarily useful for cleanup in testing scenarios or when you need to
|
|
138
|
+
* completely reset the DuckDB instance.
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```ts
|
|
142
|
+
* DuckDB.unload();
|
|
143
|
+
* // The next call to DuckDB.load() will create a fresh instance
|
|
144
|
+
* ```
|
|
69
145
|
*/
|
|
70
146
|
static unload() {
|
|
71
|
-
resetWasmWorker();
|
|
72
147
|
reset();
|
|
73
148
|
}
|
|
74
149
|
|
|
75
150
|
/**
|
|
76
|
-
*
|
|
151
|
+
* Returns the DuckDB version string.
|
|
152
|
+
*
|
|
153
|
+
* @returns The version string (e.g., "v1.4.3")
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```ts
|
|
157
|
+
* const duckdb = await DuckDB.load();
|
|
158
|
+
* console.log(duckdb.version()); // "v1.4.3"
|
|
159
|
+
* ```
|
|
77
160
|
*/
|
|
78
161
|
version(): string {
|
|
79
|
-
return this.
|
|
162
|
+
return this._module.libraryVersion();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Creates a new database connection.
|
|
167
|
+
*
|
|
168
|
+
* Each connection maintains its own transaction state and can execute queries
|
|
169
|
+
* independently. Always call `.delete()` on the connection when done to free resources.
|
|
170
|
+
*
|
|
171
|
+
* @returns A new Connection instance
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* const duckdb = await DuckDB.load();
|
|
176
|
+
* const connection = duckdb.connect();
|
|
177
|
+
*
|
|
178
|
+
* const result = connection.query("SELECT 42 AS answer");
|
|
179
|
+
* console.log(result.getValue(0, 0)); // 42
|
|
180
|
+
*
|
|
181
|
+
* result.delete();
|
|
182
|
+
* connection.delete(); // Important: clean up!
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
connect() {
|
|
186
|
+
return this.db.connect()!;
|
|
80
187
|
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Returns the number of threads available to DuckDB.
|
|
191
|
+
*
|
|
192
|
+
* @returns The number of threads
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```ts
|
|
196
|
+
* const duckdb = await DuckDB.load();
|
|
197
|
+
* console.log(duckdb.numberOfThreads()); // 1
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
numberOfThreads(): number {
|
|
201
|
+
return Number(this.db.numberOfThreads());
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Registers a binary file in the virtual file system.
|
|
206
|
+
*
|
|
207
|
+
* This allows DuckDB to read files as if they were on disk, which is useful for
|
|
208
|
+
* reading CSV, JSON, Parquet, and other file formats using DuckDB's file readers.
|
|
209
|
+
*
|
|
210
|
+
* The path is normalized to remove leading slashes and directories are created
|
|
211
|
+
* automatically as needed.
|
|
212
|
+
*
|
|
213
|
+
* @param path - The path where the file should be accessible (e.g., "data/users.csv")
|
|
214
|
+
* @param content - The file content as a Uint8Array
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* ```ts
|
|
218
|
+
* const csvData = "id,name\n1,Alice\n2,Bob";
|
|
219
|
+
* const bytes = new TextEncoder().encode(csvData);
|
|
220
|
+
* duckdb.registerFile("data.csv", bytes);
|
|
221
|
+
*
|
|
222
|
+
* const connection = duckdb.connect();
|
|
223
|
+
* const result = connection.query("SELECT * FROM read_csv_auto('data.csv')");
|
|
224
|
+
* console.log(result.rowCount()); // 2
|
|
225
|
+
* result.delete();
|
|
226
|
+
* connection.delete();
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
registerFile(path: string, content: Uint8Array): void {
|
|
230
|
+
const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
|
|
231
|
+
const split = normalizedPath.lastIndexOf("/");
|
|
232
|
+
const dir = split > 0 ? normalizedPath.substring(0, split) : "/";
|
|
233
|
+
if (dir.length > 1 && this._module.FS_createPath) {
|
|
234
|
+
this._module.FS_createPath(dir, true, true);
|
|
235
|
+
}
|
|
236
|
+
this._module.FS_createDataFile(normalizedPath, undefined, content, true, true, true);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Registers a text file in the virtual file system.
|
|
241
|
+
*
|
|
242
|
+
* This is a convenience method that encodes the string content to UTF-8 bytes
|
|
243
|
+
* and calls {@link registerFile}. Useful for registering JSON, CSV, or other
|
|
244
|
+
* text-based data files.
|
|
245
|
+
*
|
|
246
|
+
* @param fileName - The path where the file should be accessible
|
|
247
|
+
* @param content - The file content as a string
|
|
248
|
+
*
|
|
249
|
+
* @example Register JSON data
|
|
250
|
+
* ```ts
|
|
251
|
+
* const users = [
|
|
252
|
+
* { id: 1, name: "Alice", age: 30 },
|
|
253
|
+
* { id: 2, name: "Bob", age: 25 }
|
|
254
|
+
* ];
|
|
255
|
+
* duckdb.registerFileString("users.json", JSON.stringify(users));
|
|
256
|
+
*
|
|
257
|
+
* const connection = duckdb.connect();
|
|
258
|
+
* const result = connection.query(`
|
|
259
|
+
* SELECT name, age
|
|
260
|
+
* FROM read_json_auto('users.json')
|
|
261
|
+
* WHERE age > 26
|
|
262
|
+
* `);
|
|
263
|
+
* console.log(result.toJSON()); // [{"name":"Alice","age":30}]
|
|
264
|
+
* result.delete();
|
|
265
|
+
* connection.delete();
|
|
266
|
+
* ```
|
|
267
|
+
*
|
|
268
|
+
* @example Register CSV data
|
|
269
|
+
* ```ts
|
|
270
|
+
* const csvData = `id,product,price
|
|
271
|
+
* 1,Laptop,999.99
|
|
272
|
+
* 2,Mouse,29.99`;
|
|
273
|
+
*
|
|
274
|
+
* duckdb.registerFileString("products.csv", csvData);
|
|
275
|
+
*
|
|
276
|
+
* const connection = duckdb.connect();
|
|
277
|
+
* const result = connection.query("SELECT * FROM read_csv_auto('products.csv')");
|
|
278
|
+
* result.print();
|
|
279
|
+
* result.delete();
|
|
280
|
+
* connection.delete();
|
|
281
|
+
* ```
|
|
282
|
+
*/
|
|
283
|
+
registerFileString(fileName: string, content: string): void {
|
|
284
|
+
const encoded = textEncoder.encode(content);
|
|
285
|
+
this.registerFile(fileName, encoded);
|
|
286
|
+
}
|
|
287
|
+
|
|
81
288
|
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import type { MainModule, DuckDB as CPPDuckDB } from "../types/duckdblib.js";
|
|
2
|
+
import { MainModuleEx } from "@hpcc-js/wasm-util";
|
|
3
|
+
/**
|
|
4
|
+
* DuckDB WASM library - an in-process SQL OLAP Database Management System.
|
|
5
|
+
*
|
|
6
|
+
* DuckDB is designed for OLAP (Online Analytical Processing) workloads and provides:
|
|
7
|
+
* - Full SQL support with rich analytical functions
|
|
8
|
+
* - In-process execution (no server required)
|
|
9
|
+
* - Efficient columnar storage
|
|
10
|
+
* - Support for reading CSV, JSON, and other formats
|
|
11
|
+
* - Transaction management
|
|
12
|
+
* - Prepared statements with parameter binding
|
|
13
|
+
*
|
|
14
|
+
* @example Basic Query
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { DuckDB } from "@hpcc-js/wasm-duckdb";
|
|
17
|
+
*
|
|
18
|
+
* const duckdb = await DuckDB.load();
|
|
19
|
+
* const connection = duckdb.connect();
|
|
20
|
+
*
|
|
21
|
+
* const result = connection.query("SELECT 'Hello, DuckDB!' AS message");
|
|
22
|
+
* console.log(result.getValue(0, 0)); // "Hello, DuckDB!"
|
|
23
|
+
*
|
|
24
|
+
* result.delete();
|
|
25
|
+
* connection.delete();
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @example Working with JSON Files
|
|
29
|
+
* ```ts
|
|
30
|
+
* const duckdb = await DuckDB.load();
|
|
31
|
+
* const connection = duckdb.connect();
|
|
32
|
+
*
|
|
33
|
+
* const data = [
|
|
34
|
+
* { id: 1, name: "Alice", age: 30 },
|
|
35
|
+
* { id: 2, name: "Bob", age: 25 }
|
|
36
|
+
* ];
|
|
37
|
+
* duckdb.registerFileString("users.json", JSON.stringify(data));
|
|
38
|
+
*
|
|
39
|
+
* const result = connection.query("SELECT * FROM read_json_auto('users.json') WHERE age > 26");
|
|
40
|
+
* console.log(result.toJSON()); // [{"id":1,"name":"Alice","age":30}]
|
|
41
|
+
*
|
|
42
|
+
* result.delete();
|
|
43
|
+
* connection.delete();
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @example Prepared Statements
|
|
47
|
+
* ```ts
|
|
48
|
+
* const connection = duckdb.connect();
|
|
49
|
+
* connection.query("CREATE TABLE users (id INTEGER, name VARCHAR, age INTEGER)").delete();
|
|
50
|
+
*
|
|
51
|
+
* const insertStmt = connection.prepare("INSERT INTO users VALUES (?, ?, ?)");
|
|
52
|
+
* insertStmt.execute([1, "Alice", 30]).delete();
|
|
53
|
+
* insertStmt.execute([2, "Bob", 25]).delete();
|
|
54
|
+
* insertStmt.delete();
|
|
55
|
+
*
|
|
56
|
+
* const queryStmt = connection.prepare("SELECT * FROM users WHERE age > ?");
|
|
57
|
+
* const result = queryStmt.execute([26]);
|
|
58
|
+
* console.log(result.toJSON());
|
|
59
|
+
*
|
|
60
|
+
* result.delete();
|
|
61
|
+
* queryStmt.delete();
|
|
62
|
+
* connection.delete();
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @example Transaction Management
|
|
66
|
+
* ```ts
|
|
67
|
+
* const connection = duckdb.connect();
|
|
68
|
+
*
|
|
69
|
+
* connection.setAutoCommit(false);
|
|
70
|
+
* connection.beginTransaction();
|
|
71
|
+
*
|
|
72
|
+
* try {
|
|
73
|
+
* connection.query("UPDATE accounts SET balance = balance - 200 WHERE id = 1").delete();
|
|
74
|
+
* connection.query("UPDATE accounts SET balance = balance + 200 WHERE id = 2").delete();
|
|
75
|
+
* connection.commit();
|
|
76
|
+
* console.log("Transfer successful");
|
|
77
|
+
* } catch (error) {
|
|
78
|
+
* connection.rollback();
|
|
79
|
+
* console.error("Transfer failed");
|
|
80
|
+
* }
|
|
81
|
+
*
|
|
82
|
+
* connection.delete();
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* @see [DuckDB Documentation](https://duckdb.org/docs/)
|
|
86
|
+
* @see [DuckDB GitHub](https://github.com/duckdb/duckdb)
|
|
87
|
+
*/
|
|
88
|
+
export declare class DuckDB extends MainModuleEx<MainModule> {
|
|
89
|
+
db: CPPDuckDB;
|
|
90
|
+
private constructor();
|
|
91
|
+
/**
|
|
92
|
+
* Loads and initializes the DuckDB WASM module.
|
|
93
|
+
*
|
|
94
|
+
* This method compiles and instantiates the WebAssembly module. It returns a singleton
|
|
95
|
+
* instance, so subsequent calls will return the same instance.
|
|
96
|
+
*
|
|
97
|
+
* **Note:** WebAssembly compilation is disallowed on the main thread if the buffer size
|
|
98
|
+
* is larger than 4KB, which is why this method is asynchronous.
|
|
99
|
+
*
|
|
100
|
+
* @returns A promise that resolves to the DuckDB instance
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* const duckdb = await DuckDB.load();
|
|
105
|
+
* console.log(duckdb.version()); // e.g., "v1.4.3"
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
static load(): Promise<DuckDB>;
|
|
109
|
+
/**
|
|
110
|
+
* Unloads the compiled WASM instance.
|
|
111
|
+
*
|
|
112
|
+
* This is primarily useful for cleanup in testing scenarios or when you need to
|
|
113
|
+
* completely reset the DuckDB instance.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* DuckDB.unload();
|
|
118
|
+
* // The next call to DuckDB.load() will create a fresh instance
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
static unload(): void;
|
|
122
|
+
/**
|
|
123
|
+
* Returns the DuckDB version string.
|
|
124
|
+
*
|
|
125
|
+
* @returns The version string (e.g., "v1.4.3")
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```ts
|
|
129
|
+
* const duckdb = await DuckDB.load();
|
|
130
|
+
* console.log(duckdb.version()); // "v1.4.3"
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
version(): string;
|
|
134
|
+
/**
|
|
135
|
+
* Creates a new database connection.
|
|
136
|
+
*
|
|
137
|
+
* Each connection maintains its own transaction state and can execute queries
|
|
138
|
+
* independently. Always call `.delete()` on the connection when done to free resources.
|
|
139
|
+
*
|
|
140
|
+
* @returns A new Connection instance
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```ts
|
|
144
|
+
* const duckdb = await DuckDB.load();
|
|
145
|
+
* const connection = duckdb.connect();
|
|
146
|
+
*
|
|
147
|
+
* const result = connection.query("SELECT 42 AS answer");
|
|
148
|
+
* console.log(result.getValue(0, 0)); // 42
|
|
149
|
+
*
|
|
150
|
+
* result.delete();
|
|
151
|
+
* connection.delete(); // Important: clean up!
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
connect(): import("../types/duckdblib.js").Connection;
|
|
155
|
+
/**
|
|
156
|
+
* Returns the number of threads available to DuckDB.
|
|
157
|
+
*
|
|
158
|
+
* @returns The number of threads
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```ts
|
|
162
|
+
* const duckdb = await DuckDB.load();
|
|
163
|
+
* console.log(duckdb.numberOfThreads()); // 1
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
numberOfThreads(): number;
|
|
167
|
+
/**
|
|
168
|
+
* Registers a binary file in the virtual file system.
|
|
169
|
+
*
|
|
170
|
+
* This allows DuckDB to read files as if they were on disk, which is useful for
|
|
171
|
+
* reading CSV, JSON, Parquet, and other file formats using DuckDB's file readers.
|
|
172
|
+
*
|
|
173
|
+
* The path is normalized to remove leading slashes and directories are created
|
|
174
|
+
* automatically as needed.
|
|
175
|
+
*
|
|
176
|
+
* @param path - The path where the file should be accessible (e.g., "data/users.csv")
|
|
177
|
+
* @param content - The file content as a Uint8Array
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* const csvData = "id,name\n1,Alice\n2,Bob";
|
|
182
|
+
* const bytes = new TextEncoder().encode(csvData);
|
|
183
|
+
* duckdb.registerFile("data.csv", bytes);
|
|
184
|
+
*
|
|
185
|
+
* const connection = duckdb.connect();
|
|
186
|
+
* const result = connection.query("SELECT * FROM read_csv_auto('data.csv')");
|
|
187
|
+
* console.log(result.rowCount()); // 2
|
|
188
|
+
* result.delete();
|
|
189
|
+
* connection.delete();
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
registerFile(path: string, content: Uint8Array): void;
|
|
193
|
+
/**
|
|
194
|
+
* Registers a text file in the virtual file system.
|
|
195
|
+
*
|
|
196
|
+
* This is a convenience method that encodes the string content to UTF-8 bytes
|
|
197
|
+
* and calls {@link registerFile}. Useful for registering JSON, CSV, or other
|
|
198
|
+
* text-based data files.
|
|
199
|
+
*
|
|
200
|
+
* @param fileName - The path where the file should be accessible
|
|
201
|
+
* @param content - The file content as a string
|
|
202
|
+
*
|
|
203
|
+
* @example Register JSON data
|
|
204
|
+
* ```ts
|
|
205
|
+
* const users = [
|
|
206
|
+
* { id: 1, name: "Alice", age: 30 },
|
|
207
|
+
* { id: 2, name: "Bob", age: 25 }
|
|
208
|
+
* ];
|
|
209
|
+
* duckdb.registerFileString("users.json", JSON.stringify(users));
|
|
210
|
+
*
|
|
211
|
+
* const connection = duckdb.connect();
|
|
212
|
+
* const result = connection.query(`
|
|
213
|
+
* SELECT name, age
|
|
214
|
+
* FROM read_json_auto('users.json')
|
|
215
|
+
* WHERE age > 26
|
|
216
|
+
* `);
|
|
217
|
+
* console.log(result.toJSON()); // [{"name":"Alice","age":30}]
|
|
218
|
+
* result.delete();
|
|
219
|
+
* connection.delete();
|
|
220
|
+
* ```
|
|
221
|
+
*
|
|
222
|
+
* @example Register CSV data
|
|
223
|
+
* ```ts
|
|
224
|
+
* const csvData = `id,product,price
|
|
225
|
+
* 1,Laptop,999.99
|
|
226
|
+
* 2,Mouse,29.99`;
|
|
227
|
+
*
|
|
228
|
+
* duckdb.registerFileString("products.csv", csvData);
|
|
229
|
+
*
|
|
230
|
+
* const connection = duckdb.connect();
|
|
231
|
+
* const result = connection.query("SELECT * FROM read_csv_auto('products.csv')");
|
|
232
|
+
* result.print();
|
|
233
|
+
* result.delete();
|
|
234
|
+
* connection.delete();
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
registerFileString(fileName: string, content: string): void;
|
|
238
|
+
}
|