Skip to content

add findKeys method for CouchDB #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion couch_db.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,41 @@ exports.database = function(settings)
this.settings.json = false;
}

var insertDesignDocs = function insertDesignDocs()
{
// the regexp is the same as this.createFindRegex('pad:*', '*:*:*');
// copied here because we can't use this.createFindRegex method in couch
var designDoc = {
views: {
"findAllPads": {
map: function (doc, req) {
if (/(?=^pad:.*$)(?!.*:.*:.*$)/.test(doc._id)){
emit(doc._id.replace('pad:', ''), doc);
}
},
},
},
};

var handleError = function handleError(er) {
if (er) throw new Error(er);
};

// Always ensure that couchDb has the latest version of the design document
this.db.getDoc('_design/ueberDb', function (er, doc) {
if (er && er.error === 'not_found') return this.db.saveDesign('ueberDb', designDoc, handleError);
if (er) throw new Error(er);
designDoc._rev = doc._rev;
this.db.saveDesign('ueberDb', designDoc, handleError);
}.bind(this));

};

exports.database.prototype.init = function(callback)
{
this.client = couch.createClient(this.settings.port, this.settings.host, this.settings.user, this.settings.pass, this.settings.maxListeners);
this.client = couch.createClient(this.settings.port, this.settings.host, this.settings.user, this.settings.password, this.settings.maxListeners);
this.db = this.client.db(this.settings.database);
insertDesignDocs.call(this);
callback();
}

Expand All @@ -52,6 +83,24 @@ exports.database.prototype.get = function (key, callback)
});
}

exports.database.prototype.findKeys = function (key, notKey, callback)
{
var findKey = this.createFindRegex(key, notKey);
this.db.view('ueberDb', 'findAllPads', function (er, result) {
if (er) return callback(er, null);
result = result
.rows
.filter(function (pad) {
// in case of findKeys is used for a more smaller portion thant the basic wildcard
return findKey.test(pad.value._id);
})
.map(function (pad) {
return pad.key;
});
callback(null, result);
});
}

exports.database.prototype.set = function (key, value, callback)
{
var _this = this;
Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
{ "name": "spcsser"}
],
"dependencies" : {
"mysql" : ">=2.5.1",
"dirty" : "0.9.x",
"async" : "0.1.15",
"channels" : "0.0.2",
"redis" : ">=0.12.1",
"pg" : ">=4.0.0-beta2",
"helenus" : "0.6.10"
"mysql" : ">=2.5.1",
"dirty" : "0.9.x",
"async" : "0.1.15",
"channels" : "0.0.2",
"redis" : ">=0.12.1",
"pg" : ">=4.0.0-beta2",
"helenus" : "0.6.10",
"felix-couchdb" : "^1.0.7"
},
"devDependencies": {
"log4js" : "0.4.1",
Expand Down