Skip to content

openpeeps/ozark

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

40 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

import ozark/database

initOzarkDatabase("localhost", "mydb", "myuser", "mypassword", 5432.Port)

withDB or withDBPool

Use withDB to execute queries then automatically close the connection after the block is executed. Use withDBPool to execute queries using a connection pool for better performance in concurrent scenarios.

withDB do:
  # execute queries here

withDBPool do:
  # execute queries here

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 Users:
  id: Serial
  username: Varchar(50)
  name: Varchar(100)
  email: Varchar(100)

newModel Subscription:
  id: Serial
  user_id: Users.id
  plan: Varchar(50)
  status: Varchar(20)
  created_at: TimestampTz
  updated_at: TimestampTz

Create the tables

TO create a database table you can use the prepareTable macro.

withDB do:
  Models.table(Users).prepareTable().exec()

Drop a table

withDB do:
  Models.table(Users).dropTable(cascade = true).exec()

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.

Insert data

import ozark/query

withDBPool do:
  let id = Models.table(Users).insert({
    name: "John Doe",
    username: "johndoe",
    email: "johndoe@example.com",
  }).execGet() # returns the id of the inserted row

Select Query

withDBPool do:
  let res = Models.table(Users)
                  .select(["name", "email"])
                  .where("name", "John")
                  .get()

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