MARTIMM / raku-mongodb-driver

MongoDB driver for Raku
Artistic License 2.0
18 stars 8 forks source link

Object-to-Wire model discussion #1

Closed bbkr closed 12 years ago

bbkr commented 12 years ago

At the early stage of development of Mongo DB Perl 6 driver I stepped into the same problem that every language solved differently - how to separate Wire document(s) from Wire options.

For example

insert(doc_or_docs[, manipulate=True[, safe=False[, check_keys=True[, **kwargs]]]])

find([spec=None[, fields=None[, skip=0[, limit=0[, timeout=True[, snapshot=False[, tailable=False[, sort=None[, max_scan=None[, as_class=None[, slave_okay=False[, await_data=False[, partial=False[, manipulate=True[, **kwargs]]]]]]]]]]]]]]])

$coll->insert({ name => 'mongo', type => 'database' }, {safe => 1});

and sometimes chains

$cursor = $coll->query->limit($per_page)->skip($page_num * $per_page);

making it hard to understand type of object returned and when actual query to database takes place.

For example $collection.insert( %document1, %document2 ) is the most natural way of inserting multiple documents (note that Perl 6 does not flatten it the same way as Perl 5).

However slurpy parameters must appear last, and that would require placing options before them $collection.insert(:continue_on_error, %document1, %document2) # note the bool flags in Perl 6 or giving up on them $collection.insert( [%document1, %document2], :continue_on_error) and this array wrapping I personally dislike.

In my humble opinion closures are the way to go. Some examples:

$collection.insert(:continue_on_error).(%document1, %document2) or without options $collection.insert.(%document1, %document2) or reuse closure my $feeder = $collection.insert(:continue_on_error); $feeder.(%document1, %document2) $feeder.(%document3)

my $cursor = $collection.find(limit=>20, fields=>['name', 'age'] ).() yes, this is correct, many problems are solved if cursor object instance is returned from closure, not directly from collection $collection.find.(nick=>'bbkr') throwing options to closure removes nasty hash wrapping here, this is my nice-to-have

Benefits:

Problems:

Please share your thoughts.

bbkr commented 12 years ago

After analyzing available options I decided to go with variadic parameters.