squid
Version:
Provides SQL tagged template strings and a schema definition function.
49 lines (48 loc) • 2.16 kB
TypeScript
import { ValueRecord } from "../utils";
import { QueryConfig } from "./config";
import { SqlBuilder } from "./builder";
export { QueryConfig, SqlBuilder };
export declare type PgQueryConfig = QueryConfig;
/**
* SQL template string tag. Returns a query object: `{ text: string, values: any[] }`.
* Template expressions are automatically SQL-injection-proofed unless wrapped in `sql.raw()`.
*
* @example
* const { rows } = await database.query(sql`SELECT name, email FROM users WHERE id=${userID}`)
* @example
* const { rows } = await database.query<{ name: string, email: string }>(sql`
* SELECT name, email FROM users WHERE id=${userID}
* `)
*/
export declare function sql(texts: TemplateStringsArray, ...values: any[]): PgQueryConfig;
export declare namespace sql {
var raw: typeof rawExpression;
var safe: typeof safeExpression;
}
/**
* Wrap an SQL template expression value in `sql.raw()` to inject the raw value into the query.
* Attention: Can easily lead to SQL injections! Use with caution and only if necessary.
*/
declare function rawExpression(rawValue: string): SqlBuilder;
declare function safeExpression<T>(value: T): SqlBuilder<T>;
export { rawExpression as raw, safeExpression as safe };
/**
* Convenience function to keep WHERE clauses concise. Takes an object:
* Keys are supposed to be a column name, values the values that the record must have set.
*
* @example
* const { rows } = await database.query(sql`SELECT * FROM users WHERE ${spreadAnd({ name: "John", email: "john@example.com" })}`)
*/
export declare function spreadAnd<T>(record: any): SqlBuilder<T>;
/**
* Convenience function to keep INSERT statements concise.
* @example
* await database.query(sql`INSERT INTO users ${spreadInsert({ name: "John", email: "john@example.com" })}`)
*/
export declare function spreadInsert<T>(...records: ValueRecord[]): SqlBuilder<T>;
/**
* Convenience function to keep UPDATE statements concise. Takes an object:
* @example
* await database.query(sql`UPDATE users SET ${spreadUpdate({ name: "John", email: "john@example.com" })} WHERE id = 1`)
*/
export declare function spreadUpdate(record: any): SqlBuilder;