Akita: Is there a way to create a store dynamically?

Created on 12 Oct 2018  路  13Comments  路  Source: datorama/akita

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[x] Feature request
[ ] Documentation issue or request
[x] Support request
[ ] Other... Please describe:

Is there a way to create a store dynamically using Akita? I get models from the backend and I want to create an entity store for these models in runtime. Something like createStore from redux. Thx :)

help wanted

All 13 comments

Can you give me an example, please?

Before getting hands with Akita, I checked the issues on the repo, and saw this one.
I didn't have in mind a real case scenario or a reason of why I would need this, although this sounded interesting to have it into consideration when choosing between _angular-ngrx-data, ngxs_ or just _ngrx entities_.

Now today I got into a design dilemma that just takes this into consideration...

Let's say we have a _Comparing-App_ that has 2 views/components that show/list the same kind of items, for example, cars.
Those items may come from different source and may have different additional internal details/data on each item, but each of the 2 view/components would render common properties of each item.

So in this case, we can have 2 options:

  1. We can either create code for 2 store classes with different store names, or
  2. Handle by hand 2 lists of ids in the store as additions of the base CarStore's EntityState.

The 2nd option would leave useless some of the included functionality offered oriented to handling entities, as the store focuses on having 2 collections of the same entity. In this case we may even consider just handling by hand a simple Store instead of a EntityStore for this needs.

Instead, _I would prefer having a 3rd option of dynamically instantiating another CarStore_ with a different name, so we could easily handle cars for each view component individually, even facilitating adding a third source and a comparing view.

Hope this case may serve as an example.

I'm not sure what you mean. If you have one Entity, for example, a Car, why do you need two stores? I need a concrete example, please.

may have different additional internal details/data on each item

If each entity is still a Car, it should be in a CarsStore.

for example, I have several dozen entities and one dynamically generated view. the number of entities is not known in advance. the backend sends the entity description. I need to create an entity store dynamically at runtime using this description.
In redux i can do it using createStore function.

Can you send me an example of a JSON structure so that I could understand better the use case?

I made change changes to make it possible to set different names to store instances at creation time (on the constructor). This is optional, and would default to the name on the @StoreConfig() name.
It also supports passing a function, so the name can be set based on the @StoreConfig() name.

I didn't ended using it by the moment, but the tests and my POC using it worked correctly.

Somehow it feels kind of an antipattern, as the Stores classes are intended to be used as singletons.
Nevertheless it may serve anyone that requires a sister Store or a clone Store, so I think it would be useful to support this.

Documentation update on the GitHub pages is what it would be missing in case this get considered for merging.

@edmundo096 that's nice, but I can't see where it can be useful.

Somehow it feels kind of an anti-pattern, as the Stores classes are intended to be used as singletons.

Agree.

If will see that for some good reason, others need this functionality, we'll add it. Thanks.

@zvnt

I need to create an entity store dynamically at runtime using this description.

Can you send an example of this description?

I think this can be used for sub-collection. ex) Firebase's Firestore. Sub-collection can only be obtained by knowing the parent's collection name and id. And each parent id must be stored separately.

Or, if there is a way to effectively save a sub-collection in Firestore through Akita, please let me know
.
Thank you for making a good library.

@dminine thanks, but I still need a concrete example of a JSON structure to understand the use case.

Closed due to inactivity.

I am wondering did u find your solution? I have the same question

Was this page helpful?
0 / 5 - 0 ratings