All Projects → blakeembrey → Sql Template Tag

blakeembrey / Sql Template Tag

Licence: mit
ES2015 tagged template string for preparing SQL statements, works with `pg` and `mysql`

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Sql Template Tag

Jet
Type safe SQL builder with code generation and automatic query result data mapping
Stars: ✭ 373 (+182.58%)
Mutual labels:  sql, sql-query, mysql, postgresql
Goqu
SQL builder and query library for golang
Stars: ✭ 984 (+645.45%)
Mutual labels:  sql, sql-query, mysql, postgresql
Jooq
jOOQ is the best way to write SQL in Java
Stars: ✭ 4,695 (+3456.82%)
Mutual labels:  sql, sql-query, mysql, postgresql
Querybuilder
SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird
Stars: ✭ 2,111 (+1499.24%)
Mutual labels:  sql, sql-query, mysql, postgresql
Vscode Sqltools
Database management for VSCode
Stars: ✭ 741 (+461.36%)
Mutual labels:  sql, sql-query, mysql, postgresql
Fluentpdo
A PHP SQL query builder using PDO
Stars: ✭ 783 (+493.18%)
Mutual labels:  sql, mysql, postgresql
Eosio sql plugin
EOSIO sql database plugin
Stars: ✭ 21 (-84.09%)
Mutual labels:  sql, mysql, postgresql
Ddlparse
DDL parase and Convert to BigQuery JSON schema and DDL statements
Stars: ✭ 52 (-60.61%)
Mutual labels:  sql, mysql, postgresql
Sworm
a write-only ORM for Node.js
Stars: ✭ 128 (-3.03%)
Mutual labels:  sql, mysql, postgresql
Db Dumper
Dump the contents of a database
Stars: ✭ 744 (+463.64%)
Mutual labels:  sql, mysql, postgresql
Aspnetcorenlog
ASP.NET Core NLog MS SQL Server PostgreSQL MySQL Elasticsearch
Stars: ✭ 54 (-59.09%)
Mutual labels:  sql, mysql, postgresql
Sql
MySQL & PostgreSQL pipe
Stars: ✭ 81 (-38.64%)
Mutual labels:  sql, mysql, postgresql
Smartsql
SmartSql = MyBatis in C# + .NET Core+ Cache(Memory | Redis) + R/W Splitting + PropertyChangedTrack +Dynamic Repository + InvokeSync + Diagnostics
Stars: ✭ 775 (+487.12%)
Mutual labels:  sql, mysql, postgresql
Eralchemy
Entity Relation Diagrams generation tool
Stars: ✭ 767 (+481.06%)
Mutual labels:  sql, mysql, postgresql
Usql
Universal command-line interface for SQL databases
Stars: ✭ 6,869 (+5103.79%)
Mutual labels:  sql, mysql, postgresql
Squid
🦑 Provides SQL tagged template strings and schema definition functions.
Stars: ✭ 57 (-56.82%)
Mutual labels:  sql, sql-query, postgresql
Electrocrud
Database CRUD Application Built on Electron | MySQL, Postgres, SQLite
Stars: ✭ 1,267 (+859.85%)
Mutual labels:  sql, mysql, postgresql
Graphjin
GraphJin - Build APIs in 5 minutes with GraphQL. An instant GraphQL to SQL compiler.
Stars: ✭ 1,264 (+857.58%)
Mutual labels:  sql, mysql, postgresql
Kangaroo
SQL client and admin tool for popular databases
Stars: ✭ 127 (-3.79%)
Mutual labels:  sql, mysql, postgresql
Sequelize
An easy-to-use and promise-based multi SQL dialects ORM tool for Node.js
Stars: ✭ 25,422 (+19159.09%)
Mutual labels:  sql, mysql, postgresql

SQL Template Tag

NPM version NPM downloads Build status Test coverage

ES2015 tagged template string for preparing SQL statements, works with pg and mysql.

Installation

npm install sql-template-tag --save

Usage

import sql, { empty, join, raw } from "sql-template-tag";

const query = sql`SELECT * FROM books WHERE id = ${id}`;

query.sql; //=> "SELECT * FROM books WHERE id = ?"
query.text; //=> "SELECT * FROM books WHERE id = $1"
query.values; //=> [id]

pg.query(query); // Uses `text` and `values`.
mysql.query(query); // Uses `sql` and `values`.

// Embed SQL instances inside SQL instances.
const nested = sql`SELECT id FROM authors WHERE name = ${"Blake"}`;
const query = sql`SELECT * FROM books WHERE author_id IN (${nested})`;

// Join and "empty" helpers (useful for nested queries).
sql`SELECT * FROM books ${hasIds ? sql`WHERE ids IN (${join(ids)})` : empty}`;

Join

Accepts an array of values and returns a SQL instance with the values joined by the separator. E.g.

const query = join([1, 2, 3]);

query.sql; //=> "?, ?, ?"
query.values; //=> [1, 2, 3]

Raw

Accepts a string and returns a SQL instance, useful if you want some part of the SQL to be dynamic.

raw("SELECT"); // == sql`SELECT`

Do not accept user input to raw, this will create a SQL injection vulnerability.

Empty

Simple placeholder value for an empty SQL string. Equivalent to raw("").

Related

Some other modules exist that do something similar:

  • sql-template-strings: promotes mutation via chained methods and lacks nesting SQL statements. The idea to support sql and text properties for dual mysql and pg compatibility came from here.
  • pg-template-tag: missing TypeScript and MySQL support. This is the API I envisioned before writing this library, and by supporting pg only it has the ability to dedupe values.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].