WatermelonDB icon indicating copy to clipboard operation
WatermelonDB copied to clipboard

deep fetch/observe relations

Open jon-dearaujo opened this issue 8 years ago • 8 comments

Hi!

Is there a way to connect with React components and observe deep relations? I need something like this:

const enhance = withObservables(['post'], ({post}) => ({
  post: post.observe(),
  author: post.author.observe(),
  authorCat: post.author.cat.observe() // Just to illustrate, I am out of creativity here 🐱 
});
export default enhance(PostComponent);

If not, what do you guys recommend in cases like this? I could probably use another component inside "PostComponent" to enhance and observe the cat inside, since author would be already available, but I am refactoring an app and it could result in a LOT of work.

Thanks!

jon-dearaujo avatar Nov 20 '18 18:11 jon-dearaujo

I could probably use another component inside "PostComponent" to enhance and observe the cat inside, since author would be already available

Hi @jon-dearaujo! This is correct approach - you can enhance one withObservables with another.

const enhancePost = withObservables(['post'], ({post}) => ({
  post: post.observe(),
  author: post.author.observe()
});

const enhanceAuthorCat = withObservables(['author'], ({author}) => ({
  authorCat: author.cat.observe()
});

export default enhancePost(enhanceAuthorCat(PostComponent));

You may also use compose-like function (i.e. from recompose https://github.com/acdlite/recompose).

const enhance = compose(
  withObservables(['post'], ({post}) => ({
    post: post.observe(),
    author: post.author.observe(),
  }),
  withObservables(['author'], ({author}) => ({
    authorCat: author.cat.observe()
  })
);

export default enhance(PostComponent);

Or you can use switchMap operator from rx/js if you don't need author directly.

  withObservables(['post'], ({post}) => ({
    post: post.observe(),
    authorCat: post.author.observe().pipe(switchMap((author) => author.cat.observe())),
  }),

rkrajewski avatar Nov 20 '18 23:11 rkrajewski

Thanks, @rkrajewski! It is a really good solution.

jon-dearaujo avatar Nov 21 '18 12:11 jon-dearaujo

@jon-dearaujo If you can spare 15 minutes, you could help others by contributing to the documentation and explaining this case :)

radex avatar Nov 21 '18 13:11 radex

I can do it and submit the PR later today, probably patching docs/Components.md with this tip in the advanced section. Sounds good?

jon-dearaujo avatar Nov 21 '18 13:11 jon-dearaujo

Sounds great!

radex avatar Nov 21 '18 13:11 radex

I could probably use another component inside "PostComponent" to enhance and observe the cat inside, since author would be already available

Hi @jon-dearaujo! This is correct approach - you can enhance one withObservables with another.

const enhancePost = withObservables(['post'], ({post}) => ({
  post: post.observe(),
  author: post.author.observe()
});

const enhanceAuthorCat = withObservables(['author'], ({author}) => ({
  authorCat: author.cat.observe()
});

export default enhancePost(enhanceAuthorCat(PostComponent));

You may also use compose-like function (i.e. from recompose https://github.com/acdlite/recompose).

const enhance = compose(
  withObservables(['post'], ({post}) => ({
    post: post.observe(),
    author: post.author.observe(),
  }),
  withObservables(['author'], ({author}) => ({
    authorCat: author.cat.observe()
  })
);

export default enhance(PostComponent);

Or you can use switchMap operator from rx/js if you don't need author directly.

  withObservables(['post'], ({post}) => ({
    post: post.observe(),
    authorCat: post.author.observe().pipe(switchMap((author) => author.cat.observe())),
  }),

One thing I forgot to mention is that the relation, in my case, of post -> author is optional. So I ended up with the switchMap solution along with a of in case of empty author.

const enhance = withObservables(['post'], ({post}) => ({
  post: post.observe(),
  author: post.author.observe(),
  authorCat: post.author.observe().pipe(switchMap((author) => author ? author.cat.observe() : of(null)))
}))

The double-enhance solution was not working without the of from rxjs, I am not sure what i should return when the author is not present in the second enhancer.

const enhanceAuthorCat = withObservables(['author'], ({author}) => ({
  authorCat: author ? author.cat.observe() : null
});

and

const enhanceAuthorCat = withObservables(['author'], ({author}) => {
  return author ? {authorCat: author.cat.observe()} : null
};

are not valid getObservables functions. It would need to be like

const enhanceAuthorCat = withObservables(['author'], ({author}) => ({
  authorCat: author ? author.cat.observe() : of(null) // of from rxjs
});

Anyway, the switchMap seems to be the simplest way, even with all the rxjs not so friendly code.

jon-dearaujo avatar Nov 21 '18 17:11 jon-dearaujo

authorCat: author ? author.cat.observe() : of(null)

This is correct and simplest. You can Simplify to author ? author.cat : of(null). I think It would be reasonable if passing null in withObservables would just inject null as props. I can give you some pointers how to do it if you’d like to contribute. Then you’d get down to authorCat: author?.cat

radex avatar Nov 21 '18 18:11 radex

#161

jon-dearaujo avatar Nov 22 '18 14:11 jon-dearaujo