Skip to content

Commit

Permalink
Update Redux-ORM usage to work with 0.9.x
Browse files Browse the repository at this point in the history
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 12, 2017
1 parent 2dd6024 commit 3c19945
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 55 deletions.
4 changes: 2 additions & 2 deletions src/app/reducers/editingEntitiesReducer.js
@@ -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, {
});
16 changes: 8 additions & 8 deletions src/app/reducers/entitiesReducer.js
Expand Up @@ -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;

Expand All @@ -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;
}


Expand Down
8 changes: 4 additions & 4 deletions src/app/schema/schema.js
@@ -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;
15 changes: 7 additions & 8 deletions src/features/editing/editingReducer.js
@@ -1,6 +1,6 @@
import {createReducer} from "common/utils/reducerUtils";

import schema from "app/schema";
import orm from "app/schema";

import {
createEntity,
Expand Down Expand Up @@ -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;

Expand All @@ -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];

Expand All @@ -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;
}


Expand Down
4 changes: 2 additions & 2 deletions src/features/editing/editingSelectors.js
@@ -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)
);
4 changes: 2 additions & 2 deletions src/features/editing/editingUtils.js
@@ -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) {
Expand All @@ -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);
Expand Down
26 changes: 9 additions & 17 deletions src/features/entities/entityReducer.js
Expand Up @@ -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;
}


Expand Down
4 changes: 2 additions & 2 deletions src/features/entities/entitySelectors.js
@@ -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)
);
5 changes: 3 additions & 2 deletions src/features/mechs/Mech.js
@@ -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() {
Expand Down
11 changes: 9 additions & 2 deletions src/features/mechs/MechDesign.js
@@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/features/mechs/MechsList/MechsList.jsx
Expand Up @@ -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);

Expand Down
10 changes: 8 additions & 2 deletions src/features/pilots/Pilot.js
@@ -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"),
};
}
Expand All @@ -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() {
Expand Down
2 changes: 0 additions & 2 deletions src/features/pilots/PilotDetails/PilotDetails.jsx
Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion src/features/pilots/PilotsList/PilotsList.jsx
Expand Up @@ -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);

Expand Down

0 comments on commit 3c19945

Please sign in to comment.