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.
Related Packages
- kit-sqlite - SQLite bindings for Kit
- kit-postgres - PostgreSQL bindings for Kit using libpq
Installation
kit add gitlab.com/kit-lang/packages/kit-query.gitUsage
import Kit.Query as Query
import Kit.Sqlite as SQLite
# Connect to a database
db = SQLite.connect ":memory:"
defer SQLite.close db
# Setup
db.execute "CREATE TABLE posts (id INTEGER, title TEXT, status TEXT, views INTEGER)"
db.execute "INSERT INTO posts VALUES (1, 'Hello World', 'published', 100)"
db.execute "INSERT INTO posts VALUES (2, 'Draft Post', 'draft', 0)"
db.execute "INSERT INTO posts VALUES (3, 'Another Post', 'published', 50)"
# Build a query with fluent chaining using |>> (data-last pipe)
q = Query.create "posts"
|>> Query.filter "status" "=" "published"
|>> Query.filter "views" ">" "10"
|>> Query.order-by "views" Query.Desc
|>> Query.limit 10
# Generate raw SQL
println "SQL: ${Query.to-sql q}"
# => SQL: SELECT * FROM posts WHERE status = 'published' AND views > '10' ORDER BY views DESC LIMIT 10
# Generate parameterized SQL (driver-specific placeholders)
(sql, params) = Query.to-sql-params "sqlite" q
println "SQL: ${sql}"
# => SQL: SELECT * FROM posts WHERE status = ?1 AND views > ?2 ORDER BY views DESC LIMIT 10
println "Params: ${params}"
# => Params: [published, 10]
# Execute against a database connection
match Query.exec db q
| Ok rows -> println "Found ${List.length rows} rows"
| Err e -> println "Error: ${e}"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-basicRunning 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
create
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