forked from fortunejs/fortune
-
Notifications
You must be signed in to change notification settings - Fork 18
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
craigedmunds
wants to merge
4
commits into
master
Choose a base branch
from
couchdb-adapter-spike
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8bc9745
Added simple couchdb adapter and test
am-a 2af93c1
Added couchdb service to wercker
craigedmunds 4275932
Refactored couchDB code, and made suitable for wercker
craigedmunds 6e66d0d
Added mechanism to create DB if it doesn't exist
craigedmunds File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({ | ||
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 inbefore
block.