graft-db
TypeScript icon, indicating that this package has built-in type declarations

0.0.22 • Public • Published

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.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' } } }
 

Readme

Keywords

none

Package Sidebar

Install

npm i graft-db

Weekly Downloads

25

Version

0.0.22

License

ISC

Last publish

Collaborators

  • chrisfarms