AndyL
Separate Sqlite Database per Customer?
I’m writing an app that manages sensitive data. It might be nice to segregate each customer’s data into a separate Sqlite database.
I’ve done different flavors of multi-tenancy in Postgres, and have configured apps that use a pre-defined group of databases. But never have seen anyone attach, create & destroy Sqlite instances dynamically.
Is it even possible? Is anyone doing this?
Marked As Solved
dimitarvp
Ecto also allows you dynamic repositories.
Also Liked
akoutmos
A little late to the party here, but the proof of concept repo is open source and available for perusing ![]()
AndyL
@dimitarvp thanks again for the reference to dynamic repos. Using this info, I wrote an prototype library called DynDb to manage Dynamic Sqlite databases. Interested to get feedback re: API design and potential gotchas. Have a look!
#!/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()
trarbr
Alex Koutmos played with something similar previously, maybe this is useful? Alex Koutmos on X: "Using Ecto dynamic repositories, a DynamicSupervisor, and a Registry, I was able to create a SQLite powered Phoenix app where every user has their own SQLite db and interacts with their own repo instance. I have now achieved the sharded webscale. AMA #MyElixirStatus https://t.co/3qJkQokooi" / X








