Super simple query string builder. Currently only supports SELECT and cater to PostgreSQL query format.
Built using ES6 Class.
NPM:
> npm install fish-query
Yarn:
> yarn add fish-query
const QueryBuilder = require('fish-query');
const config = {
datastore: 'postgresql' //Currently support 'postgresql' and 'sqlserver'. Default to 'postgresql' when not specified.
};
const queryClass = new QueryBuilder(config);
let queryString = queryClass
.addSelect('firstName')
.setFirstTable('user')
.generateQuery();
console.log(queryString);
Output should be SELECT firstName from user;
Add the select column on query
Parameter:
- String. e.g. : user
- Object with property: tableName, columnName, columnAlias (will be formatted to PostgreSQL accepted format)
Add multiple select column on query
Parameter: Array of Parameters on addSelect
Remove select column on query Parameter:
- String. e.g. : firstName
- Object with property: tableName, columnName (will be formatted to PostgreSQL accepted format)
Table name specified on FROM
Parameter:
- String. e.g. : user
- Object with property: tableName, alias (will be formatted to PostgreSQL accepted format)
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
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.
Add Order By Parameter:
- String, e.g. : 'firstName DESC'
- Object with property:
orderBy: Object with property: tableName, columnName
orderType: 'ASC', 'DESC'
Generate the Query String after setting up the QueryBuilder instance.
> npm test
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.
Add UPDATE, INSERT, DELETE
'Ah, I want to eat fish query'
MIT License