pg-query-compose is a TypeScript library designed to help craft SQL queries from REST API inputs. This project is intended to simplify the process of building complex SQL queries using a straightforward and flexible interface.
Features
- Dynamic SQL query generation
- Support for filtering, sorting, pagination (offset/limit), and joining tables
- Basic SQL injection prevention through input sanitization
To install the dependencies for this project, use the following command:
npm install
Below is an example of how you can use the QueryCrafter class to build a SQL query.
import QueryCrafter from './src/main';
const filters = { name: 'John', age: '30' };
const fields = ['name', 'age', 'address'];
const table = 'users';
const queryCrafter = new QueryCrafter(filters, fields, table);
queryCrafter.addOffset(10);
queryCrafter.addLimit(5);
queryCrafter.sortResults('name', 'ASC');
queryCrafter.joinConditions('orders', { 'users': 'id', 'order': 'user_id' });
const sqlQuery = queryCrafter.buildFinalQuery();
console.log(sqlQuery);
Run the example file to check the output:
tsx .\examples\example_01.ts
To run the test cases, use the following command:
npx jest
test: Placeholder for test command lint: Run ESLint with automatic fixing Development To lint the code, run:
npm run lint
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please submit a pull request or open an issue to discuss what you would like to change.
For any issues, please use the GitHub Issues page.