Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Reorder dir structure

  • Loading branch information...
commit a7994265c89f34b6e7d292906ffcdbe788302ee8 1 parent 1989822
@pksunkara authored
View
1  .gitignore
@@ -1,4 +1,3 @@
node_modules
npm-debug.log
*.swp
-nosqlite.js
View
4 .npmignore
@@ -1,5 +1,3 @@
.gitignore
-test
-Cakefile
-nosqlite.coffee
+src
.travis.yml
View
5 Cakefile
@@ -1,5 +0,0 @@
-{spawn, exec} = require 'child_process'
-
-task 'lib', 'Generate the library from the src', ->
- coffee = spawn 'coffee', ['-c', 'nosqlite.coffee']
- coffee.stdout.on 'data', (data) -> console.log data.toString().trim()
View
170 lib/nosqlite.js
@@ -0,0 +1,170 @@
+// Generated by CoffeeScript 1.3.3
+(function() {
+ var fs, nosqlite, path, utile;
+
+ nosqlite = module.exports;
+
+ path = require('path');
+
+ fs = require('fs');
+
+ utile = require('utile');
+
+ nosqlite.path = path.join(__dirname, '..', 'data');
+
+ nosqlite.Connection = function(arg) {
+ var options;
+ options = {};
+ this.path = nosqlite.path;
+ if (typeof arg === 'object') {
+ options = arg;
+ return this.path = options.path;
+ } else if (typeof arg === 'string') {
+ return this.path = arg;
+ }
+ };
+
+ nosqlite.Connection.prototype.database = function(name, mode) {
+ var that;
+ that = this;
+ return {
+ dir: path.resolve(that.path, name),
+ name: name || 'test',
+ mode: mode || '0775',
+ file: function(id) {
+ return path.resolve(this.dir, id + '.json');
+ },
+ project: function(onto, from) {
+ Object.keys(from).forEach(function(k) {
+ return onto[k] = from[k];
+ });
+ return onto;
+ },
+ satisfy: function(data, cond) {
+ return Object.keys(cond).every(function(k) {
+ if (data[k] === cond[k]) {
+ return true;
+ } else {
+ return false;
+ }
+ });
+ },
+ _write: function(id, data, cb) {
+ var _this = this;
+ return fs.writeFile(this.file('.' + id), data, function(err) {
+ if (err) {
+ return cb(err);
+ } else {
+ return fs.rename(_this.file('.' + id), _this.file(id), cb);
+ }
+ });
+ },
+ _writeSync: function(id, data) {
+ fs.writeFileSync(this.file('.' + id), data);
+ return fs.renameSync(this.file('.' + id), this.file(id));
+ },
+ exists: function(cb) {
+ return path.exists(this.dir, cb);
+ },
+ existsSync: function() {
+ return path.existsSync(this.dir);
+ },
+ create: function(cb) {
+ return fs.mkdir(this.dir, this.mode, cb);
+ },
+ createSync: function() {
+ return fs.mkdirSync(this.dir, this.mode);
+ },
+ destroy: function(cb) {
+ return utile.rimraf(this.dir, cb);
+ },
+ destroySync: function() {
+ return utile.rimraf.sync(this.dir);
+ },
+ get: function(id, cb) {
+ return fs.readFile(this.file(id), 'utf8', function(err, data) {
+ return cb(err, (data ? JSON.parse(data) : void 0));
+ });
+ },
+ getSync: function(id) {
+ return JSON.parse(fs.readFileSync(this.file(id), 'utf8'));
+ },
+ "delete": function(id, cb) {
+ return fs.unlink(this.file(id), cb);
+ },
+ deleteSync: function(id) {
+ return fs.unlinkSync(this.file(id));
+ },
+ put: function(id, obj, cb) {
+ var _this = this;
+ return this.get(id, function(err, data) {
+ data = _this.project(data, obj);
+ return _this._write(id, JSON.stringify(data, null, 2), cb);
+ });
+ },
+ putSync: function(id, obj) {
+ var data;
+ data = this.project(this.getSync(id), obj);
+ return this._writeSync(id, JSON.stringify(data, null, 2));
+ },
+ post: function(obj, cb) {
+ return this._write(obj.id || obj._id, JSON.stringify(obj, null, 2), cb);
+ },
+ postSync: function(obj) {
+ return this._writeSync(obj.id || obj._id, JSON.stringify(obj, null, 2));
+ },
+ find: function(cond, cb) {
+ var _this = this;
+ return fs.readdir(this.dir, function(err, files) {
+ return utile.async.map(files, function(file, callback) {
+ return _this.get(path.basename(file, '.json'), function(err, data) {
+ if (_this.satisfy(data, cond)) {
+ return callback(err, data);
+ } else {
+ return callback(err, null);
+ }
+ });
+ }, function(err, files) {
+ return cb(err, files.filter(function(file) {
+ return file != null;
+ }));
+ });
+ });
+ },
+ findSync: function(cond) {
+ var files,
+ _this = this;
+ files = fs.readdirSync(this.dir);
+ files = files.map(function(file) {
+ var data;
+ data = _this.getSync(path.basename(file, '.json'));
+ if (_this.satisfy(data, cond)) {
+ return data;
+ } else {
+ return null;
+ }
+ });
+ return files.filter(function(file) {
+ return file != null;
+ });
+ },
+ all: function(cb) {
+ var _this = this;
+ return fs.readdir(this.dir, function(err, files) {
+ return utile.async.map(files, function(file, callback) {
+ return _this.get(path.basename(file, '.json'), callback);
+ }, cb);
+ });
+ },
+ allSync: function() {
+ var files,
+ _this = this;
+ files = fs.readdirSync(this.dir);
+ return files.map(function(file) {
+ return _this.getSync(path.basename(file, '.json'));
+ });
+ }
+ };
+ };
+
+}).call(this);
View
11 package.json
@@ -3,7 +3,7 @@
"version": "0.1.1",
"author": "Pavan Kumar Sunkara <pavan.sss1991@gmail.com>",
"description": "human readable nosql type filesystem json store",
- "main": "./nosqlite",
+ "main": "./lib/nosqlite",
"repository": {
"type": "git",
"url": "git://github.com/pksunkara/nosqlite.git"
@@ -18,8 +18,7 @@
],
"homepage": "http://pksunkara.github.com/nosqlite",
"scripts": {
- "test": "./node_modules/.bin/vows --spec $(find test -name '*-test.coffee')",
- "prepublish": "./node_modules/.bin/cake lib"
+ "test": "./node_modules/.bin/vows --spec $(find test -name '*-test.coffee')"
},
"contributors": [
{
@@ -31,11 +30,11 @@
"utile": "*"
},
"devDependencies": {
- "coffee-script": "~1.2.0",
- "vows": "~0.6.0"
+ "coffee-script": "1.3.x",
+ "vows": "0.6.x"
},
"engines": {
- "node": ">= 0.4"
+ "node": ">= 0.6"
},
"bugs": {
"url": "https://github.com/pksunkara/nosqlite/issues"
View
2  nosqlite.coffee → src/nosqlite.coffee
@@ -12,7 +12,7 @@ fs = require 'fs'
utile = require 'utile'
# Declaring variables
-nosqlite.path = path.join __dirname, 'data'
+nosqlite.path = path.join __dirname, '..', 'data'
# Connection class for nosqlite
nosqlite.Connection = (arg) ->
View
2  test/connection-test.coffee
@@ -6,7 +6,7 @@ vows
.describe('connection')
.addBatch
'Connection':
- topic: require '../nosqlite'
+ topic: require '../src/nosqlite'
'should have default path': (nosqlite) ->
assert.equal nosqlite.path, path.resolve(__dirname, '..', 'data')
View
2  test/database-test.coffee
@@ -3,7 +3,7 @@ assert = require 'assert'
fs = require 'fs'
path = require 'path'
-nosqlite = require '../nosqlite'
+nosqlite = require '../src/nosqlite'
connection = new(nosqlite.Connection) path.resolve(__dirname, 'fixtures')
vows
Please sign in to comment.
Something went wrong with that request. Please try again.