blinkdb-js / blinkdb

🗃️ An in-memory JS database optimized for large scale storage on the frontend.
https://blinkdb.io
MIT License
119 stars 10 forks source link

removeMany stops mid-remove when non-existent keys are passed #29

Closed retorquere closed 1 year ago

retorquere commented 1 year ago

I'm pretty sure I must be missing something dead obvious, but I have a script where I do two removeManys that should have identical effect (I think) that don't in practice. Sorry for the newbie questions, but I've been trying to figure out all day what I'm doing wrong, and I just don't see it.

const { createDB , createTable, BlinkKey, insertMany, removeMany, clear, many, key } = require( "blinkdb")

const blinkdb = createDB({ clone: true })
let table = createTable(blinkdb, "users")({
  primary: "itemID",
  indexes: ["itemKey", "libraryID", "citationKey"]
})

const data = [
  {
    "citationKey": "shelahStableTheories1969",
    "itemID": 2,
    "itemKey": "6STQAJ5F",
    "libraryID": 1,
    "pinned": false
  },
  {
    "citationKey": "shelahNoteMinmaxProblem1969",
    "itemID": 4,
    "itemKey": "KG9BBSIH",
    "libraryID": 1,
    "pinned": false
  },
]

const deletes = [
  { "itemID": 2 },
  { "itemID": 5 },
  { "itemID": 4 },
]

async function main() {
  await insertMany(table, data)

  // this works partially
  await removeMany(table, deletes)
  console.log('remaining:', (await many(table)).length)
  console.log('ghosts:', (await many(table, { where: { itemID: { in: deletes.map(d => d.itemID) } } })).length)

  await clear(table)
  await insertMany(table, data)
  // this does work
  deletes.sort((a, b) => a.itemID - b.itemID)
  await removeMany(table, deletes)
  console.log('remaining:', (await many(table)).length)
  console.log('ghosts:', (await many(table, { where: { itemID: { in: deletes.map(d => d.itemID) } } })).length)
}

main().catch(err => console.log(err))
retorquere commented 1 year ago

Trimming the deletes to the effective deletes does work. Does removeMany not tolerate non-existent keys to be passed?

retorquere commented 1 year ago

Looks like it doesn't. I had not expected this behavior. Does that mean I should always run a many query to detect existing items before passing ids to removeMany? This seems fairly counterintuitive to me.

retorquere commented 1 year ago

Worse -- it does a partial delete until it finds an ID it doesn't know and then stops (I've changed the script to show this, but the code I linked to makes it clear it will). Shouldn't that return false just be a continue?

maradotwebp commented 1 year ago

Yep, that's an error. Deleting an ID that does not exist within the table should be a noop (i.e. your line should be a continue instead).

Would you mind making a PR for this? :)

retorquere commented 1 year ago

https://github.com/blinkdb-js/blinkdb/pull/30

retorquere commented 1 year ago

Any indication when this could be out in a new release? I'm mid-migration to blinkdb in a tool I'm making and this is a showstopper for me.

maradotwebp commented 1 year ago

closed in #30.

maradotwebp commented 1 year ago

Any indication when this could be out in a new release? I'm mid-migration to blinkdb in a tool I'm making and this is a showstopper for me.

See https://github.com/blinkdb-js/blinkdb/releases/tag/v0.14.0.