Commit 5a94b249 authored by Milan Wikarski's avatar Milan Wikarski
Browse files

Added integration for BEGIN and COMMIT

parent 4c1ad910
......@@ -22,6 +22,16 @@ class Database {
*/
constructor({ host, port, database, user, password }) {
this._db = new Pool({ host, port, database, user, password });
this._transaction = false;
}
/**
* Returns `true` if a transaction is active; `false` otherwise
*
* @readonly
*/
get transaction() {
return this._transaction;
}
/**
......@@ -75,6 +85,36 @@ class Database {
update(table) {
return new Update(this, table);
}
/**
* Begins a transaction
*
* @returns {Promise.<Boolean>} success
*/
begin() {
if (this._transaction) {
throw new Error('A transaction is already active');
}
return this.query('BEGIN')
.execute()
.then(() => (this._transaction = true));
}
/**
* Commits an active transaction
*
* @returns {Promise.<Boolean>} success
*/
commit() {
if (!this._transaction) {
throw new Error('No transaction is active');
}
return this.query('COMMIT')
.execute()
.then(() => (this._transaction = false));
}
}
module.exports = Database;
......@@ -46,6 +46,8 @@ const del = async () => {
};
const update = async () => {
await db.begin();
console.log('UPDATE');
const update = db
.update('lorem')
......@@ -58,6 +60,8 @@ const update = async () => {
} else {
console.log(update.err);
}
await db.commit();
};
(async () => {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment