graft-db
v0.0.22
Published
```javascript import { MemStore, Repository, BaseTypes } from 'graft-db';
Downloads
20
Readme
Working with state
import { MemStore, Repository, BaseTypes } from 'graft-db';
// create the store where things get persisted
const store = new MemStore();
// create a repository (one repository can have many tags/branches)
let r = new Repository({ store, id: 'myrepo' });
// create/fetch a branch and name
let master = r.latest().checkout('master');
master = master.set('n1', {type: BaseTypes.Node});
master = master.set('n2', {type: BaseTypes.Node});
// update the 'master' tag to point to this state
master = master.commit();
// fetch new metadata into r
r = r.latest();
// checkout another copy of the master branch
let wip = r.latest().checkout('master');
wip.key() === master.key(); // => true
// change wip and commit to master
wip = wip.set('n100', {type: BaseTypes.Node});
wip = wip.commit();
// continue working on master
// and now try to commit
master = master.set('n3', {type: BaseTypes.Node});
master = master.commit(); // fail 'needs rebase' since upstream is ahead
// fix it by pulling latest
master = master.pull();
// or fix it by rebasing from wip
master = master.rebase(wip);
// now can commit
master.commit();
Building a graph (low level)
// create a node to represent a "Person" type
master = master.set('uuid-person', {type: BaseTypes.Node, name: 'Person'});
// create a property to hold the person's name
master = master.set('uuid-person-name', {type: BaseTypes.String, name: 'name', nullable: true});
// create an "instance" of a person node
master = master.set('uuid-jeff', {type: 'uuid-person'});
// store a name
master = master.setString('uuid-jeff-name', 'jeff');
master = master.set('uuid-jeff-name', {type: 'uuid-person-name', value: {
type: ValueKeyTypes.data,
id: 'uuid-jeff-name',
}});
Build a graph (high level) - DEPRECATED
// create a node to represent a "Person" type
master = master.setType({
name: 'Person',
properties: {
name: { type: BaseTypes.String, },
}
});
// create an "instance" of a person node
master = master.setNode({
type: 'Person',
values: {
name: 'jeff',
}
});
Querying the graph
let result;
[result, mastelr] = master.query(`{
node(id: 'uuid-jeff') {
...on Person {
name
}
}
}`); // => {data: { node: { name: 'jeff' } } }