Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[engines] Started to add new FS engine
- Loading branch information
Showing
2 changed files
with
142 additions
and
0 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,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(); }); | ||
}; |
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,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(); | ||
}; |