...
 
Commits (2)
const Query = require('./query.class');
const { Pool } = require('pg');
const { WhereMixin } = require('../mixins');
/**
* Class for execution of DELETE queries
*
* @class Insert
*/
class Delete extends WhereMixin(Query) {
/**
* Creates an instance of Delete.
*
* @param {Pool} db
* @param {string} table
*/
constructor(db, table) {
super(db);
this.table = table;
}
get query() {
return `DELETE FROM ${this.table} ${this.whereSQL}`;
}
}
module.exports = Delete;
module.exports = {
Query: require('./query.class'),
Select: require('./select.class'),
Insert: require('./insert.class')
Insert: require('./insert.class'),
Delete: require('./delete.class')
};
......@@ -35,7 +35,7 @@ class Query {
* @readonly
*/
get canExecute() {
return this._query != '';
return this.query != '';
}
/**
......
const { Pool } = require('pg');
const { Query, Select, Insert } = require('./classes');
const { Query, Select, Insert, Delete } = require('./classes');
/**
* Class for communication with PostgreSQL database
......@@ -43,12 +43,23 @@ class DB {
/**
* Creates an Insert object
*
* @param {string} table
* @returns {Insert} insert
*/
insert(table) {
return new Insert(this._db, table);
}
/**
* Creates a Delete object
*
* @param {string} table
* @returns {Delete} delete
*/
delete(table) {
return new Delete(this._db, table);
}
}
module.exports = DB;
{
"name": "@creanet/js-db",
"version": "1.1.10",
"version": "2.0.0",
"description": "Used for communication between NodeJS and PostgreSQL",
"main": "index.js",
"scripts": {
......
......@@ -11,10 +11,7 @@ const db = new DB({
const select = async () => {
console.log('SELECT');
const select = db
.select('lorem')
.columns('*')
.where('id > 2');
const select = db.select('lorem').columns('*');
console.log(select.query);
if (await select.execute()) {
......@@ -37,11 +34,26 @@ const insert = async () => {
}
};
const del = async () => {
console.log('DELETE');
const del = db.delete('lorem').where({ bar: 12 });
if (await del.execute()) {
console.log(del.rows);
} else {
console.log(del.err);
}
};
(async () => {
await select();
// await insert();
// await select();
await del();
await select();
process.exit();
})();
......