Horizon: Check Existence

Created on 20 May 2016  路  7Comments  路  Source: rethinkdb/horizon

Hello.

I tried to use fetch() to check for existence, but it doesn't worked, any ideas?

// Doesn't work, neither callback dispatches
calls.find({ from: 'hnordt' }).fetch()
  .subscribe(r => console.warn(r), e => console.error(e))

Most helpful comment

@danielmewes .partition() will split an observable sequence into two observable streams based on some condition. There's also .defaultIfEmpty() which is probably the way to go in this case.

All 7 comments

A work-around is using the final callback with a local variable like this:

let foundCall = false;
calls.find({ from: 'hnordt' }).fetch()
  .subscribe(() => { foundCall = true; console.log("Call existed"); }, (e) => console.error(e), () => {
    if (!foundCall) {
      console.log("Call did not exist");
    }
  });

We should come up with an API for providing a nicer way of detecting non-existence.

Maybe find should call the result callback with null if no row exists?

@danielmewes that would be what I expected initially. Awesome.

I think this is the best way to do it. We could potentially return null, but I think this works better with observable operators. For example, if you do

require('rxjs/add/operator/isEmpty')

calls.find({ from: 'hnordt' }).fetch().isEmpty().subscribe(
 empty => console.log('It was', empty? ' ' : ' not', 'empty')
)

Oh I forgot there were all those nice Observable operators.

One point in favor of generating a null is that it makes it easier to throw an error if a document _should_ exist but doesn't.

@deontologician Is there an option to "split" an Observable stream? Like what if I want to use the result, but do something different if it's empty?

@danielmewes .partition() will split an observable sequence into two observable streams based on some condition. There's also .defaultIfEmpty() which is probably the way to go in this case.

Cool, defaultIfEmpty() sounds like what I wanted. We should probably add an example with that do our docs.

@hnordt Does that satisfy your use case?

@danielmewes yes, thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

deontologician picture deontologician  路  9Comments

danielmewes picture danielmewes  路  7Comments

arthurvi picture arthurvi  路  4Comments

intellix picture intellix  路  5Comments

arthurvi picture arthurvi  路  8Comments