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

Checking if a key exists #233

Closed
raineorshine opened this issue Mar 19, 2023 · 1 comment
Closed

Checking if a key exists #233

raineorshine opened this issue Mar 19, 2023 · 1 comment

Comments

@raineorshine
Copy link
Contributor

raineorshine commented Mar 19, 2023

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 #156 (comment) are counter-intuitive.

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

@vweevers
Copy link
Member

See Level/community#106 (and give it a thumbs up). PS. The referenced #156 (comment) is for checking whether the db is empty, rather than checking whether a key exists.

As for undefined, see Level/abstract-level#49 (will happen in abstract-level v2).

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

2 participants