Last active
February 2, 2019 20:57
-
-
Save markerikson/5a1793b494c1309383a9db98d2f9320a to your computer and use it in GitHub Desktop.
Redux-ORM nested data normalization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {fk, many, Model} from 'redux-orm'; | |
class Book extends Model { | |
static get fields() { | |
authors: many('Author', 'books'), | |
publisher: fk('Publisher', 'books'), | |
} | |
static parse(data) { | |
/* | |
I call SomeModel.parse(jsonData), and that Model class should know what relational fields it has and | |
call AnotherModel.parse() appropriately on down the chain. Each parse method should eventually call the | |
static Model.create() method with the JSON data for that instance. That queues up all the Redux-ORM | |
creation behavior , and afterwards you call session.reduce() to produce the updated entities "tables" | |
*/ | |
const {Author, Publisher} = this.session; | |
const clonedData = {...data}; | |
clonedData.authors = clonedData.authors.map(author => Author.parse(author)); | |
clonedData.publisher = Publisher.parse(clonedData.publisher); | |
return this.create(clonedData); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment