Skip to content

Commit

Permalink
Creating dump(), override(), & transform()
Browse files Browse the repository at this point in the history
Updating README

Building
  • Loading branch information
avoidwork committed Sep 19, 2015
1 parent 5c80753 commit f4a37e1
Show file tree
Hide file tree
Showing 8 changed files with 467 additions and 80 deletions.
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,23 @@ store.set(null, {abc: true}).then(function (rec) {
});
```
**dump( type="records" )**
_Array_ or _Object_
Returns the records or indexes of the DataStore as mutable `Array` or `Object`, for the intention of reuse/persistent storage without relying on an adapter which would break up the data set.
Example of deleting a record:
```javascript
var store = haro();

// Data is loaded

var records = store.dump();
var indexes = store.dump('indexes');

// Save records & indexes
```
**entries()**
_MapIterator_
Expand Down Expand Up @@ -483,7 +500,7 @@ console.log(ds1.length === ds2.length); // true
console.log(JSON.stringify(ds1[0][1]) === JSON.stringify(ds2[0][1])); // false
```

**load( [adapter=mongo, key] )**
**load( [adapter="mongo", key] )**
_Promise_

Loads the DataStore, or a record from a specific persistent storage & updates the DataStore. The DataStore will be cleared
Expand All @@ -505,6 +522,23 @@ store.map(function (value) {
});
```

**override( data[, type="records"] )**
_Promise_

Returns a `Promise` for the new state. This is meant to be used in a paired override of the indexes & records, such that
you can avoid the `Promise` based code path of a `batch()` insert or `load()`.

Example of mapping a DataStore:
```javascript
var store = haro();
store.override({'field': {'value': ['pk']}}, "indexes").then(function () {
// Indexes have been overridden, no records though! override as well?
}, function (e) {
console.error(e.stack);
});
```

**reindex( [index] )**
_Haro_

Expand Down Expand Up @@ -743,6 +777,22 @@ store.batch(data, 'set').then(function (records) {
});
```

**transform( input )**
_Mixed_

Transforms `Map` to `Object`, `Object` to `Map`, `Set` to `Array`, & `Array` to `Set`.

```javascript
var store = haro(null, {key: 'guid', index: ['name'}),
data = [{guid: 'abc', name: 'John Doe', age: 30}, {guid: 'def', name: 'Jane Doe', age: 28}];
store.batch(data, 'set').then(function (records) {
console.log(store.transform(store.indexes)); // {age: {'28': ['def'], '30': ['abc']}, name: {'John Doe': ['abc'], 'Jane Doe': ['def']}}
}, function (e) {
console.error(e.stack || e.message || e);
});
```

**unload( [adapter=mongo, key] )**
_Promise_

Expand Down
73 changes: 71 additions & 2 deletions lib/haro.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @copyright 2015
* @license BSD-3-Clause
* @link http://haro.rocks
* @version 1.5.5
* @version 1.6.0
*/
"use strict";

Expand Down Expand Up @@ -358,6 +358,18 @@ class Haro {
return defer.promise;
}

dump (type = "records") {
let result;

if (type === "records") {
result = this.toArray(null, false);
} else {
result = this.transform(this.indexes);
}

return result;
}

entries () {
return this.data.entries();
}
Expand Down Expand Up @@ -475,6 +487,28 @@ class Haro {
return tuple.apply(tuple, result);
}

override (data, type = "records") {
let defer = deferred();

if (type === "indexes") {
this.indexes = this.transform(data);
defer.resolve(true);
} else if (type === "records") {
data.forEach(datum => {
let key = datum[this.key] || uuid();

this.data.set(key, datum);
this.registry.push(key);
});
this.total = this.data.size;
defer.resolve(true);
} else {
defer.reject(new Error("Invalid type"));
}

return defer.promise;
}

register (key, fn) {
adapter[key] = fn;
}
Expand Down Expand Up @@ -881,6 +915,41 @@ class Haro {
}, {}));
}

transform (input) {
let result;

switch (true) {
case input instanceof Map:
result = {};
input.forEach((value, key) => {
result[key] = this.transform(value);
});
break;
case input instanceof Set:
result = [];
input.forEach(i => {
result.push(this.transform(i));
});
break;
case input instanceof Array:
result = new Set();
input.forEach(i => {
result.add(this.transform(i));
});
break;
case input instanceof Object:
result = new Map();
Object.keys(input).forEach(i => {
result.set(i, this.transform(input[i]));
});
break;
default:
result = input;
}

return result;
}

unload (type = "mongo", key = undefined) {
let id = key !== undefined ? key : this.id;

Expand Down Expand Up @@ -912,7 +981,7 @@ function factory (data = null, config = {}, indexes = []) {
return new Haro(data, config, indexes);
}

factory.version = "1.5.5";
factory.version = "1.6.0";

// Node, AMD & window supported
if (typeof exports !== "undefined") {
Expand Down
Loading

0 comments on commit f4a37e1

Please sign in to comment.