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 Clauses —
onVal,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
- ✅ Upserts —
onConflict().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
- ✅ Streaming —
streamQuery()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#
| Database | Package | Notes |
|---|---|---|
| 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
- Installation — Add driver packages
- Quick Start — Connect and run your first query
- Database Support — All 9 supported databases with connection examples
Query Building
- WHERE Clauses — All 29 filtering methods
- Joins — INNER, LEFT, RIGHT, FULL OUTER, CROSS, LATERAL
- Write Operations — INSERT, UPDATE, DELETE, upsert
- Aggregation — COUNT, SUM, AVG, GROUP BY, HAVING
- Window Functions — RANK, LEAD, LAG, frame clauses
- Subqueries — Nested queries
- CTEs (WITH) — Common table expressions
- UNION / INTERSECT / EXCEPT — Set operations
- Schema Builder — CREATE TABLE, ALTER TABLE, indexes
Advanced
- Transactions — Atomic operations, savepoints, nesting
- Migrations — Code-first and SQL-directory migration sources
- Connection Pooling — Pool configuration for PostgreSQL and MySQL
- Streaming —
streamQuery()for large result sets
Tooling
- Dialect Lint — Optional static checks for dialect-incompatible APIs
Migration Guide
- From Knex.js — Differences and equivalents
🔗 Links#
📄 License#
MIT License — see LICENSE file for details.