A small, simple and immutable ORM to manage relational data in your Redux store.
See a a guide to creating a simple app with Redux-ORM (includes the source). Its README is not updated for 0.9 yet but the code has a branch for it.
The 0.9 which is in the release candidate stage, brings big breaking changes to the API. Please look at the migration guide if you're migrating from earlier versions.
Looking for the 0.8 docs? Read the old README.md in the repo. For the API reference, clone the repo, npm install
, make build
and open up index.html
in your browser. Sorry for the inconvenience.
API can be unstable until 1.0.0. Minor version bumps before 1.0.0 can and will introduce breaking changes. They will be noted in the changelog.
redux-orm-proptypes
: React PropTypes validation and defaultProps mixin for Redux-ORM Models
npm install redux-orm --save
Or with a script tag
<script src="https://tommikaikkonen.github.io/redux-orm/dist/redux-orm.js"></script>
You can declare your models with the ES6 class syntax, extending from Model
. You need to declare all your non-relational fields on the Model, and declaring all data fields is recommended as the library doesn't have to redefine getters and setters when instantiating Models. redux-orm
supports one-to-one and many-to-many relations in addition to foreign keys (oneToOne
, many
and fk
imports respectively). Non-related properties can be accessed like in normal JavaScript objects.
// models.js
import {fk, many, attr, Model} from 'redux-orm';
class Book extends Model {
toString() {
return `Book: ${this.name}`;
}
// Declare any static or instance methods you need.
}
Book.modelName = 'Book';
// Declare your related fields.
Book.fields = {
id: attr(), // non-relational field for any value; optional but highly recommended
name: attr(),
authors: many('Author', 'books'),
publisher: fk('Publisher', 'books'),
};
Defining fields on a Model specifies the table structure in the database for that Model. In order to generate a description of the whole database's structure, we need a central place to register all Models we want to use.
An instance of the ORM class registers Models and handles generating a full schema from all the models and passing that information to the database. Often you'll want to have a file where you can import a single ORM instance across the app, like this:
// orm.js
import { ORM } from 'redux-orm';
import { Book, Author, Publisher } from './models';
const orm = new ORM();
orm.register(Book, Author, Publisher);
export default orm;
You could also define and register the models to an ORM instance in the same file, and export them all.
Now that we've registered Models, we can generate an empty database state. Currently that's a plain, nested JavaScript object that is structured similarly to relational databases.
// index.js
import orm from './orm';
const emptyDBState = orm.getEmptyState();
When we have a database state, we can start an ORM session on that to apply updates. The ORM instance provides a session
method that accepts a database state as it's sole argument, and returns a Session instance.
const session = orm.session(emptyDBState);
Session-specific classes of registered Models are available as properties of the session object.
const Book = session.Book;
Models provide an interface to query and update the database state.
Book.withId(1).update({ name: 'Clean Code' });
Book.all().filter(book => book.name === 'Clean Code').delete();
Book.hasId(1)
// false
The initial database state is not mutated. A new database state with the updates applied can be found on the state
property of the Session instance.
const updatedDBState = session.state;
To integrate Redux-ORM with Redux at the most basic level, you can define a reducer that instantiates a session from the database state held in the Redux atom, then when you've applied all of your updates, you can return the next state from the session.
import { orm } from './models';
function ormReducer(dbState, action) {
const sess = orm.session(dbState);
// Session-specific Models are available
// as properties on the Session instance.
const { Book } = sess;
switch (action.type) {
case 'CREATE_BOOK':
Book.create(action.payload);
break;
case 'UPDATE_BOOK':
Book.withId(action.payload.id).update(action.payload);
break;
case 'REMOVE_BOOK':
Book.withId(action.payload.id).delete();
break;
case 'ADD_AUTHOR_TO_BOOK':
Book.withId(action.payload.bookId).authors.add(action.payload.author);
break;
case 'REMOVE_AUTHOR_FROM_BOOK':
Book.withId(action.payload.bookId).authors.remove(action.payload.authorId);
break;
case 'ASSIGN_PUBLISHER':
Book.withId(action.payload.bookId).publisher = action.payload.publisherId;
break;
}
// the state property of Session always points to the current database.
// Updates don't mutate the original state, so this reference is not
// equal to `dbState` that was an argument to this reducer.
return sess.state;
}
Previously Redux-ORM advocated for reducers specific to Models by attaching a static reducer
function on the Model class. If you want to define your update logic on the Model classes, you can specify a reducer
static method on your model which accepts the action as the first argument, the session-specific Model as the second, and the whole session as the third.
class Book extends Model {
static reducer(action, Book, session) {
switch (action.type) {
case 'CREATE_BOOK':
Book.create(action.payload);
break;
case 'UPDATE_BOOK':
Book.withId(action.payload.id).update(action.payload);
break;
case 'REMOVE_BOOK':
const book = Book.withId(action.payload);
book.delete();
break;
case 'ADD_AUTHOR_TO_BOOK':
Book.withId(action.payload.bookId).authors.add(action.payload.author);
break;
case 'REMOVE_AUTHOR_FROM_BOOK':
Book.withId(action.payload.bookId).authors.remove(action.payload.authorId);
break;
case 'ASSIGN_PUBLISHER':
Book.withId(action.payload.bookId).publisher = action.payload.publisherId;
break;
}
// Return value is ignored.
return undefined;
}
toString() {
return `Book: ${this.name}`;
}
}
To get a reducer for Redux that calls these reducer
methods:
import { createReducer } from 'redux-orm';
import { orm } from './models';
const reducer = createReducer(orm);
createReducer
is really simple, so I'll just paste the source here.
function createReducer(orm, updater = defaultUpdater) {
return (state, action) => {
const session = orm.session(state || orm.getEmptyState());
updater(session, action);
return session.state;
};
}
function defaultUpdater(session, action) {
session.sessionBoundModels.forEach(modelClass => {
if (typeof modelClass.reducer === 'function') {
modelClass.reducer(action, modelClass, session);
}
});
}
As you can see, it just instantiates a new Session, loops through all the Models in the session, and calls the reducer
method if it exists. Then it returns the new database state that has all the updates applied.
Use memoized selectors to make queries into the state. redux-orm
uses smart memoization: the below selector accesses Author
and AuthorBooks
branches (AuthorBooks
is a many-to-many branch generated from the model field declarations), and the selector will be recomputed only if those branches change. The accessed branches are resolved on the first run.
// selectors.js
import schema from './schema';
const authorSelector = schema.createSelector(session => {
return session.Author.map(author => {
// Returns a reference to the raw object in the store,
// so it doesn't include any reverse or m2m fields.
const obj = author.ref;
// Object.keys(obj) === ['id', 'name']
return Object.assign({}, obj, {
books: author.books.withRefs.map(book => book.name),
});
});
});
// Will result in something like this when run:
// [
// {
// id: 0,
// name: 'Tommi Kaikkonen',
// books: ['Introduction to redux-orm', 'Developing Redux applications'],
// },
// {
// id: 1,
// name: 'John Doe',
// books: ['John Doe: an Autobiography']
// }
// ]
Selectors created with createSelector
can be used as input to any additional reselect
selectors you want to use. They are also great to use with redux-thunk
: get the whole state with getState()
, pass the ORM branch to the selector, and get your results. A good use case is serializing data to a custom format for a 3rd party API call.
Because selectors are memoized, you can use pure rendering in React for performance gains.
// components.js
import PureComponent from 'react-pure-render/component';
import { authorSelector } from './selectors';
import { connect } from 'react-redux';
class App extends PureComponent {
render() {
const authors = this.props.authors.map(author => {
return (
<li key={author.id}>
{author.name} has written {author.books.join(', ')}
</li>
);
});
return (
<ul>
{authors}
</ul>
);
}
}
function mapStateToProps(state) {
return {
authors: authorSelector(state.db),
};
}
export default connect(mapStateToProps)(App);
Well, yeah. redux-orm
deals with related data, structured similar to a relational database. The database in this case is a simple JavaScript object database.
For simple apps, writing reducers by hand is alright, but when the number of object types you have increases and you need to maintain relations between them, things get hairy. ImmutableJS goes a long way to reduce complexity in your reducers, but redux-orm
is specialized for relational data.
Say we start a session from an initial database state situated in the Redux atom, update the name of a certain book.
First, a new session:
import { orm } from './models';
const dbState = getState().db; // getState() returns the redux state.
const sess = orm.session(dbState);
The session maintains a reference to a database state. We haven't updated the database state, therefore it is still equal to the original state.
sess.state === dbState
// true
Let's apply an update.
const book = sess.Book.withId(1)
book.name // 'Refactoring'
book.name = 'Clean Code'
book.name // 'Clean Code'
sess.state === dbState
// false.
The update was applied, and because the session does not mutate the original state, it created a new one and swapped sess.state
to point to the new one.
Let's update the database state again through the ORM.
// Save this reference so we can compare.
const updatedState = sess.state;
book.name = 'Patterns of Enterprise Application Architecture'
sess.state === updatedState
// true. If possible, future updates are applied with mutations. If you want
// to avoid making mutations to a session state, take the session state
// and start a new session with that state.
If possible, future updates are applied with mutations. In this case, the database was already mutated, so the pointer doesn't need to change. If you want to avoid making mutations to a session state, take the session state and start a new session with that state.
Just like you can extend Model
, you can do the same for QuerySet
(customize methods on Model instance collections). You can also specify the whole database implementation yourself (documentation pending).
The ORM abstraction will never be as performant compared to writing reducers by hand, and adds to the build size of your project (last I checked, minimizing the source files and gzipping yielded about 8 KB). If you have very simple data without relations, redux-orm
may be overkill. The development convenience benefit is considerable though.
See the full documentation for ORM here
Instantiation
const orm = new ORM(); // no arguments needed.
Instance methods:
register(...models: Array<Model>)
: registers Model classes to theORM
instance.session(state: any)
: begins a newSession
withstate
.
createReducer(orm: ORM)
: returns a reducer function that can be plugged into Redux. The reducer will return the next state of the database given the provided action. You need to register your models before calling this.createSelector(orm: ORM, [...inputSelectors], selectorFunc)
: returns a memoized selector function forselectorFunc
.selectorFunc
receivessession
as the first argument, followed by any inputs frominputSelectors
. Read the full documentation for details.
See the full documentation for Model
here.
Instantiation: Don't instantiate directly; use class method create
.
Class Methods:
hasId(id)
: returns a boolean indicating if entity with idid
exists in the state.withId(id)
: gets the Model instance with idid
.get(matchObj)
: to get a Model instance based on matching properties inmatchObj
,create(props)
: to create a new Model instance withprops
. If you don't supply an id, the newid
will beMath.max(...allOtherIds) + 1
.upsert(props)
: to create a new Model instance withprops
or to update a existing Model with sameid
- by other words its create or update behaviour.
You will also have access to almost all QuerySet instance methods from the class object for convenience.
Instance Attributes:
ref
: returns a direct reference to the plain JavaScript object representing the Model instance in the store.
Instance methods:
equals(otherModel)
: returns a boolean indicating equality withotherModel
. Equality is determined by shallow comparison of both model's attributes.set(propertyName, value)
: updatespropertyName
tovalue
. Returnsundefined
. Is equivalent to normal assignment.update(mergeObj)
: mergesmergeObj
with the Model instance properties. Returnsundefined
.delete()
: deletes the record for this Model instance in the database. Returnsundefined
.
Subclassing:
Use the ES6 syntax to subclass from Model
. Any instance methods you declare will be available on Model instances. Any static methods you declare will be available on the Model class in Sessions.
For the related fields declarations, either set the fields
property on the class or declare a static getter that returns the field declarations like this:
Declaring fields
:
class Book extends Model {
static get fields() {
return {
id: attr(),
name: attr(),
author: fk('Author'),
};
}
}
// alternative:
Book.fields = {
id: attr(),
name: attr(),
author: fk('Author'),
}
All the fields fk
, oneToOne
and many
take a single argument, the related model name. The fields will be available as properties on each Model
instance. You can set related fields with the id value of the related instance, or the related instance itself.
For fk
, you can access the reverse relation through author.bookSet
, where the related name is ${modelName}Set
. Same goes for many
. For oneToOne
, the reverse relation can be accessed by just the model name the field was declared on: author.book
.
For many
field declarations, accessing the field on a Model instance will return a QuerySet
with two additional methods: add
and remove
. They take 1 or more arguments, where the arguments are either Model instances or their id's. Calling these methods records updates that will be reflected in the next state.
When declaring model classes, always remember to set the modelName
property. It needs to be set explicitly, because running your code through a mangler would otherwise break functionality. The modelName
will be used to resolve all related fields.
Declaring modelName
:
class Book extends Model {
static get modelName() {
return 'Book';
}
}
// alternative:
Book.modelName = 'Book';
Declaring options
If you need to specify options to the redux-orm database, you can declare a static options
property on the Model class with an object key. Currently you can specify the id attribute name:
// This is the default value.
Book.options = {
idAttribute: 'id',
};
See the full documentation for QuerySet
here.
You can access all of these methods straight from a Model
class, as if they were class methods on Model
. In this case the functions will operate on a QuerySet that includes all the Model instances.
Instance methods:
toRefArray()
: returns the objects represented by theQuerySet
as an array of plain JavaScript objects. The objects are direct references to the store.toModelArray()
: returns the objects represented by theQuerySet
as an array ofModel
instances objects.count()
: returns the number ofModel
instances in theQuerySet
.exists()
: returntrue
if number of entities is more than 0, elsefalse
.filter(filterArg)
: returns a newQuerySet
representing the records from the parent QuerySet that pass the filter. ForfilterArg
, you can either pass an object thatredux-orm
tries to match to the entities, or a function that returnstrue
if you want to have it in the newQuerySet
,false
if not. The function receives a model instance as its sole argument.exclude
returns a newQuerySet
represeting entities in the parent QuerySet that do not pass the filter. Similarly tofilter
, you may pass an object for matching (all entities that match will not be in the newQuerySet
) or a function. The function receives a model instance as its sole argument.all()
returns a newQuerySet
with the same entities.at(index)
returns anModel
instance at the suppliedindex
in theQuerySet
.first()
returns anModel
instance at the0
index.last()
returns anModel
instance at thequerySet.count() - 1
index.delete()
deleted all entities represented by theQuerySet
.update(mergeObj)
updates all entities represented by theQuerySet
based on the supplied object. The object will be merged with each entity.
See the full documentation for Session here
Instantiation: you don't need to do this yourself. Use orm.session
.
Instance properties:
state
: the current database state in the session.
Additionally, you can access all the registered Models in the schema for querying and updates as properties of this instance. For example, given a schema with Book
and Author
models,
const session = orm.session(state);
session.Book // Model class: Book
session.Author // Model class: Author
session.Book.create({id: 5, name: 'Refactoring', release_year: 1999});
Minor changes before 1.0.0 can include breaking changes.
fix for filter/query #99
fix for many-many updates #136
- added 'upsert' method to Model (insert or update behaviour)
- major updates for tests
- CI integration
A lot. See the migration guide.
Adds UMD build to partially fix #41. You can now use or try out redux-orm
through a script tag:
<script src="https://tommikaikkonen.github.io/redux-orm/dist/redux-orm.js"></script>
redux-orm.js
will point to the master version of the library; If you need to stick to a version, make a copy or build it yourself.
Fixed bug that mutated the backend options in Model
if you supplied custom ones, see Issue 37. Thanks to @diffcunha for the fix!
Fixed regression in Model.prototype.update
Added babel-runtime to dependencies
Adds batched mutations. This is a big performance improvement. Previously adding 10,000 objects would take 15s, now it takes about 0.5s. Batched mutations are implemented using immutable-ops
internally.
Breaking changes:
-
Removed
indexById
option from Backend. This means that data will always be stored in both an array of id's and a map ofid => entity
, which was the default setting. If you didn't explicitly setindexById
tofalse
, you don't need to change anything. -
Batched mutations brought some internal changes. If you had custom
Backend
orSession
classes, or have overriddenModel.getNextState
, please check out the diff.
Breaking changes:
Model classes that you access in reducers and selectors are now session-specific. Previously the user-defined Model class reference was used for sessions, with a private session
property changing based on the most recently created session. Now Model classes are given a unique dummy subclass for each session. The subclass will be bound to that specific session. This allows multiple sessions to be used at the same time.
You most likely don't need to change anything. The documentation was written with this feature in mind from the start. As long as you've used the model class references given to you in reducers and selectors as arguments (not the reference to the model class you defined), you're fine.
Breaking changes:
- When calling
QuerySet.filter
orQuerySet.exclude
with an object argument, any values of that object that look like aModel
instance (i.e. they have agetId
property that is a function), will be turned into the id of that instance before performing the filtering or excluding.
E.g.
Book.filter({ author: Author.withId(0) });
Is equivalent to
Book.filter({ author: 0 });
Breaking changes:
- Model instance method
equals(otherModel)
now checks if the two model's attributes are shallow equal. Previously, it checked if the id's and model classes are equal. - Session constructor now receives a Schema instance as its first argument, instead of an array of Model classes (this only affects you if you're manually instantiating Sessions with the
new
operator).
Other changes:
- Added
hasId
static method to the Model class. It tests for the existence of the supplied id in the model's state. - Added instance method
getNextState
to the Session class. This enables you to get the next state without running model-reducers. Useful if you're bootstrapping data, writing tests, or otherwise operating on the data outside reducers. You can pass an options object that currently accepts arunReducers
key. It's value indicates if reducers should be run or not. - Improved API documentation.
- Fixed a bug that mutated props passed to Model constructors, which could be a reference from the state. I highly recommend updating from 0.3.1.
- API cleanup, see breaking changes below.
- Calling getNextState is no longer mandatory in your Model reducers. If your reducer returns
undefined
,getNextState
will be called for you.
Breaking changes:
- Removed static methods
Model.setOrder()
andBackend.order
. If you want ordered entities, use the QuerySet instance methodorderBy
. - Added helpful error messages when trying to add a duplicate many-to-many entry (Model.someManyRelated.add(...)), trying to remove an unexisting many-to-many entry (Model.exampleManyRelated.remove(...)), or creating a Model with duplicate many-to-many entry ids (Model.create(...)).
- Removed ability to supply a mapping function to QuerySet instance method
update
. If you need to record updates dynamically based on each entity, iterate through the objects withforEach
and record updates separately:
const authors = publisher.authors;
authors.forEach(author => {
const isAdult = author.age >= 18;
author.update({ isAdult });
})
or use the ability to merge an object with all objects in a QuerySet. Since the update operation is batched for all objects in the QuerySet, it can be more performant with a large amount of entities:
const authors = publisher.authors;
const isAdult = author => author.age >= 18;
const adultAuthors = authors.filter(isAdult);
adultAuthors.update({ isAdult: true });
const youngAuthors = authors.exclude(isAdult);
youngAuthors.update({ isAdult: false });
A descriptive error is now thrown when a reverse field conflicts with another field declaration. For example, the following schema:
class A extends Model {}
A.modelName = 'A';
class B extends Model {}
B.modelName = 'B';
B.fields = {
field1: one('A'),
field2: one('A'),
};
would try to define the reverse field b
on A
twice, throwing an error with an undescriptive message.
Breaking changes:
Model.withId(id)
now throws if object with idid
does not exist in the database.
Includes various bugfixes and improvements.
Breaking changes:
- Replaced
plain
andmodels
instance attributes inQuerySet
withwithRefs
andwithModels
respectively. The attributes return a newQuerySet
instead of modifying the existing one. Aref
alias is also added forwithRefs
, so you can doBook.ref.at(2)
. - After calling
filter
,exclude
ororderBy
method on aQuerySet
instance, thewithRefs
flag is always flipped off so that calling the same methods on the returnedQuerySet
would use model instances in the operations. Previously the flag value remained after calling those methods. .toPlain()
fromQuerySet
is renamed to.toRefArray()
for clarity.- Added
.toModelArray()
method toQuerySet
. - Removed
.objects()
method fromQuerySet
. Use.toRefArray()
or.toModelArray()
instead. - Removed
.toPlain()
method fromModel
, which returned a copy of the Model instance's property values. To replace that,ref
instance getter was added. It returns a reference to the plain JavaScript object in the database. So you can doBook.withId(0).ref
. If you need a copy, you can doObject.assign({}, Book.withId(0).ref)
. - Removed
.fromEmpty()
instance method fromSchema
. - Removed
.setReducer()
instance method fromSchema
. You can just doModelClass.reducer = reducerFunc;
.
MIT. See LICENSE