Permalink
Browse files

[api] Implemented basic working version of filesystem engine

  • Loading branch information...
1 parent 66c8128 commit c206424345b45e9067ddbd34e7ac68ea28c7add7 @pksunkara pksunkara committed Sep 6, 2012
Showing with 51 additions and 103 deletions.
  1. +51 −103 lib/resourceful/engines/filesystem.js
@@ -1,132 +1,80 @@
-var resourceful = require('../../resourceful'),
+var nosqlite = require('nosqlite'),
+ 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();
- }
+var Filesystem = exports.Filesystem = function (config) {
+ config = config || {};
+
+ this.uri = config.uri;
+ this.connection = new(nosqlite.Connection)(this.uri);
+ this.database = this.connection.database(config.database);
};
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;
+ throw new(Error)("Load not valid for filesystem engine");
};
Filesystem.prototype.request = function (fn) {
- var self = this;
+ var args = Array.prototype.slice.call(arguments, 1);
+ return this.connection[fn].apply(this.connection, args);
+};
+
+Filesystem.prototype.get = function (key, callback) {
+ return this.request.call(this, 'get', key, function (err, res) {
+ if (err) {
+ return callback(err);
+ }
- process.nextTick(function () {
- fn.call(self);
+ res.id = res.id || res._id;
+ delete res._id;
+
+ return callback(null, res);
});
};
-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 (key, doc, callback) {
+ delete doc.id;
+ return this.request('put', key, doc, function (err, res) {
+ if (err) {
+ return callback(err);
+ }
+
+ res.id = key;
+ delete res._id;
+ callback(null, res);
});
};
-Filesystem.prototype.put = function () {
- this.save.apply(this, arguments);
+Filesystem.prototype.post = Filesystem.prototype.create = function (doc, callback) {
+ doc._id = doc.id;
+ delete doc.id;
+ return this.request('post', doc, function (err, res) {
+ if (err) return callback(err);
+
+ doc.id = res._id;
+ delete doc._id;
+ callback(null, doc);
+ });
};
-Filesystem.prototype.update = function (key, obj, callback) {
- this.put(key, resourceful.mixin({}, this.store[key], obj), callback);
+Filesystem.prototype.save = function (key, doc, callback) {
+ return this.put.apply(this, arguments);
};
-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.update = function (key, obj, callback) {
+ return this.put.apply(this, key, obj, callback);
};
Filesystem.prototype.destroy = function (key, callback) {
- this.request(function () {
- delete this.store[key];
- return callback(null, { status: 204 });
+ return this.request.call('delete', key, function (err) {
+ if (err) return callback(err);
+
+ callback(null, {});
});
}
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(); });
+ return this.request.call(this, conditions, callback);
};

1 comment on commit c206424

Contributor

Marak commented on c206424 Sep 6, 2012

Engine tests?

Please sign in to comment.