diff --git a/bindings/javascript/Cargo.toml b/bindings/javascript/Cargo.toml index f39d35251..18a9b319d 100644 --- a/bindings/javascript/Cargo.toml +++ b/bindings/javascript/Cargo.toml @@ -8,7 +8,7 @@ repository.workspace = true description = "The Turso database library Node bindings" [lib] -crate-type = ["cdylib"] +crate-type = ["cdylib", "lib"] [dependencies] turso_core = { workspace = true } diff --git a/bindings/javascript/promise.js b/bindings/javascript/promise.js index e23c8ac9d..e00e41857 100644 --- a/bindings/javascript/promise.js +++ b/bindings/javascript/promise.js @@ -54,9 +54,14 @@ class Database { opts.timeout = opts.timeout === undefined ? 0 : opts.timeout; this.db = new NativeDB(path, opts); - this.memory = this.db.memory; - const db = this.db; - + this.initialize(db, opts.path, opts.readonly); + } + static create() { + return Object.create(this.prototype); + } + initialize(db, name, readonly) { + this.db = db; + this.memory = db.memory; Object.defineProperties(this, { inTransaction: { get() { @@ -65,12 +70,12 @@ class Database { }, name: { get() { - return path; + return name; }, }, readonly: { get() { - return opts.readonly; + return readonly; }, }, open: { diff --git a/bindings/javascript/src/lib.rs b/bindings/javascript/src/lib.rs index 01a5b3d5e..ab50ca030 100644 --- a/bindings/javascript/src/lib.rs +++ b/bindings/javascript/src/lib.rs @@ -34,8 +34,9 @@ enum PresentationMode { /// A database connection. #[napi] +#[derive(Clone)] pub struct Database { - _db: Arc, + _db: Option>, io: Arc, conn: Arc, is_memory: bool, @@ -76,13 +77,22 @@ impl Database { .connect() .map_err(|e| Error::new(Status::GenericFailure, format!("Failed to connect: {e}")))?; - Ok(Database { + Ok(Self::create(Some(db), io, conn, is_memory)) + } + + pub fn create( + db: Option>, + io: Arc, + conn: Arc, + is_memory: bool, + ) -> Self { + Database { _db: db, io, conn, is_memory, is_open: Cell::new(true), - }) + } } /// Returns whether the database is in memory-only mode. @@ -418,7 +428,8 @@ impl Statement { /// Async task for running the I/O loop. pub struct IoLoopTask { - io: Arc, + // this field is set in the turso-sync-engine package + pub io: Arc, } impl Task for IoLoopTask {