Code Monkey home page Code Monkey logo

types-redux-orm's Introduction

types-redux-orm

NO LONGER SUPPORTED. USE @types/redux-orm

Typescript types for Redux Orm 0.9.4

Important

It's an alpha version of types I currently use in one of my projects. They will be updated as I proceed with the project. No backward compatibility guaranteed.

Roadmap

01/13/18 - Beta release
02/03/18 - Stable release and merge into DefinitelyTyped

Install

npm install types-redux-orm

Modify your tsconfig.json as following:

...
"typeRoots": [
      "./node_modules/types-redux-orm",
      "./node_modules/@types"
    ],
...

Recipes

Model

import { attr, IORMCommonState, IORMId, ITableState, Model, ORM } from 'redux-orm'

export class Test extends Model<ITestStateItem, IFetchIndicatorState> {
  static modelName = 'Test'

  static fields = {
    test: attr(),
    isFetching: attr({ getDefault: () => false }),
    id: attr()
  }
}

// core data which we do not have defaults for
export interface ITestStateItem {
  test: string
}

// optional data we provide defaults for
export interface IFetchIndicatorState {
  isFetching: boolean
}

// id attr is added automatically by redux-orm therefore we have IORMId interface
export type ITestState = ITableState<ITestStateItem & IORMId & IFetchIndicatorState>

export interface ITestORMState extends IORMCommonState {
  Test: ITestState
}

interface ITestORMModels {
  Test: typeof Test
}

const orm = new ORM<ITestORMState>()
orm.register<ITestORMModels>(Test)
export default orm

Reducer

interface ITestDTO {
  test: string
}

const reducerAddItem = (state: ITestORMState, action: ActionMeta<ITestDTO, any>): ITestORMState => {
  const session = orm.session(state)
  session.Test.upsert<ITestStateItem>(action.payload)
  return session.state
}

Selector

import { createSelector as createSelectorORM, IORMId, ISession } from 'redux-orm'

interface ITestDisplayItem {
  test: string
}
type ITestDisplayItemList = ITestDisplayItem[]

export const makeGetTestDisplayList = () => {
  const ormSelector = createSelectorORM<ITestORMState>(orm, (session: ISession<ITestORMState>) =>
    session.Test
      .all<ITestStateItem, IFetchIndicatorState>()
      .toRefArray()
      .map((item) => ({ ...item }))
  })
  return createSelector<IRootState, ITestORMState, ITestDisplayItemList>(
    ({ test }) => test,
    ormSelector
  )
}

types-redux-orm's People

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

types-redux-orm's Issues

createReducer

I'm just getting to grips with redux-orm so this might be wrong, but are you using the createReducer function?
Your typing suggests that it doesn't return a reducer (i.e a function), which doesn't appear to be correct.

thanks

Overloaded function decaration for many missing.

In index.d.ts around line 202 the 'many' function is declared.

In contrast with the 'fk' and 'oneToOne' function declarations, this function is not overloaded. The version with one required and one optional string parameter is missing.

Is this intentional? AFAICT that version is still part of the redux-orm API.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.