Commit 912e3534 authored by Milan Wikarski's avatar Milan Wikarski 🕳

_getWhere patch

parent 9653b475
# js-db
Module used for communication between NodeJS and PostgreSQL
Module used for communication between NodeJS and PostgreSQL.
## Creating DB instance
To create a new DB instance call DB constructor
To create a new DB instance call DB constructor.
```
const db = new DB(params)
```javascript
const DB = require('@creanet/js-db');
const db = new DB(params);
```
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
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
......@@ -59,12 +84,10 @@ or a columnName : params pair
```javascript
const where = {
id: {
key: 'id',
operator: '>',
value: '123'
},
lorem: {
key: 'lorem',
operator: '=',
value: 'ipsum'
}
......
......@@ -54,7 +54,7 @@ class DB {
Object.entries(where)
.map(([key, value]) =>
value instanceof Object
? `"${value.key}" ${value.operator} '${value.value}'`
? `"${key}" ${value.operator} '${value.value}'`
: `"${key}" = '${value}'`
)
.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