funkensturm / ember-local-storage

The addon provides a storageFor computed property that returns a proxy and persists the changes to localStorage or sessionStorage. It ships with an ember-data adapter.
https://www.funkensturm.com/ember-local-storage/
MIT License
218 stars 76 forks source link

[Feature Request] Configurable storage key for Adapter #229

Closed mnoble01 closed 7 years ago

mnoble01 commented 7 years ago

I notice you can specify the localStorage key by using storageFor('my-key'). I don't see a similar way of specifying the localStorage key when using the ember-local-storage Adapter/Serializer. Instead, the Adapter uses index-${modelName}.

  1. Is this already possible and I'm missing it?
  2. If not, do you think this is a reasonable addition?

I am available to take a shot at implementing this, but I wanted to check in first and possibly get a few pointers. Thanks!

mnoble01 commented 7 years ago

I also wonder if you have considered storing models of the same type under the same key, like 'index-${modelName}s': "[{'id': 1}, {'id': 2}]".

I'm thinking of a possible case in the feature request detailed above where I specify a key that's not per-model specific (i.e. doesn't contain its id or another unique property). What would happen if you went to store a second model at a given key? Clobber the existing model stored at that key, or intelligently store both at the same key?

fsmanuel commented 7 years ago

@mnoble01 sorry for the late response.

To answer your questions. No it's not possible to change the index or record key. I never had the need to do that. What is your use case?

The problem with storing all models under the same key is that if your dataset grows it gets slow on de-/serialization. It's actually the reason why I wrote the addon and moved away from ember-localstorage-adapter.

The idea of an adapter at least in ember-data land is that you have a model with a type and a key. I'm curious what you have in mind. What do you want to achieve? How do you want to store them both site by site? What do you get back if you access the key? Both models?

mnoble01 commented 7 years ago

What is your use case?

My use case is actually no longer a use case as I was attempting to use a Model hammer to solve a storageFor nail. I have some old user preferences in non-Ember that I was storing at a particular localStorage key and I'm looking to move them into Ember. storageFor is actually more appropriate for this case as there's no database representation of preferences.

The problem with storing all models under the same key is that if your dataset grows it gets slow on de-/serialization.

This makes perfect sense.

What do you want to achieve?

I wanted to have full control over the prefix for the localStorage key, but I realize that it's not needed if you want the LSAdapter to "just work."

Initially, the default localStorage key of "index-${modelName}" confused me. What is "index"?

fsmanuel commented 7 years ago

I have some old user preferences in non-Ember that I was storing at a particular localStorage key and I'm looking to move them into Ember. storageFor is actually more appropriate for this case as there's no database representation of preferences.

That sounds like a storageFor use case.

The index-${modelName} keys are there to fetch all records of a model type. If you use .findAll('post') the adapter needs to know the keys of the records otherwise it would have to run over all keys in localStorage and collect what it needs.

fsmanuel commented 7 years ago

@mnoble01 Can we close the issue?