Commit 912e3534 authored by Milan Wikarski's avatar Milan Wikarski 🎪
Browse files

_getWhere patch

parent 9653b475
# js-db # js-db
Module used for communication between NodeJS and PostgreSQL Module used for communication between NodeJS and PostgreSQL.
## Creating DB instance ## Creating DB instance
To create a new DB instance call DB constructor To create a new DB instance call DB constructor.
``` ```javascript
const db = new DB(params) const DB = require('@creanet/js-db');
const db = new DB(params);
``` ```
The constructor requires parameters necessary to create connection with db: The constructor requires parameters necessary to create connection with db:
...@@ -22,9 +23,33 @@ The constructor requires parameters necessary to create connection with db: ...@@ -22,9 +23,33 @@ The constructor requires parameters necessary to create connection with db:
## Methods ## Methods
js-db contains a set of methods used to execute SQL statements. These methods share certain parameters which are explained in the Shared parameters section.
### db.tableExists(tableName)
Returns true if table exists, false otherwise
```javascript
(async () => {
if (await db.tableExists('some_table_name')) {
console.log('Table exists');
} else {
console.log('Table does not exist');
}
})();
``` ```
db.tableExists(tableName);
db.select(tableName, columns = '*', where = null, limit = null); ### db.select(tableName, columns = '\*', where = null, limit = null)
Returns an array or records from SELECT statement
```javascript
(async () => {
const rows = db.select('some_table_name', ['lorem', 'foo']);
console.log(rows);
// [ { lorem: "ipsum", foo: "bar" }, { lorem: "lorem", foo: "foo" } ]
})();
``` ```
## Shared parameters ## Shared parameters
...@@ -59,12 +84,10 @@ or a columnName : params pair ...@@ -59,12 +84,10 @@ or a columnName : params pair
```javascript ```javascript
const where = { const where = {
id: { id: {
key: 'id',
operator: '>', operator: '>',
value: '123' value: '123'
}, },
lorem: { lorem: {
key: 'lorem',
operator: '=', operator: '=',
value: 'ipsum' value: 'ipsum'
} }
......
...@@ -54,7 +54,7 @@ class DB { ...@@ -54,7 +54,7 @@ class DB {
Object.entries(where) Object.entries(where)
.map(([key, value]) => .map(([key, value]) =>
value instanceof Object value instanceof Object
? `"${value.key}" ${value.operator} '${value.value}'` ? `"${key}" ${value.operator} '${value.value}'`
: `"${key}" = '${value}'` : `"${key}" = '${value}'`
) )
.join(' AND ') .join(' AND ')
......
Markdown is supported
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