
A magical ORM for the Nim language π
nimble install ozark
- Macro-based query builder with a fluent API
- Compile-time SQL validation & type safety
- Support for PostgreSQL
- Async query execution (coming soon)
- Migration system (coming soon)
Note
Ozark is still in active development. Expect bugs and breaking changes. Contributions are welcome!
Initialize the database connection with the given parameters. Once initialized, you can use withDB scope to execute queries using the default connection. todo multiple connections and connection pooling coming soon.
import ozark/database
initOzarkDatabase("localhost", "mydb", "myuser", "mypassword", 5432.Port)Define a model by creating a new type that inherits from Model and specifying the fields with their types. See Ozark's Types documentation for supported field types and options.
import ozark/model
newModel User:
id: Serial
name: Varchar(100)
age: IntFor simple queries, you can use the macro-based query builder. The query builder provides a fluent API for constructing SQL queries in a type-safe way. The generated SQL is validated at compile time to catch errors early.
import ozark/query
withDB do:
let id = User.insert({name: "Alice", age: 30}).execGet()
let results: Collection[User] =
Models.table("users").select("*")
.where("id", id)
# you can also use .where("id", "=", id) for more complex conditions
.get(User)
# the get macro will execute the query and return
# a Collection of User instances.
assert results.len == 1
# getting the first result from the collection
let user: User = results.first()
assert user.getId == "1"
assert user.getName == "Alice"
assert user.getAge == "30"When things are getting too complex for the query builder, you can use rawSQL to write raw SQL queries while still benefiting from compile-time validation & type safety. The rawSQL macro allows you to write raw SQL queries with parameter binding to prevent SQL injection attacks.
Models.table("users")
.rawSQL("SELECT * FROM users WHERE name = ?", "Alice")
.get(Users)- π Found a bug? Create a new Issue
- π Wanna help? Fork it!
- π Get β¬20 in cloud credits from Hetzner
MIT license. Made by Humans from OpenPeeps.
Copyright OpenPeeps & Contributors β All rights reserved.