Comparing version 1.0.0 to 1.1.0
@@ -188,11 +188,17 @@ 'use strict'; | ||
key: 'get', | ||
value: function get(rootLid) { | ||
value: function get(rootLid, options) { | ||
var _this = this; | ||
var _ref = options || {}; | ||
var maxDepth = _ref.maxDepth; | ||
var inside = {}; | ||
var depth = 0; | ||
var rec = function rec(lid) { | ||
if (inside[lid]) { | ||
if (maxDepth && depth > maxDepth || inside[lid]) { | ||
return { lid: lid }; | ||
} | ||
inside[lid] = true; | ||
depth++; | ||
var obj = _this._objs[lid]; | ||
@@ -216,2 +222,3 @@ var entity = { lid: lid }; | ||
inside[lid] = false; | ||
depth--; | ||
return entity; | ||
@@ -223,5 +230,5 @@ }; | ||
key: 'lookup', | ||
value: function lookup(field, value) { | ||
value: function lookup(field, value, options) { | ||
var lid = this._lookup[field][value]; | ||
return (lid || null) && this.get(lid); | ||
return (lid || null) && this.get(lid, options); | ||
} | ||
@@ -228,0 +235,0 @@ }, { |
{ | ||
"name": "jseg", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "JavaScript Entity Graph: A super simple, in-memory, JS graph database.", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -41,3 +41,3 @@ # JavaScript Entity Graph | ||
- Client-side, in-memory only. | ||
- Assume dataset is small enough to traverse in O(N) time. | ||
- Assume dataset is small enough to traverse portions of it many times. | ||
- Plain-old JavaScript objects. | ||
@@ -91,9 +91,11 @@ - Hierarchical flattening of the graph. | ||
See [the test file][./test/index.js] for many concrete examples. | ||
See [the test file](./test/index.js) for many concrete examples. | ||
### get(lid) | ||
### get(lid, options) | ||
Gets a whole tree of related objects by `lid`. | ||
Gets a whole tree of related objects by `lid`. The `options` parameter may be | ||
omitted. | ||
Does not traverse in to cycles. | ||
Does not traverse in to cycles. If the `maxDepth` option is specified, will | ||
not recurse more than that many levels. | ||
@@ -107,10 +109,12 @@ Null field values and empty collections are omitted. | ||
Puts a whole tree of related objects. Properties are merged in to existing | ||
objects with matching `lid` fields. | ||
objects with matching `lid` fields. Collection properties are set-unioned. | ||
Fields set to null are deleted from entities. | ||
### lookup(field, value) | ||
### lookup(field, value, options) | ||
Gets an object by a unique field string value. See schema. | ||
The `options` parameter may be omitted and is delegated to `get`. | ||
Returns null if no entity exists. | ||
@@ -138,2 +142,7 @@ | ||
### Scalar Fields | ||
By default, fields may contain scalar values. These are typically strings and | ||
numbers, but any JavaScript non-null, non-undefined object is allowed. | ||
### Unique Lookup | ||
@@ -205,4 +214,8 @@ | ||
### Cascarding Delete | ||
For non-reference collections, simply put an array or other collection | ||
in a scalar field. Note that puts to scalar fields perform a complete | ||
value replacement, not set union. | ||
### Cascading Delete | ||
`destroy: true` | ||
@@ -209,0 +222,0 @@ |
@@ -157,9 +157,12 @@ let baseSchema = { | ||
get(rootLid) { | ||
get(rootLid, options) { | ||
let {maxDepth} = options || {}; | ||
let inside = {}; | ||
let depth = 0; | ||
let rec = (lid) => { | ||
if (inside[lid]) { | ||
if ((maxDepth && depth > maxDepth) || inside[lid]) { | ||
return {lid}; | ||
} | ||
inside[lid] = true; | ||
depth++; | ||
let obj = this._objs[lid]; | ||
@@ -183,2 +186,3 @@ let entity = {lid}; | ||
inside[lid] = false; | ||
depth--; | ||
return entity; | ||
@@ -189,5 +193,5 @@ }; | ||
lookup(field, value) { | ||
lookup(field, value, options) { | ||
let lid = this._lookup[field][value]; | ||
return (lid || null) && this.get(lid); | ||
return (lid || null) && this.get(lid, options); | ||
} | ||
@@ -194,0 +198,0 @@ |
@@ -8,8 +8,8 @@ import * as assert from 'assert'; | ||
let check = (lid, expected) => { | ||
assert.deepStrictEqual(db.get(lid), expected); | ||
let check = (lid, expected, options) => { | ||
assert.deepStrictEqual(db.get(lid, options), expected); | ||
}; | ||
let checkLookup = (field, value, expected) => { | ||
assert.deepStrictEqual(db.lookup(field, value), expected); | ||
let checkLookup = (field, value, expected, options) => { | ||
assert.deepStrictEqual(db.lookup(field, value, options), expected); | ||
}; | ||
@@ -548,1 +548,102 @@ | ||
check('x', {lid: 'x'}); | ||
// Recursion Limiting. | ||
db = new Database({ | ||
prev: { | ||
ref: 'next', | ||
}, | ||
next: { | ||
ref: 'prev', | ||
}, | ||
}); | ||
db.put({ | ||
lid: 'a', | ||
next: { | ||
lid: 'b', | ||
next: { | ||
lid: 'c', | ||
next: { | ||
lid: 'd', | ||
}, | ||
}, | ||
}, | ||
}); | ||
check('a', { | ||
lid: 'a', | ||
next: { | ||
lid: 'b', | ||
next: { | ||
lid: 'c', | ||
next: { | ||
lid: 'd', | ||
prev: { | ||
lid: 'c', | ||
} | ||
}, | ||
prev: { | ||
lid: 'b', | ||
} | ||
}, | ||
prev: { | ||
lid: 'a', | ||
}, | ||
}, | ||
}, {maxDepth: 0}); | ||
check('a', { | ||
lid: 'a', | ||
next: { | ||
lid: 'b', | ||
next: { | ||
lid: 'c', | ||
next: { | ||
lid: 'd', | ||
prev: { | ||
lid: 'c', | ||
} | ||
}, | ||
prev: { | ||
lid: 'b', | ||
} | ||
}, | ||
prev: { | ||
lid: 'a', | ||
}, | ||
}, | ||
}, {maxDepth: 100}); | ||
check('a', { | ||
lid: 'a', | ||
next: { | ||
lid: 'b', | ||
next: { | ||
lid: 'c', | ||
}, | ||
prev: { | ||
lid: 'a', | ||
}, | ||
}, | ||
}, {maxDepth: 1}); | ||
check('a', { | ||
lid: 'a', | ||
next: { | ||
lid: 'b', | ||
next: { | ||
lid: 'c', | ||
next: { | ||
lid: 'd', | ||
}, | ||
prev: { | ||
lid: 'b', | ||
}, | ||
}, | ||
prev: { | ||
lid: 'a', | ||
}, | ||
}, | ||
}, {maxDepth: 2}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
226
31357
8
1147