node-json-db
Advanced tools
Comparing version
@@ -12,2 +12,10 @@ (function () { | ||
/** | ||
* Create the JSON database | ||
* @param filename where to save the data base | ||
* @param saveOnPush saving on modification of the data | ||
* @param humanReadable is the json file humand readable | ||
* @returns {JsonDB} | ||
* @constructor | ||
*/ | ||
var JsonDB = function (filename, saveOnPush, humanReadable) { | ||
@@ -185,2 +193,6 @@ | ||
dbData.delete(); | ||
if (this.saveOnPush) { | ||
this.save(); | ||
} | ||
}; | ||
@@ -187,0 +199,0 @@ /** |
@@ -7,6 +7,6 @@ /** | ||
var DataError = require("../lib/Errors").DataError; | ||
function isInt(value) { | ||
return !isNaN(value) && | ||
parseInt(Number(value)) == value && | ||
!isNaN(parseInt(value, 10)); | ||
parseInt(Number(value)) == value && !isNaN(parseInt(value, 10)); | ||
} | ||
@@ -19,4 +19,6 @@ | ||
this.append = index === ""; | ||
if(isInt(this.index)) { | ||
if (isInt(this.index)) { | ||
this.index = parseInt(this.index); | ||
} else if (!this.append) { | ||
throw new DataError("Only numerical values accepted for array index", 200) | ||
} | ||
@@ -39,12 +41,6 @@ } | ||
if(dataIterable.length === 0) { | ||
if (dataIterable.length === 0) { | ||
return 0; | ||
} | ||
for (var key in dataIterable) { | ||
if (!dataIterable.hasOwnProperty(key)) { | ||
continue; | ||
} | ||
index = key; | ||
} | ||
return dataIterable.length - 1; | ||
} | ||
@@ -51,0 +47,0 @@ return index; |
{ | ||
"name": "node-json-db", | ||
"version": "0.6.5", | ||
"version": "0.7.1", | ||
"description": "Database using JSON file as storage for Node.JS", | ||
@@ -5,0 +5,0 @@ "main": "./JsonDB.js", |
@@ -116,6 +116,2 @@ [](http://travis-ci.org/Belphemur/node-json-db) [](https://coveralls.io/r/Belphemur/node-json-db?branch=master) | ||
//The index can be also non-numerical | ||
//This will create an array 'myarray' with the object '{obj:'test'}' with key test | ||
db.push("/arraytest/myarray[test]", {obj:'test'}, true); | ||
//You can retrieve a property of an object included in an array | ||
@@ -149,3 +145,3 @@ //testString = 'test'; | ||
//This will return 3 | ||
db.get("/arraytest/lastItemArray[-1]"); | ||
db.getData("/arraytest/lastItemArray[-1]"); | ||
@@ -158,3 +154,3 @@ | ||
//This will return 2 since 3 just got removed | ||
db.get("/arraytest/lastItemArray[-1]"); | ||
db.getData("/arraytest/lastItemArray[-1]"); | ||
``` | ||
@@ -179,3 +175,4 @@ ### Exception/Error | ||
|DataPath: /XXX. Can't find index INDEX in array YYY |DataError |When trying to access a non-existent index in the array. | | ||
|Can't Load Database: XXXX |DatabaseError |JsonDB can't load the database for "err" reason. You can find the nested error in **error.inner** | | ||
|Only numerical values accepted for array index |DataError |An array can have string index. For this use the normal object. | | ||
|Can't Load Database: XXXX |DatabaseError |JsonDB can't load the database for "err" reason. You can find the nested error in **error.inner** | | ||
|Can't save the database: XXX |DatabaseError |JsonDB can't save the database for "err" reason. You can find the nested error in **error.inner** | | ||
@@ -182,0 +179,0 @@ |DataBase not loaded. Can't write |DatabaseError |Since the database hasn't been loaded correctly, the module won't let you save the data to avoid erasing your database. | |
@@ -14,2 +14,3 @@ var expect = require("expect.js"); | ||
var testFile5 = "test/test_file_empty"; | ||
var testFile6 = "test/test_delete"; | ||
describe('JsonDB', function () { | ||
@@ -204,16 +205,11 @@ describe('Exception/Error', function () { | ||
it('should create an array with a string at index TEST', function () { | ||
db.push('/arraytest/myarray[TEST]', "works", true); | ||
var myarray = db.getData('/arraytest/myarray'); | ||
expect(myarray).to.be.an('array'); | ||
expect(myarray['TEST']).to.be('works'); | ||
it('should throw an Error when using an array with a string at index TEST', function () { | ||
expect(function (args) { | ||
db.push('/arraytest/myarray[TEST]', "works", true); | ||
}).withArgs("/arraytest/arrayTesting[1]").to.throwException(function (e) { | ||
expect(e).to.be.a(DataError); | ||
expect(e).to.have.property('id', 200); | ||
}); | ||
}); | ||
it('should create an array with a string at index "TEST test"', function () { | ||
db.push('/arraytest/myarray[TEST test]', "workingTest", true); | ||
var myarray = db.getData('/arraytest/myarray'); | ||
expect(myarray).to.be.an('array'); | ||
expect(myarray['TEST test']).to.be('workingTest'); | ||
}); | ||
it('should add an object at index 1', function () { | ||
@@ -364,2 +360,20 @@ var obj = {property: "perfect"}; | ||
describe('last item', function () { | ||
it('should throw an exception when array is empty when using -1', function () { | ||
db.push('/arraylast/myarrayempty', [], true); | ||
expect(function (args) { | ||
db.getData(args); | ||
}).withArgs('/arraylast/myarrayempty[-1]').to.throwException(function (e) { | ||
expect(e).to.be.a(DataError); | ||
expect(e).to.have.property('id', 10); | ||
}); | ||
}); | ||
it('should set the fist item when using -1 on empty array', function () { | ||
db.push('/arraylast/emptyArray', [], true); | ||
db.push('/arraylast/emptyArray[-1]', 3); | ||
var lastItem = db.getData('/arraylast/emptyArray[0]'); | ||
expect(lastItem).to.be(3); | ||
}); | ||
it('should return the last key when using -1', function () { | ||
@@ -385,2 +399,30 @@ db.push('/arraylast/myarray', [1, 2, 3], true); | ||
}); | ||
describe('Delete Info', function(){ | ||
var db = new JsonDB(testFile6, true); | ||
it('should delete the data and save the file if saveOnPush is set', function (done) { | ||
var object = {test: {readable: "test"}}; | ||
db.push("/", object); | ||
fs.readFile(testFile6 + ".json", "utf8", function (err, data) { | ||
if (err) { | ||
done(err); | ||
return; | ||
} | ||
expect(data).to.be(JSON.stringify(object)); | ||
db.delete('/test'); | ||
fs.readFile(testFile6 + ".json", "utf8", function (err, data) { | ||
if (err) { | ||
done(err); | ||
return; | ||
} | ||
expect(data).to.be(JSON.stringify({})); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('Cleanup', function () { | ||
@@ -393,2 +435,3 @@ it('should remove the test files', function () { | ||
fs.unlinkSync(testFile5 + ".json"); | ||
fs.unlinkSync(testFile6 + ".json"); | ||
fs.rmdirSync("test/dirCreation"); | ||
@@ -395,0 +438,0 @@ }); |
49833
4.12%928
4.86%178
-1.66%