Commit 40aba333 authored by Milan Wikarski's avatar Milan Wikarski 🕳

documentation (incomplete) + patch

parent a734503c
# js-db
Module used for communication between NodeJS and PostgreSQL
## Creating DB instance
To create a new DB instance call DB constructor
```
const db = new DB(params)
```
The constructor requires parameters necessary to create connection with db:
| Parameter | Description | Example |
| ------------ | ----------------------------------- | ---------------- |
| **user** | The user of the DB | lorem |
| **database** | The name of the DB | ipsum |
| **password** | The password of the user | dolor.sit.amet |
| **host** | The host of the DB | db.websupport.sk |
| **port** | The port to be used when connecting | 1234 |
## Methods
```
db.tableExists(tableName);
db.select(tableName, columns = '*', where = null, limit = null);
```
## Shared parameters
Some parameters are shared among multiple js-db functions. These parameters are parsed by private functions (functions which names start with an underscore)
### where
Where can either be a String which will be concatenated to the query:
```javascript
const where = `"id" = '123' AND "lorem" = 'ipsum'"`;
db.select('some_table_name', '*', where);
// SELECT * FROM some_table_name WHERE "id" = '123' AND "lorem" = 'ipsum';
```
or a columnName : value pair
```javascript
const where = {
id: '123',
lorem: 'ipsum'
};
db.select('some_table_name', '*', where);
// SELECT * FROM some_table_name WHERE "id" = '123' AND "lorem" = 'ipsum';
```
or a columnName : params pair
```javascript
const where = {
id: {
key: 'id',
operator: '>',
value: '123'
},
lorem: {
key: 'lorem',
operator: '=',
value: 'ipsum'
}
};
db.delete('some_table_name', where);
// DELETE FROM some_table_name WHERE "id" > '123' AND "lorem" = 'ipsum';
```
### columns/returning
Columns or returning parameter can either be an asterisk (`'*'`):
```javascript
const columns = '*';
db.select('some_table_name', columns);
// SELECT * FROM some_table_name;
```
or a String - the name of the column in case you want to select you one column:
```javascript
const columns = 'lorem';
db.select('some_table_name', columns);
// SELECT "lorem" FROM some_table_name;
```
or and Array of the columns you want to select:
```javascript
const columns = ['id', 'lorem', 'another_column'];
db.select('some_table_name', columns);
// SELECT "id", "lorem", "another_column" FROM some_table_name;
```
......@@ -78,7 +78,7 @@ class DB {
if (typeof returning === 'string') {
return ` RETURNING ${returning}`;
} else if (Array.isArray(returning)) {
return ` RETURNING ${returning.join(',')}`;
return ` RETURNING ${returning.map(column => `"${column}"`).join(',')}`;
}
return '';
......@@ -112,7 +112,9 @@ class DB {
if (typeof columns === 'string') {
q = `SELECT ${columns} FROM ${tableName}`;
} else if (Array.isArray(columns)) {
q = `SELECT ${columns.join(',')} FROM ${tableName}`;
q = `SELECT ${columns
.map(column => `"${column}"`)
.join(',')} FROM ${tableName}`;
} else {
throw new TypeError(
columns +
......
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