Goblin.Tx protocol (Goblin v0.5.0)
View SourceTransaction helpers for working with Goblin database transactions.
Usage
Goblin.transaction(db, fn tx ->
user = Goblin.Tx.get(tx, :user_123)
if user do
updated = Map.update!(user, :login_count, &(&1 + 1))
tx = Goblin.Tx.put(tx, :user_123, updated)
{:commit, tx, :ok}
else
:abort
end
end)
Goblin.read(db, fn tx ->
key1 = Goblin.Tx.get(tx, :start, 0)
key2 = Goblin.Tx.get(tx, key1)
{key1, key2}
end)
Summary
Functions
Retrieves a value within a transaction.
Retrieves key-value pairs associated with the provided list of keys.
Writes a key-value pair within a transaction.
Writes key-value pairs within a transaction.
Removes a key within a transaction.
Removes keys within a transaction.
Retrieves a stream over key-value pairs sorted in ascending order by key.
Types
Functions
@spec get(t(), Goblin.db_key(), term()) :: Goblin.db_value()
Retrieves a value within a transaction.
Parameters
tx- The transaction structkey- The key to look updefault- A default value if the key is not found, defaults tonil
Returns
- The value associated with the key, or
defaultif not found
Examples
counter = Goblin.Tx.get(tx, :counter, 0)
@spec get_multi(t(), [Goblin.db_key()]) :: [{Goblin.db_key(), Goblin.db_value()}]
Retrieves key-value pairs associated with the provided list of keys.
Parameters
tx- The transaction structkeys- A list of keys to look up
Returns
- Key-value pairs
Examples
[user1: %{name: "Alice"}, user2: %{name: "Bob"}] = Goblin.Tx.get_multi(tx, [:user1, :user2])
@spec put(t(), Goblin.db_key(), Goblin.db_value()) :: t()
Writes a key-value pair within a transaction.
Parameters
tx- The transaction structkey- Any Elixir term to use as the keyvalue- Any Elixir term to store
Returns
- Updated transaction struct
Examples
tx = Goblin.Tx.put(tx, :user, %{name: "Alice"})
@spec put_multi(t(), [{Goblin.db_key(), Goblin.db_value()}]) :: t()
Writes key-value pairs within a transaction.
Parameters
tx- The transaction structpairs- The key-value pairs
Returns
- Updated transaction structj
Examples
tx = Goblin.Tx.put_multi(tx, [user1: %{name: "Alice"}, user2: %{name: "Bob"}])
@spec remove(t(), Goblin.db_key()) :: t()
Removes a key within a transaction.
Parameters
tx- The transaction structkey- The key to remove
Returns
- Updated transaction struct
Examples
tx = Goblin.Tx.remove(tx, :user)
@spec remove_multi(t(), [Goblin.db_key()]) :: t()
Removes keys within a transaction.
Parameters
tx- The transaction structkeys- The list of keys to remove
Returns
- Updated transaction struct
Examples
tx = Goblin.Tx.remove_multi(tx, [:user1, :user2])
@spec select( t(), keyword() ) :: Enumerable.t({Goblin.db_key(), Goblin.db_value()})
Retrieves a stream over key-value pairs sorted in ascending order by key.
Parameters
tx- The transaction structopts- A keyword list with two options::min- The minimum key to range over:max- The maximum key to range over
Examples
[user1: %{name: "Alice"}, user2: %{name: "Bob"}] = Goblin.Tx.select(tx) |> Enum.to_list()
[user1: %{name: "Alice"}] = Goblin.Tx.select(tx, max: :user1) |> Enum.to_list()
[user2: %{name: "Bob"}] = Goblin.Tx.select(tx, min: :user2) |> Enum.to_list()
[user1: %{name: "Alice"}, user2: %{name: "Bob"}] = Goblin.Tx.select(tx, min: :user1, max: :user2) |> Enum.to_list()