Jorm.jl
is a simple Julia package designed to simplify interactions between Julia code and databases. It provides a straightforward Object-Relational Mapping (ORM) layer, allowing you to work with databases using Julia structs and functions.
The current database support includes SQLite.
- Future plan is to offer support for databases such as PostGreSQL, MySQL and more.
To install Jorm.jl
, use the Julia package manager:
using Pkg
Pkg.add("Jorm")
To connect to a SQLite database, use the connect
function:
connection_string = Jorm.SQLiteConnectionString(database_name="example.db")
db = Jorm.connect(connection_string)
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)
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)
@test isempty(result)
Close the database connection and delete the database file if needed:
Jorm.disconnect(db)
Jorm.delete_db(connection_string)
Here is a complete example demonstrating the usage of Jorm.jl
:
struct BlogArticle
title::String
content::String
end
connection_string = Jorm.SQLiteConnectionString(database_name="test.db")
tb = Jorm.tablename(BlogArticle)
db = Jorm.connect(connection_string)
Jorm.create_table(db, BlogArticle, tb)
# 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
# Serialize the data
Jorm.serialize_to_list(results)
# 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)
# Read one record by key
result = Jorm.getfirst(db, BlogArticle,"title", "First Title")
println(result)
for row in results
println(row.id)
println(row.content)
end
# Delete a record
result = Jorm.delete!(db, BlogArticle, 1)
result = Jorm.read_one(db, BlogArticle, 1)
@test isempty(result)
# Close the database connection
Jorm.disconnect(db)
Jorm.delete_db(connection_string)
For detailed API documentation, please refer to the Jorm.jl API Documentation.
Contributions are welcome Please open an issue or pull request on the Jorm.jl GitHub repository.
Jorm.jl
is licensed under the MIT License. See LICENSE.md for details.
Special thanks to the Julia community and future contributors who have helped shape this package.