Commit ce979f5a authored by Milan Wikarski's avatar Milan Wikarski

README.MD

parent 227eb88e
......@@ -20,108 +20,3 @@ The constructor requires parameters necessary to create connection with db:
| **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
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.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
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: {
operator: '>',
value: '123'
},
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;
```
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