Skip to content

Instantly share code, notes, and snippets.

@markerikson
Last active February 2, 2019 20:57
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markerikson/5a1793b494c1309383a9db98d2f9320a to your computer and use it in GitHub Desktop.
Save markerikson/5a1793b494c1309383a9db98d2f9320a to your computer and use it in GitHub Desktop.
Redux-ORM nested data normalization
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