Skip to content

ire4ever1190/ponairi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pónairí 🫘

Simple ORM to handle basic CRUD tasks. Future plans are to expand the query generation to make needing to write SQL less common

Docs here

Create

To start you want to create your objects that will define your schema

type
  Customer = object
    id {.primary, autoIncrement.}: int64
    email: string
    firstName, lastName: string

  Order = object
    id {.primary, autoIncrement.}: int64
    name: string
    customer {.references: Customer.id.}: int64

# Open connection to database, make sure to close connection when finished
let db = newConn(":memory:")
# Create the tables
db.create(Customer, Order)

Now you can start inserting data

var john = Customer(
  email: "foo@example.com",
  firstName: "John",
  lastName: "Doe"
)
john.id = db.insertID(john)

var tableOrder = Order(
  name: "Table",
  customer: john.id
)
tableOrder.id = db.insertID(tableOrder)

Read

Reading is done via the find() proc

# Look through every order
for order in db.find(seq[Order]):
  echo order

# Find first one that matches a query.
# Tables are named same as the object
echo db.find(Order, sql"SELECT * FROM Order WHERE customer = ?", john.id)

# Option[T] can also be used if the query mighn't return anything
import std/options
assert db.find(Option[Order], sql"SELECT * FROM Order WHERE customer = 99999").isNone

Currently there is some support for automatically loading objects through references (This will be expanded on in future)

# We want to load the object that is referenced by tableOrder in the customer field
let customer = db.load(tableOrder, customer)
assert customer is Customer

Update

Updating is currently only support via upsert() which either inserts an object or updates any row that it collides with

tableOrder.name = "Better table"
db.upsert(tableOrder)
assert db.find(Order, sql"SELECT * FROM Order WHERE id = ?", tableOrder.id).name == "Better table"

Delete

Deleting is done by simply calling delete with an object

assert db.exists(tableOrder)
db.delete(tableOrder)
assert not db.exists(tableOrder)