...
 
Commits (2)
{
"name": "@creanet/js-db",
"version": "2.5.4",
"version": "2.5.5",
"description": "Used for communication between NodeJS and PostgreSQL",
"main": "index.js",
"scripts": {
......
......@@ -15,7 +15,10 @@ class Query {
this.executed = false;
this.success = null;
this.rows = null;
this.response = null;
this.err = null;
this._onsuccess = null;
......@@ -57,7 +60,6 @@ class Query {
* - returns `true` if successful; `false` otherwise
*
* @returns {Promise.<boolean>} success
* @memberof Query
*/
async execute() {
if (!this.canExecute) throw new Error('This query cannot be executed yet');
......@@ -65,13 +67,16 @@ class Query {
return new Promise(resolve => {
this._db.query(this.query, this.data, (err, res) => {
this.executed = true;
this.response = res;
if (err) {
this.success = false;
this.err = err;
resolve(false);
} else {
this.rows = res.rows;
this.success = true;
if (this._onsuccess instanceof Function) {
this._onsuccess();
......
......@@ -19,7 +19,6 @@ class SpecificQuery extends Query {
*
* @param {Database} db
* @param {String} table
* @memberof SpecificQuery
*/
constructor(db, table) {
super(db);
......
/** @typedef {import("./query.class")} Query */
/** @typedef {import("./query.generic.class")} GenericQuery */
/** @typedef {import("../database.class")} Database */
class Tape {
/**
* Creates an instance of Tape.
* @param {Database} db
*/
constructor(db) {
this._db = db;
/** @type {Array.<Query>} */
this.commands = [];
}
/**
* Records a new command to the tape
* @param {Query} query
*/
add(query) {
this.commands.push(query);
return this;
}
/**
* Builds the query
* @returns {GenericQuery} query
*/
build() {
return this._db.query(this.commands.map(item => item.query).join(';\n'));
}
}
module.exports = Tape;
......@@ -4,6 +4,7 @@ const Select = require('./classes/select.class');
const Insert = require('./classes/insert.class');
const Delete = require('./classes/delete.class');
const Update = require('./classes/update.class');
const Tape = require('./classes/tape.class');
/**
* Class for communication with PostgreSQL database
......@@ -22,7 +23,6 @@ class Database {
*/
constructor({ host, port, database, user, password }) {
this._db = new Pool({ host, port, database, user, password });
this._transaction = false;
}
/**
......@@ -39,7 +39,7 @@ class Database {
*
* @param {String} query
* @param {Array.<any>} data
* @returns {Query} query
* @returns {GenericQuery} query
*/
query(query, data = []) {
return new GenericQuery(this, query, data);
......@@ -80,7 +80,6 @@ class Database {
*
* @param {String} table
* @returns {Update} update
* @memberof Database
*/
update(table) {
return new Update(this, table);
......@@ -92,11 +91,7 @@ class Database {
* @returns {Promise.<GenericQuery>} success
*/
begin() {
if (this._transaction) {
throw new Error('A transaction is already active');
}
return this.query('BEGIN').onsuccess(() => (this._transaction = true));
return this.query('BEGIN');
}
/**
......@@ -105,11 +100,16 @@ class Database {
* @returns {Promise.<GenericQuery>} success
*/
commit() {
if (!this._transaction) {
throw new Error('No transaction is active');
}
return this.query('COMMIT');
}
return this.query('COMMIT').onsuccess(() => (this._transaction = false));
/**
* Creates and returns a new `Tape` instance
*
* @returns {Tape} tape
*/
tape() {
return new Tape(this);
}
}
......
......@@ -65,9 +65,34 @@ const update = async () => {
};
(async () => {
await select();
await update();
await select();
// await select();
// await update();
// await select();
const query = db
.tape()
.add(db.begin())
.add(
db
.update('lorem')
.set({ foo: 'Updated Čau', bar: 420 })
.where({ id: 4 })
)
.add(
db.insert('lorem').values([
{ foo: 'Ahoj', bar: 12 },
{ foo: 'Čau', bar: 52 }
])
)
.add(db.select('lorem').columns('*'))
.add(db.commit())
.build();
if (await query.execute()) {
console.log(query.response);
} else {
console.log(query.err);
}
process.exit();
})();
......