Goblin.Tx protocol (Goblin v0.6.0)
View SourceProtocol for reading and writing within a transaction.
Used inside Goblin.transaction/2 (read-write) and Goblin.read/2 (read-only).
Goblin.transaction(db, fn tx ->
counter = Goblin.Tx.get(tx, :counter, default: 0)
tx = Goblin.Tx.put(tx, :counter, counter + 1)
{:commit, tx, :ok}
end)
Goblin.read(db, fn tx ->
Goblin.Tx.get(tx, :alice)
end)
Summary
Functions
Retrieves a value within a transaction.
Retrieves values for multiple keys within a transaction.
Writes a key-value pair within a transaction.
Writes multiple key-value pairs within a transaction.
Removes a key within a transaction.
Removes multiple keys within a transaction.
Types
Functions
@spec get(t(), Goblin.db_key(), keyword()) :: Goblin.db_value()
Retrieves a value within a transaction.
Parameters
tx- The transaction structkey- The key to look upopts- A keyword list with the following options (default:[])::tag- Tag the key is namespaced under:default- Value to return ifkeyis not found (default:nil)
Returns
- The value associated with the key, or
defaultif not found
Examples
Goblin.Tx.get(tx, :alice)
# => "Alice"
Goblin.Tx.get(tx, :nonexistent, default: :not_found)
# => :not_found
@spec get_multi(t(), [Goblin.db_key()], keyword()) :: [ {Goblin.db_key(), Goblin.db_value()} ]
Retrieves values for multiple keys within a transaction.
Keys not found are excluded from the result.
Parameters
tx- The transaction structkeys- A list of keys to look upopts- A keyword list with the following options (default:[])::tag- Tag the keys are namespaced under
Returns
- A list of
{key, value}tuples for keys found, sorted by key
Examples
[{:alice, "Alice"}, {:bob, "Bob"}] = Goblin.Tx.get_multi(tx, [:alice, :bob])
@spec put(t(), Goblin.db_key(), Goblin.db_value(), keyword()) :: 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 storeopts- A keyword list with the following options (default:[])::tag- Tag to namespace the key under
Returns
- Updated transaction struct
Examples
tx = Goblin.Tx.put(tx, :alice, "Alice")
Writes multiple key-value pairs within a transaction.
Parameters
tx- The transaction structpairs- A list of{key, value}tuplesopts- A keyword list with the following options (default:[])::tag- Tag to namespace the keys under
Returns
- Updated transaction struct
Examples
tx = Goblin.Tx.put_multi(tx, [{:alice, "Alice"}, {:bob, "Bob"}])
@spec remove(t(), Goblin.db_key(), keyword()) :: t()
Removes a key within a transaction.
Parameters
tx- The transaction structkey- The key to removeopts- A keyword list with the following options (default:[])::tag- Tag the key is namespaced under
Returns
- Updated transaction struct
Examples
tx = Goblin.Tx.remove(tx, :alice)
@spec remove_multi(t(), [Goblin.db_key()], keyword()) :: t()
Removes multiple keys within a transaction.
Parameters
tx- The transaction structkeys- A list of keys to removeopts- A keyword list with the following options (default:[])::tag- Tag the keys are namespaced under
Returns
- Updated transaction struct
Examples
tx = Goblin.Tx.remove_multi(tx, [:alice, :bob])