sqlite-prepare
Version:
A type-safe SQL query builder for Cloudflare D1
34 lines (33 loc) • 1.19 kB
JavaScript
;
/// <reference types="./types/d1-types" />
Object.defineProperty(exports, "__esModule", { value: true });
exports.wrapD1 = void 0;
const prepare_1 = require("./query-builder/prepare");
/**
* Creates a function that provides type-safe prepared statements for a D1 database
*
* This function wraps a D1Database instance to provide a more convenient API for
* creating prepared statements with template literals. It performs validation
* to ensure the database connection is established.
*
* @param db - The D1Database instance to wrap
* @returns A function that can be used to create prepared statements with the provided database
* @throws Error if the database connection is not established
*
* @example
* ```typescript
* // In a Cloudflare Worker
* const prepareD1 = await wrapD1(env.DB);
*
* // Then use it to create prepared statements
* const users = await prepareD1()`SELECT * FROM users WHERE id = ${userId}`.all();
* ```
*/
const wrapD1 = (db) => {
// Check if the database is connected
if (!db) {
throw new Error('Database connection is not established.');
}
return prepare_1.prepare.bind(null, db);
};
exports.wrapD1 = wrapD1;