0.4.3-1 • Published 5 months ago

@parse-it/database v0.4.3-1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

@parse-it/database

npm version Build Status

A 2-way query builder and SQL parser for TypeScript, designed to work with BigQuery and PostgresSQL.

Table of Contents

Installation

You can install the package using npm:

npm install @parse-it/database

Usage

Here is a basic example of how to use the query builder:

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)

API

parseBigQuery(input: string, databaseType?: 'bigQuery' | 'MySQL'): any

Parses the given SQL query string and returns an abstract syntax tree (AST).

QueryBuilder

constructor(mode: QueryBuilderMode)

Creates a new instance of the QueryBuilder.

build(queryNode: QueryNode): string

Builds the SQL query string from the given query node.

QueryBuilderMode

An enumeration of the query builder modes:

  • SIMPLE
  • NAMED
  • POSITIONAL

Contributing

We welcome contributions! Please read our Contributing Guide to learn how you can help.

License

This project is licensed under the MIT License - see the LICENSE file for details.

0.4.3-1

5 months ago

0.4.3

5 months ago

0.4.2

5 months ago

0.4.1

5 months ago

0.4.0

5 months ago

0.3.15

8 months ago

0.3.14

9 months ago

0.3.13

9 months ago

0.3.11

10 months ago

0.3.10

10 months ago

0.3.9

10 months ago

0.3.8

10 months ago

0.3.7

10 months ago

0.3.6

10 months ago

0.3.5

10 months ago

0.3.4

10 months ago

0.3.3

10 months ago

0.3.2

10 months ago

0.3.1

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago