Commit 8eac500f authored by Milan Wikarski's avatar Milan Wikarski
Browse files

Added DELETE command

parent 6305fcbf
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 = { module.exports = {
Query: require('./query.class'), Query: require('./query.class'),
Select: require('./select.class'), Select: require('./select.class'),
Insert: require('./insert.class') Insert: require('./insert.class'),
Delete: require('./delete.class')
}; };
...@@ -35,7 +35,7 @@ class Query { ...@@ -35,7 +35,7 @@ class Query {
* @readonly * @readonly
*/ */
get canExecute() { get canExecute() {
return this._query != ''; return this.query != '';
} }
/** /**
......
const { Pool } = require('pg'); const { Pool } = require('pg');
const { Query, Select, Insert } = require('./classes'); const { Query, Select, Insert, Delete } = require('./classes');
/** /**
* Class for communication with PostgreSQL database * Class for communication with PostgreSQL database
...@@ -43,12 +43,23 @@ class DB { ...@@ -43,12 +43,23 @@ class DB {
/** /**
* Creates an Insert object * Creates an Insert object
*
* @param {string} table * @param {string} table
* @returns {Insert} insert * @returns {Insert} insert
*/ */
insert(table) { insert(table) {
return new Insert(this._db, 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; module.exports = DB;
...@@ -11,10 +11,7 @@ const db = new DB({ ...@@ -11,10 +11,7 @@ const db = new DB({
const select = async () => { const select = async () => {
console.log('SELECT'); console.log('SELECT');
const select = db const select = db.select('lorem').columns('*');
.select('lorem')
.columns('*')
.where('id > 2');
console.log(select.query); console.log(select.query);
if (await select.execute()) { if (await select.execute()) {
...@@ -37,11 +34,26 @@ const insert = async () => { ...@@ -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 () => { (async () => {
await select(); await select();
// await insert(); // await insert();
// await select(); // await select();
await del();
await select();
process.exit(); process.exit();
})(); })();
......
Supports Markdown
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