mik-sql
SQL query builder with Mongo-style filter operators and cursor pagination.
Experimental - This is version 0.0.1. The API may change between releases.
Features
- Compile-time SQL generation - Macro-based query building
- Mongo-style operators -
$eq,$in,$between,$like, etc. - Cursor pagination - Built-in keyset pagination support
- Dialect support - Postgres (
$1) and SQLite (?1) - Standalone - Use with or without mik-sdk
Quick Start
use *;
// SELECT with filters
let = sql_read!;
// → "SELECT id, name, email FROM users WHERE active = $1 AND role = $2 ORDER BY name LIMIT 10"
// INSERT with returning
let = sql_create!;
// → "INSERT INTO users (name, email) VALUES ($1, $2) RETURNING id"
// UPDATE with filter
let = sql_update!;
// → "UPDATE users SET name = $1 WHERE id = $2"
// DELETE with filter
let = sql_delete!;
// → "DELETE FROM users WHERE id = $1"
Filter Operators
| Operator | SQL | Example |
|---|---|---|
$eq |
= |
status: { $eq: "active" } |
$ne |
!= |
status: { $ne: "deleted" } |
$gt |
> |
age: { $gt: 18 } |
$gte |
>= |
age: { $gte: 21 } |
$lt |
< |
price: { $lt: 100 } |
$lte |
<= |
price: { $lte: 50 } |
$in |
IN (...) |
status: { $in: ["a", "b"] } |
$nin |
NOT IN |
status: { $nin: ["x"] } |
$like |
LIKE |
name: { $like: "%test%" } |
$ilike |
ILIKE |
name: { $ilike: "%TEST%" } |
$starts_with |
LIKE x || '%' |
name: { $starts_with: "John" } |
$ends_with |
LIKE '%' || x |
email: { $ends_with: "@example.com" } |
$contains |
LIKE '%' || x || '%' |
bio: { $contains: "rust" } |
$between |
BETWEEN |
age: { $between: [18, 65] } |
Logical Operators
// AND (implicit)
filter:
// OR
filter:
// NOT
filter:
// Combined
filter:
Cursor Pagination
use *;
// Query with cursor
let = sql_read!;
// Build cursor for next page
let next_cursor = new
.string
.int
.encode;
// → "eyJjcmVhdGVkX2F0IjoiMjAyNS0wMS0xNSIsImlkIjoxMjN9"
SQLite Dialect
Add sqlite as first parameter:
let = sql_read!;
// → "SELECT id, name FROM users WHERE active = ?1"
Programmatic API
use ;
let result = postgres
.fields
.filter
.filter
.sort
.limit
.build;
println!;
// → SELECT id, name, email FROM users WHERE active = $1 AND role IN ($2, $3) ORDER BY created_at DESC LIMIT 20
Requirements
- Rust 1.85+ (Edition 2024)
License
Licensed under MIT license. See LICENSE-MIT.