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