diff --git a/bindings/javascript/index.d.ts b/bindings/javascript/index.d.ts index f9447e696..097640feb 100644 --- a/bindings/javascript/index.d.ts +++ b/bindings/javascript/index.d.ts @@ -11,6 +11,8 @@ export declare class Database { constructor(path: string) /** Returns whether the database is in memory-only mode. */ get memory(): boolean + /** Returns whether the database connection is open. */ + get open(): boolean /** * Executes a batch of SQL statements. * diff --git a/bindings/javascript/promise.js b/bindings/javascript/promise.js index 7a54871c3..7ff274743 100644 --- a/bindings/javascript/promise.js +++ b/bindings/javascript/promise.js @@ -87,6 +87,10 @@ class Database { * @param {string} sql - The SQL statement string to prepare. */ prepare(sql) { + if (!this.open) { + throw new TypeError("The database connection is not open"); + } + if (!sql) { throw new RangeError("The supplied SQL string contains no statements"); } diff --git a/bindings/javascript/src/lib.rs b/bindings/javascript/src/lib.rs index 7ce6eb521..df17170e1 100644 --- a/bindings/javascript/src/lib.rs +++ b/bindings/javascript/src/lib.rs @@ -35,6 +35,7 @@ pub struct Database { io: Arc, conn: Arc, is_memory: bool, + is_open: RefCell, } #[napi] @@ -76,6 +77,7 @@ impl Database { io, conn, is_memory, + is_open: RefCell::new(true), }) } @@ -85,6 +87,12 @@ impl Database { self.is_memory } + /// Returns whether the database connection is open. + #[napi(getter)] + pub fn open(&self) -> bool { + *self.is_open.borrow() + } + /// Executes a batch of SQL statements. /// /// # Arguments @@ -167,6 +175,7 @@ impl Database { /// `Ok(())` if the database is closed successfully. #[napi] pub fn close(&self) -> Result<()> { + *self.is_open.borrow_mut() = false; // Database close is handled automatically when dropped Ok(()) } diff --git a/bindings/javascript/sync.js b/bindings/javascript/sync.js index bca456232..555d2c245 100644 --- a/bindings/javascript/sync.js +++ b/bindings/javascript/sync.js @@ -87,6 +87,10 @@ class Database { * @param {string} sql - The SQL statement string to prepare. */ prepare(sql) { + if (!this.open) { + throw new TypeError("The database connection is not open"); + } + if (!sql) { throw new RangeError("The supplied SQL string contains no statements"); }