cyrilletuzi / angular-async-local-storage

Efficient client-side storage for Angular: simple API + performance + Observables + validation
MIT License
676 stars 68 forks source link

Feature: Map-like API to allow advanced queries in local storage #54

Closed cyrilletuzi closed 5 years ago

cyrilletuzi commented 5 years ago

This issue tracks the project of a full query API.

Design

The API with all methods needs to be designed. It would be better to be inspired by an existing implementation (dexis.js seems to be the reference for indexedDb).

Already requested:

Please comment with all methods that would be useful in your projects.

Warning: each method should also work when in localStorage fallback scenarios, and performance should be checked in this case.

Implementation

As we're going towards a full RxJS adaptation of indexedDb, it would be better to be done in a framework agnostic new lib, which would be used by this one.

Maybe it already exists?

cyrilletuzi commented 5 years ago

@robwormald @brandonroberts What's the status of ngrx/db? Why is it not maintained anymore?

@benlesh Are you aware of any existing and maintained implementation of indexedDb in RxJS?

Thanks for your feedback guys.

frankvdb7 commented 5 years ago

This library seems to be well maintained and highly compatible: https://github.com/pubkey/rxdb/blob/master/README.md

cyrilletuzi commented 5 years ago

@fmavdb Seen this one. Seems very good but I think it goes too far for the purpose here.

cyrilletuzi commented 5 years ago

Started the work about this and realized the requested operations are currently not possible because the lib is just missing a few low-level methods. Adding them would allow everything, without the lib having to provide an advanced query API.

As the idea of the lib is to imitate the native localStorage API, the missing things would be:

I've done the implementation, and it works, but clearly it's not a good solution because:

A better solution would be to imitate the Map API, with methods like keys().

Problem is now how to handle this, so the API stays consistent. I see several scenarios possible:

  1. Just add missing keys() and other Map-like methods in the current service

    • pros: no refactoring
    • cons: confusing API where some methods names are inspired from localStorage and others from Map
  2. Add missing keys() but also methods for already existing operations like get(), set() and keep the old ones (getItem, setItem...)

    • pros: no refactoring
    • cons: confusing API where there would be two methods doing the same thing
  3. Add missing keys() but also methods for already existing operations like get(), set() and deprecate the old ones (getItem, setItem...)

    • pros: consistent API
    • cons: refactoring needed (and v7 already introduced important refactoring)
  4. Keep the existing LocalStorage service as is, and add a new LocalStorageMap one with Map-like methods

    • pros: no duplication for the same operation
    • cons: confusing API as there would be two services doing partly the same thing

@fmavdb @juristr @dphochman @rajd90 What do you think?

cyrilletuzi commented 5 years ago

.keys() method and .size property have been added in 7.1.0-beta.0. To test:

npm install @ngx-pwa/local-storage@beta

Feedback welcome.

cyrilletuzi commented 5 years ago

v7.1.0 released with:

Documentation here.

cyrilletuzi commented 5 years ago

For info, there is currently a compatibility issue for has() and keys() in IE/Edge, see #69