@probitas/client-sql-duckdb

DuckDB client for Probitas scenario testing framework.

This package provides a DuckDB client designed for integration testing, with analytical query capabilities and Parquet/CSV file support.

Features

  • Query Execution: Parameterized queries with type-safe results
  • Transactions: Full transaction support
  • File Formats: Native support for Parquet, CSV, and JSON files
  • In-Memory Databases: Perfect for isolated test scenarios
  • Analytical Queries: Optimized for OLAP workloads
  • Resource Management: Implements AsyncDisposable for proper cleanup

Installation

deno add jsr:@probitas/client-sql-duckdb

Quick Start

import { createDuckDbClient } from "@probitas/client-sql-duckdb";

// In-memory database for testing
const client = await createDuckDbClient({
  path: ":memory:",
});

// Query from Parquet files
const result = await client.query<{ id: number; name: string }>(
  "SELECT id, name FROM read_parquet('data/*.parquet') WHERE active = ?",
  [true]
);
console.log(result.rows);

// Analytical queries
const stats = await client.query(`
  SELECT
    date_trunc('month', created_at) as month,
    COUNT(*) as count,
    AVG(amount) as avg_amount
  FROM transactions
  GROUP BY 1
  ORDER BY 1
`);

await client.close();

Transactions

import { createDuckDbClient } from "@probitas/client-sql-duckdb";
import type { SqlTransaction } from "@probitas/client-sql";

const client = await createDuckDbClient({ path: ":memory:" });
await client.transaction(async (tx: SqlTransaction) => {
  await tx.query("INSERT INTO accounts (id, balance) VALUES (?, ?)", [1, 100]);
  await tx.query("INSERT INTO accounts (id, balance) VALUES (?, ?)", [2, 200]);
  // Automatically committed if no error, rolled back on exception
});
await client.close();

Using with using Statement

import { createDuckDbClient } from "@probitas/client-sql-duckdb";

await using client = await createDuckDbClient({ path: ":memory:" });

const result = await client.query("SELECT 42 as answer");
// Client automatically closed when block exits
PackageDescription
`@probitas/client-sql`Common SQL types and utilities
`@probitas/client-sql-postgres`PostgreSQL client
`@probitas/client-sql-mysql`MySQL client
`@probitas/client-sql-sqlite`SQLite client

Installation

deno add jsr:@probitas/client-sql-duckdb

Classes

class

#CatalogError

class CatalogError extends DuckDbError

Error thrown for catalog errors (table not found, etc.).

NameDescription
name
duckdbKind
Constructor
new CatalogError(message: string, options?: DuckDbErrorOptions)
Properties
  • readonlynamestring
  • readonlyduckdbKind"catalog"
class

#ConstraintError

class ConstraintError extends SqlError
ExtendsSqlError

Error thrown when a constraint violation occurs.

NameDescription
name
kind
constraint
Constructor
new ConstraintError(
  message: string,
  constraint: string,
  options?: SqlErrorOptions,
)
Properties
  • readonlynamestring
  • readonlykind"constraint"
  • readonlyconstraintstring
class

#DeadlockError

class DeadlockError extends SqlError
ExtendsSqlError

Error thrown when a deadlock is detected.

NameDescription
name
kind
Constructor
new DeadlockError(message: string, options?: SqlErrorOptions)
Properties
  • readonlynamestring
  • readonlykind"deadlock"
class

#DuckDbError

class DuckDbError extends SqlError
ExtendsSqlError

Base error class for DuckDB-specific errors. Extends SqlError with DuckDB-specific properties.

NameDescription
name
errorType
Constructor
new DuckDbError(message: string, options?: DuckDbErrorOptions)
Properties
  • readonlynamestring
  • readonlyerrorType?string
class

#DuckDbTransactionImpl

class DuckDbTransactionImpl implements SqlTransaction
ImplementsSqlTransaction
NameDescription
begin()Begin a new transaction.
query()
queryOne()
commit()
rollback()
Constructor
new DuckDbTransactionImpl(conn: DuckDBConnection)
Methods
static begin(): unknown

Begin a new transaction.

query(): unknown
queryOne(): unknown
commit(): unknown
rollback(): unknown
class

#IoError

class IoError extends DuckDbError

Error thrown for IO-related errors (file not found, permission denied, etc.).

NameDescription
name
duckdbKind
Constructor
new IoError(message: string, options?: DuckDbErrorOptions)
Properties
  • readonlynamestring
  • readonlyduckdbKind"io"
class

#QuerySyntaxError

class QuerySyntaxError extends SqlError
ExtendsSqlError

Error thrown when a SQL query has syntax errors.

NameDescription
name
kind
Constructor
new QuerySyntaxError(message: string, options?: SqlErrorOptions)
Properties
  • readonlynamestring
  • readonlykind"query"
class

#SqlConnectionError

class SqlConnectionError extends SqlError
ExtendsSqlError

Error thrown when a connection or network-level error occurs.

This includes:

  • Connection refused (server not running)
  • Authentication failure
  • Connection timeout
  • Pool exhaustion
  • TLS handshake failure
  • DNS resolution failure
NameDescription
name
kind
Constructor
new SqlConnectionError(message: string, options?: SqlErrorOptions)
Properties
  • readonlynamestring
  • readonlykind"connection"
class

#SqlError

class SqlError extends ClientError

Base error class for SQL-specific errors. Extends ClientError with SQL-specific properties.

NameDescription
name
kind
sqlState
Constructor
new SqlError(message: string, kind: SqlErrorKind, options?: SqlErrorOptions)
Properties
  • readonlynamestring
  • readonlykindSqlErrorKind
  • readonlysqlStatestring | null

Interfaces

interface

#DuckDbClient

interface DuckDbClient extends AsyncDisposable

DuckDB client interface.

NameDescription
configThe client configuration.
dialectThe SQL dialect identifier.
query()Execute a SQL query.
queryOne()Execute a query and return the first row or undefined.
transaction()Execute a function within a transaction.
queryParquet()Query a Parquet file directly.
queryCsv()Query a CSV file directly.
close()Close the database connection.
Properties
  • readonlyconfigDuckDbClientConfig

    The client configuration.

  • readonlydialect"duckdb"

    The SQL dialect identifier.

Methods
query<T = Record<string, any>>(
  sql: string,
  params?: unknown[],
  options?: SqlQueryOptions,
): Promise<SqlQueryResult<T>>

Execute a SQL query.

Parameters
  • sqlstring
    • SQL query string
  • params?unknown[]
    • Optional query parameters
    • Optional query options
queryOne<T = Record<string, any>>(
  sql: string,
  params?: unknown[],
  options?: SqlQueryOptions,
): Promise<T | undefined>

Execute a query and return the first row or undefined.

Parameters
  • sqlstring
    • SQL query string
  • params?unknown[]
    • Optional query parameters
    • Optional query options
transaction<T>(
  fn: (tx: SqlTransaction) => unknown,
  options?: SqlTransactionOptions | DuckDbTransactionOptions,
): Promise<T>

Execute a function within a transaction. Automatically commits on success or rolls back on error.

Parameters
queryParquet<T = Record<string, any>>(path: string): Promise<SqlQueryResult<T>>

Query a Parquet file directly. DuckDB can read Parquet files without importing them.

Parameters
  • pathstring
    • Path to the Parquet file
queryCsv<T = Record<string, any>>(path: string): Promise<SqlQueryResult<T>>

Query a CSV file directly. DuckDB can read CSV files without importing them.

Parameters
  • pathstring
    • Path to the CSV file
close(): Promise<void>

Close the database connection.

interface

#DuckDbClientConfig

interface DuckDbClientConfig extends CommonOptions

Configuration for creating a DuckDB client.

NameDescription
pathDatabase file path.
readonlyOpen the database in read-only mode.
throwOnErrorWhether to throw an error for query failures.
Properties
  • readonlypath?string

    Database file path. Use :memory: or omit for an in-memory database.

  • readonlyreadonly?boolean

    Open the database in read-only mode.

  • readonlythrowOnError?boolean

    Whether to throw an error for query failures. When false, failures are returned as SqlQueryResultError or SqlQueryResultFailure. Can be overridden per-query via SqlQueryOptions.

interface

#DuckDbErrorOptions

interface DuckDbErrorOptions extends SqlErrorOptions

Options for DuckDbError constructor.

NameDescription
errorTypeDuckDB error type if available.
Properties
  • readonlyerrorType?string

    DuckDB error type if available.

interface

#DuckDbTransactionOptions

interface DuckDbTransactionOptions extends SqlTransactionOptions

DuckDB-specific transaction options.

interface

#SqlErrorOptions

interface SqlErrorOptions extends ErrorOptions

Options for SqlError constructor.

NameDescription
sqlStateSQL State code (e.g., "23505" for unique violation)
Properties
  • readonlysqlState?string

    SQL State code (e.g., "23505" for unique violation)

interface

#SqlQueryOptions

interface SqlQueryOptions extends CommonOptions

Options for individual SQL queries.

NameDescription
throwOnErrorWhether to throw an error for query failures.
Properties
  • readonlythrowOnError?boolean

    Whether to throw an error for query failures. When false, failures are returned as SqlQueryResultError or SqlQueryResultFailure.

interface

#SqlQueryResultError

interface SqlQueryResultError<T = any> extends SqlQueryResultBase<T>

SQL query result for query errors (syntax errors, constraint violations, etc.).

Server received and processed the query, but it failed due to a SQL error.

NameDescription
processedServer processed the query.
okQuery failed.
errorError describing the SQL error.
rowsEmpty rows for failed queries.
rowCountZero affected rows for failed queries.
lastInsertIdNo lastInsertId for failed queries.
warningsNo warnings for failed queries.
Properties
  • readonlyprocessedtrue

    Server processed the query.

  • readonlyokfalse

    Query failed.

  • readonlyerrorSqlError

    Error describing the SQL error.

  • readonlyrowsreadonly never[]

    Empty rows for failed queries.

  • readonlyrowCount0

    Zero affected rows for failed queries.

  • readonlylastInsertIdnull

    No lastInsertId for failed queries.

  • readonlywarningsnull

    No warnings for failed queries.

interface

#SqlQueryResultFailure

interface SqlQueryResultFailure<T = any> extends SqlQueryResultBase<T>

SQL query result for connection failures (network errors, timeouts, etc.).

Query could not be processed by the server (connection refused, timeout, pool exhausted, authentication failure, etc.).

NameDescription
processedServer did not process the query.
okQuery failed.
errorError describing the failure.
rowsNo rows (query didn't reach server).
rowCountNo row count (query didn't reach server).
lastInsertIdNo lastInsertId (query didn't reach server).
warningsNo warnings (query didn't reach server).
Properties
  • readonlyprocessedfalse

    Server did not process the query.

  • readonlyokfalse

    Query failed.

  • readonlyerrorSqlFailureError

    Error describing the failure.

  • readonlyrowsnull

    No rows (query didn't reach server).

  • readonlyrowCountnull

    No row count (query didn't reach server).

  • readonlylastInsertIdnull

    No lastInsertId (query didn't reach server).

  • readonlywarningsnull

    No warnings (query didn't reach server).

interface

#SqlQueryResultSuccess

interface SqlQueryResultSuccess<T = any> extends SqlQueryResultBase<T>

SQL query result for successful queries.

The query was executed successfully and returned results.

NameDescription
processedServer processed the query.
okQuery succeeded.
errorNo error for successful queries.
rowsQuery result rows.
rowCountNumber of affected rows.
lastInsertIdLast inserted ID (for INSERT statements).
warningsWarning messages from the database.
Properties
  • readonlyprocessedtrue

    Server processed the query.

  • readonlyoktrue

    Query succeeded.

  • readonlyerrornull

    No error for successful queries.

  • readonlyrowsreadonly T[]

    Query result rows.

  • readonlyrowCountnumber

    Number of affected rows.

  • readonlylastInsertIdbigint | string | null

    Last inserted ID (for INSERT statements).

  • readonlywarningsunknown | null

    Warning messages from the database.

interface

#SqlQueryResultSuccessParams

interface SqlQueryResultSuccessParams<T = any>

Parameters for creating a SqlQueryResultSuccess.

NameDescription
rowsThe result rows
rowCountNumber of affected rows (for INSERT/UPDATE/DELETE)
durationQuery execution duration in milliseconds
lastInsertIdLast inserted ID (for INSERT statements)
warningsWarning messages from the database
Properties
  • readonlyrowsreadonly T[]

    The result rows

  • readonlyrowCountnumber

    Number of affected rows (for INSERT/UPDATE/DELETE)

  • readonlydurationnumber

    Query execution duration in milliseconds

  • readonlylastInsertId?bigint | string

    Last inserted ID (for INSERT statements)

  • readonlywarnings?readonly string[]

    Warning messages from the database

interface

#SqlTransaction

interface SqlTransaction

SQL transaction interface. Implementations should provide actual database-specific transaction handling.

NameDescription
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[],
  options?: SqlQueryOptions,
): Promise<SqlQueryResult<T>>

Execute a query within the transaction.

Parameters
  • sqlstring
    • SQL query string
  • params?unknown[]
    • Optional query parameters
    • Optional query options
queryOne<T = Record<string, any>>(
  sql: string,
  params?: unknown[],
  options?: SqlQueryOptions,
): Promise<T | undefined>

Execute a query and return the first row or undefined.

Parameters
  • sqlstring
    • SQL query string
  • params?unknown[]
    • Optional query parameters
    • Optional query options
commit(): Promise<void>

Commit the transaction.

rollback(): Promise<void>

Rollback the transaction.

interface

#SqlTransactionOptions

interface SqlTransactionOptions

Options for starting a transaction.

NameDescription
isolationLevelIsolation level for the transaction
Properties

Functions

function

#convertDuckDbError

function convertDuckDbError(error: unknown): SqlError

Convert a DuckDB error to the appropriate error class.

DuckDB errors are classified based on message content analysis.

Parameters
  • errorunknown
function

#createDuckDbClient

async function createDuckDbClient(
  config: DuckDbClientConfig,
): Promise<DuckDbClient>

Create a new DuckDB client instance.

The client provides parameterized queries, transaction support, and DuckDB-specific features like direct Parquet and CSV file querying.

Parameters
Returns

Promise<DuckDbClient> — A promise resolving to a new DuckDB client instance

Examples

Using in-memory database (default)

import { createDuckDbClient } from "@probitas/client-sql-duckdb";

const client = await createDuckDbClient({});

const result = await client.query("SELECT 42 as answer");
if (result.ok) {
  console.log(result.rows[0]);  // { answer: 42 }
}

await client.close();

Using file-based database

import { createDuckDbClient } from "@probitas/client-sql-duckdb";

const client = await createDuckDbClient({
  path: "./data.duckdb",
});
await client.close();

Query Parquet files directly

import { createDuckDbClient } from "@probitas/client-sql-duckdb";

const client = await createDuckDbClient({ path: ":memory:" });

// Query directly from Parquet
const result = await client.queryParquet<{ id: number; value: string }>(
  "./data/events.parquet"
);

await client.close();

Query CSV files directly

import { createDuckDbClient } from "@probitas/client-sql-duckdb";

const client = await createDuckDbClient({ path: ":memory:" });

const result = await client.queryCsv<{ name: string; age: number }>(
  "./data/users.csv"
);

await client.close();

Transaction with auto-commit/rollback

import { createDuckDbClient } from "@probitas/client-sql-duckdb";
import type { SqlTransaction } from "@probitas/client-sql";

const client = await createDuckDbClient({ path: ":memory:" });
await client.transaction(async (tx: SqlTransaction) => {
  await tx.query("INSERT INTO users VALUES ($1, $2)", [1, "Alice"]);
  await tx.query("INSERT INTO users VALUES ($1, $2)", [2, "Bob"]);
});
await client.close();

Using await using for automatic cleanup

import { createDuckDbClient } from "@probitas/client-sql-duckdb";

await using client = await createDuckDbClient({});

const result = await client.query("SELECT 1");
// Client automatically closed when scope exits
function

#isConnectionError

function isConnectionError(error: unknown): boolean

Check if an error is a connection-level error. These are errors that indicate the database cannot be accessed at all, not errors that occur during query execution.

Parameters
  • errorunknown

Types

type

#DuckDbErrorKind

type DuckDbErrorKind = "io" | "catalog" | "parser" | "binder"

DuckDB-specific error kinds.

type

#SqlErrorKind

type SqlErrorKind = "query" | "constraint" | "deadlock" | "connection" | "unknown"

SQL-specific error kinds.

type

#SqlFailureError

type SqlFailureError = SqlConnectionError | AbortError | TimeoutError

Error types that indicate the operation was not processed. These are errors that occur before the query reaches the SQL server.

type

#SqlIsolationLevel

type SqlIsolationLevel = "read_uncommitted" | "read_committed" | "repeatable_read" | "serializable"

Transaction isolation level.

type

#SqlOperationError

type SqlOperationError = QuerySyntaxError | ConstraintError | DeadlockError | SqlError

Error types that indicate an operation was processed by the server. These errors occur after the query reaches the SQL server.

type

#SqlQueryResult

type SqlQueryResult<T = any> = SqlQueryResultSuccess<T> | SqlQueryResultError<T> | SqlQueryResultFailure<T>

SQL query result union type representing all possible result states.

  • Success: processed: true, ok: true, error: null
  • Error: processed: true, ok: false, error: SqlError
  • Failure: processed: false, ok: false, error: SqlConnectionError
Search Documentation