https://github.com/mindblight/referencejs
https://github.com/mindblight/referencejs
denormalize normalize redux reference
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/mindblight/referencejs
- Owner: mindblight
- License: mit
- Created: 2017-04-03T04:59:03.000Z (over 9 years ago)
- Default Branch: develop
- Last Pushed: 2017-05-02T19:17:08.000Z (about 9 years ago)
- Last Synced: 2024-04-25T09:22:48.036Z (about 2 years ago)
- Topics: denormalize, normalize, redux, reference
- Language: JavaScript
- Size: 134 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Referencejs [](https://travis-ci.org/mindblight/referencejs)
Referencejs manages references to values in plain JS or [Immutable](https://facebook.github.io/immutable-js/) objects (called stores). You can use this to:
1. Easily manage complex denormalized, without needing an explicited schema
2. referencing data that doesn't exist yet (e.g. async data)
3. normalize and denormalize immutable data
4. Lazily denormalize stored data
## Getting Started
Update a Plain store
```js
import { createReference, resolveReference, dereference } from 'referencejs';
const jon = {
id: 'user_1',
name: 'John Doe',
email: 'jon@doe.com'
};
const jonRefrence = createReference('auth', 'users', jon.id);
let store = {};
store = resolveReference(store, jonReference, jon);
dereference(store, jonRefrence) === jon;
```
Or use [Immutable](https://facebook.github.io/immutable-js/) if that's your jam
```js
import { Map, is } from 'immutable';
import { createReference, resolveReference, dereference } from 'referencejs/immutable';
const jon = Map({
id: 'user_1',
name: 'John Doe',
email: 'jon@doe.com'
});
const jonRefrence = createReference('auth', 'users', jon.id);
let store = Map();
store = resolveReference(store, jonReference, jon);
is(dereference(store, jonRefrence), jon);
```
## More Advanced: `smartDereference`
Dereferencing one reference at a time can be painful.
`smartDereference` scans an object or array and dereferences every reference it finds
```js
import {
createReference,
smartDereference,
} from 'referencejs';
const store = {
users: {
user1: {
name: 'John Doe'
},
user2: {
name: 'Jane Doe'
},
user3: {
name: 'Billy Doe'
},
user4: {
name: 'Lucy Doe'
}
}
};
const john = createReference(['users', 'user1']);
const jane = createReference(['users', 'user2']);
const billy = createReference(['users', 'user3']);
const lucy = createReference(['users', 'user4']);
const familyTreeReferences = {
father: john,
mother: jane,
children: [billy, lucy]
};
// familyTree will contain the user objects instead of the references
const familyTree = smartDereference(store, familyTree);
```
## Like what you see?
- [Read the docs](docs/README.md)