0.4.3-1 • Published 8 months ago

@parse-it/database v0.4.3-1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 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

8 months ago

0.4.3

8 months ago

0.4.2

8 months ago

0.4.1

9 months ago

0.4.0

9 months ago

0.3.15

12 months ago

0.3.14

1 year ago

0.3.13

1 year ago

0.3.11

1 year ago

0.3.10

1 year ago

0.3.9

1 year ago

0.3.8

1 year ago

0.3.7

1 year ago

0.3.6

1 year ago

0.3.5

1 year ago

0.3.4

1 year ago

0.3.3

1 year ago

0.3.2

1 year ago

0.3.1

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago