Jabol
View SourceJabol is a lightweight schema and data persistence library for Elixir applications. It provides a simple API for defining schemas, managing database connections, and performing CRUD operations.
Features
- Schema definition system with field types and validations
- Simple repository pattern for database operations
- Factory system for easy test data generation
- Minimal configuration with sensible defaults
Installation
Add jabol to your list of dependencies in mix.exs:
def deps do
[
{:jabol, "~> 0.1.0"}
]
endDocumentation
Complete documentation is available on HexDocs.
You can also generate documentation locally with:
mix docs
Usage
Configuration
Set up your database configuration in config.exs:
config :jabol, Jabol.Repo,
database: "your_database",
hostname: "localhost",
username: "postgres",
password: "postgres"Defining Schemas
defmodule MyApp.User do
use Jabol.Schema
schema "users" do
field :name, :string
field :email, :string
field :age, :integer
timestamps()
end
endDatabase Operations
# Insert a record
{:ok, user} = Jabol.Repo.insert(MyApp.User, %{name: "John", email: "john@example.com", age: 30})
# Get a record by ID
user = Jabol.Repo.get(MyApp.User, 1)
# Update a record
{:ok, updated_user} = Jabol.Repo.update(user, %{age: 31})
# Delete a record
{:ok, deleted_user} = Jabol.Repo.delete(user)Factory for Testing
# Define defaults in your test helper
defmodule Jabol.Factory do
def get_defaults(MyApp.User) do
%{
name: "Test User",
email: "test@example.com",
age: 25
}
end
end
# In your tests
test "user creation" do
user = Jabol.Factory.build(MyApp.User, %{name: "Custom Name"})
assert user.name == "Custom Name"
assert user.email == "test@example.com" # Default value
endMigrations
Generate a new migration:
mix jabol.gen.migration create_users
This creates a migration file in priv/migrations with a timestamp prefix.
Run migrations:
mix jabol.migrate
Roll back migrations:
mix jabol.migrate --down
Run migrations up to a specific version:
mix jabol.migrate --version 20230101120000
Example migration:
defmodule Jabol.Migrations.CreateUsers do
use Jabol.Migration
def up do
create_table(:users) do
add :name, :string
add :email, :string
add :age, :integer
add :inserted_at, :naive_datetime
add :updated_at, :naive_datetime
end
create_index(:users, [:email], unique: true)
end
def down do
drop_table(:users)
end
endDevelopment
To contribute to Jabol:
- Clone the repository
- Install dependencies with
mix deps.get - Run tests with
mix test
License
This project is licensed under the MIT License - see the LICENSE file for details.