Comparing version 0.0.1 to 0.0.2
{ | ||
"name" : "tinydb", | ||
"description" : "tiny json file based datebase for small nodejs projects", | ||
"version" : "0.0.1", | ||
"version" : "0.0.2", | ||
"author" : "iSayme <isaymeorg@gmail.com>", | ||
@@ -6,0 +6,0 @@ "repository" : { |
@@ -22,5 +22,25 @@ Purpose | ||
console.log('database is ready for operating'); | ||
// set info to DB | ||
test_db.setInfo('title', 'Test DB', function(err, key, value) { | ||
if (err) { | ||
console.log(err); | ||
return; | ||
} | ||
console.log('[setInfo] ' + key + ' : ' + value); | ||
}); | ||
// get info from DB | ||
test_db.getInfo('title', function(err, key, value) { | ||
if (err) { | ||
console.log(err); | ||
return; | ||
} | ||
console.log('[getInfo] ' + key + ' : ' + value); | ||
}); | ||
// do other things below | ||
todos.forEach(function (err, item) { | ||
test_db.forEach(function (err, item) { | ||
if (err) { | ||
@@ -30,3 +50,3 @@ console.log(err); | ||
} | ||
for (var key in item) { | ||
@@ -42,3 +62,3 @@ console.log(key + ' : ' + item[key]); | ||
.close(err, callback) | ||
.close(callback) | ||
------ | ||
@@ -45,0 +65,0 @@ Close opened databse, and write the data in memory to your specified databse file. |
fs = require('fs'); | ||
function TinyDB(opts) { | ||
var self = this; | ||
if (!(this instanceof TinyDB)) { | ||
@@ -15,3 +17,3 @@ return this; | ||
if (typeof opts === 'string') { | ||
this.options.file = opts; | ||
this.options.file = opts; | ||
} else if (typeof opts === 'object') { | ||
@@ -30,2 +32,6 @@ for (var idx in opts) { | ||
process.on('exit', function() { | ||
self._save(-1); | ||
}); | ||
return this; | ||
@@ -67,15 +73,30 @@ } | ||
if (this._timeoutObj) { | ||
clearTimeout(this._timeoutObj); | ||
if (typeof delay === 'number' && 0 > delay) { | ||
if (self._timeoutObj) { | ||
clearTimeout(self._timeoutObj); | ||
delete self._timeoutObj; | ||
} | ||
fs.writeFileSync(self.options.file, JSON.stringify(self._data), 'utf8'); | ||
return callback && callback(null); | ||
} else { | ||
if (this._timeoutObj) { | ||
return callback && callback(null); | ||
} | ||
this._timeoutObj = setTimeout(function() { | ||
delete self._timeoutObj; | ||
fs.writeFile(self.options.file, JSON.stringify(self._data), 'utf8', function(err) { | ||
if (err) throw err; | ||
if (typeof delay === 'function') { | ||
return delay(null); | ||
} else { | ||
return callback && callback(null); | ||
} | ||
}); | ||
}, typeof delay === 'number' ? delay : 10000); | ||
} | ||
this._timeoutObj = setTimeout(function() { | ||
fs.writeFile(self.options.file, JSON.stringify(self._data), 'utf8', function(err) { | ||
if (err) throw err; | ||
return callback && callback(); | ||
}); | ||
}, delay === undefined ? 10000 : delay); | ||
} | ||
TinyDB.prototype.close = function(err, callback) { | ||
TinyDB.prototype.close = function(callback) { | ||
var self = this; | ||
@@ -98,3 +119,3 @@ | ||
self._data = {}; | ||
return callback && callback(); | ||
return callback && callback(null); | ||
}); | ||
@@ -126,2 +147,3 @@ } | ||
this._data[key] = value; | ||
this._save(); | ||
return callback && callback(null, key, value); | ||
@@ -142,5 +164,9 @@ } | ||
TinyDB.prototype.forEach = function(err, callback) { | ||
TinyDB.prototype.forEach = function(callback) { | ||
if (typeof callback !== 'function') { | ||
return; | ||
} | ||
if ('ready' !== this._state) { | ||
return callback && callback(new Error('database not ready.')); | ||
return callback(new Error('database not ready.')); | ||
} | ||
@@ -150,3 +176,3 @@ | ||
if (this._data.data[i]._id) { | ||
return callback && callback(null, this._data.data[i]); | ||
callback(null, this._data.data[i], i); | ||
} | ||
@@ -160,3 +186,7 @@ } | ||
} | ||
if (typeof callback !== 'function') { | ||
return; | ||
} | ||
var arr = []; | ||
@@ -177,3 +207,7 @@ var flag = true; | ||
return callback && callback(null, arr); | ||
if (arr.length) { | ||
return callback(null, arr); | ||
} else { | ||
return callback(new Error('not found')); | ||
} | ||
} | ||
@@ -205,21 +239,5 @@ | ||
return callback && callback(err, item, idx); | ||
return callback && callback(null, item, idx); | ||
} | ||
}); | ||
/* | ||
if ('ready' !== this._state) { | ||
return callback && callback(new Error('database not ready.')); | ||
} | ||
for (var i = 0; i < this._data.data.length; i++) { | ||
if (this._data.data[i] | ||
&& this._data.data[i]._id === id) { | ||
var item = this._data.data[i]; | ||
this._data.data.splice(i, 1); | ||
return callback && callback(null, item, i); | ||
} | ||
} | ||
return callback && callback(new Error('not found')); | ||
*/ | ||
} | ||
@@ -238,3 +256,3 @@ | ||
this._save(); | ||
return func && func(null, item, index >= (this._data.data.length - 1) ? (this._data.data.length - 1) : index); | ||
return func && func(null, item, index > (this._data.data.length - 1) ? (this._data.data.length - 1) : index); | ||
} | ||
@@ -241,0 +259,0 @@ |
12767
299
91