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
// SELECT - Read data with filters and ordering
let result = postgres("users")
.fields(&["id", "name", "email"])
.filter("active", Operator::Eq, Value::Bool(true))
.sort("name", SortDir::Asc)
.limit(10)
.build();
assert!(result.sql.contains("SELECT id, name, email FROM users"));
assert!(result.sql.contains("ORDER BY name ASC"));§SQLite Dialect
Use sqlite() for SQLite syntax (?1, ?2 instead of $1, $2):
let result = sqlite("users")
.fields(&["id", "name"])
.filter("active", Operator::Eq, Value::Bool(true))
.build();
assert!(result.sql.contains("?1")); // SQLite placeholder§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
// Create a cursor for pagination
let cursor = Cursor::new()
.string("created_at", "2024-01-15T10:00:00Z")
.int("id", 42);
let result = postgres("posts")
.fields(&["id", "title", "created_at"])
.filter("published", Operator::Eq, Value::Bool(true))
.sort("created_at", SortDir::Desc)
.sort("id", SortDir::Asc)
.after_cursor(cursor)
.limit(20)
.build();
assert!(result.sql.contains("ORDER BY created_at DESC, id ASC"));Modules§
- json
- Re-export miniserde’s json module for runtime filter parsing.
- prelude
- Prelude module for convenient imports.
Macros§
- ids
- Collect field values from a list.
- sql_
create - Build an INSERT query using object-like syntax (CRUD: Create).
- sql_
delete - Build a DELETE query using object-like syntax (CRUD: Delete).
- sql_
read - Build a SELECT query using the query builder (CRUD: Read).
- sql_
update - Build an UPDATE query using object-like syntax (CRUD: Update).
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.
- Parse
Error - Error type for JSON filter parsing.
- 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.
- parse_
filter - Parse a Mongo-style filter from a JSON string.
- 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.