Catch&Release ORM
Catch&Release ORM is a heavily opinionated React/Redux ORM
Why an ORM?
The ORM should remove boilerplate and simplify immutable access.
Example:
Old style:
// HeroCard.jsx import React Fragment from 'react';import PropTypes from 'prop-types';import ImmutablePropTypes from 'react-immutable-proptypes'; import bindActionCreators from 'redux';import connect from 'react-redux'; import actions from 'heroActions'; import HeroAvatar from 'heroAvatar';import HeroDetails from 'heroDetails'; Component static propTypes = heroId: PropTypesnumberisRequired hero: ImmutablePropTypesMapisRequired destroyHero: PropTypesfuncisRequired { const heroId hero destroyHero = thisprops; return <Fragment> <button =>Destroy Hero</button> <HeroAvatar /> <HeroDetails /> </Fragment> ; } const mapStateToProps = state props hero: statedata; { return ;} mapsStateToProps mapDispatchToPropsHeroCard;
New Style:
// Hero.jsimport ORM from 'crorm';import actions from 'heroActions'; { ; }
// HeroCard.jsx import React Fragment from 'react';import PropTypes from 'prop-types';import connect from 'react-redux';import ORM from 'crorm'; import Hero from 'hero'; import HeroAvatar from 'heroAvatar';import HeroDetails from 'heroDetails'; Component static propTypes = heroId: PropTypesnumberisRequired hero: PropTypes { const hero = thisprops; return <Fragment> <button =>Destroy Hero</button> <HeroAvatar /> <HeroDetails /> </Fragment> ; } const mapStateToProps = state props hero: Hero; mapStateToPropsHeroCard;
Expectations
- Redux data is Immutable utilizing Immutable.Record
- Data is in the JSONAPI format
- Data has been parsed using jsonapi-normalizer
- Actions are all CRUD based
- Redux Store should have a { data } reducer via combinedReducers.
Setting Up
import { ORM } from 'crorm';
import { store } from 'mystore';
ORM.Config.database = store;
To Add Debug Output: ORM.Config.debug = true;
Create your Class
class Animal extends ORM.Base {};
Class Methods
database()
- Get the Redux Store.
dispatch()
- Get the Redux Store dispatch function.
recordType()
- Get the name of your model in redux, i.e. 'hero'.
order()
- Get an Immutable List of ids containing the server ordering for the current entityType.
ordered(props = {})
- Get an Immutable List of ordered instances for the current entityType.
pagination()
- Get the pagination for the current entityType.
findById(id)
- Get the instance matching the id for the entityType. Returns instance with empty Immutable.Map() when not found.
all()
- Get and Immutable Map for all entities of entityType.
where(props = {})
- Get the instances where all props are matching.
create(props = {})
- Create an instance with the passed in props. Call an onCreate method to be overridden for server save and state update.
Instance Methods
Dot Access
- Props can be added or edited using '.'.
valid()
- Override to provide instance validation, return a boolean.
updateProps(props = {})
- Update the current values and returns a new instance with the passed in props. Calls an onUpdate to be overridden to save on the backend and update the store.
destroy()
- Marks the current model as destroyed and returns a "destroyed" instance. Calls an onDestroy to be overridden to save on the backend and update the store.
onCreate(dispatch, createProps)
- Override to define what happens on create.
onUpdate(dispatch, updateProps)
- Override to define what happens on update.
onDestroy(dispatch)
- Override to define what happens on destroy.
Pushing a New Version
npm run build && npm publish