UNPKG

sql-template-strings-ts

Version:

Functional wrapper for sql-template-strings: ES6 tagged template strings for prepared statements with MySQL and PostgreSQL

88 lines 3.05 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Monoid = exports.Semigroup = exports.useBind = exports.setName = exports.append = exports.t = void 0; const sql_template_strings_1 = __importStar(require("sql-template-strings")); const clone = (statement) => new sql_template_strings_1.SQLStatement(statement.strings.slice(0), statement.values.slice(0)); /** * 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}` * ``` */ exports.t = sql_template_strings_1.default; /** * 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}`) * ) * ``` */ const append = (second) => (first) => clone(first).append(second); exports.append = append; /** * 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') *) * ``` */ const setName = (name) => (statement) => clone(statement).setName(name); exports.setName = setName; /** * Use a prepared statement with Sequelize. * Makes `query` return a query with `$n` syntax instead of `?` and switches the `values` * key name to `bind`. */ const useBind = (bind) => (statement) => clone(statement).useBind(bind); exports.useBind = useBind; exports.Semigroup = { concat: (x, y) => exports.append(y)(x), }; exports.Monoid = Object.assign({ empty: exports.t `` }, exports.Semigroup); //# sourceMappingURL=index.js.map