@probitas/client-sql
Common SQL types and utilities for Probitas SQL client packages.
This package provides shared types, result classes, and errors used across all SQL-related client packages.
Features
- Query Results:
SqlQueryResultclass with row iteration and metadata - Transactions: Common transaction interface with isolation levels
- Error Hierarchy: SQL-specific errors (
SqlError,QuerySyntaxError,ConstraintError,DeadlockError)
Installation
deno add jsr:@probitas/client-sql
Usage
This package is typically used as a dependency by database-specific packages. End users should import from the specific database client packages instead.
import {
SqlQueryResult,
SqlError,
ConstraintError,
} from "@probitas/client-sql";
import type { SqlTransaction, SqlIsolationLevel } from "@probitas/client-sql";
// Handle SQL errors
try {
await client.query("INSERT INTO users (email) VALUES ($1)", ["duplicate@example.com"]);
} catch (error) {
if (error instanceof ConstraintError) {
console.log("Constraint violation:", error.constraint);
}
}
Database-Specific Packages
| Package | Description |
|---|---|
@probitas/client-sql-postgres |
PostgreSQL client |
@probitas/client-sql-mysql |
MySQL client |
@probitas/client-sql-sqlite |
SQLite client |
@probitas/client-sql-duckdb |
DuckDB client |
Links
Installation
deno add jsr:@probitas/client-sqlClasses
#ConstraintError
class ConstraintError extends SqlErrorSqlErrorError thrown when a constraint violation occurs.
| Name | Description |
|---|---|
name | — |
kind | — |
constraint | — |
Constructor
new ConstraintError(message: string, constraint: string, options?: SqlErrorOptions)Properties
- readonly
namestring - readonly
kind"constraint" - readonly
constraintstring
#DeadlockError
class DeadlockError extends SqlErrorSqlErrorError thrown when a deadlock is detected.
Constructor
new DeadlockError(message: string, options?: SqlErrorOptions)Properties
- readonly
namestring - readonly
kind"deadlock"
#QuerySyntaxError
class QuerySyntaxError extends SqlErrorSqlErrorError thrown when a SQL query has syntax errors.
Constructor
new QuerySyntaxError(message: string, options?: SqlErrorOptions)Properties
- readonly
namestring - readonly
kind"query"
#SqlError
class SqlError extends ClientErrorClientErrorBase error class for SQL-specific errors. Extends ClientError with SQL-specific properties.
Constructor
new SqlError(message: string, kind: SqlErrorKind, options?: SqlErrorOptions)Properties
- readonly
namestring - readonly
sqlState?string
#SqlQueryResult
class SqlQueryResult<T = Record<string, any>>SQL query result with rows, metadata, and transformation methods.
| Name | Description |
|---|---|
type | — |
ok | — |
rows | — |
rowCount | — |
duration | — |
metadata | — |
map() | Map rows to a new type. |
as() | Create class instances from rows. |
Constructor
new SqlQueryResult(init: SqlQueryResultInit<T>)Properties
- readonly
type"sql" - readonly
okboolean - readonly
rowCountnumber - readonly
durationnumber
Methods
map(): unknownMap rows to a new type.
as(): unknownCreate class instances from rows.
#SqlRows
class SqlRows<T> extends Array<T>Array<T>Row array with first/last helper methods.
Implements ReadonlyArray
| Name | Description |
|---|---|
first() | Get the first row, or undefined if empty. |
firstOrThrow() | Get the first row, or throw if empty. |
last() | Get the last row, or undefined if empty. |
lastOrThrow() | Get the last row, or throw if empty. |
Constructor
new SqlRows(items: readonly T[])Methods
first(): unknownGet the first row, or undefined if empty.
firstOrThrow(): unknownGet the first row, or throw if empty.
last(): unknownGet the last row, or undefined if empty.
lastOrThrow(): unknownGet the last row, or throw if empty.
Interfaces
#SqlErrorOptions
interface SqlErrorOptions extends ErrorOptionsOptions for SqlError constructor.
| Name | Description |
|---|---|
sqlState | SQL State code (e.g., "23505" for unique violation) |
Properties
- readonly
sqlState?stringSQL State code (e.g., "23505" for unique violation)
#SqlQueryResultInit
interface SqlQueryResultInit<T>Options for creating a SqlQueryResult.
| Name | Description |
|---|---|
ok | Whether the query succeeded |
rows | The result rows |
rowCount | Number of affected rows (for INSERT/UPDATE/DELETE) |
duration | Query execution duration in milliseconds |
metadata | Additional metadata |
Properties
- readonly
okbooleanWhether the query succeeded
- readonly
rowsreadonly T[]The result rows
- readonly
rowCountnumberNumber of affected rows (for INSERT/UPDATE/DELETE)
- readonly
durationnumberQuery execution duration in milliseconds
Additional metadata
#SqlQueryResultMetadata
interface SqlQueryResultMetadataMetadata for SQL query results.
| Name | Description |
|---|---|
lastInsertId | Last inserted ID (for INSERT statements) |
warnings | Warning messages from the database |
Properties
- readonly
lastInsertId?bigint | stringLast inserted ID (for INSERT statements)
- readonly
warnings?readonly string[]Warning messages from the database
#SqlTransaction
interface SqlTransactionSQL transaction interface. Implementations should provide actual database-specific transaction handling.
| Name | Description |
|---|---|
query() | Execute a query within the transaction. |
queryOne() | Execute a query and return the first row or undefined. |
commit() | Commit the transaction. |
rollback() | Rollback the transaction. |
Methods
query<T = Record<string, any>>(sql: string, params?: unknown[]): Promise<SqlQueryResult<T>>Execute a query within the transaction.
Parameters
sqlstring- SQL query string
params?unknown[]- Optional query parameters
queryOne<T = Record<string, any>>(sql: string, params?: unknown[]): Promise<T | undefined>Execute a query and return the first row or undefined.
Parameters
sqlstring- SQL query string
params?unknown[]- Optional query parameters
commit(): Promise<void>Commit the transaction.
rollback(): Promise<void>Rollback the transaction.
#SqlTransactionOptions
interface SqlTransactionOptionsOptions for starting a transaction.
| Name | Description |
|---|---|
isolationLevel | Isolation level for the transaction |
Properties
Isolation level for the transaction
Type Aliases
#SqlErrorKind
type SqlErrorKind = "query" | "constraint" | "deadlock" | "unknown"SQL-specific error kinds.
#SqlIsolationLevel
type SqlIsolationLevel = "read_uncommitted" | "read_committed" | "repeatable_read" | "serializable"Transaction isolation level.
