Skip to content
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

Couchdb adapter spike #113

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
108 changes: 108 additions & 0 deletions lib/adapters/couchdb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
'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 that = this;

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);

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) {
var db = this.db;
return new Promise(function (resolve, reject) {
db.get(query, 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);
}
});
});
},
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();
}
});
}
});
});
}
};

return adapter._init(options);
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"url": "[email protected]:flyvictor/fortune.git"
},
"dependencies": {
"cradle": "^0.6.9",
"express": "~3.5.1",
"i": "~0.3.2",
"lodash": "~2.4.1",
Expand Down
40 changes: 40 additions & 0 deletions test/databases/couchdb.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var should = require('should');

var adapterInit = 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;
});

describe('CouchDB adapter', function () {
var ids;

describe('Creation', function () {
it('should be able to create document with provided id @now', function (done) {

adapterInit({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This initialisation should happen once per full CouchDB adapter test suite, so it's better to keep such things in before block.

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();
});
});
})
});
});

});
File renamed without changes.
2 changes: 1 addition & 1 deletion test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);


Expand Down
1 change: 1 addition & 0 deletions wercker.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
box: wercker/nodejs
services:
- wercker/mongodb
- mies/couchdb
no-response-timeout: 15
build:
steps:
Expand Down