You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

node-json-db

Package Overview
Dependencies
Maintainers
1
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-json-db - npm Package Compare versions

Comparing version

to
0.4.1

62

JsonDB.js

@@ -60,6 +60,6 @@ (function () {

var last = path.pop();
return new DBParentData(last, this._getData(path, create), this);
return new DBParentData(last, this._getData(path, create), this, dataPath);
};
/**
* Get the deta sotred in the data base
* Get the deta stored in the data base
* @param dataPath path leading to the data

@@ -74,16 +74,25 @@ * @returns {*}

JsonDB.prototype._getData = function (dataPath, create) {
this.load();
create = create || false;
if (dataPath.length === 0) {
return this.data;
}
var data = this.data;
for (var i in dataPath) {
if (dataPath.hasOwnProperty(i)) {
var property = dataPath[i];
function recursiveProcessDataPath(data, index) {
var property = dataPath[index];
/**
* Find the wanted Data or create it.
*/
function findData(isArray) {
isArray = isArray || false;
if (data.hasOwnProperty(property)) {
data = data[property];
} else if (create) {
data[property] = {};
if (isArray) {
data[property] = [];
} else {
data[property] = {};
}
data = data[property];

@@ -94,5 +103,34 @@ } else {

}
var arrayInfo = JsonUtils.processArray(property);
if (arrayInfo) {
property = arrayInfo.property;
findData(true);
if (!Array.isArray(data)) {
throw new DataError("DataPath: /" + dataPath.join("/") + ". " + property + " is not an array.", 11);
}
if (data.hasOwnProperty(arrayInfo.index)) {
data = data[arrayInfo.index];
} else if (create) {
data[arrayInfo.index] = {};
data = data[arrayInfo.index];
} else {
throw new DataError("DataPath: /" + dataPath.join("/") + ". Can't find index " + arrayInfo.index + " in array " + property, 10);
}
} else {
findData();
}
if (dataPath.length == ++index) {
return data;
}
return recursiveProcessDataPath(data, index);
}
return data;
if (dataPath.length === 0) {
return this.data;
}
return recursiveProcessDataPath(this.data, 0);
};

@@ -199,3 +237,3 @@ /**

exports = module.exports = JsonDB;
module.exports = JsonDB;
})();

@@ -1,7 +0,11 @@

(function() {
(function () {
"use strict";
function DBParentData(parent, data, jsonDB) {
var JsonUtils = require('./utils');
var DataError = require("../lib/Errors").DataError;
function DBParentData(parent, data, jsonDB, dataPath) {
this.parent = parent;
this.data = data;
this.db = jsonDB;
this.dataPath = dataPath;

@@ -15,3 +19,3 @@ }

return this.data[this.parent];
}
};

@@ -21,6 +25,16 @@ DBParentData.prototype.setData = function (toSet) {

this.db.data = toSet;
return;
}
var arrayInfo = JsonUtils.processArray(this.parent);
if (arrayInfo) {
if (!this.data.hasOwnProperty(arrayInfo.property)) {
this.data[arrayInfo.property] = [];
} else if(!Array.isArray(this.data[arrayInfo.property])) {
throw new DataError("DataPath: /" + this.dataPath + ". " + arrayInfo.property + " is not an array.", 11);
}
this.data[arrayInfo.property][arrayInfo.index] = toSet;
} else {
this.data[this.parent] = toSet;
}
}
};

@@ -31,6 +45,14 @@ DBParentData.prototype.delete = function () {

}
delete this.data[this.parent];
}
var arrayInfo = JsonUtils.processArray(this.parent);
if (arrayInfo) {
if(!this.data[arrayInfo.property].hasOwnProperty(arrayInfo.index)) {
throw new DataError("DataPath: /" + this.dataPath + ". Can't find index " + arrayInfo.index + " in array " + arrayInfo.property, 10);
}
delete this.data[arrayInfo.property][arrayInfo.index];
} else {
delete this.data[this.parent];
}
};
exports = module.exports = DBParentData;
})();
(function () {
"use strict";
var arrayIndexRegex = /(.+)\[(\d+)\]/;
var endsWith = function (str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
};
/**
* Check if the property want to access an Array
* @returns {undefined}
*/
var processArray = function(property) {
var match = arrayIndexRegex.exec(property);
var result;
if (match != null) {
result = {};
result.property = match[1];
result.index = match[2];
}
return result;
};
//

@@ -37,4 +52,5 @@ // Code from: https://github.com/rxaviers/cldr

strEndWith: endsWith,
mergeObject: merge
mergeObject: merge,
processArray: processArray
}
})();

10

package.json
{
"name": "node-json-db",
"version": "0.3.2",
"version": "0.4.1",
"description": "Database using JSON file as storage for Node.JS",

@@ -28,5 +28,9 @@ "main": "./JsonDB.js",

"devDependencies": {
"mocha": ">=1.17.0",
"expect.js": ">=0.3.1"
"mocha": "1.17.x",
"expect.js": "0.3.x",
"grunt": "0.4.x",
"grunt-cli": "0.1.x",
"grunt-contrib-jshint" : "0.11.x",
"grunt-simple-mocha" : "0.4.x"
}
}

@@ -103,2 +103,23 @@ [![Build Status](https://secure.travis-ci.org/Belphemur/node-json-db.png?branch=master)](http://travis-ci.org/Belphemur/node-json-db) [![Coverage Status](https://img.shields.io/coveralls/Belphemur/node-json-db.svg)](https://coveralls.io/r/Belphemur/node-json-db?branch=master)

```
###Array Support
You can also access the information stored into arrays and manipulate them.
```javascript
var JsonDB = require('node-json-db');
//The second argument is used to tell the DB to save after each push
//If you put false, you'll have to call the save() method.
//The third argument is to ask JsonDB to save the database in an human readable format. (default false)
var db = new JsonDB("myDataBase", true, false);
//This will create an array 'myarray' with the object '{obj:'test'}' at index 0
db.push("/arraytest/myarray[0]", {obj:'test'}, true);
//You can retrieve a property of an object included in an array
//testString = 'test';
var testString = db.getData("/arraytest/myarray[0]/obj");
//Doing this will delete the object stored at the index 0 of the array.
//Keep in mind this won't delete the array even if it's empty.
db.delete(("/arraytest/myarray[0]");
```
### Exception/Error

@@ -128,2 +149,8 @@ JsonDB use 2 type of Error/Exception :

####"DataPath: /" + dataPath.join("/") + ". " + property + " is not an array." *(DataError)*
When trying to access an object as an array.
####"DataPath: /" + dataPath.join("/") + ". Can't find index " + arrayInfo.index + " in array " + property *(DataError)*
When trying to access a non-existent index in the array.
####"Can't Load Database: " + err *(DatabaseError)*

@@ -130,0 +157,0 @@ JsonDB can't load the database for "err" reason.

@@ -7,3 +7,2 @@ var expect = require("expect.js");

var fs = require('fs');
var util = require('util');

@@ -13,2 +12,4 @@ var testFile1 = "test_file1";

var faulty = "test/faulty.json";
var testFile3 = "test_file3";
var testFile4 = "array_file";
describe('JsonDB', function () {

@@ -153,2 +154,115 @@ describe('Exception/Error', function () {

});
describe('Human Readable', function () {
var db = new JsonDB(testFile3, true, true);
it('should save the data in an human readable format', function (done) {
var object = {test: {readable: "test"}};
db.push("/", object);
fs.readFile(testFile3 + ".json", "utf8", function (err, data) {
if (err) {
done(err);
return;
}
expect(data).to.be(JSON.stringify(object, null, 4));
done();
});
})
});
describe('Array Support', function () {
var db = new JsonDB(testFile4, true);
it('should create an array with a string at index 0', function () {
db.push('/arraytest/myarray[0]', "test", true);
var myarray = db.getData('/arraytest/myarray');
expect(myarray).to.be.an('array');
expect(myarray[0]).to.be('test');
});
it('should add an object at index 1', function () {
var obj = {property: "perfect"};
db.push('/arraytest/myarray[1]', obj, true);
var myarray = db.getData('/arraytest/myarray');
expect(myarray).to.be.an('array');
expect(myarray[1]).to.be(obj);
});
it('should create a nested array with an object at index 0', function () {
var data = {test: "works"};
db.push('/arraytest/nested[0]/obj', data, true);
var obj = db.getData('/arraytest/nested[0]');
expect(obj).to.be.an('object');
expect(obj).to.have.property('obj', data);
});
it('should access the object at index 1', function () {
var obj = db.getData('/arraytest/myarray[1]');
expect(obj).to.be.an('object');
expect(obj).to.have.property('property', 'perfect');
});
it('should access the object property at index 1', function () {
var property = db.getData('/arraytest/myarray[1]/property');
expect(property).to.be.a('string');
expect(property).to.be('perfect');
});
it('should throw an error when accessing non-present index', function () {
var obj = {property: "perfect"};
db.push('/arraytest/arrayTesting[0]', obj, true);
expect(function (args) {
db.getData(args);
}).withArgs("/arraytest/arrayTesting[1]").to.throwException(function (e) {
expect(e).to.be.a(DataError);
expect(e).to.have.property('id', 10);
});
});
it('should delete the object at index 1', function () {
db.delete('/arraytest/myarray[1]');
expect(function (args) {
db.getData(args);
}).withArgs("/arraytest/myarray[1]").to.throwException(function (e) {
expect(e).to.be.a(DataError);
expect(e).to.have.property('id', 10);
});
});
it('should throw an error when deleting non-present index', function () {
expect(function (args) {
db.delete(args);
}).withArgs("/arraytest/myarray[10]").to.throwException(function (e) {
expect(e).to.be.a(DataError);
expect(e).to.have.property('id', 10);
});
});
it('should throw an error when trying to set an object as an array', function () {
db.push('/arraytest/fakearray', {fake: "fake"}, true);
expect(function (args) {
db.push(args, {test: 'test'}, true);
}).withArgs("/arraytest/fakearray[1]").to.throwException(function (e) {
expect(e).to.be.a(DataError);
expect(e).to.have.property('id', 11);
});
});
it('should throw an error when trying to access an object as an array', function () {
db.push('/arraytest/fakearray', {fake: "fake"}, true);
expect(function (args) {
db.getData(args);
}).withArgs("/arraytest/fakearray[1]").to.throwException(function (e) {
expect(e).to.be.a(DataError);
expect(e).to.have.property('id', 11);
});
});
it('should throw an error when trying to set an object as an array (2)', function () {
db.push('/arraytest/fakearray', {fake: "fake"}, true);
expect(function (args) {
db.push(args, {test: 'test'}, true);
}).withArgs("/arraytest/fakearray[1]/fake").to.throwException(function (e) {
expect(e).to.be.a(DataError);
expect(e).to.have.property('id', 11);
});
});
});
describe('Cleanup', function () {

@@ -158,2 +272,4 @@ it('should remove the test files', function () {

fs.unlinkSync(testFile2 + ".json");
fs.unlinkSync(testFile3 + ".json");
fs.unlinkSync(testFile4 + ".json");
fs.rmdirSync("dirCreation");

@@ -160,0 +276,0 @@ });

Sorry, the diff of this file is not supported yet