Skip to content

Commit

Permalink
Merge pull request #28 from GoblinDBRocks/dev
Browse files Browse the repository at this point in the history
Update pre-refactor
  • Loading branch information
UlisesGascon authored Nov 7, 2017
2 parents 5920a0a + e5faee6 commit bd0b0ab
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 21 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
![shieldsIO](https://img.shields.io/github/release/UlisesGascon/GoblinDB.svg)
![shieldsIO](https://img.shields.io/github/license/UlisesGascon/GoblinDB.svg)
![shieldsIO](https://img.shields.io/david/UlisesGascon/GoblinDB.svg)
![Travis](https://travis-ci.org/GoblinDBRocks/GoblinDB.svg?branch=master)
[![Build Status](https://travis-ci.org/GoblinDBRocks/GoblinDB.svg?branch=master)](https://travis-ci.org/GoblinDBRocks/GoblinDB)

# [GoblinDB](http://goblindb.osweekends.com/)

Expand Down Expand Up @@ -216,3 +216,7 @@ Just to solve issues with NPM.
**Notes:**
Just a "Hello world"

----------------------------------

Dev status: [![Build Status](https://travis-ci.org/GoblinDBRocks/GoblinDB.svg?branch=dev)](https://travis-ci.org/GoblinDBRocks/GoblinDB)

94 changes: 74 additions & 20 deletions goblin.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,38 +224,61 @@ module.exports = function(config){
},
get: function (point) {
if(point && typeof(point) === "string"){
return goblin.db[point];
var tree = point.split('.'),
parent = goblin.db;

for (var i = 0; i < tree.length; i++) {
if(i !== tree.length-1) {
if(typeof parent[tree[i]] === 'undefined') {
// If there is no child here, won't be deeper. Return undefined
return undefined;
}
parent = parent[tree[i]];
} else {
return parent[tree[i]];
}
}
} else {
return goblin.db;
}
},
push: function (data){
var newKey = randomstring.generate();
push: function (data, point){
if(!point) {
point = '';
} else if(typeof(point) === 'string') {
point = point + '.';
} else {
throw configGoblin.logPrefix, 'Database saving error: Invalid reference point type provided to push. Only string allowed.'
}

var newKey = point + randomstring.generate();

if(data && typeof(data) === "object"){
setDeep(data, newKey, true);
goblinDataEmitter.emit('change', {'type': 'push', 'value': data, 'key': newKey});
goblin.db[newKey] = data;
} else {
throw configGoblin.logPrefix, 'Database saving error: no data provided or data is not an object/Array.';
}
},
set: function(data, point){
if(point && typeof(point) === "string" && data && typeof(data) === "object"){
goblinDataEmitter.emit('change', {'type': 'set', 'value': data, 'oldValue': goblin.db[point], 'key': point});
goblin.db[point] = data;
} else if (!point && data && typeof(data) === "object"){
goblinDataEmitter.emit('change', {'type': 'set', 'value': data, 'oldValue': goblin.db});
goblin.db = data;
} else {
throw configGoblin.logPrefix, 'Database saving error: no data provided or data is not an object/Array.';
};
},
set: setDeep,
update: function(data, point){
if(point && typeof(point) === "string" && typeof(data) === "object"){
var oldValue = goblin.db[point];
goblin.db[point] = _.merge({}, goblin.db, data);
goblinDataEmitter.emit('change', {'type': 'update', 'value': goblin.db[point], 'oldValue': oldValue, 'key': point});
} else if (!point && typeof(data) === "object"){
if(point && typeof(point) === "string" && typeof(data) === "object") {
var tree = point.split('.'),
parent = goblin.db;

for (var i = 0; i < tree.length; i++) {
if(i !== tree.length-1) {
if(typeof parent[tree[i]] === 'undefined') {
parent[tree[i]] = {};
}
parent = parent[tree[i]];
} else {
var oldValue = parent[tree[i]];
parent[tree[i]] = _.merge({}, goblin.db, data);
goblinDataEmitter.emit('change', {'type': 'update', 'value': parent[tree[i]], 'oldValue': oldValue, 'key': point});
}
}
} else if (!point && typeof(data) === "object") {
var oldValue = goblin.db;
goblin.db = _.merge({}, goblin.db, data);
goblinDataEmitter.emit('change', {'type': 'update', 'value': goblin.db, 'oldValue': oldValue});
Expand All @@ -266,3 +289,34 @@ module.exports = function(config){
}

}


function setDeep(data, point, silent){
if(point && typeof(point) === "string" && data && typeof(data) === "object") {
var tree = point.split('.'),
parent = goblin.db;

for (var i = 0; i < tree.length; i++) {
if(i !== tree.length-1) {
if(typeof parent[tree[i]] === 'undefined') {
parent[tree[i]] = {};
}
parent = parent[tree[i]];
} else {
if(!silent) {
goblinDataEmitter.emit('change', {'type': 'set', 'value': data, 'oldValue': goblin.db[point], 'key': point});
}
parent[tree[i]] = data;
}
}
} else if (!point && data && typeof(data) === "object" && !Array.isArray(data)) {
if(!silent) {
goblinDataEmitter.emit('change', {'type': 'set', 'value': data, 'oldValue': goblin.db});
}
goblin.db = data;
} else if (!point && data && (data instanceof Array)) {
throw configGoblin.logPrefix, 'Database saving error: Setting all the db to an Array is forbiden. Database must be an object.';
} else {
throw configGoblin.logPrefix, 'Database saving error: no data provided or data is not an object/Array.';
}
}
12 changes: 12 additions & 0 deletions test/goblin.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,18 @@ describe("Database", function() {
goblinDB.push({"more":"data"})
expect(Object.keys(goblinDB.get()).length).to.be.equal(4);
});
it("Deep method set(): Create a deep object", function() {
goblinDB.set({are: 'deep'}, 'internal.references.in.goblin');
expect(goblinDB.get("internal")).to.deep.equal({"references": {"in": {"goblin": {"are": "deep"}}}}); // internal.references.in.goblin.are.deep
});
it("Deep method get(): Get a deep node", function() {
expect(goblinDB.get('internal.references.in.goblin.are')).to.deep.equal('deep');
});
it("Deep method push(): Push two objects deep", function() {
goblinDB.push({"deeper":"than expected"}, 'internal.references.in.goblin.push');
goblinDB.push({"cooler":"than expected"}, 'internal.references.in.goblin.push');
expect(Object.keys(goblinDB.get('internal.references.in.goblin.push')).length).to.be.equal(2);
});
it("Method getConfig(): Content", function() {
expect(goblinDB.getConfig()).to.deep.equal({"fileName": "./test/testDB", "files": {"ambush": "./test/testDB.goblin", "db": "./test/testDB.json"}, logPrefix: '[GoblinDB]', recordChanges: true });
});
Expand Down

0 comments on commit bd0b0ab

Please sign in to comment.