cubos / rethinkdb.cr

RethinkDB Driver for Crystal
MIT License
34 stars 4 forks source link

is Lambda supported? #9

Closed kingsleyh closed 6 years ago

kingsleyh commented 6 years ago

hi

is this kind of query possible currently?

r.table('orders').concat_map(lambda order:
    order['items'].map(lambda item:
        {'item_id': item['item_id'], 'quantity': item['quantity'], 'count': 1}
    ))

I searched the repo and found only references to lambda in yaml files so I guess it's not implemented.

lbguilherme commented 6 years ago

It is, but using crystal blocks:

r.table("orders").concat_map do |order|
  order["items"].map do |item|
    {"item_id" => item["item_id"], "quantity" => item["quantity"], "count" => 1}
  end
end

Same syntax as in Ruby

kingsleyh commented 6 years ago

aah awesome thanks - I couldn't see it

kingsleyh commented 6 years ago

concat_map doesn't seem to exist though - is there an equivalent?

lbguilherme commented 6 years ago

f9a52b76 :wink:

kingsleyh commented 6 years ago

awesome thanks :) - I'm still getting my head around how the library works!

kingsleyh commented 6 years ago

I notice that

 r.table("users").concat_map do |user| user end 

complains

  'RethinkDB::TableTerm#concat_map' is expected to be invoked with a block, but no block was given

but it works in this format ok:

 r.table("users").concat_map{|user| user } 

The nested ones seem to work fine with do end though