Skip to content

Commit 9637493

Browse files
committed
Implement Database.close()
1 parent a0141a9 commit 9637493

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

docs/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Executes a SQL statement.
7474

7575
### close() ⇒ this
7676

77-
This function is currently not supported.
77+
Closes the database connection.
7878

7979
# class Statement
8080

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { load, currentTarget } = require('@neon-rs/load');
55
// Static requires for bundlers.
66
if (0) { require('./.targets'); }
77

8-
const { databaseOpen, databaseOpenWithRpcSync, databaseSync, databaseExec, databasePrepare, statementRaw, statementGet, statementRun, statementRows, rowsNext } = load(__dirname) || require(`@libsql/experimental-${currentTarget()}`);
8+
const { databaseOpen, databaseOpenWithRpcSync, databaseClose, databaseSync, databaseExec, databasePrepare, statementRaw, statementGet, statementRun, statementRows, rowsNext } = load(__dirname) || require(`@libsql/experimental-${currentTarget()}`);
99

1010
/**
1111
* Database represents a connection that can prepare and execute SQL statements.
@@ -109,8 +109,11 @@ class Database {
109109
databaseExec.call(this.db, sql);
110110
}
111111

112+
/**
113+
* Closes the database connection.
114+
*/
112115
close() {
113-
throw new Error("not implemented")
116+
databaseSync.call(this.db);
114117
}
115118

116119
defaultSafeIntegers(...args) {

src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ impl Database {
3838
Ok(cx.boxed(db))
3939
}
4040

41+
fn js_close(mut cx: FunctionContext) -> JsResult<JsUndefined> {
42+
let db = cx.this().downcast_or_throw::<JsBox<Database>, _>(&mut cx)?;
43+
db.db.close();
44+
Ok(cx.undefined())
45+
}
46+
47+
4148
fn js_sync(mut cx: FunctionContext) -> JsResult<JsUndefined> {
4249
let db = cx.this().downcast_or_throw::<JsBox<Database>, _>(&mut cx)?;
4350
db.rt.block_on(db.db.sync()).unwrap();
@@ -289,6 +296,7 @@ fn convert_row_raw(
289296
fn main(mut cx: ModuleContext) -> NeonResult<()> {
290297
cx.export_function("databaseOpen", Database::js_open)?;
291298
cx.export_function("databaseOpenWithRpcSync", Database::js_open_with_rpc_sync)?;
299+
cx.export_function("databaseClose", Database::js_close)?;
292300
cx.export_function("databaseSync", Database::js_sync)?;
293301
cx.export_function("databaseExec", Database::js_exec)?;
294302
cx.export_function("databasePrepare", Database::js_prepare)?;

0 commit comments

Comments
 (0)