bindings/javascript: Improve error when prepare() called after close()

This commit is contained in:
Pekka Enberg
2025-08-06 07:48:15 +03:00
parent f6fb786cc9
commit 79412ea2cc
4 changed files with 19 additions and 0 deletions

View File

@@ -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.
*

View File

@@ -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");
}

View File

@@ -35,6 +35,7 @@ pub struct Database {
io: Arc<dyn turso_core::IO>,
conn: Arc<turso_core::Connection>,
is_memory: bool,
is_open: RefCell<bool>,
}
#[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(())
}

View File

@@ -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");
}