Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write Options ignored in chained batch #13

Closed
MeirionHughes opened this issue Sep 26, 2017 · 1 comment
Closed

write Options ignored in chained batch #13

MeirionHughes opened this issue Sep 26, 2017 · 1 comment

Comments

@MeirionHughes
Copy link
Member

MeirionHughes commented Sep 26, 2017

with a custom encoder:

let fooEncoding = {
  encode: (x) => x.message,
  decode: (x) => { return { message: String(x) }; },
  type: "foo",
  buffer: false
}

when you batch with the option to use the encoder:

await new Promise(done =>
  db.batch()
    .put({ message: "john" }, "adams")
    .put({ message: "james" }, "kirk")
    .write({ keyEncoding: fooEncoding }, done));

then iterate:

let it = db.iterator({ keyEncoding: fooEncoding });

while (true) {
  let item = await new Promise((r, x) => {
    it.next((err, key, value) => {
      if (err == null && key === undefined && value === undefined) r();
      if (err != null) x(err);
      r({ key: key, value: value });
    });
  });

  if (item == undefined) break;

  console.log(item);
}

keys is stored as "[object Object]" for both.

If you go through put directly:

  await new Promise((r, x) =>
    db.put(
      { message: "john" }, "adams",
      { keyEncoding: fooEncoding },
      (err) => { if (err) x(err); r() }
    ));

  await new Promise((r, x) =>
    db.put(
      { message: "james" }, "kirk",
      { keyEncoding: fooEncoding },
      (err) => { if (err) x(err); r() }
    ));

OR, use the array batch version:

  await new Promise((r, x) =>
    db.batch([
      { type: "put", key: { message: "john" }, value: "adams" },
      { type: "put", key: { message: "james" }, value: "kirk" }],
      { keyEncoding: fooEncoding },
      (e) => ((e) ? x(e) : r())));

You get the expected result:

{ key: { message: 'james' }, value: 'kirk' }
{ key: { message: 'john' }, value: 'adams' }

@MeirionHughes MeirionHughes changed the title write Options ignored in batch write Options ignored in chained batch Sep 26, 2017
@ralphtheninja ralphtheninja self-assigned this Sep 26, 2017
@ralphtheninja ralphtheninja removed their assignment Jun 11, 2018
@vweevers
Copy link
Member

vweevers commented Jan 1, 2019

Discussing a general solution in Level/levelup#633.

@vweevers vweevers closed this as completed Jan 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants