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

postgres-typescript

v1.0.2

Published

> TLDR: This is somewhat similar to an ORM, but rather bridges typescript with SQL: write SQL statements and use these statements as typescript code.

Downloads

24

Readme

Typescript generator for Postgresql SQL statements

TLDR: This is somewhat similar to an ORM, but rather bridges typescript with SQL: write SQL statements and use these statements as typescript code.

This plugs into your application, reads SQL statements, database connection env variables and generates typescript files with functions matching SQl statements.

How to use it?

  • step 1: create SQL files with annotations about the arguments (names & types) and the data returned (names & types)
  • step 2: call postgres-typescript-codegen <srcDir1> <srcDir2>
  • step 3: use typescript in your application logic

The command postgres-typescript-codegen will recursively parse all folders passed as argument.

for better developer experience, create a SQL file watcher that automatically calls postgres-typescript-codegen.

For example:

-- file ./queries/fetchUsers.query.sql
-- @arg company: string
-- @arg isActive: boolean
-- @return id: number
-- @return email: string
-- @return company: string
-- @return isActive: boolean
SELECT id, email, company, is_active
FROM app_users
WHERE company = :company  
  AND is_active = :isActive

Will generate the following files

// file ./queries/index.ts
export { fetchUsers, Result as fetchUsersResult, Arguments as fetchUsersArgs } from './fetchUsers.query'
// file ./queries/fetchUsers.query.ts
import { buildQuery } from 'postgres-typescript'

export interface Arguments {
  company: string
  isActive: boolean
}

export interface Result {
  id: number
  email: string
  company: string
  isActive: boolean
}

export const fetchUsers = buildQuery<Arguments, Result>(__filename)

which you can use in your application as follow:

import {fetchUsers, fetchUsersResult} from './queries'

fetchUsers({
  company: 'Example',
  isActive: true
}).then((users:fetchUsersResult[]) => {
  // users[0].id
  // users[0].email
  // users[0].company
  // users[0].active
  ...
}).catch((err:Error) => {
  ...
})

Annotations

  • arg describes an input of the SQL query. eg @arg myInput:string
  • return describes an output object or field of the SQL query. eg. @return id:number
  • unique flag the SQL query as returning only one row. When used, this will make the typescript function return a single object and not an array of objects. Usage: @unique

Note that when specifying arg or return data types, you can regroup the returned fields into a datatype you have defined in your typescript application by using directly the default export @return ./User or a specific export @return ./DataTypes{User}. You can do the same with external dependencies; for example @return moment{Moment}

As a practcal example, these 2 files would generate what you'd expect:

// file ./DataTypes.ts
export interface User {
  id: number
  email: string
  company: string
  isActive: boolean
}
-- file ./queries/fetchUsers.query.sql
-- @arg company: string
-- @arg isActive: boolean
-- @return ../DataTypes{User}
SELECT id, email, company, is_active
FROM app_users
WHERE company = :company  
  AND is_active = :isActive

Collisions

Using default imports, you may create a collision that this library won't solve for you. For example, the following annotations:

-- @arg ../DataType
-- @return ../other/DataType

will generate the following conflicting imports:

import { DataType } from '../DataType'
import { DataType } from '../other/DataType'

this holds true for importing types. The following is NOT valid:

-- @arg ../File1{User}
-- @return ../File2{User}

as it'll generate:

import { User } from '../File1'
import { User } from '../File2'

Thus you need to make sure you specifically import non-colliding sub types:

-- @arg ../File1{User}
-- @return ../File2{ShallowUser}

as it'll generate thje imports:

import { User } from '../File1'
import { ShallowUser } from '../File2'