From 8bc9745d3188ffbf27094942f92f8513baf4fe79 Mon Sep 17 00:00:00 2001 From: Alexander Mackay-Austin Date: Thu, 15 Jan 2015 17:03:29 +0000 Subject: [PATCH 1/4] Added simple couchdb adapter and test --- lib/adapters/couchdb.js | 96 ++++++++++++++++++++++++++++ package.json | 1 + test/fortune-mongodb/couchdb.spec.js | 38 +++++++++++ 3 files changed, 135 insertions(+) create mode 100644 lib/adapters/couchdb.js create mode 100644 test/fortune-mongodb/couchdb.spec.js diff --git a/lib/adapters/couchdb.js b/lib/adapters/couchdb.js new file mode 100644 index 000000000..7d9d4bae0 --- /dev/null +++ b/lib/adapters/couchdb.js @@ -0,0 +1,96 @@ +(function () { + + 'use strict'; + + var util = require('util'); + + var cradle = require('cradle'); + var _ = require('lodash'); + var RSVP = require('rsvp'); + var Promise = RSVP.Promise; + + var adapter = {}; + + adapter._init = function (options) { + + var opt = _.defaults(options, { + db: 'test', + host: 'localhost', + port: 5984 + }); + + var connectionString = util.format('http://%s', opt.host); + + var connection = new(cradle.Connection)(connectionString, opt.port, { + cache: true, + raw: false, + forceSave: true + }); + + this.db = connection.database(options.database); + + }; + + adapter.find = function (model, query, projection) { + var db = this.db; + return new Promise(function (resolve, reject) { + db.get(query, function (err, doc) { + if (err) { + reject(err); + } + else { + resolve(doc); + } + }); + }); + }; + + adapter.findMany = function (model, query, projection) { + + }; + + adapter.create = function (model, id, resource) { + var db = this.db; + + if (!resource) { + resource = id; + id = model; + } + + return new Promise(function (resolve, reject) { + db.save(id, resource, function (err, doc) { + if (err) { + reject(err); + } + else { + resolve(doc); + } + }); + }); + }; + + adapter.awaitConnection = function () { + var db = this.db; + return Promise(function(resolve, reject) { + db.exist(function(err, doesExist) { + if (doesExist) { + resolve(); + } else { + db.create(function(err) { + if (err) { + reject(err) + } else { + resolve(); + } + }); + } + }); + }); + }; + + module.exports = adapter; + + adapter._init({}); + + +}()) \ No newline at end of file diff --git a/package.json b/package.json index 3be6a88c7..edb769cf5 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "url": "git@github.com:flyvictor/fortune.git" }, "dependencies": { + "cradle": "^0.6.9", "express": "~3.5.1", "i": "~0.3.2", "lodash": "~2.4.1", diff --git a/test/fortune-mongodb/couchdb.spec.js b/test/fortune-mongodb/couchdb.spec.js new file mode 100644 index 000000000..cfb49f273 --- /dev/null +++ b/test/fortune-mongodb/couchdb.spec.js @@ -0,0 +1,38 @@ +var should = require('should'); + +var adapter = require('../../lib/adapters/couchdb'); + +var RSVP = require('rsvp'); +var Promise = RSVP.Promise; +var _ = require('lodash'); + +RSVP.on('error', function (err) { + console.log('rsvp err handler', err); + throw err; +}); + +//module.exports = function (options) { + + describe('MongoDB adapter', function () { + var ids; + + describe('Creation', function () { + it('should be able to create document with provided id', function (done) { + + var doc = { + id: '123456789012345678901234' + }; + + adapter.create(doc.id, doc).then(function () { + adapter.find('', doc.id).then(function (doc) { + should.exist(doc); + done(); + }); + }); + + }); + }); + + }); + +//}; From 2af93c1289fe0268e36b3a943985b2a89abec00a Mon Sep 17 00:00:00 2001 From: Craig Edmunds Date: Fri, 16 Jan 2015 12:35:23 +0000 Subject: [PATCH 2/4] Added couchdb service to wercker --- wercker.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/wercker.yml b/wercker.yml index 706f23653..798f3421d 100644 --- a/wercker.yml +++ b/wercker.yml @@ -1,6 +1,7 @@ box: wercker/nodejs services: - wercker/mongodb + - mies/couchdb no-response-timeout: 15 build: steps: From 4275932494b208c14cdfcfb48a496511872e285c Mon Sep 17 00:00:00 2001 From: Craig Edmunds Date: Fri, 16 Jan 2015 12:51:40 +0000 Subject: [PATCH 3/4] Refactored couchDB code, and made suitable for wercker --- lib/adapters/couchdb.js | 175 +++++++++--------- .../couchdb.spec.js | 5 +- .../mongodb.spec.js | 0 test/runner.js | 2 +- 4 files changed, 88 insertions(+), 94 deletions(-) rename test/{fortune-mongodb => databases}/couchdb.spec.js (77%) rename test/{fortune-mongodb => databases}/mongodb.spec.js (100%) diff --git a/lib/adapters/couchdb.js b/lib/adapters/couchdb.js index 7d9d4bae0..e5958c2a1 100644 --- a/lib/adapters/couchdb.js +++ b/lib/adapters/couchdb.js @@ -1,96 +1,89 @@ -(function () { - - 'use strict'; - - var util = require('util'); - - var cradle = require('cradle'); - var _ = require('lodash'); - var RSVP = require('rsvp'); - var Promise = RSVP.Promise; - - var adapter = {}; - - adapter._init = function (options) { - - var opt = _.defaults(options, { - db: 'test', - host: 'localhost', - port: 5984 - }); - - var connectionString = util.format('http://%s', opt.host); - - var connection = new(cradle.Connection)(connectionString, opt.port, { - cache: true, - raw: false, - forceSave: true - }); - - this.db = connection.database(options.database); - - }; - - adapter.find = function (model, query, projection) { - var db = this.db; - return new Promise(function (resolve, reject) { - db.get(query, function (err, doc) { - if (err) { - reject(err); - } - else { - resolve(doc); - } - }); +'use strict'; + +var util = require('util'); + +var cradle = require('cradle'); +var _ = require('lodash'); +var RSVP = require('rsvp'); +var Promise = RSVP.Promise; + +module.exports = function(options) { + + var adapter = { + _init : function (options) { + + var opt = _.defaults(options, { + db: 'test', + host: 'localhost', + port: 5984 + }); + + var connectionString = util.format('http://%s', opt.host); + + var connection = new(cradle.Connection)(connectionString, opt.port, { + cache: true, + raw: false, + forceSave: true + }); + + this.db = connection.database(options.database); + + }, + find : function (model, query, projection) { + var db = this.db; + return new Promise(function (resolve, reject) { + db.get(query, function (err, doc) { + if (err) { + reject(err); + } + else { + resolve(doc); + } }); - }; - - adapter.findMany = function (model, query, projection) { - - }; - - adapter.create = function (model, id, resource) { - var db = this.db; - - if (!resource) { - resource = id; - id = model; - } - - return new Promise(function (resolve, reject) { - db.save(id, resource, function (err, doc) { - if (err) { - reject(err); - } - else { - resolve(doc); - } - }); + }); + }, + findMany : function (model, query, projection) { + + }, + create : function (model, id, resource) { + var db = this.db; + + if (!resource) { + resource = id; + id = model; + } + + return new Promise(function (resolve, reject) { + db.save(id, resource, function (err, doc) { + if (err) { + reject(err); + } + else { + resolve(doc); + } }); - }; - - adapter.awaitConnection = function () { - var db = this.db; - return Promise(function(resolve, reject) { - db.exist(function(err, doesExist) { - if (doesExist) { - resolve(); - } else { - db.create(function(err) { - if (err) { - reject(err) - } else { - resolve(); - } - }); - } + }); + }, + awaitConnection : function () { + var db = this.db; + return Promise(function(resolve, reject) { + db.exist(function(err, doesExist) { + if (doesExist) { + resolve(); + } else { + db.create(function(err) { + if (err) { + reject(err) + } else { + resolve(); + } }); + } }); - }; - - module.exports = adapter; - - adapter._init({}); - + }); + } + }; -}()) \ No newline at end of file + adapter._init(options); + return adapter; +} \ No newline at end of file diff --git a/test/fortune-mongodb/couchdb.spec.js b/test/databases/couchdb.spec.js similarity index 77% rename from test/fortune-mongodb/couchdb.spec.js rename to test/databases/couchdb.spec.js index cfb49f273..da7ebb61c 100644 --- a/test/fortune-mongodb/couchdb.spec.js +++ b/test/databases/couchdb.spec.js @@ -1,6 +1,6 @@ var should = require('should'); -var adapter = require('../../lib/adapters/couchdb'); +var Adapter = require('../../lib/adapters/couchdb'); var RSVP = require('rsvp'); var Promise = RSVP.Promise; @@ -13,12 +13,13 @@ RSVP.on('error', function (err) { //module.exports = function (options) { - describe('MongoDB adapter', function () { + describe('CouchDB adapter', function () { var ids; describe('Creation', function () { it('should be able to create document with provided id', function (done) { + var adapter = Adapter({ host: process.env.WERCKER_COUCHDB_HOST || 'localhost', port: process.env.WERCKER_COUCHDB_PORT ||5984}) var doc = { id: '123456789012345678901234' }; diff --git a/test/fortune-mongodb/mongodb.spec.js b/test/databases/mongodb.spec.js similarity index 100% rename from test/fortune-mongodb/mongodb.spec.js rename to test/databases/mongodb.spec.js diff --git a/test/runner.js b/test/runner.js index 48b016bed..d061bf797 100644 --- a/test/runner.js +++ b/test/runner.js @@ -110,7 +110,7 @@ describe('Fortune test runner', function(){ }); require('./fortune/all')(options); - require('./fortune-mongodb/mongodb.spec.js')(options); + require('./databases/mongodb.spec.js')(options); require('./querytree')(options); From 6e66d0dffe7ed213f044a5d777a5f06674ecd11d Mon Sep 17 00:00:00 2001 From: Craig Edmunds Date: Fri, 16 Jan 2015 13:22:00 +0000 Subject: [PATCH 4/4] Added mechanism to create DB if it doesn't exist --- lib/adapters/couchdb.js | 23 ++++++++++++++-- test/databases/couchdb.spec.js | 49 +++++++++++++++++----------------- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/lib/adapters/couchdb.js b/lib/adapters/couchdb.js index e5958c2a1..1c2950fb0 100644 --- a/lib/adapters/couchdb.js +++ b/lib/adapters/couchdb.js @@ -12,6 +12,8 @@ module.exports = function(options) { var adapter = { _init : function (options) { + var that = this; + var opt = _.defaults(options, { db: 'test', host: 'localhost', @@ -27,6 +29,24 @@ module.exports = function(options) { }); this.db = connection.database(options.database); + + return new Promise(function (resolve, reject) { + that.db.exists(function (err, exists) { + if (err) reject(err); + + if (exists) { + console.log('the force is with you.'); + resolve(adapter); + } else { + console.log('database does not exists.'); + that.db.create(function(err){ + if (err) reject(err); + resolve(adapter); + }); + } + }); + }); + }, find : function (model, query, projection) { @@ -84,6 +104,5 @@ module.exports = function(options) { } }; - adapter._init(options); - return adapter; + return adapter._init(options); } \ No newline at end of file diff --git a/test/databases/couchdb.spec.js b/test/databases/couchdb.spec.js index da7ebb61c..5095849b7 100644 --- a/test/databases/couchdb.spec.js +++ b/test/databases/couchdb.spec.js @@ -1,6 +1,6 @@ var should = require('should'); -var Adapter = require('../../lib/adapters/couchdb'); +var adapterInit = require('../../lib/adapters/couchdb'); var RSVP = require('rsvp'); var Promise = RSVP.Promise; @@ -11,29 +11,30 @@ RSVP.on('error', function (err) { throw err; }); -//module.exports = function (options) { - - describe('CouchDB adapter', function () { - var ids; - - describe('Creation', function () { - it('should be able to create document with provided id', function (done) { - - var adapter = Adapter({ host: process.env.WERCKER_COUCHDB_HOST || 'localhost', port: process.env.WERCKER_COUCHDB_PORT ||5984}) - var doc = { - id: '123456789012345678901234' - }; - - adapter.create(doc.id, doc).then(function () { - adapter.find('', doc.id).then(function (doc) { - should.exist(doc); - done(); - }); - }); - - }); +describe('CouchDB adapter', function () { + var ids; + + describe('Creation', function () { + it('should be able to create document with provided id @now', function (done) { + + adapterInit({ + host: process.env.WERCKER_COUCHDB_HOST || 'localhost', + port: process.env.WERCKER_COUCHDB_PORT || 5984 + }) + .then(function(adapter) { + console.log("adapter initialised", adapter); + var doc = { + id: '123456789012345678901234' + }; + + adapter.create(doc.id, doc).then(function () { + adapter.find('', doc.id).then(function (doc) { + should.exist(doc); + done(); + }); }); - + }) }); + }); -//}; +}); \ No newline at end of file