techfort / LokiJS

javascript embeddable / in-memory database
http:/techfort.github.io/LokiJS
MIT License
6.73k stars 482 forks source link

.chain({ type: 'mapReduce' }) and .chain().mapReduce() return raw data, rather than ResultSet #854

Closed patarapolw closed 3 years ago

patarapolw commented 4 years ago

These destroy the meaning of chaining. I cannot use Loki methods further.

  console.log(
    zhDict
      .chain()
      .find({ entry: { $nin: ['尖嘴鱼'] } })
      .mapReduce(
        (o: any) => {
          o._alt = o.alt ? o.alt.join('\x1f') : undefined
          return o
        },
        (vs: any[]) => {
          const prev: any = []
          vs.map((o) => {
            if (o._alt && ['開', '魚'].some((el) => o._alt.includes(el))) {
              prev.push(o)
            }
          })

          return prev
        }
      )
  )
// Is Array of data, not ResultSet

  console.log(
    zhDict.vocab.chain([
      {
        type: 'mapReduce',
        mapFunction: (o) => {
          o._alt = o.alt ? o.alt.join('\x1f') : undefined
          return o
        },
        reduceFunction: (vs: any[]) => {
          const prev: any = []
          vs.map((o) => {
            if (o._alt && ['開', '魚'].some((el) => o._alt.includes(el))) {
              prev.push(o)
            }
          })

          return prev
        },
      },
    ])
  )

I see that I use string operations anyway, I am not use if I can use indices...

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.