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
import { G, V, E } from 'dynamo-graph'
const g = G.define('my-graph')
const USER = V.define('User')
const NOTE = V.define('Note')
const AUTHORED = E.define('Authored', E.ONE_TO_MANY)
const overview = async () => {
await G.generate(g)
const me = await V.create(g, USER, { name: "James", job: "Intern" })
const boss = await V.create(g, USER, { name: "Tyler", job: "CTO" })
const note = await V.create(g, NOTE, { message: "some note" })
await V.all(g, USER)
await V.update(g, USER, me.id, { name: "James", job: "Engineer" })
await V.putByKey(g, NOTE, 'important', { message: 'Publish dynamo-graph!' })
const important = await V.putByKey(g, NOTE, 'important', { message: 'Published' })
const e1 = await E.set(g, important.id, AUTHORED, E.IN, E.GENERATE, me.id)
const e2 = await E.set(g, me.id, AUTHORED, E.OUT, +Date.now(), note.id)
await E.get(g, note.id, AUTHORED, E.IN, +Date.now(), me.id)
await E.range(g, me.id, AUTHORED, E.OUT)
await E.range(g, me.id, AUTHORED, E.OUT, { first: 1, after: 99999 })
await E.range(g, me.id, AUTHORED, E.OUT, { last: 1, before: -1 })
await E.set(g, boss.id, AUTHORED, E.OUT, E.GENERATE, important.id)
await E.get(g, me.id, AUTHORED, E.OUT, important.id)
await E.get(g, important.id, AUTHORED, E.IN, me.id)
await E.remove(g, boss.id, AUTHORED, E.OUT, important.id)
}
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"
, id: () => Promise<Id>
, weight: () => Promise<Weight>
, putCounter: (key: string, value: number) => Promise<number>
, incrCounter: (key: string, value: 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 many OUT
edges, but only one IN
edge, typically surjectiveE.MANY_TO_ONE
: a vertex may have many IN
edges, but many OUT
edge, i.e. an injective mappingE.ONE_TO_ONE
: a vertex may have only one IN
or OUT
edge, i.e. a bipartition
E.define(label, multiplicity)
E.get(g, from, def, direction, to)
E.range(g, from, def, direction, cursor)
E.count(g, from, def, direction, cursor)
E.set(g, from, def, direction, weight, to, attrs)
E.remove(g, from, def, to)