npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

querytyper

v3.0.0

Published

Convert SQL annotations into Typescript files

Downloads

24

Readme

QueryTyper

Description

QueryTyper is a tool for Typescript projects that make extensive use of raw SQL queries. It enables type checking between the SQL file that defines a query and the Typescript statement where the query is executed. This catches several common mistakes at compile-time, such as:

  • An argument X was added to a query, but the application code was not updated to pass X
  • The query expects a non-nullable argument X, but the application code passes a nullable value for X
  • The query returns an integer field Y, but the application code treats Y as a string

Query directory structure

QueryTyper supports one or more root directories. A root directory can contain one or more query collection directories. A query collection directory can contain one or more query files. Query files should have the name pattern *.query.sql

E.g. a root directory could be server/queries, a query collection directory could be server/queries/users, and a query file could be server/queries/users/addUsers.query.sql.

Query Annotations

Query files can contain single-line annotations using the syntax -- @annotationName [annotationValue]. Every annotation is a SQL comment, so does not affect the query's behavior.

Available query annotations

@arg argName: argType

Defines a query argument argName of type argType

@return fieldName: fieldType

Defines a query return field fieldName or type fieldType

@extendsArgs dt.ArgType

Imports multiple query arguments from a custom type ArgType

@extendsResults dt.ResultType

Imports multiple query results from a custom type ResultType

@unique

Marks the query as returning zero or one row

Configuration file

The configuration file should be named querytyper.config.json and should be placed in the npm package root directory.

Configuration fields

rootDirs

Defines the paths of the root directories. Paths are specified relative to the npm package root.

queryTemplatePath

Defines the path to the query template, relative to the npm package root.

dataTypesPath

Defines the path to a data types Typescript module, relative to the queries. QueryTyper annotations can use custom types from that module.

exportsFileName

QueryTyper will create an exports file inside each query collection directory. exportsFileName defines the name of the exports file.

helperMapping

QueryTyper will support multiple function to execute sql code. You can adde them as property of this object where property name will be the keyword used in sql header and value will be the name of the function to call.

Example configuration file

{
    "rootDirs": ["server/queries", "tests/queries"],
    "queryTemplatePath": "server/queries/query.ts.template",
    "dataTypesPath": "../../../common/DataTypes",
    "exportsFileName": "index.ts",
    "helperMapping": {
        "default": "buildQuery",
        "unique": "buildQueryWithUniqueResult"
    }
}

Query template

The query template defines how to create a Typescript stub from the extracted QueryTyper annotations. A query template can contain symbols prefixed by @ which will be replaced by values

Query template symbols

@extraImports

@extraImports is replaced with addtional import statements e.g. a custom data types module

@helperFunction

@helperFunction is replaced with buildQuery or buildQueryWithUniqueResult

@argumentFields

@argumentFields is replaced with the argument fields

@extendArg

@extendArg is replaced with an extends statement for the argument type

@resultFields

@resultFields is replaced with the result fields

@extendResult

@extendResult is replaced with an extends statement for the result type

@queryName

@queryName is replaced with the query name

@helperFunction

@helperFunction is replaced with non deafult query execution function

Example query template:

/* tslint:disable */
@extraImports
import { @helperFunction } from '../queryHelper';

export interface Arguments @extendArg {
@argumentFields
}

export interface Result @extendResult {
@resultFields
}

export const @queryName = @helperFunction<Arguments, Result>(__filename);