Dynamic Sqlite databases for Elixir.
It's easy to manage dynamic key/value datastores using :dets or CubDB .
DynDb intends to make it easy to manage dynamic SqLite datastores.
Use DynDb to manage one or more segregated / isolated databases. This may be useful in situations with:
- privacy concerns - medical or financial data
- regulatory issues - GDPR etc.
- portability requirements - allowing users to download data and run locally
Key features:
- Dynamic creation and use of Sqlite data stores
- Encrypted data stores
- Ecto Migrations and Query operations
def deps do
[
{:dyn_db, github: "andyl/dyn_db"}
]
end#!/usr/bin/env elixir
Mix.install([
{:dyn_db, github: "andyl/dyn_db"}
])
# Write a Migration
defmodule Migration0 do
use Ecto.Migration
def change do
create table("chats") do
add(:message, :string)
timestamps(type: :utc_datetime_usec)
end
end
end
# Write a Schema
defmodule Chat do
use Ecto.Schema
schema "chats" do
field(:message, :string)
timestamps(type: :utc_datetime_usec)
end
end
# Interact with the DynDb
defmodule Main do
def main do
# The database file will be auto-created
{:ok, db1} = DynDb.start_link(database: "./data1.db")
# Migrate
DynDb.migrate(db1, Migration0)
# Query
DynDb.insert!(db1, %Chat{message: "HELLO at #{Time.utc_now()} UTC"})
DynDb.all(db1, Chat) |> IO.inspect()
end
end
Main.main()Run the script, then query the database from the command line!
> sqlite3 -batch data1.db "select * from chats;"DynDb is experimental. Basic functions and best practices are not yet known.
Here are some references to learn about dynamic repos and comparable software.
| Source | Title |
|---|---|
| Elixir Forum | Separate Sqlite Database per Customer? |
| HexDocs | Ecto Migrator |
| HexDocs | Ecto Dynamic Repos |
| Underjord | ECTO & MULTI-TENANCY - DYNAMIC REPOS - PART 1 |
| Underjord | ECTO & MULTI-TENANCY - DYNAMIC REPOS - PART 2 |
| GitHub | CubDB |
| Alex Koutmos Experiment |