TinyData
Simple and tiny DB engine, driven by RegExp and inspired by map-reduce.
Description:
TinyData implement simple 'text-like' searching in any deep-structure data, for example as MongoDB inner collection, with RegExp or simple function. Work in node and in browser too.
Install:
In node.js
npm install tiny-data
At browser (see 'lib_browser' dir)
<script src="tinydata.min.js"></script>
Example:
let tinyDataObj = exampleData let gradeRule = /{2}grades\.\./ let { let count = valueslength let result = values } let averageGrades = tinyDataObj console
With dataset as
result for questions "What total average score by student" will be:
Usage:
Constructor
let tinyDataObj = dataset/*Object*/
Create new object with dataset.
Dataset wil be stringified internal like this
let dataset = foo: 102030 bar: baz: 'one' rebar: 'two' quix: 2 // internal represention as array (simplified - realy dots '.' not a dot but some internal delimiter) "foo.0.10" "foo.1.20" "foo.2.30" "bar.baz.one" "bar.rebar.two" "quix.2"
Search methods
let result = tinyDataObj
Do search with some rule
and process selected data with finalizer
.
About rule
In depth - rule
may be RegExp or function. If rule
is RegExp - it may be usefull set interpSequence
- how interpretate matched data - by default it {key: 1, value: 2}
- first RegExp capture group us key and second - values.
In case of reverse position as {key: 2, value: 1}
to readability code will be used tinyDataObj.searchBack()
method.
If rule
is function it must have next structure
let { // logic to process stringifiedItem }
About finalizer
As last step selected data may be prosecced with finalizer
- its function with next structure
let { // logic to process data }
Its called for every 'key', selected in first stage, but MAY return different key (even duplicated) and any value as result
Internal delimiter
let internalDelimiter = tinyDataObj
This method return internal delimiter, which used internal instead of dots ('.'), to separete full stringified value.
In case of RegExp used at search
- all transformation cloaked, but in case of function used - internal delimiter must be used. For example:
// search for black color at foo at { foo : { color : 'black' }, bar : { color: 'white'} }if stringifiedItem === `foocolorblack` // some logic
RegExp transfomtation
// search for white color at foo at { foo : { color : 'black' }, bar : { color: 'white'} }let color = 'white'let re = `[^.]\\.color\\.`let internalRegExp = tinyDataObj if stringifiedItem // some logic
To simplify delimiter transfomtation and use dots ('.') for readability, doTransormRegExp
may be used
Data by path helper
let path = 'foo.color'let fooColor = tinyDataObj
Helper to get data by path
General Notes:
In some cases TinyData
may be faster and cleaner than routine forEach cycles. In some cases not.