Comparing version
383
lib/index.js
// Generated by LiveScript 1.2.0 | ||
(function(){ | ||
var util, hat, ddb, DB, Object, ID, Data, slice$ = [].slice; | ||
var util, hat, ddb, Node, ID, Data, DB, Query, slice$ = [].slice; | ||
util = require('util'); | ||
hat = require('hat'); | ||
ddb = exports; | ||
ddb.DB = DB = (function(){ | ||
DB.displayName = 'DB'; | ||
var prototype = DB.prototype, constructor = DB; | ||
function DB(){ | ||
this.objects = new Map; | ||
this._assocs = new Map; | ||
this.rack = hat.rack(); | ||
} | ||
prototype.register = function(obj){ | ||
var objs; | ||
objs = this.objects.get(obj.constructor); | ||
if (objs == null) { | ||
objs = {}; | ||
this.objects.set(obj.constructor, objs); | ||
} | ||
if (objs[obj.id()] == null) { | ||
obj.registered(this); | ||
objs[obj.id()] = obj; | ||
this.rack.set(obj.id(), obj); | ||
} | ||
return this; | ||
ddb.registry = {}; | ||
(function(){ | ||
this.kinds = {}; | ||
this.from = function(arg$){ | ||
var id, j; | ||
id = arg$[0], j = arg$[1]; | ||
return this.kinds[id].fromJson(j); | ||
}; | ||
prototype.all = function(kind){ | ||
var objs, all, id, obj; | ||
objs = this.objects.get(kind); | ||
if (objs != null) { | ||
all = []; | ||
for (id in objs) { | ||
obj = objs[id]; | ||
all.push(obj); | ||
} | ||
return all; | ||
} else { | ||
return []; | ||
this.register = function(kind){ | ||
if (this.kinds[kind.id] != null) { | ||
throw new Error("A kind with that id: " + util.inspect(kind.id) + " is already registered"); | ||
} | ||
return this.kinds[kind.id] = kind; | ||
}; | ||
prototype.assocs = function(obj, kind){ | ||
var allAssocs, assocs; | ||
kind == null && (kind = ddb.Object); | ||
allAssocs = this._assocs.get(obj.constructor); | ||
if (allAssocs != null) { | ||
assocs = allAssocs[obj.id()]; | ||
if (assocs != null) { | ||
return assocs.filter(function(it){ | ||
return it instanceof kind; | ||
}); | ||
} else { | ||
return new Set; | ||
} | ||
} else { | ||
return new Set; | ||
} | ||
}; | ||
prototype.assoc = function(a, b){ | ||
var this$ = this; | ||
(function(){ | ||
var allAssocs, assocs; | ||
allAssocs = this$._assocs.get(a.constructor); | ||
if (allAssocs == null) { | ||
allAssocs = {}; | ||
this$._assocs.set(a.constructor, allAssocs); | ||
} | ||
assocs = allAssocs[a.id()]; | ||
if (assocs == null) { | ||
assocs = []; | ||
allAssocs[a.id()] = assocs; | ||
} | ||
if (!~assocs.indexOf(b)) { | ||
return assocs.push(b); | ||
} | ||
})(); | ||
(function(){ | ||
var allAssocs, assocs; | ||
allAssocs = this$._assocs.get(b.constructor); | ||
if (allAssocs == null) { | ||
allAssocs = {}; | ||
this$._assocs.set(b.constructor, allAssocs); | ||
} | ||
assocs = allAssocs[b.id()]; | ||
if (assocs == null) { | ||
assocs = []; | ||
allAssocs[b.id()] = assocs; | ||
} | ||
if (!~assocs.indexOf(a)) { | ||
return assocs.push(a); | ||
} | ||
})(); | ||
return this; | ||
}; | ||
return DB; | ||
}()); | ||
ddb.Object = Object = (function(){ | ||
Object.displayName = 'Object'; | ||
var prototype = Object.prototype, constructor = Object; | ||
}.call(ddb.registry)); | ||
ddb.Node = Node = (function(){ | ||
Node.displayName = 'Node'; | ||
var prototype = Node.prototype, constructor = Node; | ||
prototype.registered = function(db){}; | ||
@@ -105,4 +29,18 @@ prototype.id = function(){ | ||
}; | ||
function Object(){} | ||
return Object; | ||
prototype.toJson = function(){ | ||
throw new Error(this.constructor.name + " must implement to-json"); | ||
}; | ||
prototype.qid = function(){ | ||
return this.constructor.id + "-" + this.id(); | ||
}; | ||
prototype.filter = function(filter){ | ||
if (filter instanceof Function && filter.prototype instanceof ddb.Node) { | ||
if (!(this instanceof filter)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
function Node(){} | ||
return Node; | ||
}()); | ||
@@ -118,4 +56,13 @@ ddb.ID = ID = (function(superclass){ | ||
prototype.registered = function(db){ | ||
if (this._id != null) { | ||
throw new Error('You\'re using an ID with multiple databases, that won\'t work'); | ||
} | ||
return this._id = db.rack(this); | ||
}; | ||
prototype.toJson = function(){ | ||
return {}; | ||
}; | ||
ID.fromJson = function(j){ | ||
return new this; | ||
}; | ||
function ID(){ | ||
@@ -125,7 +72,23 @@ ID.superclass.apply(this, arguments); | ||
return ID; | ||
}(ddb.Object)); | ||
}(ddb.Node)); | ||
ddb.Data = Data = (function(superclass){ | ||
var prototype = extend$((import$(Data, superclass).displayName = 'Data', Data), superclass).prototype, constructor = Data; | ||
function Data(){ | ||
var i$, len$; | ||
this.data = slice$.call(arguments); | ||
if (this.constructor.names != null) { | ||
for (i$ = 0, len$ = this.constructor.names.length; i$ < len$; ++i$) { | ||
(fn$.call(this, i$, this.constructor.names[i$])); | ||
} | ||
} | ||
function fn$(i, name){ | ||
this[name] = function(newVal){ | ||
if (newVal != null) { | ||
this.data[i] = newVal; | ||
return this; | ||
} else { | ||
return this.data[i]; | ||
} | ||
}; | ||
} | ||
} | ||
@@ -142,4 +105,232 @@ prototype.inspect = function(){ | ||
}; | ||
prototype.toJson = function(){ | ||
return this.data; | ||
}; | ||
Data.fromJson = function(j){ | ||
return (function(func, args, ctor) { | ||
ctor.prototype = func.prototype; | ||
var child = new ctor, result = func.apply(child, args), t; | ||
return (t = typeof result) == "object" || t == "function" ? result || child : child; | ||
})(this, j, function(){}); | ||
}; | ||
prototype.filter = function(filter){ | ||
var i$, ref$, len$, i, d; | ||
if (filter instanceof this.constructor) { | ||
for (i$ = 0, len$ = (ref$ = filter.data).length; i$ < len$; ++i$) { | ||
i = i$; | ||
d = ref$[i$]; | ||
if (!(d != null && this.data[i] === d)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return superclass.prototype.filter.call(this, filter); | ||
}; | ||
return Data; | ||
}(ddb.Object)); | ||
}(ddb.Node)); | ||
ddb.DB = DB = (function(superclass){ | ||
var prototype = extend$((import$(DB, superclass).displayName = 'DB', DB), superclass).prototype, constructor = DB; | ||
function DB(){ | ||
this.nodes = {}; | ||
this._assocs = {}; | ||
this.rack = hat.rack(); | ||
this.register(this); | ||
} | ||
prototype.toJson = function(){ | ||
var j, indexes, id, ref$, node, savedAssocs, aId, assocs, i$, len$, bId, ref1$; | ||
j = { | ||
nodes: [], | ||
assocs: [] | ||
}; | ||
indexes = {}; | ||
for (id in ref$ = this.nodes) { | ||
node = ref$[id]; | ||
if (node !== this) { | ||
j.nodes.push([node.constructor.id, node.toJson()]); | ||
indexes[node.qid()] = j.nodes.length - 1; | ||
} | ||
} | ||
savedAssocs = {}; | ||
for (aId in ref$ = this._assocs) { | ||
assocs = ref$[aId]; | ||
savedAssocs[aId] = new Set; | ||
for (i$ = 0, len$ = assocs.length; i$ < len$; ++i$) { | ||
bId = assocs[i$]; | ||
if (!(((ref1$ = savedAssocs[bId]) != null && ref1$.has(aId)) || bId === this.qid() || aId === this.qid())) { | ||
j.assocs.push([indexes[aId], indexes[bId]]); | ||
} | ||
savedAssocs[aId].add(bId); | ||
} | ||
} | ||
return j; | ||
}; | ||
prototype.loadJson = function(j){ | ||
var nodes, i$, ref$, len$, i, node, assoc; | ||
nodes = {}; | ||
for (i$ = 0, len$ = (ref$ = j.nodes).length; i$ < len$; ++i$) { | ||
i = i$; | ||
node = ref$[i$]; | ||
node = ddb.registry.from(node); | ||
nodes[i] = node; | ||
this.register(node); | ||
} | ||
for (i$ = 0, len$ = (ref$ = j.assocs).length; i$ < len$; ++i$) { | ||
assoc = ref$[i$]; | ||
this.assoc(nodes[assoc[0]], nodes[assoc[1]]); | ||
} | ||
return this; | ||
}; | ||
DB.fromJson = function(j){ | ||
return new this().loadJson(j); | ||
}; | ||
prototype.register = function(node){ | ||
if (this.nodes[node.qid()] == null) { | ||
node.registered(this); | ||
this.nodes[node.qid()] = node; | ||
this.assoc(this, node); | ||
this.assoc(node, node); | ||
this.rack.set(node.qid(), node); | ||
} | ||
return this; | ||
}; | ||
prototype.all = function(kind){ | ||
kind == null && (kind = ddb.Node); | ||
return this.assocs(this, kind); | ||
}; | ||
prototype.assocs = function(node, kind){ | ||
var assocs, this$ = this; | ||
node == null && (node = this); | ||
kind == null && (kind = ddb.Node); | ||
assocs = this._assocs[node.qid()]; | ||
if (assocs != null) { | ||
return assocs.map(function(it){ | ||
return this$.nodes[it]; | ||
}).filter(function(it){ | ||
return it instanceof kind; | ||
}); | ||
} else { | ||
return []; | ||
} | ||
}; | ||
prototype.assoc = function(a, b){ | ||
var this$ = this; | ||
(function(){ | ||
var assocs; | ||
assocs = this$._assocs[a.qid()]; | ||
if (assocs == null) { | ||
assocs = []; | ||
this$._assocs[a.qid()] = assocs; | ||
} | ||
if (!~assocs.indexOf(b.qid())) { | ||
return assocs.push(b.qid()); | ||
} | ||
})(); | ||
(function(){ | ||
var assocs; | ||
assocs = this$._assocs[b.qid()]; | ||
if (assocs == null) { | ||
assocs = []; | ||
this$._assocs[b.qid()] = assocs; | ||
} | ||
if (!~assocs.indexOf(a.qid())) { | ||
return assocs.push(a.qid()); | ||
} | ||
})(); | ||
return this; | ||
}; | ||
return DB; | ||
}(ddb.ID)); | ||
ddb.Query = Query = (function(){ | ||
Query.displayName = 'Query'; | ||
var Assoc, Nodes, Filter, prototype = Query.prototype, constructor = Query; | ||
function Query(db){ | ||
this.db = db; | ||
this.base = slice$.call(arguments, 1); | ||
if (!this.base.length) { | ||
this.base.push(this.db); | ||
} | ||
this.parts = []; | ||
} | ||
prototype.assoc = function(kind){ | ||
return this.parts.push(new constructor.Assoc(kind)); | ||
}; | ||
prototype.add = function(){ | ||
var nodes; | ||
nodes = slice$.call(arguments); | ||
return this.parts.push((function(func, args, ctor) { | ||
ctor.prototype = func.prototype; | ||
var child = new ctor, result = func.apply(child, args), t; | ||
return (t = typeof result) == "object" || t == "function" ? result || child : child; | ||
})(constructor.Nodes, nodes, function(){})); | ||
}; | ||
prototype.filter = function(filter){ | ||
return this.parts.push(new constructor.Filter(filter)); | ||
}; | ||
prototype.run = function(){ | ||
var this$ = this; | ||
return this.parts.reduce(function(acc, val){ | ||
return val.find(this$.db, acc); | ||
}, this.base); | ||
}; | ||
Query.Assoc = Assoc = (function(){ | ||
Assoc.displayName = 'Assoc'; | ||
var prototype = Assoc.prototype, constructor = Assoc; | ||
function Assoc(kind){ | ||
this.kind = kind != null | ||
? kind | ||
: ddb.Node; | ||
} | ||
prototype.find = function(db, prev){ | ||
var this$ = this; | ||
return prev.reduce(function(acc, node){ | ||
var assocs, i$, len$; | ||
acc = acc.slice(); | ||
assocs = db.assocs(node, this$.kind); | ||
for (i$ = 0, len$ = assocs.length; i$ < len$; ++i$) { | ||
node = assocs[i$]; | ||
if (acc.indexOf(node) === -1) { | ||
acc.push(node); | ||
} | ||
} | ||
return acc; | ||
}, []); | ||
}; | ||
return Assoc; | ||
}()); | ||
Query.Nodes = Nodes = (function(){ | ||
Nodes.displayName = 'Nodes'; | ||
var prototype = Nodes.prototype, constructor = Nodes; | ||
function Nodes(){ | ||
this.nodes = slice$.call(arguments); | ||
} | ||
prototype.find = function(db, prev){ | ||
var after, i$, ref$, len$, node; | ||
after = prev.slice(); | ||
for (i$ = 0, len$ = (ref$ = this.nodes).length; i$ < len$; ++i$) { | ||
node = ref$[i$]; | ||
if (after.indexOf(node) === -1) { | ||
after.push(node); | ||
} | ||
} | ||
return after; | ||
}; | ||
return Nodes; | ||
}()); | ||
Query.Filter = Filter = (function(){ | ||
Filter.displayName = 'Filter'; | ||
var prototype = Filter.prototype, constructor = Filter; | ||
function Filter(filter){ | ||
this.filter = filter; | ||
} | ||
prototype.find = function(db, prev){ | ||
var this$ = this; | ||
return prev.filter(function(it){ | ||
return it.filter(this$.filter); | ||
}); | ||
}; | ||
return Filter; | ||
}()); | ||
return Query; | ||
}()); | ||
function extend$(sub, sup){ | ||
@@ -146,0 +337,0 @@ function fun(){} fun.prototype = (sub.superclass = sup).prototype; |
// Generated by LiveScript 1.2.0 | ||
(function(){ | ||
var ddb, db, Person, Name, johnDoe, firstName, lastName; | ||
var util, ddb, db, Person, Name, johnDoe, firstName, lastName, q, flatten; | ||
util = require('util'); | ||
ddb = require('./'); | ||
@@ -8,2 +9,3 @@ db = new ddb.DB; | ||
var prototype = extend$((import$(Person, superclass).displayName = 'Person', Person), superclass).prototype, constructor = Person; | ||
Person.id = 'person'; | ||
function Person(){ | ||
@@ -14,4 +16,7 @@ Person.superclass.apply(this, arguments); | ||
}(ddb.ID)); | ||
ddb.registry.register(Person); | ||
Name = (function(superclass){ | ||
var prototype = extend$((import$(Name, superclass).displayName = 'Name', Name), superclass).prototype, constructor = Name; | ||
Name.id = 'name'; | ||
Name.names = ['type', 'val']; | ||
Name.type = [String, String]; | ||
@@ -23,2 +28,3 @@ function Name(){ | ||
}(ddb.Data)); | ||
ddb.registry.register(Name); | ||
[['John', 'Doe'], ['Jane', 'Doe']].forEach(function(arg$){ | ||
@@ -45,4 +51,13 @@ var first, last, person, firstName, lastName; | ||
db.assoc(lastName, firstName); | ||
console.log(db.assocs(johnDoe)); | ||
console.log(db.all(Name)); | ||
q = new ddb.Query(db); | ||
q.add(new Name('last', 'Doe')); | ||
q.assoc(Person); | ||
q.assoc(Name); | ||
q.filter(new Name('first')); | ||
console.log(q.run()); | ||
flatten = function(it){ | ||
return it.reduce(function(acc, val){ | ||
return acc.concat(val); | ||
}, []); | ||
}; | ||
console.log(db.assocs(new Name('last', 'Doe'), Person)); | ||
@@ -49,0 +64,0 @@ function extend$(sub, sup){ |
{ | ||
"name": "ddb", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"author": "CoderPuppy", | ||
@@ -5,0 +5,0 @@ |
@@ -7,9 +7,9 @@ # Dynamic Database | ||
## Overview | ||
There are two types of objects (by default): | ||
There are two types of nodes (by default): | ||
- **Identity Objects** -- Purely identity, no state, the id is random | ||
- **Data Objects** -- Purely data, the id is based on the data | ||
- **Identity Nodes** -- Purely identity, no state, the id is random | ||
- **Data Nodes** -- Purely data, the id is based on the data | ||
You can register objects into a database and associate two objects together. | ||
To get the objects back you can either get all objects of a type or get all associated objects of an object (optionally restricting it to a type). | ||
You can register nodes into a database and associate two nodes together. | ||
To get the nodes back you can either get all nodes of a type or get all associated nodes of an node (optionally restricting it to a type). | ||
@@ -19,15 +19,15 @@ ## Usage | ||
- *register(Object)*: Register an object with the database | ||
- *assoc(a: Object, b: Object)*: Associate *a* with *b* | ||
- *assocs(obj: Object, [kind: Kind])*: Get all the objects associated with *obj* of kind: *kind* | ||
- *all(kind: Kind)*: Get all objects of kind: *kind* | ||
- *register(Node)*: Register an node with the database | ||
- *assoc(a: Node, b: Node)*: Associate *a* with *b* | ||
- *assocs(node: Node, [kind: Kind])*: Get all the nodes associated with *node* of kind: *kind* | ||
- *all(kind: Kind)*: Get all nodes of kind: *kind* | ||
**Object** | ||
**Node** | ||
- *registered(db: DB)*: Callback for when it's registered with a database | ||
- *id()*: ID for this object, abstract | ||
- *id()*: ID for this node, abstract | ||
**Data** | ||
- *new Data(data...)*: Create a new data object with data: *data* | ||
- *new Data(data...)*: Create a new data node with data: *data* | ||
@@ -34,0 +34,0 @@ **ID** |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
21721
75.58%10
25%441
87.66%1
Infinity%