New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

tinydb

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tinydb - npm Package Compare versions

Comparing version 0.0.3 to 0.1.0

2

package.json
{
"name" : "tinydb",
"description" : "tiny json file based datebase for small nodejs projects",
"version" : "0.0.3",
"version" : "0.1.0",
"author" : "iSayme <isaymeorg@gmail.com>",

@@ -6,0 +6,0 @@ "repository" : {

@@ -60,6 +60,5 @@ Purpose

.close(callback)
.flush(callback)
------
Close opened databse, and write the data in memory to your specified databse file.
All other APIs calling will be disable by sending a error type to callback function.
Write the data in memory to your specified databse file immediately.

@@ -66,0 +65,0 @@ .find(query, callback)

@@ -8,3 +8,3 @@ TinyDB = require('../tinydb');

console.log('========> ' + name);
console.log('\n========> ' + name);
func();

@@ -14,3 +14,3 @@ }

//todos = new TinyDB({file: './test.db'});
todos = new TinyDB('./test.db');
todos = TinyDB('./test.db');

@@ -20,3 +20,3 @@ todos.onReady = function() {

test('.forEach', false, function() {
test('.forEach', true, function() {
todos.forEach(function (item) {

@@ -33,23 +33,22 @@ for (var key in item) {

test('.findById', false, function() {
todos.findById('53599ebd2935fa7c82178097', function(err, item) {
if (err) { console.log(err); return; }
console.log(item);
test('.setInfo/.getInfo', true, function() {
todos.getInfo('testinfo', function(err, value) {
console.log('getInfo [testinfo] return {err: ' + err + ', value: ' + value + '}');
});
todos.findById('53599ebd2935a7c82x178097', function(err, item) {
if (err) { console.log(err); return; }
console.log(item);
todos.setInfo('testinfo', 'testinfo value', function(err, value) {
console.log('setInfo [testinfo] return {err: ' + err + ', value: ' + value + '}');
});
todos.getInfo('testinfo', function(err, value) {
console.log('getInfo [testinfo] return {err: ' + err + ', value: ' + value + '}');
});
});
test('.findByIdAndRemove', false, function() {
todos.findByIdAndRemove('53599ebd2935fa7c82178097', function(err, item) {
test('.findById', true, function() {
todos.findById('53599ebd2935fa7c82178097', function(err, item) {
if (err) { console.log(err); return; }
console.log(item);
});
todos.findByIdAndRemove('53599ebd2935a7c82x178097', function(err, item) {
todos.findById('53599ebd2935a7c82x178097', function(err, item) {
if (err) { console.log(err); return; }

@@ -61,25 +60,2 @@

test('.setInfo/.getInfo', false, function() {
todos.getInfo('description', function(err, value) {
console.log(arguments);
});
todos.getInfo('testinfo', function(err, value) {
console.log(arguments);
});
todos.setInfo('testinfo', 'testinfo value', function(err, value) {
console.log(arguments);
});
todos.getInfo('testinfo', function(err, value) {
console.log(arguments);
});
});
test('.close', false, function() {
todos.findByIdAndRemove('53599ebd2935fa7c82178097', function(err, item) {
if (err) { console.log(err); return; }
console.log(item);
todos.close();
});
});
test('.insertItem', true, function() {

@@ -91,4 +67,8 @@ todos.insertItem(

},
function() {
//console.log(todos._data.data);
function(err) {
if (err) {
console.log('insert failed.');
} else {
console.log('insert success.');
}
});

@@ -102,4 +82,8 @@

3,
function() {
//console.log(todos._data.data);
function(err) {
if (err) {
console.log('insert failed.');
} else {
console.log('insert success.');
}
});

@@ -112,8 +96,58 @@

},
function() {
//console.log(todos._data.data);
function(err) {
if (err) {
console.log('append failed.');
} else {
console.log('append success.');
}
});
});
test('.find/.findById.findByIdAndRemove', true, function() {
var rel;
todos.find({status: 'new'}, function(err, items) {
console.log('.find return:');
if (err) { console.log(err); return; }
rel = items;
console.log(items);
});
todos.findById('53599ebd2935fa7c82178097', function(err, item) {
console.log('.findById return:');
if (err) { console.log(err); return; }
console.log(item);
});
todos.findByIdAndRemove('53599ebd2935fa7c82178097', function(err, item) {
console.log('.findByIdAndRemove return:');
if (err) { console.log(err); return; }
console.log(item);
});
if (rel) {
todos.findById(rel[0]._id, function(err, item) {
console.log('.findById return:');
if (err) { console.log(err); return; }
console.log(item);
});
todos.findByIdAndRemove(rel[0]._id, function(err, item) {
console.log('.findByIdAndRemove return:');
if (err) { console.log(err); return; }
console.log(item);
});
}
todos.find({status: 'new'}, function(err, items) {
console.log('.find after item removed return:');
if (err) { console.log(err); return; }
rel = items;
console.log(items);
});
});
}

@@ -7,3 +7,3 @@ fs = require('fs');

if (!(this instanceof TinyDB)) {
return this;
return new TinyDB(opts);
}

@@ -14,3 +14,4 @@

this.options = {
'file': ''
'file': '',
'flushonexit': true
};

@@ -28,5 +29,7 @@

process.on('exit', function() {
self._save(-1);
});
if (this.options['flushonexit'] === true) {
process.on('exit', function() {
self.flush();
});
}

@@ -48,3 +51,7 @@ return this;

} else {
self._data = JSON.parse(data.toString());
try {
self._data = JSON.parse(data.toString());
} catch(e) {
throw new Error('database format error!');
}
}

@@ -55,10 +62,36 @@

}
if (Object.prototype.toString.call(self._data.data) !== '[object Array]') {
throw new Error('self._data.data not a Array.');
}
self._state = 'ready';
if (typeof self.onReady == 'function') {
if (typeof self.onReady === 'function') {
self.onReady();
}
});
}
};
TinyDB.prototype.flush = function(callback) {
if ('ready' !== this._state) {
return callback && callback(new Error('database not ready.'));
}
if (this._timeoutObj) {
clearTimeout(this._timeoutObj);
delete this._timeoutObj;
}
var err = null;
try {
fs.writeFileSync(this.options.file, JSON.stringify(this._data), 'utf8');
} catch (e) {
err = new Error(e);
}
return callback && callback(err);
};
TinyDB.prototype._save = function(delay, callback) {

@@ -72,9 +105,3 @@ var self = this;

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);
this.flush();
} else {

@@ -88,7 +115,11 @@ if (this._timeoutObj) {

fs.writeFile(self.options.file, JSON.stringify(self._data), 'utf8', function(err) {
if (err) throw err;
var e = null;
if (err) {
e = err;
}
if (typeof delay === 'function') {
return delay(null);
return delay(e);
} else {
return callback && callback(null);
return callback && callback(e);
}

@@ -98,26 +129,4 @@ });

}
}
};
TinyDB.prototype.close = function(callback) {
var self = this;
if ('ready' !== this._state) {
return callback && callback(new Error('database not ready.'));
}
this._state = 'closed';
if (this._timeoutObj) {
clearTimeout(this._timeoutObj);
}
fs.writeFile(this.options.file, JSON.stringify(this._data), 'utf8', function(err) {
if (err) throw err;
delete self._data;
self._data = {};
return callback && callback(null);
});
}
TinyDB.prototype._guid = function() {

@@ -128,3 +137,3 @@ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {

});
}
};

@@ -135,3 +144,3 @@ TinyDB.prototype.showOpts = function() {

}
}
};

@@ -150,3 +159,3 @@ TinyDB.prototype.setInfo = function(key, value, callback) {

return callback && callback(null, key, value);
}
};

@@ -163,3 +172,3 @@ TinyDB.prototype.getInfo = function(key, callback) {

}
}
};

@@ -176,3 +185,3 @@ TinyDB.prototype.forEach = function(callback) {

}
}
};

@@ -204,3 +213,3 @@ TinyDB.prototype.find = function(query, callback) {

}
}
};

@@ -219,3 +228,3 @@ TinyDB.prototype.findById = function(id, callback) {

return callback && callback(new Error('not found'));
}
};

@@ -235,3 +244,3 @@ TinyDB.prototype.findByIdAndRemove = function(id, callback) {

});
}
};

@@ -250,3 +259,3 @@ TinyDB.prototype.insertItem = function(item, idx, callback) {

return func && func(null, item, index > (this._data.data.length - 1) ? (this._data.data.length - 1) : index);
}
};

@@ -263,4 +272,4 @@ TinyDB.prototype.appendItem = function(item, callback) {

return callback && callback(null, item, this._data.data.length - 1);
}
};
module.exports = TinyDB;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc