Skip to content

Architecture

typed-sql separates the application query contract, SQL grammar, schema metadata, database-driver integration, and developer tooling. Each package owns one part of that boundary.

Package responsibilities

PackageResponsibilityInstalls a runtime driver
@typed-sql/coreSQL tag, query and fragment types, rendering, database and dialect contractsNo
@typed-sql/astBounded tokenizer, parser, AST, and source rangesNo
@typed-sql/configDialect-neutral project config discovery and loadingNo
@typed-sql/schemaSnapshot envelope, deterministic generation, hashes, and driftNo
@typed-sql/compilerDialect-neutral extraction, transforms, structural expansion, and diagnosticsNo
@typed-sql/postgresPostgreSQL grammar, catalog model, resolver, type policy, and codecsNo
@typed-sql/mysqlMySQL grammar, catalog model, resolver, type policy, and codecsNo
@typed-sql/cliGeneration, checking, drift, and provider discoveryNo
@typed-sql/ts-bridgeExperimental TypeScript semantic overlay and isolated preview bridgeNo
@typed-sql/language-serverExperimental TypeScript and LSP semantic proxyNo

Applications select one grammar and explicitly install its driver. Adding PostgreSQL support does not install MySQL, SQLite, or another database client.

Driver ownership

Grammar packages may refer to driver types, expose driver-specific adapters, and use drivers in their own tests. They do not install runtime drivers for applications.

Driver adapters load the application dependency only when their explicit subpath is used. Missing drivers fail with an actionable install message. This avoids hidden clients, duplicate pools, unexpected install size, and driver lifecycle decisions made by typed-sql.

Dialect contract

Every grammar implements the same public contract for:

  • its exact sql module entrypoint;
  • parsing and source ranges;
  • identifier, quoting, and placeholder rules;
  • catalog snapshot validation and introspection;
  • result-column and ordered-parameter resolution;
  • database-to-TypeScript mapping;
  • runtime encoding and decoding;
  • feature and server-version capabilities.

The compiler recognizes the sqlModule declared by the configured dialect. It does not branch on package names, dialect ids, or drivers. PostgreSQL, MySQL, and third-party grammars use the same compiler and schema infrastructure while owning their SQL semantics.

Core exposes grammar-neutral resolver mechanisms such as indexed catalog lookup, ordered parameter collection, literal-union normalization, and name suggestions. Identifiers, operators, built-ins, nullability, feature gates, and diagnostics remain grammar responsibilities.

Generated metadata

Generated output contains schema metadata for tooling and review. Application code imports sql and typePolicy from the dialect package and imports a driver adapter only when it needs introspection or execution.

A custom type policy belongs in an application module shared by config and runtime. Generated output is never an application API entrypoint.

Composition model

The core runtime stores immutable SQL and parameter segments. It does not interpret clauses.

  • SqlFragment<Parameters> represents trusted static structure and ordered values.
  • sql.where(), sql.and(), and sql.or() compose optional predicates.
  • sql.append() combines a static base with directly visible fragments.
  • sql.empty represents zero structural content.
  • Conditional templates are expanded into a bounded set of complete statements before grammar analysis.

The compiler owns static extraction and structural expansion. The selected grammar analyzes only complete SQL statements.

Correctness boundary

Only static SQL supported by the configured grammar receives exact inference. Dynamic identifiers, unsupported syntax, ambiguous resolution, or missing schema evidence produce diagnostics or unknown, never any.

Editor inference is a development aid. CI correctness comes from the same compiler transform through typed-sql check, and runtime execution does not depend on an editor process.

Released under the MIT License.