mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-25 12:04:21 +01:00
javascript: Implement transactions API
This commit is contained in:
@@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user