A simple query builder for TypeScript
npm install @parse-it/database

A 2-way query builder and SQL parser for TypeScript, designed to work with BigQuery and PostgresSQL.
- Installation
- Usage
- API
- Contributing
- License
You can install the package using npm:
``sh`
npm install @parse-it/database
Here is a basic example of how to use the query builder:
`ts
import { QueryBuilder, QueryBuilderMode } from "@parse-it/database"
import { parseBigQuery } from "@parse-it/database"
// Sample SQL query
const sqlQuery =
WITH RegisteredUsers AS (
SELECT
id,
registrationDate
FROM UserSnapshots
WHERE status = 'Active'
AND registrationDate IS NOT NULL
),
EarliestRegistrationDates AS (
SELECT
id,
MIN(registrationDate) AS earliestRegistrationDate
FROM RegisteredUsers
GROUP BY id
),
CurrentlyActiveUsers AS (
SELECT
id
FROM UserSnapshots
WHERE status = 'Active'
GROUP BY id
)
SELECT
DATEDIFF(DAY, e.earliestRegistrationDate, GETDATE()) AS daysSinceRegistration
FROM EarliestRegistrationDates e
JOIN CurrentlyActiveUsers c
ON e.id = c.id;
const queryNode = parseBigQuery(sqlQuery)
const queryBuilder = new QueryBuilder(QueryBuilderMode.NAMED)
const query = queryBuilder.build(queryNode)
console.log(query)
`
Parses the given SQL query string and returns an abstract syntax tree (AST).
#### constructor(mode: QueryBuilderMode)
Creates a new instance of the QueryBuilder.
#### build(queryNode: QueryNode): string
Builds the SQL query string from the given query node.
An enumeration of the query builder modes:
- SIMPLENAMED
- POSITIONAL`
-
We welcome contributions! Please read our Contributing Guide to learn how you can help.
This project is licensed under the MIT License - see the LICENSE file for details.