@@ -3,7 +3,7 @@ import load, { reset } from "../../../build/packages/duckdb/duckdblib.wasm";
33import type { MainModule , DuckDB as CPPDuckDB } from "../types/duckdblib.js" ;
44import { MainModuleEx } from "@hpcc-js/wasm-util" ;
55
6- let g_duckdb : Promise < DuckDB > ;
6+ let g_duckdb : Promise < DuckDB > | undefined ;
77const textEncoder = new TextEncoder ( ) ;
88
99/**
@@ -91,7 +91,7 @@ const textEncoder = new TextEncoder();
9191 * @see [DuckDB Documentation](https://duckdb.org/docs/)
9292 * @see [DuckDB GitHub](https://github.com/duckdb/duckdb)
9393 */
94- export class DuckDB extends MainModuleEx < MainModule > implements Disposable {
94+ export class DuckDB extends MainModuleEx < MainModule > {
9595
9696 db : CPPDuckDB
9797
@@ -103,47 +103,6 @@ export class DuckDB extends MainModuleEx<MainModule> implements Disposable {
103103 FS_createPath ( "/" , "home/web_user" , true , true ) ;
104104 }
105105
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-
147106 /**
148107 * Loads and initializes the DuckDB WASM module.
149108 *
@@ -167,7 +126,7 @@ export class DuckDB extends MainModuleEx<MainModule> implements Disposable {
167126 return new DuckDB ( module )
168127 } ) ;
169128 }
170- return g_duckdb ;
129+ return g_duckdb ! ;
171130 }
172131
173132 /**
@@ -182,8 +141,20 @@ export class DuckDB extends MainModuleEx<MainModule> implements Disposable {
182141 * // The next call to DuckDB.load() will create a fresh instance
183142 * ```
184143 */
185- static unload ( ) {
186- reset ( ) ;
144+ static async unload ( ) {
145+ try {
146+ const duckdb = await g_duckdb ;
147+ if ( duckdb ?. db ) {
148+ try {
149+ duckdb . db . terminate ( ) ;
150+ } finally {
151+ duckdb . db . delete ( ) ;
152+ }
153+ }
154+ } finally {
155+ reset ( ) ;
156+ g_duckdb = undefined ;
157+ }
187158 }
188159
189160 /**
@@ -240,6 +211,27 @@ export class DuckDB extends MainModuleEx<MainModule> implements Disposable {
240211 return Number ( this . db . numberOfThreads ( ) ) ;
241212 }
242213
214+ getExceptionMessage ( error : unknown ) : { type : string , message ?: string } {
215+ const mod : any = this . _module as any ;
216+ try {
217+ const result = mod ?. getExceptionMessage ?.( error ) ;
218+ if ( Array . isArray ( result ) ) {
219+ return {
220+ type : result [ 0 ] ?? "" ,
221+ message : result [ 1 ] ?? ""
222+ } ;
223+ }
224+ } catch ( _err ) {
225+ void _err ;
226+ }
227+
228+ const err : any = error ;
229+ return {
230+ type : err ?. name ?? "Error" ,
231+ message : typeof err ?. message === "string" ? err . message : String ( error )
232+ } ;
233+ }
234+
243235 /**
244236 * Registers a binary file in the virtual file system.
245237 *
@@ -371,5 +363,5 @@ export class DuckDB extends MainModuleEx<MainModule> implements Disposable {
371363 const encoded = textEncoder . encode ( content ) ;
372364 this . registerFile ( fileName , encoded ) ;
373365 }
374-
375366}
367+
0 commit comments