SQL as template literal (for a lot of stuff)
Go to file
Reinaldy Rafli d1ebe73a75
build: using mjs extension to fix compatibility
2021-08-18 01:11:40 +07:00
.github/workflows chore: trailing spaces 2021-07-13 13:22:43 +07:00
examples chore: mysql and postgres examples 2021-07-13 19:34:34 +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 docs: example link 2021-07-13 19:47:44 +07:00
package.json chore: mysql and postgres examples 2021-07-13 19:34:34 +07:00
rollup.config.js build: using mjs extension to fix compatibility 2021-08-18 01:11:40 +07:00
tsconfig.json chore: update docs 2021-07-13 13:40:22 +07:00
yarn.lock chore: update docs 2021-07-13 13:40:22 +07:00

README.md

SQL DSL for your Node App

npm npm bundle size npm Codecov GitHub branch checks state CodeFactor GitHub

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]
// }

See more examples with the database drivers on examples.

Hey, I want a feature!

Want more features? Open something up on issues. To be honest, I don't know what else to add.