javascript: Implement transactions API

This commit is contained in:
Pekka Enberg
2025-08-19 16:29:02 +03:00
parent 387d384394
commit 54b4fdaa7d
5 changed files with 90 additions and 15 deletions

View File

@@ -38,6 +38,7 @@ class Database {
db: NativeDB;
memory: boolean;
open: boolean;
private _inTransaction: boolean = false;
/**
* Creates a new database connection. If the database file pointed to by `path` does not exists, it will be created.
@@ -61,9 +62,7 @@ class Database {
Object.defineProperties(this, {
inTransaction: {
get() {
throw new Error("not implemented");
},
get: () => this._inTransaction,
},
name: {
get() {
@@ -117,12 +116,15 @@ class Database {
const wrapTxn = (mode) => {
return (...bindParameters) => {
db.exec("BEGIN " + mode);
db._inTransaction = true;
try {
const result = fn(...bindParameters);
db.exec("COMMIT");
db._inTransaction = false;
return result;
} catch (err) {
db.exec("ROLLBACK");
db._inTransaction = false;
throw err;
}
};

View File

@@ -38,6 +38,7 @@ class Database {
db: NativeDB;
memory: boolean;
open: boolean;
private _inTransaction: boolean = false;
/**
* Creates a new database connection. If the database file pointed to by `path` does not exists, it will be created.
*
@@ -65,9 +66,7 @@ class Database {
this.memory = db.memory;
Object.defineProperties(this, {
inTransaction: {
get() {
throw new Error("not implemented");
},
get: () => this._inTransaction,
},
name: {
get() {
@@ -121,12 +120,15 @@ class Database {
const wrapTxn = (mode) => {
return (...bindParameters) => {
db.exec("BEGIN " + mode);
db._inTransaction = true;
try {
const result = fn(...bindParameters);
db.exec("COMMIT");
db._inTransaction = false;
return result;
} catch (err) {
db.exec("ROLLBACK");
db._inTransaction = false;
throw err;
}
};