Skip to content

Commit d239e41

Browse files
committed
Make Database.exec() actually async
1 parent 4403151 commit d239e41

File tree

5 files changed

+30
-16
lines changed

5 files changed

+30
-16
lines changed

index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export interface SyncResult {
2525
export declare function databasePrepareSync(db: Database, sql: string): Statement
2626
/** Syncs the database in blocking mode. */
2727
export declare function databaseSyncSync(db: Database): SyncResult
28+
/** Executes SQL in blocking mode. */
29+
export declare function databaseExecSync(db: Database, sql: string): void
2830
export declare function statementIterateSync(stmt: Statement, params?: unknown | undefined | null): RowsIterator
2931
/** SQLite `run()` result object */
3032
export interface RunResult {
@@ -109,7 +111,7 @@ export declare class Database {
109111
* * `env` - The environment.
110112
* * `sql` - The SQL statement to execute.
111113
*/
112-
exec(sql: string): void
114+
exec(sql: string): Promise<void>
113115
/**
114116
* Syncs the database.
115117
*

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,12 @@ if (!nativeBinding) {
310310
throw new Error(`Failed to load native binding`)
311311
}
312312

313-
const { Database, databasePrepareSync, databaseSyncSync, Statement, statementIterateSync, RowsIterator, iteratorNextSync, Record } = nativeBinding
313+
const { Database, databasePrepareSync, databaseSyncSync, databaseExecSync, Statement, statementIterateSync, RowsIterator, iteratorNextSync, Record } = nativeBinding
314314

315315
module.exports.Database = Database
316316
module.exports.databasePrepareSync = databasePrepareSync
317317
module.exports.databaseSyncSync = databaseSyncSync
318+
module.exports.databaseExecSync = databaseExecSync
318319
module.exports.Statement = Statement
319320
module.exports.statementIterateSync = statementIterateSync
320321
module.exports.RowsIterator = RowsIterator

promise.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ class Database {
8888

8989
const db = this;
9090
const wrapTxn = (mode) => {
91-
return (...bindParameters) => {
92-
db.exec("BEGIN " + mode);
91+
return async (...bindParameters) => {
92+
await db.exec("BEGIN " + mode);
9393
try {
94-
const result = fn(...bindParameters);
95-
db.exec("COMMIT");
94+
const result = await fn(...bindParameters);
95+
await db.exec("COMMIT");
9696
return result;
9797
} catch (err) {
98-
db.exec("ROLLBACK");
98+
await db.exec("ROLLBACK");
9999
throw err;
100100
}
101101
};
@@ -172,9 +172,9 @@ class Database {
172172
*
173173
* @param {string} sql - The SQL statement string to execute.
174174
*/
175-
exec(sql) {
175+
async exec(sql) {
176176
try {
177-
this.db.exec(sql);
177+
await this.db.exec(sql);
178178
} catch (err) {
179179
throw convertError(err);
180180
}

src/lib.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -501,14 +501,18 @@ impl Database {
501501
/// * `env` - The environment.
502502
/// * `sql` - The SQL statement to execute.
503503
#[napi]
504-
pub fn exec(&self, env: Env, sql: String) -> Result<()> {
505-
let rt = runtime()?;
504+
pub async fn exec(&self, sql: String) -> Result<()> {
506505
let conn = match &self.conn {
507506
Some(conn) => conn.clone(),
508-
None => return Err(throw_database_closed_error(&env).into()),
507+
None => {
508+
return Err(throw_sqlite_error(
509+
"The database connection is not open".to_string(),
510+
"SQLITE_NOTOPEN".to_string(),
511+
0,
512+
));
513+
}
509514
};
510-
rt.block_on(async move { conn.execute_batch(&sql).await })
511-
.map_err(Error::from)?;
515+
conn.execute_batch(&sql).await.map_err(Error::from)?;
512516
Ok(())
513517
}
514518

@@ -584,6 +588,13 @@ pub fn database_sync_sync(db: &Database) -> Result<SyncResult> {
584588
rt.block_on(async move { db.sync().await })
585589
}
586590

591+
/// Executes SQL in blocking mode.
592+
#[napi]
593+
pub fn database_exec_sync(db: &Database, sql: String) -> Result<()> {
594+
let rt = runtime()?;
595+
rt.block_on(async move { db.exec(sql).await })
596+
}
597+
587598
fn is_remote_path(path: &str) -> bool {
588599
path.starts_with("libsql://") || path.starts_with("http://") || path.starts_with("https://")
589600
}

wrapper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
const { Database: NativeDb, databasePrepareSync, databaseSyncSync, statementIterateSync, iteratorNextSync } = require("./index.js");
3+
const { Database: NativeDb, databasePrepareSync, databaseSyncSync, databaseExecSync, statementIterateSync, iteratorNextSync } = require("./index.js");
44
const SqliteError = require("./sqlite-error.js");
55
const Authorization = require("./auth");
66

@@ -178,7 +178,7 @@ class Database {
178178
*/
179179
exec(sql) {
180180
try {
181-
this.db.exec(sql);
181+
databaseExecSync(this.db, sql);
182182
} catch (err) {
183183
throw convertError(err);
184184
}

0 commit comments

Comments
 (0)