...
 
Commits (2)
{
"singleQuote": true,
"tabWidth": 2,
"useTabs": false,
"printWidth": 80
}
{
"name": "@creanet/js-db",
"version": "2.1.1",
"version": "2.1.2",
"description": "Used for communication between NodeJS and PostgreSQL",
"main": "index.js",
"scripts": {
......@@ -19,8 +19,8 @@
"author": "Milan Wikarski,Marcel Odumorek",
"license": "ISC",
"dependencies": {
"pg": "^7.8.2",
"@creanet/js-type": "1.0.0"
"@creanet/js-type": "^1.2.0",
"pg": "^7.8.2"
},
"devDependencies": {
"dotenv": "^8.2.0"
......
const { is } = require('@creanet/js-type');
const SpecificQuery = require('./query.specific.class');
const { ValuesMixin } = require('../mixins');
class Insert extends ValuesMixin(SpecificQuery) {
get canExecute() {
return this._values != null;
return !is.null(this._values);
}
get query() {
......
/** @typedef {import('../database.class.js') Database} */
const { is } = require('@creanet/js-type');
class Query {
/**
* Creates an instance of Query.
......@@ -34,7 +36,7 @@ class Query {
* @readonly
*/
get canExecute() {
return this.query != '';
return !is.emptyString(this.query);
}
/**
......
const { is } = require('@creanet/js-type');
const SpecificQuery = require('./query.specific.class');
const {
ColumnsMixin,
LimitMixin,
OffsetMixin,
WhereMixin
WhereMixin,
OrderByMixin
} = require('../mixins');
class Select extends WhereMixin(
OffsetMixin(LimitMixin(ColumnsMixin(SpecificQuery)))
OrderByMixin(OffsetMixin(LimitMixin(ColumnsMixin(SpecificQuery))))
) {
get canExecute() {
return this._columns != null;
return !is.null(this._columns);
}
get query() {
return `SELECT ${this.columnsSQL} FROM ${this.table} ${this.whereSQL} ${this.limitSQL} ${this.offsetSQL}`;
return `SELECT ${this.columnsSQL} FROM ${this.table} ${this.whereSQL} ${this.orderBySQL} ${this.limitSQL} ${this.offsetSQL}`;
}
}
......
const is = require('@creanet/js-type');
const { is, force } = require('@creanet/js-type');
const ColumnsMixin = C =>
class ColumnsMixin extends C {
......@@ -8,10 +8,6 @@ const ColumnsMixin = C =>
}
get columnsSQL() {
if (is.string(this._columns)) {
return this._columns;
}
return this._columns.join(',');
}
......@@ -24,8 +20,11 @@ const ColumnsMixin = C =>
* @param {string|string[]} columns
*/
columns(columns) {
if (!is(columns, ['string', 'array']))
columns = force.array(columns);
if (!is.array(columns, 'string')) {
throw TypeError('columns is of invalid type');
}
this._columns = columns;
......
......@@ -3,5 +3,6 @@ module.exports = {
LimitMixin: require('./limit.mixin'),
OffsetMixin: require('./offset.mixin'),
ValuesMixin: require('./values.mixin'),
WhereMixin: require('./where.mixin')
WhereMixin: require('./where.mixin'),
OrderByMixin: require('./order-by.mixin')
};
const is = require('@creanet/js-type');
const { is } = require('@creanet/js-type');
const LimitMixin = C =>
class LimitMixin extends C {
......@@ -8,7 +8,7 @@ const LimitMixin = C =>
}
get limitSQL() {
if (this._limit == null) {
if (is.null(this._limit)) {
return '';
}
......@@ -22,7 +22,9 @@ const LimitMixin = C =>
* @param {number} limit
*/
limit(limit) {
if (!is(limit, 'numeric')) throw TypeError('limit is of invalid type');
if (!is.numeric(limit)) {
throw TypeError('limit is of invalid type');
}
this._limit = limit;
......
const is = require('@creanet/js-type');
const { is } = require('@creanet/js-type');
const OffsetMixin = C =>
class OffsetMixin extends C {
......@@ -8,7 +8,7 @@ const OffsetMixin = C =>
}
get offsetSQL() {
if (this._offset == null) {
if (is.null(this._offset)) {
return '';
}
......@@ -22,7 +22,9 @@ const OffsetMixin = C =>
* @param {number} offset
*/
offset(offset) {
if (!is(offset, 'numeric')) throw TypeError('offset is of invalid type');
if (!is.numeric(offset)) {
throw TypeError('offset is of invalid type');
}
this._offset = offset;
......
const { is, force } = require('@creanet/js-type');
const OrderByMixin = C =>
class OrderByMixin extends C {
constructor() {
super(...arguments);
this._orderBy = null;
}
get orderBySQL() {
if (is.null(this._orderBy)) {
return '';
}
// This assumes Object.entries(obj).length === 2
const orderBy = this._orderBy
.map(obj => Object.entries(obj)[0].join(' '))
.join(',');
return `ORDER BY ${orderBy}`;
}
orderBy(orderBy) {
orderBy = force.array(orderBy);
if (!is.array(orderBy, 'keyValuePair')) {
throw TypeError('orderBy is of invalid type');
}
this._orderBy = orderBy;
return this;
}
};
module.exports = OrderByMixin;
const is = require('@creanet/js-type');
const { is, force } = require('@creanet/js-type');
const ValuesMixin = C =>
class ValuesMixin extends C {
......@@ -22,14 +22,13 @@ const ValuesMixin = C =>
* - `{Object}` - one record with key:value pairs
* - `{Array<Object>} - multiple records
*
* @param {Array<Object>} values
* @param {Object|Array<Object>} values
*/
values(values) {
if (!is(values, ['array', 'object']))
throw new TypeError('values is of invalid type');
values = force.array(values);
if (!is.array(values)) {
values = [values];
if (!is.array(values, 'object')) {
throw new TypeError('values is of invalid type');
}
this._values = values;
......
const is = require('@creanet/js-type');
const { is } = require('@creanet/js-type');
const WhereMixin = C =>
class WhereMixin extends C {
......@@ -8,7 +8,7 @@ const WhereMixin = C =>
}
get whereSQL() {
if (this._where == null) {
if (is.null(this._where)) {
return '';
}
......
......@@ -11,7 +11,13 @@ const db = new DB({
const select = async () => {
console.log('SELECT');
const select = db.select('lorem').columns('*');
const select = db
.select('lorem')
.columns('*')
.orderBy({ id: 'DESC' })
.where(`"id" != '2'`)
.limit(1)
.offset(1);
console.log(select.query);
if (await select.execute()) {
......