Skip to content Skip to sidebar Skip to footer

Generating Uuids For Indexeddb Keys?

The W3C spec for IndexedDB defines a key generator as: A key generator generates a monotonically increasing numbers [sic] every time a key is needed. Now, it seems (to me) that a

Solution 1:

This is a pretty charged question but, if you want my thoughts on it after working with IndexedDB for a couple years, I'd say that a UUID API would be nice but it seems beyond scope of what the working group is trying to achieve.

A UUID is, eponymously, "universally" unique. The highest concept of uniqueness in IndexedDB is the object store, at the data level and the database at the browser level.

In practice, I like that I can get predictable, repeatable results when re-running code on a fresh database or object store. Although I can see situations where I would want to maintain uniqueness across all client dbs, I definitely wouldn't want it to be the default.

I personally don't require universal uniqueness with my clientside databases, but, in any case, there are great UUID JS implementations available if you do.

Solution 2:

The objectStore keys are meant to be unique to the objectStore in the local database, and nothing else. Any higher level semantics about uniqueness are really beyond the scope of IndexedDB. You describe UUID, but even that is just one form of unique keys. There are lots of different ways to generate a UUID that have different properties, including a specific prefix combined with monotonically increasing lower-bits, randomly generating the whole thing, scoping it by ethernet card address, etc.

By picking something that is only scoped to the local instance, IDB (for better or worse) forces developers to make a careful choice about a "globally" unique key.

Post a Comment for "Generating Uuids For Indexeddb Keys?"