Referencejs manages references to values in plain JS or Immutable objects (called stores). You can use this to:
- Easily manage complex denormalized, without needing an explicited schema
- referencing data that doesn't exist yet (e.g. async data)
- normalize and denormalize immutable data
- Lazily denormalize stored data
Update a Plain store
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 if that's your jam
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);
Dereferencing one reference at a time can be painful.
smartDereference
scans an object or array and dereferences every reference it finds
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);