What it does
Stores your data in an elegant way that will give you great flexibility and leverage. This library is designed to be used as a state management solution for applications that need to be performant for quickly mutating state over many different types of entities with similar characteristics. Applications such as games and simulations. If performance isn't this crucial then check out DataScript which provides immutability, versioning, undo/redo, and a powerful datalog query engine.
All data stored in Wharf is organized uniformly as
- Entity - just an id (i.e. 1)
- Attribute - an attribute of the entity (i.e. name or age)
- Value - the value of the attribute (i.e. "bob" or 50)
For example let's describe some shapes.
- entity 1 is a blue square
- entity 2 is a red triangle
- entity 3 is a red circle
1 | color | blue |
1 | width | 20 |
1 | height | 20 |
1 | n_edges | 4 |
2 | color | red |
2 | base | 10 |
2 | height | 40 |
2 | n_edges | 3 |
3 | color | red |
3 | radius | 20 |
3 | n_edges | 0 |
Although there are 3 distinct types we can perform generic queries that cross types.
All entities that have a height
attribute returns [ 1, 2 ]
All entities that have a height
and a width
attribute returns [ 1 ]
All entities with a value 20
returns [ 1, 3 ]
1 | width | 20 |
1 | height | 20 |
3 | radius | 20 |
All entities where color == red
returns [ 2, 3 ]
How to use it
var db = Wharf();
var square_id = db.add({color: "blue", width: 20, height: 20, n_edges: 4});
var triangle_id = db.add({color: "red", base: 10, height: 40, n_edges: 3});
var circle_id = db.add({color: "blue", radius: 20});
db.set(circle_id, {color: "red", n_edges: 0});
db.get(circle_id);
db.q({a: ["height"]});
db.q({a: ["height", "width"]});
db.q({v: [20]});
db.q({av: [["color", "red"]]});
db.remove(square_id);
db.remove(square_id);
Installing
There are 3 ways to include this library
With browserify
$ npm install --save entity-wharf
Then use it
var Wharf = require('entity-wharf');
...
With a script tag
Download this script then include it in your html
<script src="entity-wharf.min.js"></script>
Then use it
var Wharf = ENTITY_WHARF;
...
With RequireJS
Download and use this minified script then require it
require(['entity-wharf'], function(Wharf) {
...
});
License
The MIT License (MIT)
Copyright (c) 2014 Small Helm LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.