Redux Organizing Normalized Data in State - redux

Been playing around with adding state management to my application, and was hoping for a little clarification on best practices for organizing normalized data.
Based on the documentation in the Redux Docs a common pattern is to organize things like "authors" and "books" under "entities" inside your state.
However, most examples that I've come by doesn't fit this pattern. It seems as though a common practice is placing "authors" and "books" on their own inside the state rather than within "entities".
Just hoping to get some clarification on this, and an understanding of how to get "authors" and "books" within "entities" to fit the structure of the example docs.

I wrote that whole "Structuring Reducers" docs section, so I'm gonna say I'm qualified to answer :)
Putting all the different normalized data under an entities key is just a suggestion. If you prefer to put each item type at the top of your state tree, that's fine and up to you.
A number of my suggestions in the normalization docs pages are based off of how the Redux-ORM library organizes things. It generates a reducer function that defines all the "tables" for your different item types, so that reducer function usually becomes a slice in your root reducer (and is usually named either orm or entities).
For more info on using Redux-ORM, you might want to read through my "Practical Redux" tutorial series, which shows a number of techniques for using Redux-ORM, and demonstrates its usage in a sample app.

Related

Is there any tool or example of creating a custom createEntityAdapter for a flat object instead of an array of entities?

createEntityAdapter is a fantastic encapsulation of tooling to work with redux toolkits createSlice however it only works with arrays of entities. I like that it comes with all the reducers I would want and with selectors.
What I want is the same but for a flat object. So if my slice was just a flat object that I could create a similar adapter but with a singular update reducer and selectors for each element of the state object. Does anyone know of tooling, links, examples of anything like this?
I attempted to create this myself but am having difficulties creating something similar to createDraftSafeSelector however it is still looking at everything as arrays. Also this is all in Typescript so any type help on this would be much appreciated.

Add same child object in different parents firebase

I am trying to add objects into firebase real time database, so far, i have been creating the same object for different parents. My question is how to save the same object in different parents. So far I am not saving with code, just in firebase console.
Pic to help understand.
This is the structure Pais>Belgica>Partidos>2>Estadio and this stadium is Spartak Stadium
And I want to add the same Stadium that is in Belgica into my Tunez>Partidos>Estadio>2 with out creating a new Object, is there a way to tell firebase that this object is the same as the on in Belgica. Thanks in advance.
There is no real way to relate something elsewhere in the database to
what you are doing unless you save it yourself in both places.
However, there is another option. Cloud Functions are extremely powerful tools and are fantastic for data consistency. You can take a look at some of the samples Firebase has put out there for just this instance:
https://github.com/firebase/functions-samples/blob/master/README.md

Redux, I can modify the state - it is not read only, outside of an action dispatch?

Perhaps someone can shine some light here for me.
I have been doing redux, and just was reading about and I read "State is read only". Ok, I figured that was the case - but let me try.
So, in my component I wrap it in a connect and what have you and now I have access to the state... so, I did this, in the render method.
this.props.state.MYSTORE = {}
Well, that actually killed my store. I get the concept of pure functions, but this idea that the 'state is read only' is not entirely true. I figured it would kill just this instance of the store (passed into my component), but the actual store is kaboomed!! I'm a little confused with "state is read only" - clearly, it is not.
What is actually read only? I get that you set state via action creators, but that doesn't forbide bad practice. Just like in a regular component you use "setState", BUT you could just make an assignment on the state tree...
Much of Redux's behavior is expectations and convention over an absolute enforcement. If you're using plain objects for your Redux state, technically you can mutate it directly if you want (but it's almost always a very bad idea!).
Beyond that, Redux is primarily a way to organize all the "write" logic for your application into a single structured approach, rather than having random writes scattered throughout your codebase.
If you do want additional assistance to make sure you don't ever actually mutate your state accidentally, there's several tools you can use. The DevTools#Linting page of my Redux addons catalog lists some plugins that will warn you if you accidentally mutated the state, although those obviously should only be used in development. You can also use a specific immutable data library to either "freeze" your plain objects or give you a specialized set of data structures that wrap up your state contents.

OO design: should collections always be stored?

I've wondered this in many different situations, so here I come, looking for the experts knowledge.
Let's say I have to model something that requires a collection. A simple example: an application that stores famous quotes along with their author and a set of tags or keywords. The user should be able to enter a tag or keyword and get matching quotes for it.
My question is: do I really need a class that contains my collection of quotes? Something like this:
Or would this also be correct?
I'm asking this in the more abstract way possible (after all, UML should never depend on the implementation).
I've always thought the second example (just 1 class) was incorrect, but now I'm thinking that maybe the user can press a button on some interface and that button executes some code that gets a quote stored somewhere, and the second example would also be correct?
Basically, should I always have a collection stored somewhere, even if the storing class does nothing else but just store the collection (and provide the methods to access it)?
I definitelly prefer only one class, if there is no strong reason to have another container class (especially on abstract conceptual level). Then I add the collection methods as static functions. A separate container class would only bring more complexity, more dependencies and doubts like yours. :) Doubts often indicate the lack of a real need. When you really need something, you know it.
Here an example with some explanations. I find it simple, clear, elegant and abstract, meaning non-restrictive, easy to transform to any implementation you like:
When it comes to relationships of this class to other class, then you actially have your collection, without introducing new class. This diagram shows two examples. "Other class" actually sees a collection "quotes" which is ordered, like Vector. "One more class" also has a collection of Quotes with different characteristics.
Later on implementation level you can implement it directly like this or eventually adding a Factory or Container class, according to concrete, implemention resctrictions and special reqs.

What are the rules for deciding when a new catalog should be created?

I'd like to learn about using catalogs correctly.
I have about 30 useful content types, about 50 indexes in catalog.xml, and about 45 metadatas. There are just three types which account for most of the site's data - and I may need millions of these. I've been reading, and there's lots to do, but I want to have the basic configuration right before I begin all that.
This page told me that any non-default indexes should not be added to the portal_catalog. I've even read people explaining how removing one, or two of the default indexes makes a performance difference.
My question is: what are the rules for dividing up the indexes into different catalogs, and for selecting which catalog(s) index which type(s)?
So far I have created one additional catalog, used to catalog all indexes for my 'site-setup' objects (which I have caused to no longer be indexed in portal_catalog). The site-setup indexes are very often used, but more rarely modified than others, so I thought it was correct to separate them from objects which are reindexed more often. I'm not sure if that's the main consideration though.
Another similar question (a good example of the kind of thing I want to solve): how would you handle something like secondary workflow review_state variables? I give each workflow's review_state variable an index (and search on them quite often), but some of my workflows are only used on just a few types. (my most prolific objects have secondary workflows...)
I'd be very grateful for advice!
Campbell
This won't cover everything but I'll bring up some points..
Anything not in the portal_catalog won't work with collections, folder_contents view, getFolderContents method, search, portlet collections, related items(I think) and anything else the assumes you're using the portal_catalog.
I like to use an additional catalog when I need to be able to query the data but it only affects a sub-set of the content objects.
Use collective.indexing to speed up indexing operations.
Mount the catalogs on their own mount points so you can cache them differently from the rest of the site(so you can cache the whole catalog). Then, you can even serve the the catalogs from dedicated zeoserver.
Also, if your content doesn't have to be cataloged by the portal_catalog(with all the constraints listed), you may even want to think about if you need it as a full-fledged (archetype|dexterity) type in the first place. You can use a more slim repoze.catalog to catalog arbitrary objects(which could be very simple data) for whatever your purpose is and get even more performance. Or better yet, look into Solr for indexing it for VERY good performance.
On more thing, depending on the type of data you're storing, you could even look into using a relational database for a data store. But I don't know what kind of queries, indexes, data, etc you have...
30 different types seems like a lot but I don't know what your use case is. Care to share? Perhaps there is a better way to do it.

Resources