dynamo-graph
Low-level graph database operations implemented in DynamoDB. Logic involving complex traversals, lazy lists, intersections, and unions, are deferred to the consuming client.
Ensure that you have your AWS CLI properly configured, so that the aws-sdk
dependency can do its magic.
Documentation (TODO)
All code is written in a literate style. For more detailed explanations, refer to the source code.
Example
// create a graph instance const g = G // create vertex definitions const USER = Vconst NOTE = V // create edge definitions const AUTHORED = E const overview = async { // if this is our first time using the graph // G.generate will instantiate all the DynamoDB tables, indices, and system objects await G // V.create const me = await V // => { id: '3Rx', label: 'User', updatedAt: 14699502051042, attrs: { name: "James", job: "Intern" } } const boss = await V // => { id: '3Ry', label: 'User', updatedAt: 14699502056492, attrs: { name: "Tyler", job: "CTO" }} const note = await V // => { id: '3Rz', label: 'Note', updatedAt: 14699502285314, attrs: { message: "some note" }} // V.all await Vallg USER // => TODO: document // V.update await V // => { id: '3Rx', label: 'User', updatedAt: 14699502496014, attrs: { name: "James", job: "Engineer" } } // V.putByKey await V // => { id: 'd7', label: 'Note', updatedAt: 1469950262268, key: 'important', attrs: { message: 'Publish dynamo-graph!' } } // n.b. the id remains the same, the key uniquely identifies the vertex const important = await V // => { id: 'd7', label: 'Note', updatedAt: 1469950309677, key: 'important', attrs: { message: 'Published' } } // E.set const e1 = await E // => { from: 'd7', label: 'Authored', direction: '<', weight: 1042, to: '3Rx', updatedAt: 1469950464801 } const e2 = await E // => { from: '3Rx', label: 'Authored', direction: '>', weight: 1469950468427, to: '3Rz', updatedat: 1469950468427 } // E.get await E // => { from: '3Rz', label: 'Authored', direction: '<', weight: 1469950468427, to: '3Rx', updatedat: 1469950468427 } // E.range await E // => { items: [ e2, e1 ], hasMore: false, lastCursor: 1042 } await E // => { items: [ e1 ], hasMore: false, lastCursor: 1042 } await E // => { items: [ e1 ], hasMore: true, lastCursor: 1042 } // Edge multiplicities are respected: await E // => { from: '3Ry', label: 'Authored', direction: '>', weight: 1043, to: 'd7', updatedAt: 1469981321205 } await E // => null await E // => null // E.remove await E // => { from: '3Ry', label: 'Authored', direction: '>', weight: 1043, to: 'd7', updatedAt: 1469981321205 } }
G
A graph is an abstraction over the database, containing meta information as well as exposure to the raw operations
type Graph = name: string env: "production" | "beta" | "development" region: "us-east-1" | "us-west-1" | ... | "local" Promise<Id> Promise<Weight> Promise<number> Promise<number></number>
G.define(name[, { env, region }]): Graph
G.generate(g): Promise<Graph>
V
A vertex is a tuple (id, label, attrs, key?, updatedAt)
such that the id uniquely determines the vertex,
the label uniquely determines the type of the attributes, and the label-key pair uniquely determines a vertex
type Vertex<a> = id : string label : Label attrs : a key? : string updatedAt : number
V.define(label)
V.create(g, def, attrs)
V.update(g, def, id, attrs)
V.putByKey(g, def, key, attrs)
V.get(g, id)
V.getMany(g, ids)
V.getByKey(g, def, key)
V.all(g, def, cursor)
V.count(g, def, cursor)
V.remove(g, id)
E
An edge is a tuple (from, label, direction, weight, to, attrs, updatedAt)
such that
(from, label, direction, to)
and (from, label, direction, weight)
both uniquely identify the edge.
type Edge<a> = from : string label : Label direction : ">" | "<" weight : number to : string attrs : a updatedAt : number
Directions
Edge operations require a direction: E.OUT
, or E.IN
Multiplicities
Defining an Edge requires the use of one of the following multiplicities:
E.MANY_TO_MANY
: no restrictions, describes a simple graphE.ONE_TO_MANY
: a vertex may have manyOUT
edges, but only oneIN
edge, typically surjectiveE.MANY_TO_ONE
: a vertex may have manyIN
edges, but manyOUT
edge, i.e. an injective mappingE.ONE_TO_ONE
: a vertex may have only oneIN
orOUT
edge, i.e. a bipartition