Skip to content

openpeeps/ozark

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

34 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Ozark ORM
A magical ORM for the Nim language πŸ‘‘

nimble install ozark

API reference
Github Actions Github Actions

😍 Key Features

  • 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!

Examples

Connecting to the database

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

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: Int

Querying the database

For 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"

Querying with raw SQL

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)

❀ Contributions & Support

🎩 License

MIT license. Made by Humans from OpenPeeps.
Copyright OpenPeeps & Contributors β€” All rights reserved.

Packages

 
 
 

Contributors

Languages