Creates a new SimpleDB instance.
DuckDB sessions use UTC so temporal parsing, extraction, and returned
TIMESTAMP WITH TIME ZONE strings are consistent across environments.
Existing tables and SDA index metadata are restored on first use. The
__sda schema is reserved for versioned SDA metadata. SQLite files can be
imported with loadDB() but cannot be used as the persistent database.
Configuration options for the SimpleDB instance.
OptionalcacheVerbose?: booleanWhether to log cache hits and misses, code and input changes, TTL status, and cache read/write timing.
OptionalcharsToLog?: numberThe maximum characters to display for text and stringified nested cells, including truncation markers (default: 75). Independent of column width. Use Infinity for unlimited text.
OptionalduckDbCache?: boolean | nullA flag indicating whether to use DuckDB's external file cache.
OptionalexplainSQL?: booleanA flag indicating whether to log DuckDB query plans for supported statements.
OptionalexpressionSyntax?: "sql" | "js"Operator syntax for expressions and custom queries: "js" (default) translates JavaScript-style operators in SQL expressions; "sql" preserves SQL unchanged.
Optionalfile?: stringA .db or .duckdb file to open or create. Defaults to an in-memory database.
OptionallogDuration?: booleanA flag indicating whether to log the total execution duration.
OptionallogSQL?: booleanA flag indicating whether to log SQL immediately before execution.
OptionalmemoryLimit?: stringThe maximum amount of memory DuckDB is allowed to use (e.g., '4GB'). Defaults to 80% of system RAM.
Optionaloverwrite?: booleanIf true, replaces an existing file on first use. Defaults to false, which opens the existing file.
OptionalprogressBar?: booleanA flag indicating whether to display a progress bar for long-running operations.
OptionalreadOnly?: booleanOpens an existing DuckDB file read-only. Defaults to false. Requires a file and cannot be combined with overwrite.
OptionalrowsToLog?: numberThe number of rows to display when logging a table.
OptionaltempDir?: stringThe path to the directory used for temporary files when data exceeds the memory limit (e.g., '/tmp/duckdb_swap'). Defaults to .tmp for in-memory databases or <file>.tmp for file-based databases. Automatically removed when calling close().
OptionaltypesToLog?: booleanA flag indicating whether to include data types when logging a table.
Executes a custom SQL query directly against the DuckDB instance.
With the default SimpleDB.expressionSyntax: "js", queries support JavaScript-style operators (&&, ||, ===, !==). Set expressionSyntax: "sql" for unchanged SQL.
Queries run in UTC. When data is returned, temporal values use the same
JavaScript representations as SimpleTable.getData().
customQuery() bypasses the dependency and table-generation tracking used
by SimpleTable.cache(). Reading or changing a table with customQuery()
can therefore cause cache() to return stale data. Include a value that
identifies the custom query's dependencies in the cache's options.inputs
(such as a table content hash), or use tracked SimpleTable methods.
The SQL query string to execute.
Configuration options for the query.
OptionalreturnData?: booleanIf true, the query result is returned. Defaults to false.
Optionaltable?: stringThe name of the table associated with the query, primarily used for debugging and logging.
A promise that resolves to the query result as an array of objects if returnData is true, otherwise null.
Returns a list of installed DuckDB extensions.
A promise that resolves to an array of objects, each representing an installed extension.
Imports a copy of a .db or .duckdb (DuckDB) or .sqlite (SQLite)
file into the current database. The source is opened read-only and detached
after importing; subsequent transformations do not modify the source file.
Imports work with in-memory and writable persistent databases. Existing
table-name conflicts are rejected. A failed import may leave partial changes
in the destination database.
DuckDB files restore embedded SDA index definitions when present. SQLite
imports copy data without SDA index metadata. The __sda schema is reserved
for SDA metadata.
To edit an existing DuckDB file in place, use new SimpleDB({ file }).
The relative or absolute path to the database file.
A promise that resolves to the database, so methods can be chained.
Exports a snapshot of the current database after executing pending work.
Does not change the working database or where subsequent changes persist.
DuckDB outputs (.db and .duckdb) preserve database objects and embed SDA
index definitions in the reserved __sda schema.
SQLite output (.sqlite) materializes main-schema tables and views as
tables. It does not preserve DuckDB schemas, indexes, constraints, or SDA
metadata, and SQLite type conversion may lose type information. Unsupported
conversions fail without replacing an existing destination.
Existing files require explicit overwrite permission. The completed export is published only after its connection is detached. Database files attached to this instance, directories, and symbolic links cannot be replaced.
The relative or absolute path to the output file.
Configuration options for writing the database.
Optionalmetadata?: booleanIf false, omits logical SDA index definitions from DuckDB exports. Defaults to true. Does not control physical indexes; SQLite exports never include SDA metadata.
Optionaloverwrite?: booleanIf true, permits atomic replacement of an existing output file. Defaults to false.
A promise that resolves to the database, so methods can be chained.
Executes pending transformations, saves SDA metadata for writable persistent databases, and closes the connection and instance. Also cleans up temporary files and the cache. Does not copy, compact, or replace the database file.
A promise that resolves to the SimpleDB instance after cleanup.
Executes all queued methods across every table in the database. Sync
builder methods (like filter() or convert()) only queue their
operation; execution happens when an async observer method (like
getData(), log(), or writeData()) is awaited. Use run() when
your script ends in pure mutations with nothing to observe and you want
the work done now.
The whole database is flushed in program order, so operations on
different tables execute exactly in the order they were queued. This is
the database-level counterpart to SimpleTable.run().
A promise that resolves to the SimpleDB instance once the queued methods have been executed.
The maximum number of characters to display for text and stringified nested cells, including the truncation marker. This does not control column width. Set to Infinity to show the entire text.
A connection to a DuckDB database.
A DuckDB database instance.
A flag indicating if the table name was assigned by default.
The number of rows to display when logging a table.
A function for running SQL queries. Subclasses can wrap this seam to observe
execution while forwarding every argument. To run custom SQL, use
SimpleDB.customQuery() instead.
The exact SQL statement to execute.
The DuckDB connection that executes the statement.
Whether to convert and return result rows.
Error-attribution, binding, and observability options.
Whether to log a supported DuckDB query plan before execution.
Whether to log the SQL immediately before execution.
The SDA method responsible for the query, when available.
The method arguments recorded for error attribution.
Optionaltable?: string | nullThe table associated with returned values and warnings.
Optionalvalues?: DuckDBValue[]Data values bound to placeholders in the SQL statement.
The converted rows when requested, otherwise null.
A flag indicating whether to include data types when logging a table.
An array of paths to the data sources used in the cache.
The total time saved by using the cache, in milliseconds.
The total time spent writing to the cache, in milliseconds.
Whether to log cache hits and misses, code and input changes, TTL status, and cache read/write timing.
A flag indicating whether to use DuckDB's external file cache.
A timestamp marking the start of a duration measurement.
Whether to log DuckDB query plans before supported statements execute.
ReadonlyexpressionThe operator syntax used by expressions and custom queries in this database.
"js" translates JavaScript-style operators (&&, ||, ==, ===,
!==) and null comparisons within SQL expressions. It does not evaluate
arbitrary JavaScript. || remains concatenation when string literals, list
literals, explicit text casts, or concat() results make that intent clear.
Otherwise || becomes OR; column types are not inferred. Use concat()
when concatenation would otherwise be ambiguous, such as first || last.
"sql" passes SQL through unchanged, including || and null comparisons.
Choose the syntax when constructing the database; it applies to all tables.
const sdb = new SimpleDB({ expressionSyntax: "js" }); // The default mode
// || becomes SQL OR: keep rows where at least one condition is true.
await sdb.newTable().loadArray([{ active: true, admin: false }])
.filter("active || admin").log();
const sdb = new SimpleDB({ expressionSyntax: "js" });
// && becomes SQL AND: both conditions must be true.
// === becomes SQL =: compare the age with 18.
await sdb.newTable().loadArray([{ active: true, age: 18 }])
.filter("active && age === 18").log();
const sdb = new SimpleDB({ expressionSyntax: "js" });
// SQL AND and OR also work in JS mode; they pass through unchanged.
// Keep active adults, or anyone who is an admin.
await sdb.newTable().loadArray([{ active: true, age: 18, admin: false }])
.filter("(active AND age >= 18) OR admin").log();
const sdb = new SimpleDB({ expressionSyntax: "js" });
// Bare column names do not establish concatenation. Use concat() explicitly.
await sdb.newTable().loadArray([{ first: "Jane", last: "Doe" }])
.addColumn("name", "string", "concat(first, ' ', last)").log();
const sdb = new SimpleDB(); // JS mode is the default
// The string literal ' ' makes concatenation clear, so both || stay unchanged.
await sdb.newTable().loadArray([{ first: "Jane", last: "Doe" }])
.addColumn("name", "string", "first || ' ' || last").log();
const sdb = new SimpleDB({ expressionSyntax: "sql" });
// SQL mode preserves || as concatenation: the name becomes "Jane Doe".
await sdb.newTable().loadArray([{ first: "Jane", last: "Doe" }])
.addColumn("name", "string", "first || ' ' || last").log();
const sdb = new SimpleDB({ expressionSyntax: "sql" });
// Use SQL AND and OR for logical conditions; && and || are not translated.
// Both active and adult must be true, unless admin is true.
await sdb.newTable().loadArray([{ active: true, age: 18, admin: false }])
.filter("(active AND age >= 18) OR admin").log();
The path to the persistent DuckDB database, opened or created on first use. If not provided, an in-memory database is used. Imports and exports do not change this path.
A flag indicating whether to log the total execution duration.
Whether to log each SQL statement immediately before execution.
The maximum amount of memory DuckDB is allowed to use (e.g., '4GB'). Defaults to 80% of system RAM.
Whether to replace an existing DuckDB file on first use instead of opening it.
A flag indicating whether to display a progress bar for long-running operations.
Whether the persistent DuckDB file is opened read-only. Requires an existing
file and cannot be combined with overwrite. DuckDB rejects writes to it.
The class used to create table instances. Defaults to SimpleTable.
Override this property when subclassing to ensure all table-creating
methods (e.g., newTable(), clone()) return instances of your
custom table class.
The class used to create table instances. Defaults to SimpleTable.
Override this property when subclassing to ensure all table-creating
methods (e.g., newTable(), clone()) return instances of your
custom table class.
Optionaloptions: { charsToLog?: number; rowsToLog?: number; typesToLog?: boolean }A counter for incrementing default table names.
The path to the directory used for temporary files when data exceeds the memory limit (e.g., '/tmp/duckdb_swap'). Defaults to .tmp for in-memory databases or <file>.tmp for file-based databases. Temporary directories are automatically removed when calling close().
Retrieves an existing SimpleTable instance from the database.
The name of the table to retrieve.
A promise that resolves to the SimpleTable instance if found.
Returns an array of all SimpleTable instances in the database.
The returned array is a snapshot and cannot mutate the database's internal table registry.
A read-only array of SimpleTable instances.
Checks if a table exists in the database.
The name of the table or a SimpleTable instance.
A promise that resolves to true if the table exists, false otherwise.
Creates a new SimpleTable instance within the database. The name must be unique among registered tables, ignoring ASCII letter case.
Optionalname: string
The name of the new table. If not provided, a default name is generated (e.g., "table1").
A new table instance.
Manages a DuckDB database instance, providing a simplified interface for database operations.
Example
Example
Example