@kuindji/sql-type-parser
Version:
Type-level SQL parser for TypeScript
66 lines • 2.05 kB
JavaScript
/**
* Database Integration Utilities
*
* Provides factory functions to create type-safe query wrappers.
* Users write their query ONCE and get full type inference.
*
* @example
* ```typescript
* import { createSelectFn } from '@kuindji/sql-type-parser'
*
* const select = createSelectFn<MySchema>((sql, params) =>
* pool.query(sql, params).then(r => r.rows)
* )
*
* // Query written once, result fully typed
* const users = await select("SELECT id, name FROM users WHERE id = $1", [1])
* // users: Array<{ id: number; name: string }>
*
* // Invalid queries show compile error
* const bad = await select("SELECT bad FROM users") // Error!
* ```
*/
// ============================================================================
// Factory Functions
// ============================================================================
/**
* Create a type-safe select function for your schema.
* This is a primitive example for you to use as a starting point.
*
* The returned function:
* - Validates the query at compile time (invalid queries won't compile)
* - Infers the result type from the query
*
* @example
* ```typescript
* import { createSelectFn } from '@kuindji/sql-type-parser'
*
* type Schema = {
* defaultSchema: "public",
* schemas: {
* public: {
* users: { id: number; name: string; email: string }
* }
* }
* }
*
* // Create the typed select function
* const select = createSelectFn<Schema>((sql, params) =>
* db.query(sql, params) // Your actual database call
* )
*
* // Use it
* const users = await select("SELECT id, name FROM users WHERE active = $1", [true])
* // users: Array<{ id: number; name: string }>
*
* // Invalid queries cause compile errors
* const bad = await select("SELECT unknown FROM users")
* // Error: Argument of type '"SELECT unknown FROM users"' is not assignable...
* ```
*/
export function createSelectFn(handler) {
return function select(query, params) {
return handler(query, params);
};
}
//# sourceMappingURL=db.js.map