koppa96 / linq-functional

3 stars 0 forks source link

Composition of queries #8

Open airtonix opened 5 months ago

airtonix commented 5 months ago

What's the design vision with regards to this?


  const posts = await query(
    from(collection.items),
    where((item) => {  
      if (import.meta.env.MODE === 'development') {
        return true;
      }
      return item.stage !== 'draft';
    }),
  )

  ...

  const post = await query(
    from(posts),
    where((item) => {
      if (item.metadata.slug !== params.slug) {
        return false;
      }
    }),
  )

or are we expected to do :

  const whereDraftOnlyInDevelopment = where((item) => {  
      if (import.meta.env.MODE === 'development') {
        return true;
      }
      return item.stage !== 'draft';
    })
  const whereSlugEqualsParam = (slug: string) => where((item) => {
      if (item.metadata.slug !== slug) {
        return false;
      }
    })

  const posts = await query(
    from(collection.items),
    whereDraftOnlyInDevelopment,
  )

  ...

  const post = await query(
    from(collection.items),
    whereDraftOnlyInDevelopment,
    whereSlugEqualsParam(params.slug)
  )
koppa96 commented 4 months ago

I'd prefer the first approach generally. I think creating operators is beneficial only if you'd use them multiple times.