...
 
Commits (2)
{
"name": "@creanet/js-db",
"version": "2.2.0",
"version": "2.3.0",
"description": "Used for communication between NodeJS and PostgreSQL",
"main": "index.js",
"scripts": {
......
......@@ -4,7 +4,7 @@ const { ValuesMixin } = require('../mixins');
class Insert extends ValuesMixin(SpecificQuery) {
get canExecute() {
return !is.null(this._values);
return is.not.null(this._values);
}
get query() {
......
......@@ -36,7 +36,7 @@ class Query {
* @readonly
*/
get canExecute() {
return !is.emptyString(this.query);
return is.not.emptyString(this.query);
}
/**
......
......@@ -12,7 +12,7 @@ class Select extends WhereMixin(
OrderByMixin(OffsetMixin(LimitMixin(ColumnsMixin(SpecificQuery))))
) {
get canExecute() {
return !is.null(this._columns);
return is.not.null(this._columns);
}
get query() {
......
const { is } = require('@creanet/js-type');
const SpecificQuery = require('./query.specific.class');
const { SetMixin, WhereMixin } = require('../mixins');
class Update extends WhereMixin(SetMixin(SpecificQuery)) {
get canExecute() {
return is.not.null(this._set);
}
get query() {
return `UPDATE ${this.table} ${this.setSQL} ${this.whereSQL}`;
}
}
module.exports = Update;
......@@ -3,6 +3,7 @@ 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');
const Update = require('./classes/update.class');
/**
* Class for communication with PostgreSQL database
......@@ -63,6 +64,17 @@ class Database {
delete(table) {
return new Delete(this, table);
}
/**
* Creates an Update object
*
* @param {String} table
* @returns {Update} update
* @memberof Database
*/
update(table) {
return new Update(this, table);
}
}
module.exports = Database;
......@@ -22,7 +22,7 @@ const ColumnsMixin = C =>
columns(columns) {
columns = force.array(columns);
if (!is.array(columns, 'string')) {
if (is.not.array(columns, 'string')) {
throw TypeError('columns is of invalid type');
}
......
......@@ -4,5 +4,6 @@ module.exports = {
OffsetMixin: require('./offset.mixin'),
ValuesMixin: require('./values.mixin'),
WhereMixin: require('./where.mixin'),
OrderByMixin: require('./order-by.mixin')
OrderByMixin: require('./order-by.mixin'),
SetMixin: require('./set.mixin')
};
......@@ -22,7 +22,7 @@ const LimitMixin = C =>
* @param {number} limit
*/
limit(limit) {
if (!is.numeric(limit)) {
if (is.not.numeric(limit)) {
throw TypeError('limit is of invalid type');
}
......
......@@ -22,7 +22,7 @@ const OffsetMixin = C =>
* @param {number} offset
*/
offset(offset) {
if (!is.numeric(offset)) {
if (is.not.numeric(offset)) {
throw TypeError('offset is of invalid type');
}
......
......@@ -23,7 +23,7 @@ const OrderByMixin = C =>
orderBy(orderBy) {
orderBy = force.array(orderBy);
if (!is.array(orderBy, 'keyValuePair')) {
if (is.not.array(orderBy, 'keyValuePair')) {
throw TypeError('orderBy is of invalid type');
}
......
const { is, force } = require('@creanet/js-type');
const SetMixin = C =>
class SetMixin extends C {
constructor() {
super(...arguments);
this._set = null;
}
get setSQL() {
const set = Object.entries(this._set)
.map(([key, value]) => `${key}='${value}'`)
.join(',');
return `SET ${set}`;
}
/**
* Sets the values of set. Valid values are:
* - `{Object}` - one record with key:value pairs
*
* @param {Object|Array<Object>} values
*/
set(set) {
if (is.not.object(set)) {
throw new TypeError('values is of invalid type');
}
this._set = set;
return this;
}
};
module.exports = SetMixin;
......@@ -4,7 +4,7 @@ const ValuesMixin = C =>
class ValuesMixin extends C {
constructor() {
super(...arguments);
this._offset = null;
this._values = null;
}
get valuesSQL() {
......@@ -27,7 +27,7 @@ const ValuesMixin = C =>
values(values) {
values = force.array(values);
if (!is.array(values, 'object')) {
if (is.not.array(values, 'object')) {
throw new TypeError('values is of invalid type');
}
......
......@@ -22,12 +22,12 @@ const WhereMixin = C =>
* @param {Array<Object>} where
*/
where(where) {
if (!is(where, ['string', 'object']))
if (is.not(where, ['string', 'object']))
throw new TypeError('where is of invalid type');
if (is.object(where)) {
this._where = Object.entries(where)
.map(([key, value]) => `${key} = '${value}'`)
.map(([key, value]) => `${key}='${value}'`)
.join(' AND ');
} else {
this._where = where;
......
......@@ -11,13 +11,7 @@ const db = new DB({
const select = async () => {
console.log('SELECT');
const select = db
.select('lorem')
.columns('*')
.orderBy({ id: 'DESC' })
.where(`"id" != '2'`)
.limit(1)
.offset(1);
const select = db.select('lorem').columns('*');
console.log(select.query);
if (await select.execute()) {
......@@ -51,13 +45,24 @@ const del = async () => {
}
};
(async () => {
await select();
// await insert();
// await select();
const update = async () => {
console.log('UPDATE');
const update = db
.update('lorem')
.set({ foo: 'Updated Čau', bar: 420 })
.where({ id: 4 });
await del();
console.log(update.query);
if (await update.execute()) {
console.log(update.rows);
} else {
console.log(update.err);
}
};
(async () => {
await select();
await update();
await select();
process.exit();
......