fish-query
v0.2.4
Published
Simple query string builder
Downloads
42
Readme
Fish Query
Super simple query string builder. Currently only supports SELECT and cater to PostgreSQL query format.
Built using ES6 Class.
Installation
NPM:> npm install fish-query
Yarn:> yarn add fish-query
Usage
const QueryBuilder = require('fish-query');
const queryClass = new QueryBuilder();
let queryString = queryClass
.addSelect('firstName')
.setFirstTable('user')
.generateQuery();
console.log(queryString);
Output should be SELECT firstName from user;
Method
> addSelect
Add the select column on query
Parameter:
- String. e.g. : user
- Object with property: tableName, columnName, columnAlias (will be formatted to PostgreSQL accepted format)
> addMultipleSelect
Add multiple select column on query
Parameter: Array of Parameters on addSelect
> removeSelect
Remove select column on query Parameter:
- String. e.g. : firstName
- Object with property: tableName, columnName (will be formatted to PostgreSQL accepted format)
> setFirstTable
Table name specified on FROM
Parameter:
- String. e.g. : user
- Object with property: tableName, alias (will be formatted to PostgreSQL accepted format)
> joinTable
Join the tables Parameter Object:
- joinType: 'FULL JOIN', 'INNER JOIN', etc
- firstTable: new table that wants to be joined
- firstTableAlias: alias of the new table (optional)
- firstKey: the key that wants to be connected to second table
- secondTable: other table that wants to be compared with first table
- secondTableAlias: other table alias (optional)
- secondKey: the key that wants to be connected to first table
Alias will be prioritized when comparing
> addWhere
Add where condition Parameter Object:
- column: String or Object. If Object the properties are : tableName, columnName
- operator: '=', etc
- value: value to be compared
Currently only supports AND for all the where clause.
> addOrder
Add Order By Parameter:
- String, e.g. : 'firstName DESC'
- Object with property:
orderBy: Object with property: tableName, columnName
orderType: 'ASC', 'DESC'
> generateQuery
Generate the Query String after setting up the QueryBuilder instance.
Tests
> npm test
Contributing
In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.
As this is a very initial phase, any contribution would be welcomed to support other query formats, or to have UPDATE, INSERT, DELETE, etc.
Future Plan
Add UPDATE, INSERT, DELETE
Author Note
'Ah, I want to eat fish query'
License
MIT License