sqlite-prepare
Version:
A type-safe SQL query builder for Cloudflare D1
92 lines (91 loc) • 3.29 kB
TypeScript
/**
* Represents valid SQL parameter types that can be used in prepared statements
*/
export type SQLParam = string | number | boolean | null | undefined | Date | Uint8Array | ArrayBuffer;
/**
* Represents a SQL query with prepared statement parameters
*/
export interface SQLQuery {
/** The SQL query string with parameter placeholders */
query: string;
/** The parameters to bind to the prepared statement */
params: SQLParam[];
}
/**
* Creates a raw SQL fragment that will be inserted directly into the query without escaping
* @param value - The raw SQL string to insert
* @returns An object marking the string as raw SQL
*/
export declare function raw(value: string): {
__raw: true;
value: string;
};
/**
* Creates a raw SQL fragment using template literals that will be inserted directly into the query
* @param strings - Template strings array
* @param values - Values to interpolate into the template
* @returns An object marking the string as raw SQL
*/
export declare function raw(strings: TemplateStringsArray, ...values: any[]): {
__raw: true;
value: string;
};
/**
* Builds a SQL query with parameterized values
*
* @param strings - SQL query string with placeholders or template strings array
* @param values - Values to interpolate into the query
* @returns A SQLQuery object with the query string and parameters
*
* @example
* // Using a template literal
* const userId = 123;
* const query = build`SELECT * FROM users WHERE id = ${userId}`;
*
* @example
* // Using an array for IN clause
* const roleIds = [1, 2, 3];
* const query = build`SELECT * FROM users WHERE role_id IN ${roleIds}`;
*
* @example
* // Using a raw SQL fragment
* const query = build`SELECT * FROM users WHERE ${raw('created_at > NOW()')}`;
*
* @example
* // Using a subquery
* const subquery = build`SELECT id FROM active_users`;
* const query = build`SELECT * FROM users WHERE id IN ${subquery}`;
*/
export declare function build(strings: string | TemplateStringsArray, ...values: any[]): SQLQuery;
/**
* Creates a prepared statement with bound parameters in a type-safe way
*
* @param db - The D1 database instance
* @param string - SQL query string with ? placeholders
* @param values - Values to bind to the prepared statement
* @returns A D1PreparedStatement with bound parameters
*
* @example
* // Using a string with placeholders
* const stmt = prepare(db, "SELECT * FROM users WHERE id = ?", userId);
*/
export declare function prepare(db: D1Database, string: string, ...values: any[]): D1PreparedStatement;
/**
* Creates a prepared statement with bound parameters using template literals
*
* @param db - The D1 database instance
* @param strings - Template strings array
* @param values - Values to interpolate into the query
* @returns A D1PreparedStatement with bound parameters
*
* @example
* // Using a template literal
* const userId = 123;
* const stmt = prepare(db, `SELECT * FROM users WHERE id = ${userId}`);
*
* @example
* // Using an array for IN clause
* const roleIds = [1, 2, 3];
* const stmt = prepare(db, `SELECT * FROM users WHERE role_id IN ${roleIds}`);
*/
export declare function prepare(db: D1Database, strings: TemplateStringsArray, ...values: any[]): D1PreparedStatement;