View Source FDBC.Foundation behaviour (fdbc v0.1.4)
A convenience behaviour to ease the use of a database instance within an OTP application while also promoting best practices.
Firstly the configuration for the behaviour must be set and is designed in such a way that it can be changed for each environment:
config :example, Example.Foundation,
cluster: "./fdb.cluster", # Path to the cluster file
directory: "example", # The directory the application should operate under
The behaviour can then be used to create and manage a database instance.
defmodule Example.Foundation do
use FDBC.Foundation,
otp_app: :example,
options: [
transaction_retry_limit: 100,
transaction_timeout: 60_000
]
end
The module must be added to the applications supervision tree, once this is done it can then be used to easily transact with FoundationDB.
defmodule Example.App do
alias FDBC.{Directory, Subspace, Transaction}
alias Example.Foundation
def get_value(key) do
Foundation.transact(fn tr, dir ->
key = Directory.subspace(dir) |> Subspace.pack([key])
Transaction.get(tr, key)
end)
end
def set_value(key, value) do
Foundation.transact(fn tr, dir ->
key = Directory.subspace(dir) |> Subspace.pack([key])
Transaction.set(tr, key, value)
end)
end
end
Summary
Callbacks
@callback database() :: FDBC.Database.t()
Returns the underlying database instance.
@callback transact( fun :: (FDBC.Transaction.t(), FDBC.Directory.t() -> any()), opts :: keyword() ) :: any()
Performs a transaction against the database.
The directory passed to fun
is that which namespaces the application as
defined in the configuration.
The options are used for creating the transaction and therefore are the same
as those for FDBC.Transaction.create/2
.