cpfiffer / Wasabi.jl

ORM for Julia

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wasabi

Stable Dev Build Status Coverage Aqua QA

Wasabi is a simple yet powerful ORM for the Julia Language. It currently supports postgresql and sqlite (more to come soon). Wasabi gives you access to a powerful query builder so that you don't need to write SQL and it automatically takes care of query params. It also gives you a simple way to manage tables and their updates using simple migrations.

WARNING: The latest version of Wasabi (0.3.0) works only with Julia 1.9 due to the use of Pkg Extension. You can use version 0.2.2 for previous versions of Julia, but note that this version depends both from LibPQ and SQLite.

Getting Started

using Wasabi
using SQLite

configuration = SQLiteConnectionConfiguration("test.db")
conn = Wasabi.connect(configuration)

# declare models
mutable struct User <: Wasabi.Model
    id::Union{Nothing, AutoIncrement}
    name::String

    User(name::string) = new(nothing, name)
end

Wasabi.primary_key(m::Type{User}) = Wasabi.PrimaryKeyConstraint(Symbol[:id])

struct UserProfile <: Wasabi.Model
    id::Int
    user_id::Int
    bio::Union{String,Nothing}
end

Wasabi.primary_key(m::Type{UserProfile}) = Wasabi.PrimaryKeyConstraint(Symbol[:id])
Wasabi.foreign_keys(m::Type{UserProfile}) = [Wasabi.ForeignKeyConstraint(Symbol[:user_id], :user, Symbol[:id])]

Wasabi.create_schema(conn, User)
Wasabi.create_schema(conn, UserProfile)

user = User("John Doe")
keys = Wasabi.insert!(conn, user)

# println(user.id) -> 1

u = Wasabi.first(conn, User, keys[!, :id])

Wasabi.begin_transaction(conn)
try
    Wasabi.insert!(conn, user)
    Wasabi.commit!(conn)
catch e
    Wasabi.rollback(conn)
    rethrow(e)
end

res = Wasabi.execute_query(conn, rq"SELECT * FROM user where name = ?", Any["John Doe"])
users = Wasabi.df2model(User, res)

u.name = "Jane Doe"
Wasabi.update!(conn, user)

Wasabi.delete!(conn, user)

qb = QueryBuilder.from(User) |> QueryBuilder.select() |> QueryBuilder.where(:(and, (User, name, like, "%John%"))) |> QueryBuilder.limit(1)
users = Wasabi.execute_query(conn, qb)

Wasabi.disconnect(conn)

About

ORM for Julia

License:MIT License


Languages

Language:Julia 100.0%