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

gen-sql-type

v1.0.1

Published

Generate Typescript Types from raw SQL statements

Downloads

43

Readme

gen-sql-type

Generate Typescript Types from raw SQL statements

npm Package Version

Playground: https://gen-sql-type.surge.sh/

How it works

This library scan the plain sql statements then auto generate Typescript types of the result row and named parameters. (Also supports prepared statement)

Features

  • [x] Extract types from sql statement
    • [x] Support alias column name
    • [x] Support function call (e.g. COUNT(*))
    • [x] Support quoted column name with escape sequence (e.g. treat 'can''t' as "can't")
    • [x] Support named parameters (e.g. :id and @username)
    • [x] Supported statement types:
      • SELECT
      • UPDATE
      • DELETE
      • INSERT
    • [x] Support select from WITH-clause
  • [x] Generate Typescript type for:
    • [x] Row of select result
    • [x] Named parameters for prepared statement
  • [x] Isomorphic, support both node.js and browser

Usage Example

Given user-service.ts:

const sqlTypeFile = SqlTypeFile.withPrefix(__filename)

export class UserService {
  async login(parameters: LoginUserParameters): Promise<LoginUserRow> {
    const sql: string = sqlTypeFile.wrapSql(
      'LoginUser',
      'select password_hash from user where id = :id',
    )
    return mockExec(sql, parameters)[0]
  }

  async logout(parameters: LogoutUserParameters) {
    const sql: string = sqlTypeFile.wrapSql(
      'LogoutUser',
      'update session set active = false where token = :token',
    )
    return mockExec(sql, parameters)
  }
}

Upon execution, it will auto generate user-service.types.ts:

export type LoginUserParameters = {
  id: any
}
export type LoginUserRow = {
  password_hash: any
}

export type LogoutUserParameters = {
  token: any
}

Complete example refers to ./examples/user-service.ts

License

This is free and open-source software (FOSS) with BSD-2-Clause License