@variablesoftware/mock-d1
Version:
🎛️🗂️🧠 Mock D1 Database implementation for testing Cloudflare Workers
27 lines (26 loc) • 1.33 kB
TypeScript
/**
* Checks if the given SQL command is supported by D1.
*
* Validation funnel:
* 1. Trim the SQL statement.
* 2. Explicitly allow CREATE TABLE (with or without IF NOT EXISTS, with or without columns).
* - This is handled first to avoid false positives from unsupported pattern checks (e.g., "IN" in "IF NOT EXISTS").
* 3. For all other statements, check for unsupported SQL patterns (e.g., IN, LIKE, BETWEEN, etc.).
* - If any unsupported pattern is found, the statement is rejected.
* 4. Allow other primary SQL operations (INSERT, SELECT, UPDATE, DELETE, DROP, TRUNCATE, ALTER) if they do not contain unsupported patterns.
* 5. Reject everything else.
*
* @param sql - The SQL statement string.
* @returns True if the SQL command is supported and does not contain unsupported features, false otherwise.
*/
export declare function validateSQLSyntax(sql: string): boolean;
/**
* Centralized SQL validation for D1 mock engine.
* Throws a D1-like error if the SQL is not a supported command or contains unsupported features/operators.
* Optionally skips malformed SQL checks (for prepare-time validation only).
* @param sql - The SQL statement string.
* @param opts - Optional options object.
*/
export declare function validateSqlOrThrow(sql: string, opts?: {
skipMalformed?: boolean;
}): void;