Level / level

Universal abstract-level database for Node.js and browsers.
MIT License
1.55k stars 106 forks source link

Checking if a key exists #233

Closed raineorshine closed 1 year ago

raineorshine commented 1 year ago

I was surprised to find that level does not have a has function to check if a key exists in the database. Map has a has function. localStorage.getItem returns ~undefined~ null. level throws a "Key not found error".

Checking if a key exists is a common thing to do when initializing values in a key-value store.

Currently I put db.get in a try-catch, but that feels awkward. The solutions suggested in https://github.com/Level/level/issues/156#issuecomment-527757444 are counter-intuitive.

So out of curiosity, why not have a db.has function, or return null from db.get?

vweevers commented 1 year ago

See https://github.com/Level/community/issues/106 (and give it a thumbs up). PS. The referenced https://github.com/Level/level/issues/156#issuecomment-527757444 is for checking whether the db is empty, rather than checking whether a key exists.

As for undefined, see https://github.com/Level/abstract-level/pull/49 (will happen in abstract-level v2).