Skip to content

Commit

Permalink
[engines] Started to add new FS engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Marak authored and pksunkara committed Sep 2, 2012
1 parent ff957d1 commit 66c8128
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
132 changes: 132 additions & 0 deletions lib/resourceful/engines/filesystem.js
@@ -0,0 +1,132 @@
var resourceful = require('../../resourceful'),
Cache = resourceful.Cache;

exports.stores = {};
exports.caches = {};


var Filesystem = exports.Filesystem = function (options) {
var counter = 0;
options = options || {};
this.uri = options.uri;

this.increment = function () {
return ++counter;
}

if (typeof(this.uri) === 'string') {
// Application-wide store
if (!exports.stores[this.uri]) {
this.store = exports.stores[this.uri] = {};
this.cache = exports.caches[this.uri] = new Cache();
} else {
// Use store that was created before
this.store = exports.stores[this.uri];
this.cache = exports.caches[this.uri];
}
}
else {
// Connection-wise store
this.store = {};
this.cache = new Cache();
}
};

Filesystem.prototype.protocol = 'filesystem';

Filesystem.prototype.load = function (data) {


if (data instanceof Array) {
var tmp = {};
data.forEach(function (e) {
tmp[e._id] = e;
});
data = tmp;
}

this.store = data;

// Update cache
if (this.uri) {
exports.stores[this.uri] = data;
}

return this;
};

Filesystem.prototype.request = function (fn) {
var self = this;

process.nextTick(function () {
fn.call(self);
});
};

Filesystem.prototype.save = function (key, val, callback) {
var args = Array.prototype.slice.call(arguments);
var callback = args.pop(), val = args.pop();
if (!args.length || !key) {
key = this.increment();
val._id = key;
}

this.request(function () {
var update = key in this.store;
this.store[key] = val;
callback(null, resourceful.mixin({ status: update ? 200 : 201 }, val));
});
};

Filesystem.prototype.put = function () {
this.save.apply(this, arguments);
};

Filesystem.prototype.update = function (key, obj, callback) {
this.put(key, resourceful.mixin({}, this.store[key], obj), callback);
};

Filesystem.prototype.get = function (key, callback) {

this.request(function () {
key = key.toString();
return key in this.store ?
callback(null, this.store[key])
:
callback({ status: 404 });
});
};

Filesystem.prototype.destroy = function (key, callback) {
this.request(function () {
delete this.store[key];
return callback(null, { status: 204 });
});
}

Filesystem.prototype.find = function (conditions, callback) {
this.filter(function (obj) {
return Object.keys(conditions).every(function (k) {
return conditions[k] === obj[k];
});
}, callback);
};

Filesystem.prototype.filter = function (filter, callback) {
this.request(function () {
var result = [],
store = this.store;

Object.keys(this.store).forEach(function (k) {
if (filter(store[k])) {
result.push(store[k]);
}
});

callback(null, result);
});
};

Filesystem.prototype.sync = function (factory, callback) {
process.nextTick(function () { callback(); });
};
10 changes: 10 additions & 0 deletions test/engines/filesystem.js
@@ -0,0 +1,10 @@
var engine = exports;

engine.name = 'filesystem';
engine.options = { uri: 'test' };

engine.load = function (resourceful, data, callback) {
console.log(resourceful.engines.FileSystem);
new(resourceful.engines.Filesystem)(engine.options).load(data);
callback();
};

0 comments on commit 66c8128

Please sign in to comment.