sql-template-strings-ts
Version:
Functional wrapper for sql-template-strings: ES6 tagged template strings for prepared statements with MySQL and PostgreSQL
65 lines (64 loc) • 2.12 kB
TypeScript
import { Monoid as Monoid_ } from 'fp-ts/Monoid';
import { Semigroup as Semigroup_ } from 'fp-ts/Semigroup';
import { SQLStatement as SQLStatement_ } from 'sql-template-strings';
export interface SQLStatement extends Pick<SQLStatement_, 'text' | 'query' | 'sql' | 'values' | 'name'> {
readonly SQLStatement: unique symbol;
}
/**
* The template string tag.
*
* @example
* ```ts
* import * as SQL from 'spl-template-strings-ts'
* import { pipe } from 'fp-ts/function'
*
* declare const book: string
* declare const author: string
* const query = SQL.t`
* SELECT author FROM books
* WHERE name = ${book} AND author = ${author}`
* ```
*/
export declare const t: (strings: TemplateStringsArray, ...values: unknown[]) => SQLStatement;
/**
* Appends a string or another statement.
*
* @example
* ```ts
* import * as SQL from 'spl-template-strings-ts'
* import { identity, pipe } from 'fp-ts/function'
*
* declare const name: string | undefined
* declare const offset: number
* const query = pipe(
* SQL.t`SELECT * FROM books`,
* name ? SQL.append(SQL.t` WHERE name = ${name}`) : identity,
* SQL.append(SQL.t` LIMIT 10 OFFSET ${offset}`)
* )
* ```
*/
export declare const append: (second: SQLStatement | string | number) => (first: SQLStatement) => SQLStatement;
/**
* Sets the name property of this statement for prepared statements in PostgreSQL.
*
* @example
* ```ts
* import * as SQL from 'spl-template-strings-ts'
* import { pipe } from 'fp-ts/function'
*
* declare const book: string
* const query = pipe(
* SQL.t`SELECT author FROM books WHERE name = ${book}`,
* SQL.setName('my_query')
*)
* ```
*/
export declare const setName: (name: string) => (statement: SQLStatement) => SQLStatement;
/**
* Use a prepared statement with Sequelize.
* Makes `query` return a query with `$n` syntax instead of `?` and switches the `values`
* key name to `bind`.
*/
export declare const useBind: (bind: boolean) => (statement: SQLStatement) => SQLStatement;
export declare const Semigroup: Semigroup_<SQLStatement>;
export declare const Monoid: Monoid_<SQLStatement>;