SQL as template literal (for a lot of stuff)
Go to file
Reinaldy Rafli e59a7e473c test: multiple values instead of single one 2021-07-13 13:40:15 +07:00
.github/workflows chore: trailing spaces 2021-07-13 13:22:43 +07:00
src fix: typings 2021-07-11 21:10:22 +07:00
test test: multiple values instead of single one 2021-07-13 13:40:15 +07:00
.eslintrc.js feat: launch 2021-07-11 18:04:04 +07:00
.gitignore feat: launch 2021-07-11 18:04:04 +07:00
LICENSE feat: launch 2021-07-11 18:04:04 +07:00
README.md fix: wrong package name sorry 2021-07-11 21:05:27 +07:00
package.json fix: wrong package name sorry 2021-07-11 21:05:27 +07:00
rollup.config.js fix: typings 2021-07-11 21:10:22 +07:00
tsconfig.json feat: launch 2021-07-11 18:04:04 +07:00
yarn.lock feat: launch 2021-07-11 18:04:04 +07:00

README.md

SQL DSL for your Node App

Based on an idea from @ronnygunawan. This was supposed to be a challenge for someone else, but I thought, why not? Lol.

As always:

  • Supports CJS and ESM
  • Built-in typings
  • No external dependencies
npm install sql-dsl

What this does?

This outputs simple SQL query & argument object from your template literal string.

import { sql } from 'sql-dsl'

const query = sql`INSERT INTO users (name, email, age) VALUES (${`Thomas Worgdjik`}, ${`thom@thunder.zn`}, ${30})`

// {
//   sql: 'INSERT INTO users (name, email, age) VALUES ($1, $2, $3)',
//   values: ['Thomas Worgdjik', 'thom@thunder.zn', 30]
// }

// Oh, you work with MySQL instead of Postgres? Sure!

const postgres = query.formatQuestion()

// {
//   sql: 'INSERT INTO users (name, email, age) VALUES (?, ?, ?)',
//   values: ['Thomas Worgdjik', 'thom@thunder.zn', 30]
// }

// More elegant way to do that is this:
const query = sql`INSERT INTO users (name, email, age) VALUES (${`Thomas Worgdjik`}, ${`thom@thunder.zn`}, ${30})`.formatQuestion()

// Your database work with colon notation? Something like :1 :2 etc? No problem.

const colon = query.formatColon()

// {
//   sql: 'INSERT INTO users (name, email, age) VALUES (:1, :2, :3)',
//   values: ['Thomas Worgdjik', 'thom@thunder.zn', 30]
// }

// Your database work with @p notation? Yeah, we can do that.

const atp = query.formatAtP()

// {
//   sql: 'INSERT INTO users (name, email, age) VALUES (@p1, @p2, @p3)',
//   values: ['Thomas Worgdjik', 'thom@thunder.zn', 30]
// }

Want more features? Open something up on issues.