@railsbros-dirk and me discussed this today face to face ๐
We will try to mimic the API of Array & Enumerable as close as possible. Basically a collection in ArangoDB is a big array with a lot of hashes in it. Let's give it a try.
Return all documents of the characters collection:
result = Brazil::Collection.new('characters').to_a
A projection via map
:
result = Brazil::Collection.new('casting').map do |cast|
{ name: cast.actor, rating: 'awesome' }
end.to_a
Filtering via find_all
or select
:
result = Brazil::Collection.new('characters')
.find_all { |character| character.name == 'Sam Lawry' }
.to_a
Sorting via sort_by
:
result = Brazil::Collection.new('characters')
.sort_by { |character| character.name }
.to_a
Limit via first
, drop
and slice
:
result = Brazil::Collection.new('characters').first(5).to_a
result = Brazil::Collection.new('characters').slice(5, 10).to_a
# and slice's well known alias:
result = Brazil::Collection.new('characters').slice[5, 10].to_a
# Also: Ranges
result = Brazil::Collection.new('characters').slice[5...10].to_a
Pagination via each_slice
:
result = Brazil::Collection.new('characters').each_slice(5) do |some_characters|
p some_characters
end
Joins via product
(to join more collections, just add more to the product
call):
result = Brazil::Collection.new('characters').product('casting').find_all do |character, casting| do
character.name == casting.character
end.map do |character, casting|
{ character: character, casting: casting }
end.to_a