Architecture Map
A short map of the source tree for contributors.
Entry Points and Runtime Modes
src/driver.ts:drizzle(...)factory with five overloads. The connection-string and{ connection }forms get a sharedDuckDBInstancefor the path throughDuckDBInstance.fromCache()and create either one connection (pool: false) or a pool fromcreateDuckDBConnectionPool(). The client forms wrap an existing connection or pool, and wrap a rawpg.PoolwithcreatePgDuckConnectionPool(). Passescasingto the dialect. BuildsDuckDBDatabase, which addsexecuteBatches,executeBatchesRaw,executeArrow,close(),$clientand$instance.src/pool.ts: connection pool that opens connections withDuckDBConnection.create(instance)and hands them out withacquire()andrelease(), an acquire timeout, a cap on waiting requests, and recycling viamaxLifetimeMsandidleTimeoutMs. Idle connections are reused newest first. Waiting requests are served in arrival order. Also defines the MotherDuck size presets.src/pgduck.ts: adapter types andcreatePgDuckConnectionPool()for Postgres wire clients connected to pg_duckdb.
Drizzle Integration Points
src/dialect.ts:DuckDBDialectextendsPgDialect. OverridesprepareTyping()andmigrate(), rejectsPgJsonandPgJsonb, tracks savepoint support per instance, and runs every generated query through the AST transformer insqlToQuery().migrate()retries on write-write conflicts and uses a plain journal table when DuckLake is the current catalog.src/migrator.ts:migrate(), which queues calls on the same database file or client before calling the dialect.src/instance-keys.ts: records which database path each shared DuckDB instance points at, somigrate()can queue per database.src/session.ts:DuckDBSessionextendsPgSession. Pins one pooled connection per transaction, probes savepoint support for nested transactions, checks string parameters for Postgres array literals, keepsnumeric()columns as exact DECIMAL strings, and wires streaming, columnar fetch and the prepared statement cache intoDuckDBPreparedQuery.src/select-builder.ts: DuckDB select builder used bydb.select(). Aliases duplicate column names and exposes subquery, CTE and view fields qualified by their alias.
Client and Value Conversion
src/client.ts: parameter preparation, value conversion to@duckdb/node-apivalues with explicit bind types, materialized execution with a per-column result converter, streaming (executeInBatches,executeInBatchesRaw) with a per-connection streaming marker, the columnar path behindexecuteArrow, column name deduplication, and connection cleanup that interrupts running queries.src/prepared-statement-cache.ts: optional per-connection LRU cache for prepared statements.src/value-wrappers*.ts: wrappers for list, array, struct, map, JSON, blob and timestamp values, and the list item type inference used for binding.
DuckDB Types, Helpers, and Rewriting
src/columns.ts: DuckDB column helpers (duckDbList,duckDbArray,duckDbMap,duckDbStruct,duckDbJson,duckDbTimestampand others), literal builders, and the array predicate helpers.src/operators.tsre-exports those predicates asarrayHasAll,arrayHasAnyandarrayContainedBy.src/olap.ts: numeric aggregates, window helpers, Lance search helpers and theolap()builder.src/motherduck.ts: MotherDuck table function and session identity helpers.src/jev.tsandsrc/prompt.ts: MotherDuckprompt_jevandpromptbuilders. Both inline settings as constants throughsrc/sql-constants.ts.src/ducklake.ts: DuckLake attach, pool setup and the compaction helper.src/sql/ast-transformer.ts: AST-based SQL transformer usingnode-sql-parser. It parses only queries that match a rewrite pattern and keeps the original SQL when the printed result would drop string literals, parameters or quoted identifiers. Visitors insrc/sql/visitors/rewritearray_lower/array_upper(array-bounds.ts, with a text fallback insrc/sql/array-bounds-text.tsfor statements the parser cannot read), qualify ambiguous join columns (column-qualifier.ts), aliasgenerate_series(generate-series-alias.ts) and hoistWITHclauses out of set operation arms (union-with-hoister.ts). Postgres array operators pass through unchanged.src/sql/result-mapper.ts: maps result rows into Drizzle’s nested selection shape.
Introspection and CLI
src/introspect.tsandsrc/bin/duckdb-introspect.ts: readinformation_schemaandduckdb_*tables to emitschema.tsplus JSON metadata. Generated columns are found in theCREATE TABLEtext fromduckdb_tables(). The CLI checks that the requested database and schemas exist before it writes. Argument parsing lives insrc/bin/duckdb-introspect-args.ts.
Examples and Perf Harness
example/: runnable examples.test/perf/,scripts/run-perf.ts,scripts/compare-perf.ts: Vitest bench suites covering builder paths, streaming, columnar fetch, prepared reuse and pooled mode, plus scripts that record and compare results.