...
 
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'),
Delete: require('./delete.class')
};
const { Pool } = require('pg');
const { Query, Select, Insert, Delete } = require('./classes');
/**
* Class for communication with PostgreSQL database
*
* @class DB
*/
class DB {
/**
*
* @param {Object} params
* @param {string} params.host - Connection Host
* @param {number} params.port - Connection Port
* @param {string} params.database - DB Name
* @param {string} params.user - DB User
* @param {string} params.password - DB Password
*/
constructor({ host, port, database, user, password }) {
this._db = new Pool({ host, port, database, user, password });
}
/**
* Creates a Query object
*
* @param {string} query
* @param {Array.<any>} data
* @returns {Query} query
*/
query(query, data = []) {
return new Query(this._db, query, data);
}
/**
* Creates a Select object
*
* @param {string} table
* @returns {Select} select
*/
select(table) {
return new Select(this._db, table);
}
/**
* 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;
module.exports = require('./src/database.class');
{
"name": "@creanet/js-db",
"version": "2.0.0",
"version": "2.1.0",
"description": "Used for communication between NodeJS and PostgreSQL",
"main": "index.js",
"scripts": {
......
const SpecificQuery = require('./query.specific.class');
const { WhereMixin } = require('../mixins');
class Delete extends WhereMixin(SpecificQuery) {
get query() {
return `DELETE FROM ${this.table} ${this.whereSQL}`;
}
}
module.exports = Delete;
const Query = require('./query.class');
const { Pool } = require('pg');
const SpecificQuery = require('./query.specific.class');
const { ValuesMixin } = require('../mixins');
/**
* Class for execution of INSERT queries
*
* @class Insert
*/
class Insert extends ValuesMixin(Query) {
/**
* Creates an instance of Insert.
*
* @param {Pool} db
* @param {string} table
*/
constructor(db, table) {
super(db);
this.table = table;
}
class Insert extends ValuesMixin(SpecificQuery) {
get canExecute() {
return this._values != null;
}
......
const { Pool } = require('pg');
/** @typedef {import('../database.class.js') Database} */
class Query {
/**
* Creates an instance of Query.
*
* @param {Pool} db
* @param {string} [query='']
* @param {array} [data=[]]
* @param {Database} db
*/
constructor(db, query = '', data = []) {
this._db = db;
this._query = query;
this.data = data;
constructor(db) {
this._db = db._db;
this.data = [];
this.executed = false;
this.success = null;
......@@ -20,12 +18,13 @@ class Query {
}
/**
* Getter for query; can be overridden
* Returns the PostgreSQL query
*
* @type String
* @readonly
*/
get query() {
return this._query;
return null;
}
/**
......
/** @typedef {import('../database.class') Database}*/
const Query = require('./query.class');
/**
* Generic query is not bound to any table
*
* @class GenericQuery
* @extends {Query}
*/
class GenericQuery extends Query {
/**
* Creates an instance of GenericQuery.
*
* @param {Database} db
* @param {String} query
* @param {Object[]} [data=[]]
*/
constructor(db, query, data = []) {
super(db);
this._query = query;
this.data = data;
}
/**
* Getter for query; can be overridden
*
* @readonly
*/
get query() {
return this._query;
}
}
module.exports = GenericQuery;
/** @typedef {import('../database.class') Database}*/
const Query = require('./query.class');
/**
* Specific query is bound to a table.
* This class is used as a parent class for other queries:
* - SELECT
* - INSERT
* - DELETE
* - UPDATE
*
* @class SpecificQuery
* @extends {Query}
*/
class SpecificQuery extends Query {
/**
* Creates an instance of SpecificQuery.
*
* @param {Database} db
* @param {String} table
* @memberof SpecificQuery
*/
constructor(db, table) {
super(db);
this.table = table;
}
}
module.exports = SpecificQuery;
const Query = require('./query.class');
const { Pool } = require('pg');
const SpecificQuery = require('./query.specific.class');
const {
ColumnsMixin,
LimitMixin,
......@@ -7,23 +6,9 @@ const {
WhereMixin
} = require('../mixins');
/**
* Class for execution of SELECT queries
*
* @class Select
*/
class Select extends WhereMixin(OffsetMixin(LimitMixin(ColumnsMixin(Query)))) {
/**
* Creates an instance of Select.
*
* @param {Pool} db
* @param {string} table
*/
constructor(db, table) {
super(db);
this.table = table;
}
class Select extends WhereMixin(
OffsetMixin(LimitMixin(ColumnsMixin(SpecificQuery)))
) {
get canExecute() {
return this._columns != null;
}
......
const { Pool } = require('pg');
const GenericQuery = require('./classes/query.specific.class');
const Select = require('./classes/select.class');
const Insert = require('./classes/insert.class');
const Delete = require('./classes/delete.class');
/**
* Class for communication with PostgreSQL database
*
* @class Database
*/
class Database {
/**
*
* @param {Object} params
* @param {String} params.host - Connection Host
* @param {number} params.port - Connection Port
* @param {String} params.database - DB Name
* @param {String} params.user - DB User
* @param {String} params.password - DB Password
*/
constructor({ host, port, database, user, password }) {
this._db = new Pool({ host, port, database, user, password });
}
/**
* Creates a Query object
*
* @param {String} query
* @param {Array.<any>} data
* @returns {Query} query
*/
query(query, data = []) {
return new GenericQuery(this, query, data);
}
/**
* Creates a Select object
*
* @param {String} table
* @returns {Select} select
*/
select(table) {
return new Select(this, table);
}
/**
* Creates an Insert object
*
* @param {String} table
* @returns {Insert} insert
*/
insert(table) {
return new Insert(this, table);
}
/**
* Creates a Delete object
*
* @param {String} table
* @returns {Delete} delete
*/
delete(table) {
return new Delete(this, table);
}
}
module.exports = Database;