@hpcc-js/wasm-duckdb 1.13.1 → 1.15.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hpcc-js/wasm-duckdb",
3
- "version": "1.13.1",
3
+ "version": "1.15.0",
4
4
  "description": "hpcc-js - WASM DuckDB",
5
5
  "type": "module",
6
6
  "exports": {
@@ -38,7 +38,7 @@
38
38
  "update-major": "npx -y npm-check-updates -u"
39
39
  },
40
40
  "devDependencies": {
41
- "@hpcc-js/esbuild-plugins": "1.7.0",
41
+ "@hpcc-js/esbuild-plugins": "1.8.0",
42
42
  "@hpcc-js/wasm-util": "1.0.0"
43
43
  },
44
44
  "keywords": [
@@ -55,5 +55,5 @@
55
55
  },
56
56
  "homepage": "https://hpcc-systems.github.io/hpcc-js-wasm/",
57
57
  "license": "Apache-2.0",
58
- "gitHead": "11395f5acfc865accde71905509bb45d5c79b11b"
58
+ "gitHead": "81504905bd26f44c1e68b31760269dee081c0cbb"
59
59
  }
package/src/duckdb.ts CHANGED
@@ -91,20 +91,59 @@ const textEncoder = new TextEncoder();
91
91
  * @see [DuckDB Documentation](https://duckdb.org/docs/)
92
92
  * @see [DuckDB GitHub](https://github.com/duckdb/duckdb)
93
93
  */
94
-
95
-
96
-
97
- export class DuckDB extends MainModuleEx<MainModule> {
94
+ export class DuckDB extends MainModuleEx<MainModule> implements Disposable {
98
95
 
99
96
  db: CPPDuckDB
100
97
 
101
98
  private constructor(_module: MainModule) {
102
99
  super(_module);
103
100
  this.db = this._module.create()!;
101
+ // Special home directory for web_user (needed for some extensions)
104
102
  const { FS_createPath } = this._module;
105
103
  FS_createPath("/", "home/web_user", true, true);
106
104
  }
107
105
 
106
+ /**
107
+ * Disposes of the DuckDB database instance and releases resources.
108
+ *
109
+ * This method should be called when you're done using the database to free up
110
+ * memory and clean up resources. After calling dispose, the instance should not be used.
111
+ *
112
+ * @example
113
+ * ```ts
114
+ * const duckdb = await DuckDB.load();
115
+ * const connection = duckdb.connect();
116
+ * // ... use the database ...
117
+ * connection.delete();
118
+ * duckdb.dispose();
119
+ * ```
120
+ *
121
+ * @example Using statement (TypeScript 5.2+)
122
+ * ```ts
123
+ * {
124
+ * using duckdb = await DuckDB.load();
125
+ * const connection = duckdb.connect();
126
+ * // ... use the database ...
127
+ * connection.delete();
128
+ * } // duckdb.dispose() is called automatically
129
+ * ```
130
+ */
131
+ dispose(): void {
132
+ try {
133
+ this.db?.terminate();
134
+ } finally {
135
+ this.db?.delete();
136
+ }
137
+ }
138
+
139
+ /**
140
+ * Symbol.dispose implementation for use with `using` declarations.
141
+ * @internal
142
+ */
143
+ [Symbol.dispose](): void {
144
+ this.dispose();
145
+ }
146
+
108
147
  /**
109
148
  * Loads and initializes the DuckDB WASM module.
110
149
  *
@@ -229,13 +268,61 @@ export class DuckDB extends MainModuleEx<MainModule> {
229
268
  registerFile(path: string, content: Uint8Array): void {
230
269
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
231
270
  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);
271
+ if (split > 0) {
272
+ const dir = normalizedPath.substring(0, split);
273
+ const parts = dir.split("/");
274
+ let currentPath = "/";
275
+ for (const part of parts) {
276
+ if (part && this._module.FS_createPath) {
277
+ this._module.FS_createPath(currentPath, part, true, true);
278
+ currentPath = currentPath === "/" ? "/" + part : currentPath + "/" + part;
279
+ }
280
+ }
235
281
  }
236
282
  this._module.FS_createDataFile(normalizedPath, undefined, content, true, true, true);
237
283
  }
238
284
 
285
+ /**
286
+ * Unregisters and removes a file from the virtual file system.
287
+ *
288
+ * This removes a file that was previously registered using {@link registerFile} or
289
+ * {@link registerFileString}. Once unregistered, the file will no longer be accessible
290
+ * to DuckDB queries.
291
+ *
292
+ * The path is normalized to remove leading slashes before removal.
293
+ *
294
+ * @param path - The path of the file to remove (e.g., "data/users.csv")
295
+ *
296
+ * @example
297
+ * ```ts
298
+ * const duckdb = await DuckDB.load();
299
+ *
300
+ * // Register a file
301
+ * duckdb.registerFileString("temp.csv", "id,name\n1,Alice");
302
+ *
303
+ * const connection = duckdb.connect();
304
+ * let result = connection.query("SELECT * FROM read_csv_auto('temp.csv')");
305
+ * console.log(result.rowCount()); // 1
306
+ * result.delete();
307
+ *
308
+ * // Unregister the file
309
+ * duckdb.unregisterFile("temp.csv");
310
+ *
311
+ * // File is no longer accessible
312
+ * try {
313
+ * result = connection.query("SELECT * FROM read_csv_auto('temp.csv')");
314
+ * } catch (error) {
315
+ * console.error("File not found"); // Error: file not accessible
316
+ * }
317
+ *
318
+ * connection.delete();
319
+ * ```
320
+ */
321
+ unregisterFile(path: string): void {
322
+ const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
323
+ this._module.FS_unlink(normalizedPath);
324
+ }
325
+
239
326
  /**
240
327
  * Registers a text file in the virtual file system.
241
328
  *
package/types/duckdb.d.ts CHANGED
@@ -85,9 +85,40 @@ import { MainModuleEx } from "@hpcc-js/wasm-util";
85
85
  * @see [DuckDB Documentation](https://duckdb.org/docs/)
86
86
  * @see [DuckDB GitHub](https://github.com/duckdb/duckdb)
87
87
  */
88
- export declare class DuckDB extends MainModuleEx<MainModule> {
88
+ export declare class DuckDB extends MainModuleEx<MainModule> implements Disposable {
89
89
  db: CPPDuckDB;
90
90
  private constructor();
91
+ /**
92
+ * Disposes of the DuckDB database instance and releases resources.
93
+ *
94
+ * This method should be called when you're done using the database to free up
95
+ * memory and clean up resources. After calling dispose, the instance should not be used.
96
+ *
97
+ * @example
98
+ * ```ts
99
+ * const duckdb = await DuckDB.load();
100
+ * const connection = duckdb.connect();
101
+ * // ... use the database ...
102
+ * connection.delete();
103
+ * duckdb.dispose();
104
+ * ```
105
+ *
106
+ * @example Using statement (TypeScript 5.2+)
107
+ * ```ts
108
+ * {
109
+ * using duckdb = await DuckDB.load();
110
+ * const connection = duckdb.connect();
111
+ * // ... use the database ...
112
+ * connection.delete();
113
+ * } // duckdb.dispose() is called automatically
114
+ * ```
115
+ */
116
+ dispose(): void;
117
+ /**
118
+ * Symbol.dispose implementation for use with `using` declarations.
119
+ * @internal
120
+ */
121
+ [Symbol.dispose](): void;
91
122
  /**
92
123
  * Loads and initializes the DuckDB WASM module.
93
124
  *
@@ -190,6 +221,43 @@ export declare class DuckDB extends MainModuleEx<MainModule> {
190
221
  * ```
191
222
  */
192
223
  registerFile(path: string, content: Uint8Array): void;
224
+ /**
225
+ * Unregisters and removes a file from the virtual file system.
226
+ *
227
+ * This removes a file that was previously registered using {@link registerFile} or
228
+ * {@link registerFileString}. Once unregistered, the file will no longer be accessible
229
+ * to DuckDB queries.
230
+ *
231
+ * The path is normalized to remove leading slashes before removal.
232
+ *
233
+ * @param path - The path of the file to remove (e.g., "data/users.csv")
234
+ *
235
+ * @example
236
+ * ```ts
237
+ * const duckdb = await DuckDB.load();
238
+ *
239
+ * // Register a file
240
+ * duckdb.registerFileString("temp.csv", "id,name\n1,Alice");
241
+ *
242
+ * const connection = duckdb.connect();
243
+ * let result = connection.query("SELECT * FROM read_csv_auto('temp.csv')");
244
+ * console.log(result.rowCount()); // 1
245
+ * result.delete();
246
+ *
247
+ * // Unregister the file
248
+ * duckdb.unregisterFile("temp.csv");
249
+ *
250
+ * // File is no longer accessible
251
+ * try {
252
+ * result = connection.query("SELECT * FROM read_csv_auto('temp.csv')");
253
+ * } catch (error) {
254
+ * console.error("File not found"); // Error: file not accessible
255
+ * }
256
+ *
257
+ * connection.delete();
258
+ * ```
259
+ */
260
+ unregisterFile(path: string): void;
193
261
  /**
194
262
  * Registers a text file in the virtual file system.
195
263
  *
@@ -127,6 +127,7 @@ export interface Connection extends ClassHandle {
127
127
  getQueryProgress(): number;
128
128
  prepare(_0: EmbindString): PreparedStatement | null;
129
129
  query(_0: EmbindString): MaterializedQueryResult | null;
130
+ queryToJSON(_0: EmbindString): string;
130
131
  }
131
132
 
132
133
  export interface DuckDB extends ClassHandle {