Skip to content
VaelJS Blog

Database API (vaeljs-dbapi)

vaeljs-dbapi is the workspace database crate backing vael:db, vael:db/migrate, and vjs db migrate in vjs (vaeljs / vaeljs-core). It gives Rust callers and the vael:db JavaScript surface one SQL shape: strict JSON configuration (with verifyFull TLS enabled by default on network drivers), named @name or positional ? parameters, bounded driver-neutral rows, fluent AST query builders, deterministic SQL migrations, and redacted errors.

The crate is asynchronous and does not create a Tokio runtime; callers execute inside an existing Tokio runtime.

What Shipped (v0.0.0-alpha.0 Baseline, Multi-Driver & Builder/Migrate in v0.0.0-alpha.3)

Section titled “What Shipped (v0.0.0-alpha.0 Baseline, Multi-Driver & Builder/Migrate in v0.0.0-alpha.3)”
  • Strict configuration parser: Parses SQLite, PostgreSQL, MySQL, MariaDB, and Microsoft SQL Server configs (Rust SqliteConfig / NetworkConfig in vaeljs-dbapi, corresponding to JS SqliteDbConfig / NetworkDbConfig in vael:db). Network drivers default to fail-closed verified TLS (tlsMode: "verifyFull" / tls: true, with optional caFile; pass tls: false or tlsMode: "plaintext" only to opt into cleartext for local dev). Unknown fields, wrong types, empty required values, and out-of-range numbers fail closed with static descriptions that never echo a field value.
  • Lexical binding compiler: A hand-written UTF-8 byte lexer rewrites @name or ? into the target dialect’s placeholder syntax and builds the ordered parameter vector. Handles quoted strings/identifiers, comments, PostgreSQL dollar quoting, and operator sequences containing @ or ?.
  • Default SQLite provider: End-to-end execution via neotales-dbapi-sqlite (and neotales-migration-sqlite), backing vael:db openSqlite() and open({ driver: "sqlite", ... }). Default vaeljs / vaeljs-dbapi builds stay lean with SQLite only.
  • Opt-in network SQL drivers (shipped in v0.0.0-alpha.3): Cargo features on vaeljs / vaeljs-core (vael-db-postgres, vael-db-mysql, vael-db-mariadb, vael-db-mssql, vael-db-all, backed by vaeljs-dbapi features postgres, mysql, mariadb, mssql, all-drivers) link pure-Rust providers (neotales-dbapi-postgres, neotales-dbapi-mysql, neotales-dbapi-mariadb, neotales-dbapi-mssql, and sibling neotales-migration-* crates with rustls) over vaeljs-dbapi’s unified @name/? parameter binding, query AST, and redacted DbError layer.
  • Lean feature gating (--no-default-features): Building vaeljs-dbapi with --no-default-features keeps all drivers out (leaving only the config parser and binding compiler), and requesting an uncompiled driver in default builds still validates config strictly before failing closed with ErrorKind::DriverUnavailable (DbError code === "DRIVER_UNAVAILABLE") without opening a socket.
  • Facade, fluent query AST, savepoints & migrations (v0.0.0-alpha.3): Db, Connection, and Transaction with execute, bounded query, cardinality-one query_one, explicit commit/rollback, nested SQL savepoint(), lossless rich-type encoding/decoding (uuid, date, timestamp, timestampTz, decimal, json), zero-ORM fluent AST query builder + sql tagged templates in vael:db, and deterministic migrations in vael:db/migrate and vjs db migrate.

neotales-dbapi (0.0.0-alpha.1), neotales-migration (0.0.0-alpha.1), and the driver provider crates (neotales-dbapi-* and neotales-migration-*, all pinned at =0.0.0-alpha.1) are published from neotales/rs-db under MIT. Reusing them provides sanitized errors, bounded row access (RowLimit::default(): 1,000 rows / 1 MiB), transaction lifecycle guards, pooled/network backends, and deterministic migration execution without duplicating row/value models. vaeljs-dbapi layers the portable binding compiler, query AST, and strict configuration facade on top.

  • Values are never interpolated: The compiler emits SQL text and a separate parameter vector so values cannot alter statement structure. (SQL text itself remains code; placeholders do not sanitize identifiers or SQL fragments.)
  • No regular expressions: The scanner is a deterministic byte lexer over UTF-8 with ASCII delimiters.
  • Ambiguity fails closed: ?NNN, :name, $name, ??, a bare @, mixed named and positional placeholders, multiple statements, and trigger bodies are rejected. A trailing ; is accepted only when followed solely by whitespace, comments, and semicolons. MySQL -- is treated as a comment only when followed by whitespace or a control character.
  • Dialect-aware literals: Handles single/double quotes, backticks (SQLite/MySQL), brackets (SQLite/SQL Server), line/block comments, nested block comments (PostgreSQL/SQL Server), PostgreSQL dollar quoting, and backslash escapes. MySQL (/*! ... */) and MariaDB (/*M! ... */) executable comments are rejected in both the primary scan and trailing terminator scan.
  • Server-mode ambiguity checks: Ordinary MySQL and PostgreSQL strings can change end boundaries depending on NO_BACKSLASH_ESCAPES or standard_conforming_strings. The scanner evaluates both interpretations and rejects strings whose end boundaries disagree (explicit E'...' and U&'...' forms in PostgreSQL are accepted).
  • Redacted static errors & zero logging: DbError carries a category and fixed detail string — never paths, hosts, usernames, passwords, SQL text, or driver messages. Debug on network configs redacts sensitive fields, and SQL statement logging is disabled.
  • The scanner is not a full SQL parser: it cannot distinguish T-SQL local variables (DECLARE @x) or MySQL user variables (@var) from named parameters. PostgreSQL’s bare ? jsonb operator conflicts with positional placeholders, so PostgreSQL queries in portable mode require @name parameters.
  • SQLite uses dynamic typing; JSON null binds as SQL NULL with declared TEXT type (Rust callers can use Param::TypedNull).
  • JSON parameters from JavaScript accept Uint8Array for BLOB bytes in vael:db, while Param::Bytes is used directly in Rust.
  • Parameter name matching is exact and case-sensitive; SQL Server rewrites named parameters to @p1, @p2, etc. to avoid case-insensitive collisions.
  • query_one materializes the bounded batch before checking cardinality; use explicit LIMIT / keyset paging for large datasets.
  • Dropping an in-flight future signals cancellation via a SQLite progress handler and discards an interrupted dispatched connection. Validation failures before dispatch do not invalidate the session.
  • WriteOutcomeUnknown and CommitOutcomeUnknown identify post-dispatch failures where SQLite may have applied the write or committed the transaction (discarding the connection on CommitOutcomeUnknown). Callers must not blindly retry post-dispatch errors; acquire a fresh connection and verify state.
Terminal window
cargo fmt --check
cargo clippy -p vaeljs-dbapi --all-targets --all-features -- -D warnings
cargo test -p vaeljs-dbapi
cargo test -p vaeljs-dbapi --no-default-features
cargo tree -p vaeljs-dbapi --no-default-features # no Node/V8 dependency