LogoKnex Dart
knex-dartkartikey321/knex-dart 999999

Knex Dart

A Knex.js port for Dart — powerful SQL query builder supporting PostgreSQL, MySQL, SQLite, DuckDB, MSSQL, BigQuery, Snowflake, Turso, and Cloudflare D1

Knex Dart#

A powerful, flexible SQL query builder for Dart, ported from Knex.js.

✨ Features#

  • Complete Query Building — SELECT, INSERT, UPDATE, DELETE with full support
  • 29 WHERE Methods — basic, grouped, BETWEEN, EXISTS, IN, NULL, column comparisons, JSON, full-text
  • All JOIN Types — INNER, LEFT, RIGHT, FULL OUTER, CROSS, LATERAL
  • Advanced JOIN ClausesonVal, onIn, onBetween, onExists, using, onJsonPathEquals
  • Subqueries — in WHERE IN, FROM, and SELECT clauses
  • UNION / INTERSECT / EXCEPT — with ALL variants
  • CTEs — WITH and WITH RECURSIVE
  • Aggregates — COUNT, SUM, AVG, MIN, MAX with DISTINCT and FILTER variants
  • Window Functions — RANK, DENSE_RANK, ROW_NUMBER, LEAD, LAG, FIRST_VALUE, LAST_VALUE, NTH_VALUE + frame clauses
  • UpsertsonConflict().merge()
  • Schema Builder — createTable, alterTable, dropTable, foreign keys, indexes
  • Migrations — code-first, SQL-directory, and JSON-schema sources
  • Connection Pooling — built-in pool for PostgreSQL and MySQL
  • StreamingstreamQuery() for memory-efficient large result sets
  • Nested Transactions — savepoint-based nesting on all drivers
  • Web / WASM — DuckDB runs in Chrome/headless browser via dart_duckdb WASM
  • 591+ Tests Passing — comprehensive coverage with >85% line coverage
  • 10 Driver Packages — install only what you need

📦 Driver Packages#

DatabasePackageNotes
PostgreSQL knex_dart_postgres Pooled, savepoints, RETURNING
MySQL knex_dart_mysql Pooled, savepoints
SQLite knex_dart_sqlite File + in-memory, savepoints
DuckDB knex_dart_duckdb OLAP, native + WASM/web
SQL Server knex_dart_mssql FreeTDS-based
Google BigQuery knex_dart_bigquery HTTP-based
Snowflake knex_dart_snowflake HTTP-based
Turso (libSQL) knex_dart_turso HTTP + sqld
Cloudflare D1 knex_dart_d1 HTTP-based

🚀 Quick Start#

Pick your database driver:

# PostgreSQL
dart pub add knex_dart_postgres

# MySQL
dart pub add knex_dart_mysql

# SQLite
dart pub add knex_dart_sqlite

# DuckDB (OLAP / analytics)
dart pub add knex_dart_duckdb

Connect and query:

import 'package:knex_dart_sqlite/knex_dart_sqlite.dart';

final db = await KnexSQLite.connect(filename: ':memory:');

final users = await db.select(
  db('users')
    .select(['id', 'name', 'email'])
    .where('active', '=', true)
    .orderBy('name'),
);

await db.destroy();

Or generate SQL without a connection (for testing or logging):

import 'package:knex_dart/knex_dart.dart';
import 'package:knex_dart_capabilities/knex_dart_capabilities.dart';

final q = KnexQuery.forDialect(KnexDialect.postgres);

print(q.from('users').where('active', '=', true).toSQL().sql);
// select * from "users" where "active" = $1

🎯 Why Knex Dart?#

Familiar API#

Coming from Node.js/Knex.js? Most query-building patterns transfer directly — same method names, same chaining style.

No Bundled Drivers#

Install only the driver you need. No transitive dependencies on databases you don't use.

Type-Safe#

Leverages Dart's strong typing while maintaining the flexibility of dynamic query building.

Well-Tested#

591+ tests with >85% line coverage, ensuring correctness and behavioral parity with Knex.js.

Works Everywhere#

Native (macOS/Linux/Windows), Flutter mobile, and browser/WASM (DuckDB).

📚 Documentation#

Getting Started

Query Building

Advanced

Tooling

  • Dialect Lint — Optional static checks for dialect-incompatible APIs

Migration Guide

📄 License#

MIT License — see LICENSE file for details.