Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Markus Tiefenbacher
committed
Dec 15, 2013
1 parent
2710fdd
commit 1170996
Showing
6 changed files
with
379 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
staticstore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
staticstore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,62 @@ | ||
staticstore | ||
=========== | ||
#staticstore# | ||
|
||
staticstore is key/value database for jsons who uses the filesysteme and static json-files. | ||
|
||
this is in early development and will be tested soon in my other project in production mode. | ||
|
||
##documentation## | ||
|
||
more documentation will follow | ||
|
||
###basic usage### | ||
|
||
npm install staticstore | ||
|
||
var store = require('staticstore'); | ||
|
||
/* | ||
initialize your db. by default in './staticstore' | ||
you can change this default-path by adding you own as paramater like store.initdb('own-path'); | ||
*/ | ||
store.initdb(); | ||
|
||
|
||
###store.createdb('database', callback)### | ||
creates a database (=folder) | ||
|
||
... | ||
|
||
###store.deletedb('database', callback)### | ||
deletes a database | ||
|
||
... | ||
|
||
###store.setItem('database/key', callback)### | ||
saves a item in a database | ||
|
||
... | ||
|
||
###store.getItem('database/key', callback)### | ||
|
||
... | ||
|
||
###store.deleteItem('database/key', callback)### | ||
|
||
... | ||
|
||
###store.getAllItems('database', callback)### | ||
|
||
... | ||
|
||
###store.itemCount('database', callback)### | ||
|
||
... | ||
|
||
###store.getIndex('database', callback)### | ||
|
||
... | ||
|
||
###store.itemExists('database/key', callback)### | ||
|
||
... | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
var fs = require('fs.extra'); | ||
var path = require('path'); | ||
var async = require('async'); | ||
|
||
(function(exports) { | ||
|
||
// default path | ||
var dbpath = '/staticstore'; | ||
var initialized = false; | ||
|
||
exports.initdb = function(pathname, callback) { | ||
callback = callback || function() {}; | ||
dbpath = __dirname + (pathname || dbpath); | ||
fs.exists(dbpath, function (exists) { | ||
if(exists) { | ||
initialized = true; | ||
callback(false); | ||
} else { | ||
fs.mkdir(dbpath, function() { | ||
initialized = true; | ||
callback(false); | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
exports.createdb = function(name, callback) { | ||
callback = callback || function() {}; | ||
var newdb = dbpath+'/'+name; | ||
|
||
if(!name) { | ||
return callback(true, 'Please define database'); | ||
} | ||
|
||
fs.exists(newdb, function (exists) { | ||
if(exists) { | ||
callback('Database already exists!'); | ||
} else { | ||
fs.mkdir(newdb, function() { | ||
callback(false); | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
exports.deletedb = function(name, callback) { | ||
callback = callback || function() {}; | ||
var removedb = dbpath+'/'+name; | ||
|
||
if(!name) { | ||
return callback(true, 'Please define database'); | ||
} | ||
|
||
fs.exists(removedb, function (exists) { | ||
if(!exists) { | ||
callback('Database not found!'); | ||
} else { | ||
fs.rmrf(removedb, function() { | ||
callback(false); | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
exports.getItem = function(id, callback) { | ||
callback = callback || function() {}; | ||
var database = id.replace(/(.*)\/(.*)/gm, '$1'); | ||
id = id.replace(/(.*)\/(.*)/gm, '$2'); | ||
|
||
if(!database || !id) { | ||
return callback(true, 'Please define database and key e.g. (database/key)'); | ||
} | ||
|
||
var itemid = dbpath + '/' + database + '/' + id + '.json'; | ||
|
||
fs.readFile(itemid, function (err, data) { | ||
if (err) { | ||
return callback('Item not found!'); | ||
} else { | ||
data = JSON.parse(data); | ||
callback(false, data); | ||
} | ||
}); | ||
}; | ||
|
||
exports.setItem = function(id, data, callback) { | ||
callback = (typeof data === 'function') ? data : callback; | ||
data = (typeof data === 'function') ? undefined : data; | ||
callback = callback || function() {}; | ||
|
||
if(!database || !id) { | ||
return callback(true, 'Please define database and key e.g. (database/key)'); | ||
} | ||
|
||
var database = id.replace(/(.*)\/(.*)/gm, '$1'); | ||
id = String(id.replace(/(.*)\/(.*)/gm, '$2')); | ||
data = data || {}; | ||
data._id = id; | ||
data.timestamp = new Date(); | ||
data = JSON.stringify(data); | ||
|
||
var itemid = dbpath + '/' + database + '/' + id + '.json'; | ||
|
||
fs.writeFile(itemid, data, function (err) { | ||
if (err) { | ||
return callback('Item not saved!'); | ||
} else { | ||
callback(false, id); | ||
} | ||
}); | ||
|
||
}; | ||
|
||
exports.deleteItem = function(id, callback) { | ||
callback = callback || function() {}; | ||
var database = id.replace(/(.*)\/(.*)/gm, '$1'); | ||
id = id.replace(/(.*)\/(.*)/gm, '$2'); | ||
|
||
if(!database || !id) { | ||
return callback(true, 'Please define database and key e.g. (database/key)'); | ||
} | ||
|
||
var itemid = dbpath + '/' + database + '/' + id + '.json'; | ||
|
||
fs.unlink(itemid, function (err) { | ||
if (err) { | ||
return callback('Item not deleted/found!'); | ||
} else { | ||
callback(false, id); | ||
} | ||
}); | ||
|
||
}; | ||
|
||
exports.getAllItems = function(database, callback) { | ||
callback = callback || function() {}; | ||
var db = dbpath+'/'+database; | ||
|
||
if(!database) { | ||
return callback(true, 'Please define database'); | ||
} | ||
|
||
fs.exists(db, function (exists) { | ||
if(exists) { | ||
fs.readdir(db, function(err, files) { | ||
if(err) { | ||
callback('Cant read all indizies'); | ||
} else { | ||
var filesArray = []; | ||
var q = async.queue(function(file, cb) { | ||
fs.readFile(db+'/'+file, function(err, data) { | ||
if(!err) { | ||
filesArray.push(JSON.parse(data)); | ||
} | ||
cb(); | ||
}); | ||
}); | ||
|
||
q.drain = function() { | ||
callback(false, filesArray); | ||
}; | ||
|
||
q.push(files); | ||
} | ||
}); | ||
} else { | ||
callback('Database not found!'); | ||
} | ||
}); | ||
|
||
}; | ||
|
||
exports.itemExists = function(id, callback) { | ||
callback = callback || function() {}; | ||
var database = id.replace(/(.*)\/(.*)/gm, '$1'); | ||
id = id.replace(/(.*)\/(.*)/gm, '$2'); | ||
|
||
if(!database || !id) { | ||
return callback(true, 'Please define database and key e.g. (database/key)'); | ||
} | ||
|
||
var itemid = dbpath + '/' + database + '/' + id + '.json'; | ||
|
||
fs.exists(itemid, function (exists) { | ||
if(exists) { | ||
callback(false, true); | ||
} else { | ||
callback(false, false); | ||
} | ||
}); | ||
|
||
}; | ||
|
||
exports.getIndex = function(database, callback) { | ||
callback = callback || function() {}; | ||
var db = dbpath+'/'+database; | ||
|
||
if(!database) { | ||
return callback(true, 'Please define database'); | ||
} | ||
|
||
fs.exists(db, function (exists) { | ||
if(exists) { | ||
fs.readdir(db, function(err, files) { | ||
if(err) { | ||
callback('Cant read all indizies'); | ||
} else { | ||
files.forEach(function(item, index) { | ||
files[index] = item.replace(/\.json/gm, ''); | ||
}); | ||
callback(false, files); | ||
} | ||
}); | ||
} else { | ||
callback('Database not found!'); | ||
} | ||
}); | ||
}; | ||
|
||
exports.itemCount = function(database, callback) { | ||
callback = callback || function() {}; | ||
var db = dbpath+'/'+database; | ||
|
||
if(!database) { | ||
return callback(true, 'Please define database'); | ||
} | ||
|
||
fs.exists(db, function (exists) { | ||
if(exists) { | ||
fs.readdir(db, function(err, files) { | ||
if(err || !files) { | ||
callback('Cant read all indizies'); | ||
} else { | ||
callback(false, (files.length || 0)); | ||
} | ||
}); | ||
} else { | ||
callback('Database not found!'); | ||
} | ||
}); | ||
|
||
}; | ||
|
||
})(module.exports); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "staticstore", | ||
"version": "0.0.1", | ||
"author": "Markus Tiefenbacher <markus.tiefenbacher@gmail.com>", | ||
"description": "key/value database who use static json files as store", | ||
"main": "app.js", | ||
"dependencies" : { | ||
"fs.extra": ">=1.2.1", | ||
"async": "*" | ||
}, | ||
"engines": { | ||
"node": ">=0.10.13" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
var store = require('../app'); | ||
|
||
store.initdb(); | ||
|
||
/* | ||
store.createdb('test', function(err) { | ||
console.log(err); | ||
}); | ||
*/ | ||
|
||
/* | ||
store.deletedb('test', function(err) { | ||
console.log(err); | ||
}); | ||
*/ | ||
|
||
/* | ||
store.getItem('test/testitem', function(err, data) { | ||
console.log(err, data); | ||
}); | ||
*/ | ||
|
||
/* | ||
store.deleteItem('test/testitem', function(err, data) { | ||
console.log(err, data); | ||
}); | ||
*/ | ||
|
||
/* | ||
store.getIndex('test', function(err, data) { | ||
console.log(err, data); | ||
}); | ||
*/ | ||
|
||
/* | ||
store.getAllItems('test', function(err, data) { | ||
console.log(err, data); | ||
}); | ||
*/ | ||
|
||
/* | ||
store.itemCount('test', function(err, data) { | ||
console.log(err, data); | ||
}); | ||
*/ | ||
|
||
/* | ||
store.itemExists('test/testitem', function(err, data) { | ||
console.log(err, data); | ||
}); | ||
*/ | ||
|
||
|
||
|
||
|
||
|