Provider factory for Creating, Reading, Updating, and Deleting resources.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
test
.babelrc
.gitignore
.npmignore
.travis.yml
LICENSE
README.md
package-lock.json
package.json
webpack.config.base.js
webpack.config.development.js
webpack.config.production.js

README.md

provide-crud

build status npm version npm downloads

Provider factory for Creating, Reading, Updating, and Deleting resources.

Table of contents

  1. Installation
  2. Usage
  3. Example
  4. Real world example
  5. Protip

Installation

npm install provide-crud --save

Usage

provideCrud (String name, Optional Object init, Optional Object replication, Optional Array clientStateKeys)

Creates a provider with namespaced actions and reducers specific to CRUD operations.

The init object should contain a map of the unprefixed reducerKeys to their initial states. Each key will be prefixed (namespaced) with the name and become each reducerKey and will have actions which can be used to set each state. The only reserved keys for this object are id (defaults to empty string) and deleted (defaults to false). You can set a different default id or deleted initial state by including your own within this object, if necessary.

Sane defaults are added to the replication object if they're undefined. All reducerKeys are replicated and queryable by default. And the baseQuery ensures that only instances which have not been deleted are queried by default.

The provider key also defaults to:

const key = ({ props }) => props[idKey] ? `${idKey}=${props[idKey]}` : null;

You can override this key by simply setting it on the created provider object afterwards, if necessary.

Example

Let's create a test provider, with testFoo and testBar as states to be created/updated/deleted. The testFoo state will default to "foo", and the testBar state will default to "bar".

// src/providers/test.js

import provideCrud from 'provide-crud';

const test = provideCrud('test', {
  foo: 'foo',
  bar: 'bar'
});

export default test;

You'll then have a provider with the following actions:

  • createTest (Object state, Function genId, Optional Function onSuccess)
  • updateTest (Object updates, Optional Function onSuccess)
  • deleteTest (Optional Function onSuccess)
  • undeleteTest (Optional Function onSuccess)
  • setTestFoo (Mixed testFoo)
  • setTestBar (Mixed testBar)

And reducers:

  • testId
  • testDeleted
  • testFoo
  • testBar

Real world example

See provide-user. Also see Lumbur's user login component for an example where this is used.

Protip

Use provide-id-gen for your genId argument when creating an instance!