Expand description
§mik-sql - SQL Query Builder with Mongo-style Filters
A standalone SQL query builder with intuitive macro syntax for CRUD operations.
Supports Postgres and SQLite dialects.
§Quick Start
ⓘ
use mik_sql::prelude::*;
// SELECT - Read data
let (sql, params) = sql_read!(users {
select: [id, name, email],
filter: { active: true },
order: name,
limit: 10,
});
// → "SELECT id, name, email FROM users WHERE active = $1 ORDER BY name LIMIT 10"
// INSERT - Create data
let (sql, params) = sql_create!(users {
name: str(name),
email: str(email),
returning: [id],
});
// → "INSERT INTO users (name, email) VALUES ($1, $2) RETURNING id"
// UPDATE - Update data
let (sql, params) = sql_update!(users {
set: { name: str(new_name) },
filter: { id: int(user_id) },
});
// → "UPDATE users SET name = $1 WHERE id = $2"
// DELETE - Delete data
let (sql, params) = sql_delete!(users {
filter: { id: int(user_id) },
});
// → "DELETE FROM users WHERE id = $1"§SQLite Dialect
Add sqlite as first parameter for SQLite syntax (?1, ?2 instead of $1, $2):
ⓘ
let (sql, params) = sql_read!(sqlite, users { ... });§Supported 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 $1 || '%' | "name": { "$starts_with": "John" } |
$ends_with | LIKE '%' || $1 | "email": { "$ends_with": "@example.com" } |
$contains | LIKE '%' || $1 || '%' | "bio": { "$contains": "developer" } |
$between | BETWEEN $1 AND $2 | "age": { "$between": [18, 65] } |
§Cursor Pagination
ⓘ
use mik_sql::prelude::*;
let (sql, params) = sql_read!(posts {
select: [id, title, created_at],
filter: { published: true },
order: [-created_at, -id],
after: req.query("after"),
limit: 20,
});
// Create cursor for next page
let next_cursor = Cursor::new()
.string("created_at", &last_item.created_at)
.int("id", last_item.id)
.encode();Modules§
- prelude
- Prelude module for convenient imports.
Macros§
- ids
- Collect field values from a list for batched loading.
- sql_
create - Build an INSERT query using object-like syntax.
- sql_
delete - Build a DELETE query using object-like syntax.
- sql_
read - Build a SELECT query using the query builder (CRUD: Read).
- sql_
update - Build an UPDATE query using object-like syntax.
Structs§
- Aggregate
- An aggregation expression.
- Compound
Filter - A compound filter combining multiple expressions with a logical operator.
- Computed
Field - A computed field expression with alias.
- Cursor
- A cursor for cursor-based pagination.
- Delete
Builder - Builder for DELETE queries.
- Filter
- Filter condition.
- Filter
Validator - Validation configuration for user-provided filters.
- Insert
Builder - Builder for INSERT queries.
- Keyset
Condition - Keyset pagination condition.
- Page
Info - Page information for paginated responses.
- Postgres
- Postgres dialect.
- Query
Builder - SQL query builder with dialect support.
- Query
Result - Query result with SQL string and parameters.
- Sort
Field - Sort field with direction.
- Sqlite
SQLitedialect.- Update
Builder - Builder for UPDATE queries.
Enums§
- Aggregate
Func - Aggregation functions.
- Cursor
Direction - Cursor pagination direction.
- Cursor
Error - Errors that can occur when parsing a cursor.
- Filter
Expr - A filter expression that can be simple or compound.
- Logical
Op - Logical operators for compound filters.
- Operator
- SQL comparison operators.
- SortDir
- Sort direction.
- Validation
Error - Validation error types.
- Value
- SQL parameter values.
Traits§
- Dialect
- SQL dialect trait for database-specific syntax.
- Into
Cursor - Trait for types that can be converted into a cursor.
Functions§
- and
- Helper function to create an AND compound filter.
- assert_
valid_ sql_ expression - Assert that a SQL expression is valid for computed fields.
- assert_
valid_ sql_ identifier - Assert that a string is a valid SQL identifier.
- delete
- Create a DELETE builder for Postgres.
- insert
- Create an INSERT builder for Postgres.
- is_
valid_ sql_ expression - Validate a SQL expression for computed fields.
- is_
valid_ sql_ identifier - Validate that a string is a safe SQL identifier.
- merge_
filters - Merge trusted filters with validated user filters.
- not
- Helper function to create a NOT filter.
- or
- Helper function to create an OR compound filter.
- postgres
- Build a query for Postgres.
- simple
- Helper function to create a simple filter expression.
- sqlite
- Build a query for
SQLite. - update
- Create an UPDATE builder for Postgres.