Using Jorm.jl with Oxygen.jl Web Framework

Oxygen.jl is a nice to use web framework in Julia. In this tutorial we will explore how to create a CRUD (Create, Read, Update, Delete) application using Oxygen.jl and Jorm.jl, you can follow these steps.

Installation and Setup

First, ensure you have the necessary packages installed:

using Pkg
Pkg.add("Oxygen")
Pkg.add("Jorm")
Pkg.add("SQLite")
Pkg.add("HTTP")
Pkg.add("StructTypes")

Basic App Structure

We can setup a basic structure for our CRUD application using Oxygen.jl and Jorm.jl.

Define Your Model

Define a struct to represent your data model:

struct BlogPost
    title::String
    content::String
end

# Support JSON serialization and deserialization
StructTypes.StructType(::Type{BlogPost}) = StructTypes.Struct()

Connect to the Database

Use Jorm.jl to connect to the SQLite database and create the necessary table:

using Jorm

connection_string = Jorm.SQLiteConnectionString(database_name="blog.db")
db = Jorm.connect(connection_string)
tb = Jorm.tablename(BlogPost)
Jorm.create_table(db, BlogPost, tb)

CRUD Endpoints with Oxygen.jl

Define the CRUD endpoints using Oxygen.jl:

using Oxygen


# Output Model
struct BlogPostOutput
    id::Int # to handle autoincrement ID field
    title::String
    content::String
end

# Create a new blog post
@post "/api/v1/blogs/" function(req::HTTP.Request)
    data = Oxygen.json(req, BlogPost)
    Jorm.insert!(db, BlogPost, data)
    return data
end

# Read all blog posts
@get "/api/v1/blogs/" function(req::HTTP.Request)
    results = Jorm.read_all(db, BlogPost)
    return Jorm.serialize_to_list(BlogPostOutput,results)
end

# Read one blog post by ID
@get "/api/v1/blogs/{blog_id}" function(req::HTTP.Request, blog_id::Int)
    result = Jorm.read_one(db, BlogPost, blog_id)
    return Jorm.serialize_to_list(BlogPostOutput,result)
end

# Update an existing blog post
@patch "/api/v1/blogs/{blog_id}" function(req::HTTP.Request, blog_id::Int)
    data = Oxygen.json(req, BlogPost)
    Jorm.update!(db, BlogPost, blog_id, data)
    return data
end

# Delete a blog post by ID
@delete "/api/v1/blogs/{blog_id}" function(req::HTTP.Request, blog_id::Int)
    result = Jorm.delete!(db, BlogPost, blog_id)
    return result
end

# Start the server
serve(port=8001)

View in Browser

Once you start the server ,you can check your browser or any web client to see the API endpoints available on http://localhost:8001

This is one of the ways that provides a clear and concise guide on how to set up and use the CRUD API with Oxygen.jl and Jorm.jl.