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

sql-string-ts

v0.1.3

Published

Slightly easier and safer SQL string queries

Downloads

20

Readme

sql-string-ts

npm license

A lib with some functions to make writing SQL strings easier and safer.

Works with mysql, mysql2 and postgres

It is not an ORM or Query Builder, it just try to rely on Typescript types to make the SQL strings safer.

import { schema, SQL, bind } from 'sql-string-ts'

enum userColumns {
  id,
  name,
  email,
  password,
  favorite_movie_id,
  is_active,
  inserted_at,
  updated_at
}

const user = schema({ table: 'user', columns: userColumns, quote: '`', alias: 'u' })

const q1 = SQL`select ${user.name}
from ${user}
where ${user.id} > ${bind(5)} and ${user.is_active} = ${bind(true)}`

// generated query
select `u`.`name`
from `user` `u`
where `u`.`id` > ? and `u`.`is_active` = ?

// generated bind values
[  5,  true  ]

So you can run the query with your favorite RDBMS/lib:

mysql.query(q1)
// or
pg.query(q1)

A more complex example:

enum movieColumns {
  id,
  name,
  year,
  director_id,
  inserted_at,
  updated_at
}

const movie = schema({ table: 'movie', columns: movieColumns, quote: '`', alias: 'm' })

enum directorColumns {
  id,
  name,
  inserted_at,
  updated_at
}

const director = schema({ table: 'director', columns: directorColumns, quote: '`', alias: 'd' })

const q2 = SQL`select ${user.name}, ${movie.name}, ${director.name}
from ${user}
left join ${movie} on ${movie.id} = ${user.favorite_movie_id}
left join ${director} on ${director.id} = ${movie.director_id}
where ${user.id} = ${bind(1)}

// generated query
select `u`.`name`, `m`.`name`, `d`.`name`
from `user` `u`
left join `movie` `m` on `m`.`id` = `u`.`favorite_movie_id`
left join `director` `d` on `d`.`id` = `m`.`director_id`
where `u`.`id` = ?

// generated bind values
[ 1 ]

Fragments

concat(fragment) You can use the concat method to combine queries fragments.

const id = 1
const email = '[email protected]'

const base = SQL`select ${u.name} from ${u} where true`
const whereId = SQL`${u.id} = ${bind(id)}`
const whereEmail = SQL`${u.email} = ${bind(email)}`

const q3 = base.concat(SQL` and ${whereId} and ${whereEmail}`)

// generated query
select `u`.`name` from `user` `u` where true and `u`.`id` = ? and `u`.`email` = ?

// generated bind values
[ 1, '[email protected]' ]

Functions

table(schema, { alias: boolean, quote: boolean }?) Receives a schema an returns its table name.

table(user) // user
table(user, { alias: true }) // user u
table(user, { quote: true }) // `user`
table(user, { alias: true, quote: true }) // `user` `u`

column(column, { as: boolean, prefix: boolean, quote: boolean }?) Receives a column an returns its name.

column(user.email) // email
column(user.email, { as: true }) // email as u_email
column(user.email, { prefix: true }) // u.email
column(user.email, { quote: true }) // `email`
column(user.email, { as: true, prefix: true, quote: true }) // `u`.`email` as `u_email`

insert([column, value]*) insert is a variadic function to generate insert statements, to insert binded values is important to use the function bind (or its alias b), otherwise values will be treated as raw values.

const q4 = insert([u.name, b('User Name')], [u.email, b('[email protected]')], [u.active, b(true)], [u.inserted_at, 'NOW()'])

// generated query
insert into `user` (`name`, `email`, `active`, `inserted_at`) values (?, ?, ?, NOW())

// generated bind values
[ 'User Name', '[email protected]', true ]

update([column, value]*) update is a variadic function to generate update statements, to update binded values is important to user the function bind (or its alias b), otherwise values will be treated as raw values.

const q5 = update([u.name, bind('User New Name')], [u.updated_at, 'NOW()'])

// generated query
update `user` set `name`=?, `updated_at`=NOW()

// generated bind values
[ 'User New Name' ]

Note

This lib is based in the sql-template-strings, I decided to write this lib mainly because the .append method of sql-template-strings is not pure.

Contributing

  • Pull requests are welcome.