Skip to content
This repository was archived by the owner on Dec 1, 2024. It is now read-only.
This repository was archived by the owner on Dec 1, 2024. It is now read-only.

Add iterator(options) and return [Symbol.asyncIterator] #477

Closed
@MeirionHughes

Description

@MeirionHughes

I'd like to propose adding a method to levelup that would allow direct iteration of the underlying store.iterator. This would negate the need for consumers to convert the ReadStream to an Iterable.

from a usage perspective, something along the lines of:

async function main(){
  let db = levelup(encode(leveldown()));

  await db.put("foo", "hello");
  await db.put("bar", "world");

  let search = db.read( { leq: "bar", limit: 10 });

  for await (let data of search){
    let key = data.key;
    let value = data.value;
  }
}

not a huge change to implement this:

LevelUP.prototype.read= function (options) {
  options = extend({ keys: true, values: true }, options)
  if (typeof options.limit !== 'number') { options.limit = -1 }
  return new LevelAsyncIterator(this.db.iterator(options), options)
}

where LevelAsyncIterator can be the stream iterator butchered to remove the Stream aspects and simply implement [Symbol.asyncIterator] instead - along with ability to end (clean) the underlying store.iterator

If people are happy with this proposal I can make the PRs and a new level-iterator repo, based off level-stream-iterator?

I'll do it as a plugin to levelup due to Symbol.asyncIterator being still stage-3.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions