Permalink
Browse files

Update Redux-ORM usage to work with 0.9.x

Replaced "schema" object with "orm"
Used `getEmptyState()` instead of `getDefaultState()`
Removed uses of `session.reduce()` - now just `return session.state`
Replaced `withModels` with `toModelArray()`
Updated Model classes to define attributes and use `upsert()`
  • Loading branch information...
markerikson committed Jul 1, 2017
1 parent 2dd6024 commit 3c1994530b18c3195c95cf9e59c9b159a873209f
@@ -1,7 +1,7 @@
import {createReducer} from "common/utils/reducerUtils";
import schema from "app/schema";
const defaultEditingEntities = schema.getDefaultState();
import orm from "app/schema";
const defaultEditingEntities = orm.getEmptyState();
export default createReducer(defaultEditingEntities, {
});
@@ -2,13 +2,13 @@ import {createReducer} from "common/utils/reducerUtils";
import {DATA_LOADED} from "features/tools/toolConstants";
import schema from "app/schema"
import orm from "app/schema"
const initialState = schema.getDefaultState();
const initialState = orm.getEmptyState();
export function loadData(state, payload) {
// Create a Redux-ORM session from our entities "tables"
const session = schema.from(state);
const session = orm.session(state);
// Get a reference to the correct version of model classes for this Session
const {Pilot, MechDesign, Mech} = session;
@@ -17,17 +17,17 @@ export function loadData(state, payload) {
// Clear out any existing models from state so that we can avoid
// conflicts from the new data coming in if data is reloaded
[Pilot, Mech, MechDesign].forEach(modelType => {
modelType.all().withModels.forEach(model => model.delete());
session.state = session.reduce();
modelType.all().toModelArray().forEach(model => model.delete());
});
// Queue up creation commands for each entry
// Immutably update the session state as we insert items
pilots.forEach(pilot => Pilot.parse(pilot));
designs.forEach(design => MechDesign.parse(design));
mechs.forEach(mech => Mech.parse(mech));
// Apply the queued updates and return the updated "tables"
return session.reduce();
// Return the new "tables" object containing the updates
return session.state;
}
View
@@ -1,10 +1,10 @@
import {Schema} from "redux-orm";
import {ORM } from "redux-orm";
import Pilot from "features/pilots/Pilot";
import MechDesign from "features/mechs/MechDesign";
import Mech from "features/mechs/Mech";
const schema = new Schema();
schema.register(Pilot, MechDesign, Mech);
const orm = new ORM();
orm.register(Pilot, MechDesign, Mech);
export default schema;
export default orm;
@@ -1,6 +1,6 @@
import {createReducer} from "common/utils/reducerUtils";
import schema from "app/schema";
import orm from "app/schema";
import {
createEntity,
@@ -42,7 +42,7 @@ export function copyEntity(sourceEntities, destinationEntities, payload) {
export function updateEditedEntity(sourceEntities, destinationEntities, payload) {
// Start by reading our "work-in-progress" data
const readSession = schema.from(sourceEntities);
const readSession = orm.session(sourceEntities);
const {itemType, itemID} = payload;
@@ -51,7 +51,7 @@ export function updateEditedEntity(sourceEntities, destinationEntities, payload)
// We of course will be updating our "current" relational data
let writeSession = schema.from(destinationEntities);
let writeSession = orm.session(destinationEntities);
const ModelClass = writeSession[itemType];
@@ -62,15 +62,14 @@ export function updateEditedEntity(sourceEntities, destinationEntities, payload)
if(existingItem.updateFrom) {
// Each model class should know how to properly update itself and its
// relations from another model of the same type. Ask the original model to
// update itself based on the "work-in-progress" model, which queues up a
// series of immutable add/update/delete actions internally
// update itself based on the "work-in-progress" model. Redux-ORM will apply
// those changes as we go, and update `session.state` immutably.
existingItem.updateFrom(model);
}
}
// Immutably apply the changes and generate our new "current" relational data
const updatedEntities = writeSession.reduce();
return updatedEntities;
// Return the updated "current" relational data.
return writeSession.state;
}
@@ -1,11 +1,11 @@
import {createSelector} from "reselect";
import schema from "app/schema";
import orm from "app/schema";
export const selectEditingEntities = state => state.editingEntities;
export const getEditingEntitiesSession = createSelector(
selectEditingEntities,
editingEntities => schema.from(editingEntities)
editingEntities => orm.session(editingEntities)
);
@@ -1,4 +1,4 @@
import schema from "app/schema";
import orm from "app/schema";
import {getModelByType} from "common/utils/modelUtils";
export function updateEditingEntitiesState(state, updatedEditingEntities) {
@@ -16,7 +16,7 @@ export function updateEntitiesState(state, updatedEntities) {
}
export function readEntityData(entities, itemType, itemID) {
const readSession = schema.from(entities);
const readSession = orm.session(entities);
// Look up the model instance for the requested item
const model = getModelByType(readSession, itemType, itemID);
@@ -6,58 +6,50 @@ import {
import {createConditionalSliceReducer} from "common/utils/reducerUtils";
import schema from "app/schema";
import orm from "app/schema";
export function updateEntity(state, payload) {
const {itemType, itemID, newItemAttributes} = payload;
const session = schema.from(state);
const session = orm.session(state);
const ModelClass = session[itemType];
let newState = state;
if(ModelClass.hasId(itemID)) {
const modelInstance = ModelClass.withId(itemID);
modelInstance.update(newItemAttributes);
newState = session.reduce();
}
return newState;
return session.state;
}
export function deleteEntity(state, payload) {
const {itemID, itemType} = payload;
const session = schema.from(state);
const session = orm.session(state);
const ModelClass = session[itemType];
let newState = state;
if(ModelClass.hasId(itemID)) {
const modelInstance = ModelClass.withId(itemID);
// The session will immutably update its state reference
modelInstance.delete();
// Immutably apply updates and return the new entities structure
newState = session.reduce();
}
return newState;
// This will either be the original state object or the updated one
return session.state;
}
export function createEntity(state, payload) {
const {itemType, newItemAttributes} = payload;
const session = schema.from(state);
const session = orm.session(state);
const ModelClass = session[itemType];
ModelClass.parse(newItemAttributes);
const newState = session.reduce();
return newState;
return session.state;
}
@@ -1,10 +1,10 @@
import {createSelector} from "reselect";
import schema from "app/schema";
import orm from "app/schema";
export const selectEntities = state => state.entities;
export const getEntitiesSession = createSelector(
selectEntities,
entities => schema.from(entities)
entities => orm.session(entities)
);
@@ -1,16 +1,17 @@
import {Model, fk} from "redux-orm";
import {Model, fk, attr} from "redux-orm";
export default class Mech extends Model {
static get fields() {
return {
id : attr(),
type : fk("MechDesign"),
pilot : fk("Pilot"),
};
}
static parse(mechData) {
return this.create(mechData);
return this.upsert(mechData);
}
toJSON() {
@@ -1,8 +1,15 @@
import {Model} from "redux-orm";
import {Model, attr} from "redux-orm";
export default class MechDesign extends Model {
static get fields() {
return {
id : attr(),
};
}
static parse(designData) {
return this.create(designData);
return this.upsert(designData);
}
toJSON() {
@@ -15,7 +15,7 @@ const mapState = (state) => {
const session = getEntitiesSession(state);
const {Mech} = session;
const mechs = Mech.all().withModels.map(mechModel => mechModel.getId());
const mechs = Mech.all().toModelArray().map(mechModel => mechModel.getId());
const currentMech = selectCurrentMech(state);
@@ -1,9 +1,15 @@
import {Model, fk} from "redux-orm";
import {Model, fk, attr} from "redux-orm";
export default class Pilot extends Model {
static get fields() {
return {
id : attr(),
name : attr(),
rank : attr(),
gunnery : attr(),
piloting : attr(),
age : attr(),
mech : fk("Mech"),
};
}
@@ -15,7 +21,7 @@ export default class Pilot extends Model {
// Note that in a static class method, `this` is the
// class itself, not an instance
return this.create(pilotData);
return this.upsert(pilotData);
}
toJSON() {
@@ -99,12 +99,10 @@ export class PilotDetails extends Component {
}
onStartEditingClicked = () => {
const {id} = this.props.pilot;
this.props.startEditingPilot();
}
onStopEditingClicked = () => {
const {id} = this.props.pilot;
this.props.stopEditingPilot();
}
@@ -31,7 +31,7 @@ const mapState = (state) => {
// for each entry, rather than the plain JS objects.
// Extract a list of IDs for each Pilot entry
const pilots = Pilot.all().withModels.map(pilotModel => pilotModel.getId());
const pilots = Pilot.all().toModelArray().map(pilotModel => pilotModel.getId());
const currentPilot = selectCurrentPilot(state);

0 comments on commit 3c19945

Please sign in to comment.