Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first commit

  • Loading branch information...
commit 11709964785cf6a4fd810d6cfbf033b51d2534cd 1 parent 2710fdd
Markus Tiefenbacher authored
View
2  .gitignore
@@ -0,0 +1,2 @@
+node_modules
+staticstore
View
2  .npmignore
@@ -0,0 +1,2 @@
+node_modules
+staticstore
View
62 README.md
@@ -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)###
+
+...
+
View
245 app.js
@@ -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);
+
View
14 package.json
@@ -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"
+ }
+}
View
56 test/test.js
@@ -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);
+});
+*/
+
+
+
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.