...
 
Commits (5)
{
"name": "@creanet/js-db",
"version": "2.5.1",
"version": "2.5.4",
"description": "Used for communication between NodeJS and PostgreSQL",
"main": "index.js",
"scripts": {
......
......@@ -17,6 +17,8 @@ class Query {
this.success = null;
this.rows = null;
this.err = null;
this._onsuccess = null;
}
/**
......@@ -39,6 +41,17 @@ class Query {
return is.not.emptyString(this.query);
}
/**
* Sets the onsuccess callback
*
* @param {Function} onsuccess
* @returns Query
*/
onsuccess(onsuccess) {
this._onsuccess = onsuccess;
return this;
}
/**
* Executes a generic query
* - returns `true` if successful; `false` otherwise
......@@ -59,6 +72,11 @@ class Query {
resolve(false);
} else {
this.rows = res.rows;
if (this._onsuccess instanceof Function) {
this._onsuccess();
}
resolve(true);
}
});
......
......@@ -96,7 +96,7 @@ class Database {
throw new Error('A transaction is already active');
}
return this.query('BEGIN');
return this.query('BEGIN').onsuccess(() => (this._transaction = true));
}
/**
......@@ -109,7 +109,7 @@ class Database {
throw new Error('No transaction is active');
}
return this.query('COMMIT');
return this.query('COMMIT').onsuccess(() => (this._transaction = false));
}
}
......