Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thorsson/b0e4905aeae360d8a04af33db4de051e to your computer and use it in GitHub Desktop.
Save Thorsson/b0e4905aeae360d8a04af33db4de051e 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