Skip to content

Instantly share code, notes, and snippets.

@dkubb
Last active December 19, 2015 22:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkubb/6031208 to your computer and use it in GitHub Desktop.
Save dkubb/6031208 to your computer and use it in GitHub Desktop.
Examples for axiom-memory-adapter
require 'axiom-memory-adapter'
adapter = Axiom::Adapter::Memory.new(
customers: Axiom::Relation.new([[:id, Integer], [:name, String]]),
orders: Axiom::Relation.new([[:id, Integer], [:customer_id, Integer]])
)
# Insert customer data
customers = adapter[:customers]
customers.insert([[1, 'Dan Kubb']])
customers.insert([[2, 'John Doe']])
# Insert order data
orders = adapter[:orders]
orders.insert([[1, 1]])
orders.insert([[2, 1]])
orders.insert([[3, 1]])
orders.insert([[4, 2]])
# Join customers and orders
customer_orders = customers.
rename(id: :customer_id).
join(orders.rename(id: :order_id))
# Demonstrate writable view-like behaviour
# Insert into the join
customer_orders.insert([[3, 'Jane Doe', 5]])
# Inserts are propagated to the base relations
customers.count # => 3
orders.count # => 5
# Delete from a join
customer_orders.delete([[3, 'Jane Doe', 5]])
# Deletes are propagated to the base relations
customers.count # => 2
orders.count # => 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment