Jorm.jl – An Object Relational Mapper for Julia

Object Relational Mapping (ORM) is a powerful technique that bridges the gap between object-oriented programming languages and relational databases. It simplifies the work of developers when dealing with SQL related databases, making it easier.

Jorm.jl is a simple ORM tool designed to simplify interactions between Julia applications and relational databases. This article will delve into what Jorm.jl is, its key features, and how to use it effectively.

What is Jorm.jl?

Jorm.jl is a Julia package that implements Object Relational Mapping, allowing developers to interact with relational databases using Julia’s object-oriented structures. It serves as an abstraction layer between the application code and the database, enabling developers to work with data in the form of Julia objects rather than writing raw SQL queries.

Key Features of Jorm.jl

  1. Simplified Database Interactions:
    • Jorm.jl abstracts away the complexities of SQL, allowing developers to perform CRUD (Create, Read, Update, Delete) operations using Julia methods. This simplifies the development process and reduces the need for manual SQL coding.
  2. Struct:
    • Since there are no classes in Julia, we use structs to represent our model schema. Developers can define Julia structs that map to database tables. 
  3. Automatic SQL Generation:
    • Jorm.jl generates the necessary SQL queries based on the operations performed on the Julia objects. This automation saves development time and reduces the risk of SQL-related errors.
  4. Support for Various Databases:
    • Currently Jorm.jl supports SQLite and PostgresQL via LibQL.jl.Plans to support other Databases are in progress.
  5. Code Reuse:
    • The use of Julia structs to represent database data enables code reuse across different parts of the application via Jorm.jl

Installation

To install Jorm.jl, use the Julia package manager:

using Pkg
Pkg.add("Jorm")

Connecting to Databases

To connect to a SQLite database, use the connect function:

connection_string = Jorm.SQLiteConnectionString(database_name="example.db")
db = Jorm.connect(connection_string)

Creating Tables and Model Schema

Create a table based on a Julia struct. We omit the id since it is by default an autoincrement but you can still access it via .id

struct BlogArticle
    title::String
    content::String
end

tb = Jorm.tablename(BlogArticle)
Jorm.create_table(db, BlogArticle, tb)

CRUD Operations via Jorm.jl

To perform basic CRUD (Create, Read, Update, Delete) operations:

# Create a new record
data = BlogArticle("First Title", "My Blog Post")
Jorm.insert!(db, BlogArticle, data)

# Read all records
results = Jorm.read_all(db, BlogArticle)
for row in results
    @test row.id == 1
end

# Update an existing record
updated_data = BlogArticle("First Title", "Updated Blog Post")
Jorm.update!(db, BlogArticle, 1, updated_data)

# Read one record by ID
result = Jorm.read_one(db, BlogArticle, 1)
println(result)

# Delete a record
result = Jorm.delete!(db, BlogArticle, 1)
result = Jorm.read_one(db, BlogArticle, 1)

Closing the Database Connection

Close the database connection and delete the database file if needed:

Jorm.disconnect(db)
Jorm.delete_db(connection_string)

Jorm.jl is a valuable tool for Julia developers looking to interact with relational databases in a more object-oriented and efficient manner. By providing a layer of abstraction between the application code and the database, Jorm.jl simplifies database interactions, increases productivity, and improves application design. As the Julia ecosystem continues to grow, tools like Jorm.jl will play a crucial role in making database-backed application development more accessible and streamlined.

You can check out the video tutorial below

Thank You for your attention

Jesus Saves

By Jesse E.Agbe(JCharis)

Leave a Comment

Your email address will not be published. Required fields are marked *