From 09cf53e1182a5a1cb01addb1a56be86199727626 Mon Sep 17 00:00:00 2001 From: Hiswe Date: Tue, 21 Apr 2015 16:28:34 +0700 Subject: [PATCH] =?UTF-8?q?FIX=20=E2=80=93=20add=20findKeys=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- couch_db.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- package.json | 15 ++++++++------- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/couch_db.js b/couch_db.js index b569fcc9..fd27919f 100644 --- a/couch_db.js +++ b/couch_db.js @@ -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(); } @@ -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; diff --git a/package.json b/package.json index fdee9bc1..b05190a9 100644 --- a/package.json +++ b/package.json @@ -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",