query
| Kind | kit |
|---|---|
| Categories | database |
| Keywords | query sql database orm builder |
Fluent SQL query builder for Kit
Files
| File | Description |
|---|---|
.editorconfig | Editor formatting configuration |
.gitignore | Git ignore rules for build artifacts and dependencies |
.tool-versions | asdf tool versions (Zig, Kit) |
LICENSE | MIT license file |
README.md | This file |
examples/postgres-basic.kit | Example: postgres basic |
examples/sqlite-basic.kit | Example: sqlite basic |
kit.toml | Package manifest with metadata and dependencies |
src/main.kit | Query Builder for Kit |
tests/query.test.kit | Tests for query |
Dependencies
No Kit package dependencies.
Installation
kit add gitlab.com/kit-lang/packages/kit-query.gitUsage
import Kit.Query as Query
main = fn =>
# Build a query with fluent chaining using |>> (data-last pipe)
q = Query.create "posts"
|>> Query.columns ["id", "title", "views"]
|>> Query.filter "status" "=" "published"
|>> Query.filter "views" ">" 10
|>> Query.order-by "views" Query.Desc
|>> Query.limit 10
# Generate escaped SQL
sql = Query.to-sql q
println "SQL: ${sql}"
# SQL: SELECT id, title, views FROM posts WHERE status = 'published' AND views > 10 ORDER BY views DESC LIMIT 10
# Generate parameterized SQL with driver-specific placeholders
(sqlite-sql, sqlite-params) = Query.to-sql-params "sqlite" q
println "SQLite SQL: ${sqlite-sql}"
println "SQLite params: ${sqlite-params}"
# SQLite SQL: SELECT id, title, views FROM posts WHERE status = ?1 AND views > ?2 ORDER BY views DESC LIMIT 10
(postgres-sql, postgres-params) = Query.to-sql-params "postgres" q
println "Postgres SQL: ${postgres-sql}"
println "Postgres params: ${postgres-params}"
# Postgres SQL: SELECT id, title, views FROM posts WHERE status = $1 AND views > $2 ORDER BY views DESC LIMIT 10
# String values are escaped for raw SQL output
escaped = Query.create "posts"
|>> Query.filter "title" "=" "Bob's Post"
println "Escaped SQL: ${Query.to-sql escaped}"
# Escaped SQL: SELECT * FROM posts WHERE title = 'Bob''s Post'
mainAPI Notes
Query.create tablestarts aSELECTquery for a table name.Query.columns,Query.filter,Query.order-by,Query.limit,Query.offset, andQuery.typedare curried for|>>chaining.Query.to-sqlreturns escaped SQL for display, debugging, or drivers that only expose plainquery.Query.to-sql-paramsreturns(sql, params)and supportssqliteplaceholders (?1) andpostgresplaceholders ($1). Unknown driver names use Postgres-style placeholders.Query.exec db qexpects a database connection record with aqueryfunction and executes the escaped SQL produced byQuery.to-sql.Query.typedrecords the desired result type in the query state, but it does not change generated SQL.
Related Packages
- kit-sqlite - SQLite bindings for Kit
- kit-postgres - PostgreSQL bindings for Kit using libpq
Development
Running Examples
Run examples with the interpreter:
kit run examples/sqlite-basic.kit
kit run examples/postgres-basic.kitCompile examples to a native binary:
kit build examples/sqlite-basic.kit -o sqlite-basic && ./sqlite-basic
kit build examples/postgres-basic.kit -o postgres-basic && ./postgres-basicRunning Parity
Compare interpreter output with compiled binary output for the examples:
kit parity --no-spinner --failures-onlyRunning Tests
Run the test suite:
kit testRun the test suite with coverage:
kit test --coverageRunning kit dev
Run the standard development workflow (format, check, test):
kit devThis will:
- Format and check source files in
src/ - Run tests in
tests/with coverage
Generating Documentation
Generate API documentation from doc comments:
kit docNote: Kit sources with doc comments (##) will generate HTML documents in docs/*.html
Cleaning Build Artifacts
Remove generated files, caches, and build artifacts:
kit task cleanNote: Defined in kit.toml.
Local Installation
To install this package locally for development:
kit installThis installs the package to ~/.kit/packages/@kit/query/, making it available for import as Kit.Query in other projects.
License
This package is released under the MIT License - see LICENSE for details.
Exported Functions & Types
Direction
Query Builder for Kit
A fluent, functional SQL query builder that works with any Kit database driver. Uses |>> (data-last pipe) for chaining operations.
import Query
import Postgres
type Post @table("posts") = Post(id @primary-key, title, status, created-at)
db = Postgres.connect "postgresql://localhost/mydb"
posts = Query.create "Post"
|>> Query.filter "status" "=" "Published"
|>> Query.order-by "created-at" Query.Desc
|>> Query.limit 10
|>> Query.typed "Post"
|>> Query.exec db
# posts : Result (List Post) StringVariants
AscDesccreate
NonEmptyString -> QueryState
columns
List String -> QueryState -> QueryState
filter
String -> String -> a -> QueryState -> QueryState
order-by
String -> Direction -> QueryState -> QueryState
limit
NonNegativeInt -> QueryState -> QueryState
offset
NonNegativeInt -> QueryState -> QueryState
typed
NonEmptyString -> QueryState -> QueryState
to-sql
QueryState -> String
to-sql-params
String -> QueryState -> (String, List a)
exec
Ptr -> QueryState -> Result (List a) String
Asc
Ascending sort direction for ORDER BY clauses
Direction
Desc
Descending sort direction for ORDER BY clauses
Direction