Comparing version 0.9.11 to 1.0.0-alpha-01
@@ -1,23 +0,11 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.9.0 | ||
(function() { | ||
var BaseAdapter, BaseCollection, merge, type, _ref; | ||
var BaseAdapter, BaseCollection, call, merge, type, _ref; | ||
_ref = require("fairmont"), type = _ref.type, merge = _ref.merge; | ||
call = require("when/generator").call; | ||
BaseAdapter = (function() { | ||
function BaseAdapter(configuration) { | ||
this.configuration = configuration; | ||
this.events = this.configuration.events; | ||
if ((this.configuration.log != null) && type(this.configuration.log) === "function") { | ||
this.log = function(msg) { | ||
return this.configuration.log(msg); | ||
}; | ||
} else if ((this.configuration.log == null) || this.configuration.log === true) { | ||
this.log = function(msg) { | ||
return console.log(msg); | ||
}; | ||
} else { | ||
this.log = function() {}; | ||
} | ||
} | ||
function BaseAdapter() {} | ||
@@ -32,31 +20,20 @@ return BaseAdapter; | ||
BaseCollection.prototype.patch = function(key, object) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
var _events; | ||
_events = _this.get(key); | ||
_events.on("success", function(data) { | ||
var __events; | ||
if (data != null) { | ||
if (type(data) === "array") { | ||
return call((function(_this) { | ||
return function*() { | ||
var data; | ||
data = (yield _this.get(key)); | ||
switch (type(data)) { | ||
case "array": | ||
if (type(object) === "array") { | ||
data = data.concat(object); | ||
data.concat(object); | ||
} else { | ||
data.push(object); | ||
} | ||
} else if (type(data) === "object") { | ||
break; | ||
case "object": | ||
data = merge(data, object); | ||
} | ||
} | ||
__events = _this.put(key, data); | ||
__events.on("success", function(data) { | ||
return events.emit("success", data); | ||
}); | ||
return __events.on("error", function(err) { | ||
return events.emit("error", err); | ||
}); | ||
}); | ||
return _events.on("error", function(err) { | ||
return events.emit("error", err); | ||
}); | ||
}); | ||
return (yield _this.put(key, data)); | ||
}; | ||
})(this)); | ||
}; | ||
@@ -63,0 +40,0 @@ |
@@ -1,6 +0,6 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.9.0 | ||
(function() { | ||
var Adapter, BaseAdapter, BaseCollection, Collection, ElasticSearchClient, defaults, merge, overload, type, _ref, _ref1, | ||
__hasProp = {}.hasOwnProperty, | ||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; | ||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, | ||
__hasProp = {}.hasOwnProperty; | ||
@@ -34,43 +34,45 @@ _ref = require("fairmont"), type = _ref.type, merge = _ref.merge; | ||
function Adapter(configuration) { | ||
var _this = this; | ||
this.configuration = configuration; | ||
function Adapter(_at_configuration) { | ||
this.configuration = _at_configuration; | ||
this.configuration = merge(defaults, this.configuration); | ||
Adapter.__super__.constructor.call(this, this.configuration); | ||
this.events.safely(function() { | ||
_this.client = new ElasticSearchClient(_this.configuration); | ||
return _this.client.createCall({ | ||
path: "", | ||
method: "GET" | ||
}, _this.configuration).on("data", function(data) { | ||
var versionString, versionTokens; | ||
versionString = JSON.parse(data).version.number; | ||
versionTokens = versionString.split("."); | ||
_this.esVersion = { | ||
major: parseInt(versionTokens[0]), | ||
minor: parseInt(versionTokens[1]), | ||
patch: parseInt(versionTokens[2]) | ||
}; | ||
_this.log("ElasticsearchAdapter: Connected to Elasticsearch server @ " + _this.configuration.host + ":" + _this.configuration.port + " v" + versionString); | ||
return _this.events.emit("ready", _this); | ||
}).on("error", function(err) { | ||
_this.log("ElasticsearchAdapter: Error connecting to Elasticsearch server @ " + _this.configuration.host + ":" + _this.configuration.port + " - " + err); | ||
return _this.events.emit("error", err); | ||
}).exec(); | ||
}); | ||
this.events.safely((function(_this) { | ||
return function() { | ||
_this.client = new ElasticSearchClient(_this.configuration); | ||
return _this.client.createCall({ | ||
path: "", | ||
method: "GET" | ||
}, _this.configuration).on("data", function(data) { | ||
var versionString, versionTokens; | ||
versionString = JSON.parse(data).version.number; | ||
versionTokens = versionString.split("."); | ||
_this.esVersion = { | ||
major: parseInt(versionTokens[0]), | ||
minor: parseInt(versionTokens[1]), | ||
patch: parseInt(versionTokens[2]) | ||
}; | ||
_this.log("ElasticsearchAdapter: Connected to Elasticsearch server @ " + _this.configuration.host + ":" + _this.configuration.port + " v" + versionString); | ||
return _this.events.emit("ready", _this); | ||
}).on("error", function(err) { | ||
_this.log("ElasticsearchAdapter: Error connecting to Elasticsearch server @ " + _this.configuration.host + ":" + _this.configuration.port + " - " + err); | ||
return _this.events.emit("error", err); | ||
}).exec(); | ||
}; | ||
})(this)); | ||
} | ||
Adapter.prototype.collection = function(index, type) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
var result; | ||
result = Collection.make({ | ||
index: index, | ||
type: type, | ||
events: _this.events, | ||
adapter: _this, | ||
log: _this.log | ||
}); | ||
return events.emit("success", result); | ||
}); | ||
return this.events.source((function(_this) { | ||
return function(events) { | ||
var result; | ||
result = Collection.make({ | ||
index: index, | ||
type: type, | ||
events: _this.events, | ||
adapter: _this, | ||
log: _this.log | ||
}); | ||
return events.emit("success", result); | ||
}; | ||
})(this)); | ||
}; | ||
@@ -97,38 +99,39 @@ | ||
match("array", function(keys) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
return events.safely(function() { | ||
return _this.adapter.client.count(_this.index, _this.type, { | ||
query: { | ||
terms: { | ||
_id: keys | ||
return this.events.source((function(_this) { | ||
return function(events) { | ||
return events.safely(function() { | ||
return _this.adapter.client.count(_this.index, _this.type, { | ||
query: { | ||
terms: { | ||
_id: keys | ||
} | ||
} | ||
} | ||
}).on("data", function(data) { | ||
var findEvents, jsonData; | ||
jsonData = JSON.parse(data); | ||
if (jsonData.error == null) { | ||
findEvents = _this.find({ | ||
filter: { | ||
terms: { | ||
_id: keys | ||
}).on("data", function(data) { | ||
var findEvents, jsonData; | ||
jsonData = JSON.parse(data); | ||
if (jsonData.error == null) { | ||
findEvents = _this.find({ | ||
filter: { | ||
terms: { | ||
_id: keys | ||
} | ||
} | ||
} | ||
}, { | ||
size: jsonData.count | ||
}); | ||
findEvents.on("success", function(data) { | ||
return events.emit("success", data); | ||
}); | ||
return findEvents.on("error", function(err) { | ||
return events.emit("error", err); | ||
}); | ||
} else { | ||
return events.emit("error", jsonData.error); | ||
} | ||
}).on("error", function(err) { | ||
return events.emit("error", err); | ||
}).exec(); | ||
}); | ||
}); | ||
}, { | ||
size: jsonData.count | ||
}); | ||
findEvents.on("success", function(data) { | ||
return events.emit("success", data); | ||
}); | ||
return findEvents.on("error", function(err) { | ||
return events.emit("error", err); | ||
}); | ||
} else { | ||
return events.emit("error", jsonData.error); | ||
} | ||
}).on("error", function(err) { | ||
return events.emit("error", err); | ||
}).exec(); | ||
}); | ||
}; | ||
})(this)); | ||
}); | ||
@@ -159,25 +162,26 @@ match("string", function(queryString) { | ||
return match("object", "object", function(query, options) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
return events.safely(function() { | ||
return _this.adapter.client.search(_this.index, _this.type, query, options).on("data", function(data) { | ||
var jsonData, results; | ||
jsonData = JSON.parse(data); | ||
if (jsonData.error == null) { | ||
results = jsonData.hits.hits.map(function(dataElem) { | ||
var result; | ||
result = dataElem._source; | ||
result._id = dataElem._id; | ||
result.score = dataElem._score; | ||
return result; | ||
}); | ||
return events.emit("success", results); | ||
} else { | ||
return events.emit("error", jsonData.error); | ||
} | ||
}).on("error", function(err) { | ||
return events.emit("error", err); | ||
}).exec(); | ||
}); | ||
}); | ||
return this.events.source((function(_this) { | ||
return function(events) { | ||
return events.safely(function() { | ||
return _this.adapter.client.search(_this.index, _this.type, query, options).on("data", function(data) { | ||
var jsonData, results; | ||
jsonData = JSON.parse(data); | ||
if (jsonData.error == null) { | ||
results = jsonData.hits.hits.map(function(dataElem) { | ||
var result; | ||
result = dataElem._source; | ||
result._id = dataElem._id; | ||
result.score = dataElem._score; | ||
return result; | ||
}); | ||
return events.emit("success", results); | ||
} else { | ||
return events.emit("error", jsonData.error); | ||
} | ||
}).on("error", function(err) { | ||
return events.emit("error", err); | ||
}).exec(); | ||
}); | ||
}; | ||
})(this)); | ||
}); | ||
@@ -193,24 +197,25 @@ }); | ||
return match("object", function(query) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
return events.safely(function() { | ||
return _this.adapter.client.get(_this.index, _this.type, query._id).on("data", function(data) { | ||
var jsonData, result; | ||
jsonData = JSON.parse(data); | ||
if (jsonData.error == null) { | ||
if (jsonData._source != null) { | ||
result = jsonData._source; | ||
result._id = jsonData._id; | ||
return this.events.source((function(_this) { | ||
return function(events) { | ||
return events.safely(function() { | ||
return _this.adapter.client.get(_this.index, _this.type, query._id).on("data", function(data) { | ||
var jsonData, result; | ||
jsonData = JSON.parse(data); | ||
if (jsonData.error == null) { | ||
if (jsonData._source != null) { | ||
result = jsonData._source; | ||
result._id = jsonData._id; | ||
} else { | ||
result = null; | ||
} | ||
return events.emit("success", result); | ||
} else { | ||
result = null; | ||
return events.emit("error", jsonData.error); | ||
} | ||
return events.emit("success", result); | ||
} else { | ||
return events.emit("error", jsonData.error); | ||
} | ||
}).on("error", function(err) { | ||
return events.emit("error", err); | ||
}).exec(); | ||
}); | ||
}); | ||
}).on("error", function(err) { | ||
return events.emit("error", err); | ||
}).exec(); | ||
}); | ||
}; | ||
})(this)); | ||
}); | ||
@@ -239,16 +244,17 @@ }); | ||
return match("object", "object", "object", function(key, object, options) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
return _this.adapter.client.index(_this.index, _this.type, object, key._id, options).on("data", function(data) { | ||
var jsonData; | ||
jsonData = JSON.parse(data); | ||
if (jsonData.error == null) { | ||
return events.emit("success", object); | ||
} else { | ||
return events.emit("error", jsonData.error); | ||
} | ||
}).on("error", function(err) { | ||
return events.emit("error", err); | ||
}).exec(); | ||
}); | ||
return this.events.source((function(_this) { | ||
return function(events) { | ||
return _this.adapter.client.index(_this.index, _this.type, object, key._id, options).on("data", function(data) { | ||
var jsonData; | ||
jsonData = JSON.parse(data); | ||
if (jsonData.error == null) { | ||
return events.emit("success", object); | ||
} else { | ||
return events.emit("error", jsonData.error); | ||
} | ||
}).on("error", function(err) { | ||
return events.emit("error", err); | ||
}).exec(); | ||
}; | ||
})(this)); | ||
}); | ||
@@ -272,25 +278,26 @@ }); | ||
return match("object", "object", "object", function(key, object, options) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
var _events; | ||
_events = _this.get(key._id); | ||
_events.on("success", function(data) { | ||
var __events; | ||
delete data._id; | ||
delete data.score; | ||
if (data != null) { | ||
data = merge(data, object); | ||
} | ||
__events = _this.put(key._id, data, options); | ||
__events.on("success", function(data) { | ||
return events.emit("success", data); | ||
return this.events.source((function(_this) { | ||
return function(events) { | ||
var _events; | ||
_events = _this.get(key._id); | ||
_events.on("success", function(data) { | ||
var __events; | ||
delete data._id; | ||
delete data.score; | ||
if (data != null) { | ||
data = merge(data, object); | ||
} | ||
__events = _this.put(key._id, data, options); | ||
__events.on("success", function(data) { | ||
return events.emit("success", data); | ||
}); | ||
return __events.on("error", function(err) { | ||
return events.emit("error", err); | ||
}); | ||
}); | ||
return __events.on("error", function(err) { | ||
return _events.on("error", function(err) { | ||
return events.emit("error", err); | ||
}); | ||
}); | ||
return _events.on("error", function(err) { | ||
return events.emit("error", err); | ||
}); | ||
}); | ||
}; | ||
})(this)); | ||
}); | ||
@@ -306,44 +313,9 @@ }); | ||
return match("object", function(key) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
return _this.adapter.client.deleteDocument(_this.index, _this.type, key._id).on("data", function(data) { | ||
var jsonData; | ||
jsonData = JSON.parse(data); | ||
if (jsonData.error == null) { | ||
return events.emit("success"); | ||
} else { | ||
return events.emit("error", jsonData.error); | ||
} | ||
}).on("error", function(err) { | ||
return events.emit("error", err); | ||
}).exec(); | ||
}); | ||
}); | ||
}); | ||
Collection.prototype.all = function() { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
return events.safely(function() { | ||
var countEvents; | ||
countEvents = _this.count(); | ||
countEvents.on("success", function(resultCount) { | ||
return _this.adapter.client.search(_this.index, _this.type, { | ||
query: { | ||
match_all: {} | ||
} | ||
}, { | ||
size: resultCount | ||
}).on("data", function(data) { | ||
var jsonData, results; | ||
return this.events.source((function(_this) { | ||
return function(events) { | ||
return _this.adapter.client.deleteDocument(_this.index, _this.type, key._id).on("data", function(data) { | ||
var jsonData; | ||
jsonData = JSON.parse(data); | ||
if (jsonData.error == null) { | ||
results = jsonData.hits.hits.map(function(dataElem) { | ||
var result; | ||
result = dataElem._source; | ||
result._id = dataElem._id; | ||
result.score = dataElem._score; | ||
return result; | ||
}); | ||
return events.emit("success", results); | ||
return events.emit("success"); | ||
} else { | ||
@@ -355,29 +327,67 @@ return events.emit("error", jsonData.error); | ||
}).exec(); | ||
}; | ||
})(this)); | ||
}); | ||
}); | ||
Collection.prototype.all = function() { | ||
return this.events.source((function(_this) { | ||
return function(events) { | ||
return events.safely(function() { | ||
var countEvents; | ||
countEvents = _this.count(); | ||
countEvents.on("success", function(resultCount) { | ||
return _this.adapter.client.search(_this.index, _this.type, { | ||
query: { | ||
match_all: {} | ||
} | ||
}, { | ||
size: resultCount | ||
}).on("data", function(data) { | ||
var jsonData, results; | ||
jsonData = JSON.parse(data); | ||
if (jsonData.error == null) { | ||
results = jsonData.hits.hits.map(function(dataElem) { | ||
var result; | ||
result = dataElem._source; | ||
result._id = dataElem._id; | ||
result.score = dataElem._score; | ||
return result; | ||
}); | ||
return events.emit("success", results); | ||
} else { | ||
return events.emit("error", jsonData.error); | ||
} | ||
}).on("error", function(err) { | ||
return events.emit("error", err); | ||
}).exec(); | ||
}); | ||
return countEvents.on("error", function(err) { | ||
return events.emit("error", err); | ||
}); | ||
}); | ||
return countEvents.on("error", function(err) { | ||
return events.emit("error", err); | ||
}); | ||
}); | ||
}); | ||
}; | ||
})(this)); | ||
}; | ||
Collection.prototype.count = function() { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
return _this.adapter.client.count(_this.index, _this.type, { | ||
query: { | ||
match_all: {} | ||
} | ||
}).on("data", function(data) { | ||
var jsonData; | ||
jsonData = JSON.parse(data); | ||
if (jsonData.error == null) { | ||
return events.emit("success", jsonData.count); | ||
} else { | ||
return events.emit("error", jsonData.error); | ||
} | ||
}).on("error", function(err) { | ||
return events.emit("error", err); | ||
}).exec(); | ||
}); | ||
return this.events.source((function(_this) { | ||
return function(events) { | ||
return _this.adapter.client.count(_this.index, _this.type, { | ||
query: { | ||
match_all: {} | ||
} | ||
}).on("data", function(data) { | ||
var jsonData; | ||
jsonData = JSON.parse(data); | ||
if (jsonData.error == null) { | ||
return events.emit("success", jsonData.count); | ||
} else { | ||
return events.emit("error", jsonData.error); | ||
} | ||
}).on("error", function(err) { | ||
return events.emit("error", err); | ||
}).exec(); | ||
}; | ||
})(this)); | ||
}; | ||
@@ -384,0 +394,0 @@ |
@@ -1,7 +0,5 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.9.0 | ||
(function() { | ||
module.exports = { | ||
Mongo: require("./mongo-adapter"), | ||
Memory: require("./memory-adapter"), | ||
ElasticSearch: require("./elasticsearch-adapter"), | ||
Redis: require("./redis-adapter") | ||
@@ -8,0 +6,0 @@ }; |
@@ -1,11 +0,12 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.9.0 | ||
(function() { | ||
var Adapter, BaseAdapter, BaseCollection, Collection, overload, type, _ref, | ||
var Adapter, BaseAdapter, BaseCollection, Collection, type, w, _ref, | ||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, | ||
__hasProp = {}.hasOwnProperty, | ||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; | ||
__slice = [].slice; | ||
w = require("when"); | ||
type = require("fairmont").type; | ||
overload = require("typely").overload; | ||
_ref = require("./base-adapter"), BaseAdapter = _ref.BaseAdapter, BaseCollection = _ref.BaseCollection; | ||
@@ -20,23 +21,23 @@ | ||
function Adapter(configuration) { | ||
this.configuration = configuration; | ||
function Adapter(_at_configuration) { | ||
this.configuration = _at_configuration; | ||
Adapter.__super__.constructor.call(this, this.configuration); | ||
this.database = {}; | ||
this.events.emit("ready", this); | ||
} | ||
Adapter.prototype.connect = function() { | ||
return w(this); | ||
}; | ||
Adapter.prototype.collection = function(name) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
_this.database[name] = Collection.make({ | ||
collection: {}, | ||
events: _this.events, | ||
adapter: _this, | ||
log: _this.log | ||
}); | ||
return events.emit("success", _this.database[name]); | ||
}); | ||
return w(this.database[name] = Collection.make({ | ||
collection: {}, | ||
adapter: this, | ||
logger: this.logger | ||
})); | ||
}; | ||
Adapter.prototype.close = function() {}; | ||
Adapter.prototype.close = function() { | ||
return w(this); | ||
}; | ||
@@ -55,69 +56,40 @@ return Adapter; | ||
function Collection(_arg) { | ||
this.events = _arg.events, this.collection = _arg.collection, this.adapter = _arg.adapter, this.log = _arg.log; | ||
this.collection = _arg.collection, this.adapter = _arg.adapter; | ||
} | ||
Collection.prototype.find = overload(function(match, fail) { | ||
match("array", function(keys) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
var values; | ||
values = keys.map(function(key) { | ||
return _this.collection[key]; | ||
}); | ||
return events.emit("success", values); | ||
}); | ||
}); | ||
return match("string", function(key) { | ||
return this.get(key); | ||
}); | ||
}); | ||
Collection.prototype.find = function() { | ||
var key, keys; | ||
keys = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
return w.all((function() { | ||
var _i, _len, _results; | ||
_results = []; | ||
for (_i = 0, _len = keys.length; _i < _len; _i++) { | ||
key = keys[_i]; | ||
_results.push(this.get(key)); | ||
} | ||
return _results; | ||
}).call(this)); | ||
}; | ||
Collection.prototype.get = function(key) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
return events.emit("success", _this.collection[key]); | ||
}); | ||
return w(this.collection[key]); | ||
}; | ||
Collection.prototype.put = function(key, object) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
_this.collection[key] = object; | ||
return events.emit("success", object); | ||
}); | ||
return w(this.collection[key] = object); | ||
}; | ||
Collection.prototype["delete"] = function(key) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
var object; | ||
object = _this.collection[key]; | ||
delete _this.collection[key]; | ||
return events.emit("success"); | ||
}); | ||
var old; | ||
old = this.collection[key]; | ||
delete this.collection[key]; | ||
return w(old); | ||
}; | ||
Collection.prototype.all = function() { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
var key, values; | ||
values = (function() { | ||
var _i, _len, _ref1, _results; | ||
_ref1 = Object.keys(this.collection); | ||
_results = []; | ||
for (_i = 0, _len = _ref1.length; _i < _len; _i++) { | ||
key = _ref1[_i]; | ||
_results.push(this.collection[key]); | ||
} | ||
return _results; | ||
}).call(_this); | ||
return events.emit("success", values); | ||
}); | ||
return this.find.apply(this, Object.keys(this.collection)); | ||
}; | ||
Collection.prototype.count = function() { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
return events.emit("success", Object.keys(_this.collection).length); | ||
}); | ||
return w((Object.keys(this.collection)).length); | ||
}; | ||
@@ -124,0 +96,0 @@ |
@@ -1,6 +0,6 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.9.0 | ||
(function() { | ||
var Adapter, BaseAdapter, BaseCollection, Collection, MongoDB, defaults, merge, overload, type, _ref, _ref1, | ||
__hasProp = {}.hasOwnProperty, | ||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; | ||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, | ||
__hasProp = {}.hasOwnProperty; | ||
@@ -30,43 +30,45 @@ _ref = require("fairmont"), type = _ref.type, merge = _ref.merge; | ||
function Adapter(configuration) { | ||
var database, host, options, port, _ref2, | ||
_this = this; | ||
this.configuration = configuration; | ||
function Adapter(_at_configuration) { | ||
var database, host, options, port, _ref2; | ||
this.configuration = _at_configuration; | ||
this.configuration = merge(defaults, this.configuration); | ||
Adapter.__super__.constructor.call(this, this.configuration); | ||
_ref2 = this.configuration, host = _ref2.host, port = _ref2.port, options = _ref2.options, database = _ref2.database; | ||
this.events.safely(function() { | ||
var server; | ||
server = new MongoDB.Server(host, port, options); | ||
_this.database = new MongoDB.Db(database, server, { | ||
w: 1 | ||
}); | ||
return _this.database.open(function(error, database) { | ||
if (error == null) { | ||
_this.log("MongoAdapter: Connected to MongoDB server @ " + host + ":" + port); | ||
return _this.events.emit("ready", _this); | ||
} else { | ||
_this.log("MongoAdapter: Error connecting to MongoDB server @ " + host + ":" + port + " - " + error); | ||
return _this.events.emit("error", error); | ||
} | ||
}); | ||
}); | ||
this.events.safely((function(_this) { | ||
return function() { | ||
var server; | ||
server = new MongoDB.Server(host, port, options); | ||
_this.database = new MongoDB.Db(database, server, { | ||
w: 1 | ||
}); | ||
return _this.database.open(function(error, database) { | ||
if (error == null) { | ||
_this.log("MongoAdapter: Connected to MongoDB server @ " + host + ":" + port); | ||
return _this.events.emit("ready", _this); | ||
} else { | ||
_this.log("MongoAdapter: Error connecting to MongoDB server @ " + host + ":" + port + " - " + error); | ||
return _this.events.emit("error", error); | ||
} | ||
}); | ||
}; | ||
})(this)); | ||
} | ||
Adapter.prototype.collection = function(name) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
return _this.database.collection(name, function(error, collection) { | ||
if (error == null) { | ||
return events.emit("success", Collection.make({ | ||
collection: collection, | ||
events: _this.events, | ||
adapter: _this, | ||
log: _this.log | ||
})); | ||
} else { | ||
return events.emit("error", error); | ||
} | ||
}); | ||
}); | ||
return this.events.source((function(_this) { | ||
return function(events) { | ||
return _this.database.collection(name, function(error, collection) { | ||
if (error == null) { | ||
return events.emit("success", Collection.make({ | ||
collection: collection, | ||
events: _this.events, | ||
adapter: _this, | ||
log: _this.log | ||
})); | ||
} else { | ||
return events.emit("error", error); | ||
} | ||
}); | ||
}; | ||
})(this)); | ||
}; | ||
@@ -100,4 +102,3 @@ | ||
return match("object", function(query) { | ||
var key, values, _query, | ||
_this = this; | ||
var key, values, _query; | ||
_query = query; | ||
@@ -110,13 +111,15 @@ for (key in query) { | ||
} | ||
return this.events.source(function(events) { | ||
return events.safely(function() { | ||
return _this.collection.find(_query, function(error, cursor) { | ||
if (error) { | ||
return events.emit("error", error); | ||
} else { | ||
return cursor.toArray(events.callback); | ||
} | ||
return this.events.source((function(_this) { | ||
return function(events) { | ||
return events.safely(function() { | ||
return _this.collection.find(_query, function(error, cursor) { | ||
if (error) { | ||
return events.emit("error", error); | ||
} else { | ||
return cursor.toArray(events.callback); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; | ||
})(this)); | ||
}); | ||
@@ -132,8 +135,9 @@ }); | ||
return match("object", function(query) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
return events.safely(function() { | ||
return _this.collection.findOne(query, events.callback); | ||
}); | ||
}); | ||
return this.events.source((function(_this) { | ||
return function(events) { | ||
return events.safely(function() { | ||
return _this.collection.findOne(query, events.callback); | ||
}); | ||
}; | ||
})(this)); | ||
}); | ||
@@ -149,14 +153,15 @@ }); | ||
return match("object", "object", function(key, object) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
if (key._id != null) { | ||
object._id = key._id; | ||
} else { | ||
delete object._id; | ||
} | ||
return _this.collection.update(key, object, { | ||
upsert: true, | ||
safe: true | ||
}, events.callback); | ||
}); | ||
return this.events.source((function(_this) { | ||
return function(events) { | ||
if (key._id != null) { | ||
object._id = key._id; | ||
} else { | ||
delete object._id; | ||
} | ||
return _this.collection.update(key, object, { | ||
upsert: true, | ||
safe: true | ||
}, events.callback); | ||
}; | ||
})(this)); | ||
}); | ||
@@ -172,11 +177,12 @@ }); | ||
return match("object", "object", function(key, patch) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
delete patch._id; | ||
return _this.collection.update(key, { | ||
$set: patch | ||
}, { | ||
safe: true | ||
}, events.callback); | ||
}); | ||
return this.events.source((function(_this) { | ||
return function(events) { | ||
delete patch._id; | ||
return _this.collection.update(key, { | ||
$set: patch | ||
}, { | ||
safe: true | ||
}, events.callback); | ||
}; | ||
})(this)); | ||
}); | ||
@@ -192,23 +198,7 @@ }); | ||
return match("object", function(key) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
return _this.collection.remove(key, function(error, results) { | ||
if (error == null) { | ||
return events.emit("success"); | ||
} else { | ||
return events.emit("error", error); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
Collection.prototype.all = function() { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
return _this.collection.find({}, function(error, results) { | ||
if (error == null) { | ||
return results.toArray(function(error, results) { | ||
return this.events.source((function(_this) { | ||
return function(events) { | ||
return _this.collection.remove(key, function(error, results) { | ||
if (error == null) { | ||
return events.emit("success", results); | ||
return events.emit("success"); | ||
} else { | ||
@@ -218,20 +208,39 @@ return events.emit("error", error); | ||
}); | ||
} else { | ||
return events.emit("error", error); | ||
} | ||
}); | ||
}; | ||
})(this)); | ||
}); | ||
}); | ||
Collection.prototype.all = function() { | ||
return this.events.source((function(_this) { | ||
return function(events) { | ||
return _this.collection.find({}, function(error, results) { | ||
if (error == null) { | ||
return results.toArray(function(error, results) { | ||
if (error == null) { | ||
return events.emit("success", results); | ||
} else { | ||
return events.emit("error", error); | ||
} | ||
}); | ||
} else { | ||
return events.emit("error", error); | ||
} | ||
}); | ||
}; | ||
})(this)); | ||
}; | ||
Collection.prototype.count = function() { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
return _this.collection.count(function(error, count) { | ||
if (error == null) { | ||
return events.emit("success", count); | ||
} else { | ||
return events.emit("error", error); | ||
} | ||
}); | ||
}); | ||
return this.events.source((function(_this) { | ||
return function(events) { | ||
return _this.collection.count(function(error, count) { | ||
if (error == null) { | ||
return events.emit("success", count); | ||
} else { | ||
return events.emit("error", error); | ||
} | ||
}); | ||
}; | ||
})(this)); | ||
}; | ||
@@ -238,0 +247,0 @@ |
@@ -1,11 +0,16 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.9.0 | ||
(function() { | ||
var Adapter, BaseAdapter, BaseCollection, Collection, defaults, merge, overload, redis, type, _ref, _ref1, | ||
var Adapter, BaseAdapter, BaseCollection, Collection, async, defaults, liftAll, liftCommands, merge, redis, type, w, _ref, _ref1, | ||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, | ||
__hasProp = {}.hasOwnProperty, | ||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; | ||
__slice = [].slice; | ||
w = require("when"); | ||
async = (require("when/generator")).lift; | ||
liftAll = require("when/node").liftAll; | ||
_ref = require("fairmont"), type = _ref.type, merge = _ref.merge; | ||
overload = require("typely").overload; | ||
redis = require("redis"); | ||
@@ -15,2 +20,9 @@ | ||
liftCommands = function(proto, f, n) { | ||
if ((require("redis/lib/commands")).indexOf(n) >= 0) { | ||
proto[n] = f; | ||
} | ||
return proto; | ||
}; | ||
defaults = { | ||
@@ -28,35 +40,37 @@ port: 6379, | ||
function Adapter(configuration) { | ||
var _this = this; | ||
this.configuration = configuration; | ||
function Adapter(_at_configuration) { | ||
this.configuration = _at_configuration; | ||
this.configuration = merge(defaults, this.configuration); | ||
Adapter.__super__.constructor.call(this, this.configuration); | ||
this.events.safely(function() { | ||
_this.client = redis.createClient(_this.configuration.port, _this.configuration.host); | ||
_this.client.on("ready", function() { | ||
_this.log("RedisAdapter: Connected to Redis server @ " + _this.configuration.host + ":" + _this.configuration.port); | ||
return _this.events.emit("ready", _this); | ||
}); | ||
return _this.client.on("error", function(err) { | ||
_this.log("RedisAdapter: Error connecting to Redis server @ " + _this.configuration.host + ":" + _this.configuration.port + " - " + err); | ||
return _this.events.emit("error", err); | ||
}); | ||
}); | ||
if (this.log == null) { | ||
this.log = console.log; | ||
} | ||
} | ||
Adapter.prototype.connect = function() { | ||
return w.promise((function(_this) { | ||
return function(resolve, reject) { | ||
var client; | ||
return client = redis.createClient(_this.configuration.port, _this.configuration.host).on("ready", function() { | ||
_this.log("RedisAdapter: Connected to Redis server @ " + _this.configuration.host + ":" + _this.configuration.port); | ||
_this.client = liftAll(redis.RedisClient.prototype, liftCommands, client); | ||
return resolve(_this.client); | ||
}).on("error", function(err) { | ||
_this.log("RedisAdapter: Error connecting to Redis server @ " + _this.configuration.host + ":" + _this.configuration.port + " - " + err); | ||
return reject(err); | ||
}); | ||
}; | ||
})(this)); | ||
}; | ||
Adapter.prototype.collection = function(name) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
return events.emit("success", Collection.make({ | ||
name: name, | ||
events: _this.events, | ||
adapter: _this, | ||
log: _this.log | ||
})); | ||
return Collection.make({ | ||
name: name, | ||
adapter: this | ||
}); | ||
}; | ||
Adapter.prototype.close = function() { | ||
return this.client.end(); | ||
}; | ||
Adapter.prototype.close = async(function*() { | ||
return ((yield this.client)).end(); | ||
}); | ||
@@ -75,106 +89,52 @@ return Adapter; | ||
function Collection(_arg) { | ||
this.name = _arg.name, this.events = _arg.events, this.adapter = _arg.adapter, this.log = _arg.log; | ||
this.name = _arg.name, this.adapter = _arg.adapter; | ||
} | ||
Collection.prototype.find = overload(function(match, fail) { | ||
match("array", function(keys) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
return _this.adapter.client.hmget(_this.name, keys, function(err, res) { | ||
var data; | ||
if (err == null) { | ||
data = res.map(function(item, index) { | ||
var obj; | ||
obj = null; | ||
if (item != null) { | ||
obj = JSON.parse(item); | ||
obj._id = keys[index]; | ||
} | ||
return obj; | ||
}); | ||
return events.emit("success", data); | ||
} else { | ||
return events.emit("error", err); | ||
} | ||
}); | ||
}); | ||
Collection.prototype.find = async(function*() { | ||
var keys, res; | ||
keys = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | ||
res = (yield this.adapter.client.hmget(this.name, keys)); | ||
return res.map(function(item, index) { | ||
var obj; | ||
obj = null; | ||
if (item != null) { | ||
obj = JSON.parse(item); | ||
} | ||
return obj; | ||
}); | ||
return match("string", function(key) { | ||
return this.get(key); | ||
}); | ||
}); | ||
Collection.prototype.get = function(key) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
return _this.adapter.client.hget(_this.name, key, function(err, res) { | ||
var data; | ||
if (err == null) { | ||
data = res != null ? JSON.parse(res) : null; | ||
return events.emit("success", data); | ||
} else { | ||
return events.emit("error", err); | ||
} | ||
}); | ||
}); | ||
}; | ||
Collection.prototype.get = async(function*(key) { | ||
var res; | ||
res = (yield this.adapter.client.hget(this.name, key)); | ||
if (res != null) { | ||
return JSON.parse(res); | ||
} else { | ||
return null; | ||
} | ||
}); | ||
Collection.prototype.put = function(key, object) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
return _this.adapter.client.hset(_this.name, key, JSON.stringify(object), function(err, res) { | ||
if (err == null) { | ||
return events.emit("success", object); | ||
} else { | ||
return events.emit("error", err); | ||
} | ||
}); | ||
}); | ||
return this.adapter.client.hset(this.name, key, JSON.stringify(object)); | ||
}; | ||
Collection.prototype["delete"] = function(key) { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
return _this.adapter.client.hdel(_this.name, key, function(err, res) { | ||
if (err == null) { | ||
return events.emit("success"); | ||
} else { | ||
return events.emit("error", err); | ||
} | ||
}); | ||
}); | ||
return this.adapter.client.hdel(this.name, key); | ||
}; | ||
Collection.prototype.all = function() { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
return _this.adapter.client.hgetall(_this.name, function(err, res) { | ||
var data, key, obj, value; | ||
if (err == null) { | ||
data = []; | ||
for (key in res) { | ||
value = res[key]; | ||
obj = JSON.parse(value); | ||
obj._id = key; | ||
data.push(obj); | ||
} | ||
return events.emit("success", data); | ||
} else { | ||
return events.emit("error", err); | ||
} | ||
}); | ||
}); | ||
}; | ||
Collection.prototype.all = async(function*() { | ||
var data, key, obj, res, value; | ||
res = (yield this.adapter.client.hgetall(this.name)); | ||
data = []; | ||
for (key in res) { | ||
value = res[key]; | ||
obj = JSON.parse(value); | ||
obj._id = key; | ||
data.push(obj); | ||
} | ||
return data; | ||
}); | ||
Collection.prototype.count = function() { | ||
var _this = this; | ||
return this.events.source(function(events) { | ||
return _this.adapter.client.hlen(_this.name, function(err, res) { | ||
if (err == null) { | ||
return events.emit("success", res); | ||
} else { | ||
return events.emit("error", err); | ||
} | ||
}); | ||
}); | ||
return this.adapter.client.hlen(this.name); | ||
}; | ||
@@ -181,0 +141,0 @@ |
{ | ||
"name": "pirate", | ||
"version": "0.9.11", | ||
"description": "A simple storage interface with adapters for different storage systems", | ||
"version": "1.0.0-alpha-01", | ||
"description": "A simple key-value storage interface with adapters for different databases", | ||
"main": "lib/index.js", | ||
@@ -11,15 +11,11 @@ "directories": { | ||
"fairmont": "0.4.x", | ||
"elasticsearchclient": "0.5.x", | ||
"mongodb": "1.2.x", | ||
"mutual": "0.4.x", | ||
"redis": "~0.10.1", | ||
"typely": "0.0.0" | ||
"when": "^3.4.6" | ||
}, | ||
"devDependencies": { | ||
"testify": "~0.2.8", | ||
"sleep": "1.1.1", | ||
"coffee-script": "~1.6.3" | ||
"coffee-script": "^1.9.0" | ||
}, | ||
"scripts": { | ||
"prepublish": "coffee -o lib/ -c src/*.coffee" | ||
"test": "coffee --nodejs --harmony test/index.coffee", | ||
"prepublish": "coffee --nodejs --harmony -o lib/ -c src/*.coffee" | ||
}, | ||
@@ -31,2 +27,4 @@ "repository": { | ||
"keywords": [ | ||
"key-value", | ||
"store", | ||
"storage", | ||
@@ -33,0 +31,0 @@ "interface", |
3
1
33969
836
+ Addedwhen@^3.4.6
+ Addedwhen@3.7.8(transitive)
- Removedelasticsearchclient@0.5.x
- Removedmongodb@1.2.x
- Removedmutual@0.4.x
- Removedtypely@0.0.0
- Removedbson@0.1.8(transitive)
- Removedelasticsearchclient@0.5.3(transitive)
- Removedfairmont@0.7.4(transitive)
- Removedgeneric-pool@2.1.1(transitive)
- Removedkey-forge@0.1.3(transitive)
- Removedmongodb@1.2.14(transitive)
- Removedmutual@0.4.20(transitive)
- Removedredis@0.8.6(transitive)
- Removedtypely@0.0.0(transitive)
- Removedwhen@3.6.4(transitive)