Skip to content

Commit

Permalink
Added simple iteration filter for CouchDB adapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Aug 1, 2010
1 parent 62f463c commit d2a77f4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 38 deletions.
19 changes: 5 additions & 14 deletions geddy-core/lib/clients/couchdb.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var sys = require("sys");
var http = require("http");

var couchdb = {};
Expand All @@ -12,7 +11,6 @@ couchdb.Client = function (h, p, n) {

couchdb.Client.prototype = new function () {
this.createDocument = function (uuid, item, callback) {
//sys.puts('creating doc ...');
this.request({url: '/' + this.dbName +
'/' + uuid, method: 'PUT', data: item}, function (response) {
if (response.statusCode == 201) {
Expand All @@ -29,31 +27,26 @@ couchdb.Client.prototype = new function () {
};

this.request = function (params, callback) {
var req = {};
var req = {}
, headers
, request;
// DB name can be set for the adapter, or passed in on the params,
// or might even be empty
var dbName = params.dbName || this.dbName || '';
req.method = params.method || 'GET';
req.data = JSON.stringify(params.data) || null;
req.url = '/' + dbName + '/' + params.url;

var headers = {host: this.hostname};
headers = {host: this.hostname};
if (req.data) {
headers['content-length'] = req.data.length;
}

//sys.puts(sys.inspect(this));
//sys.puts(sys.inspect(req));
//sys.puts('making request ...');
var request = this.client.request(req.method, req.url, headers);
request = this.client.request(req.method, req.url, headers);
request.addListener('response', function (response) {
//sys.puts(sys.inspect(response));
//sys.puts("STATUS: " + response.statusCode);
//sys.puts("HEADERS: " + JSON.stringify(response.headers));
response.setEncoding("utf8");
var resp = '';
response.addListener("data", function (chunk) {
//sys.puts("BODY: " + chunk);
resp += chunk;
});
response.addListener("end", function () {
Expand All @@ -66,8 +59,6 @@ couchdb.Client.prototype = new function () {

});
if (req.data) {
//sys.puts('PUT data:');
//sys.puts(req.data);
request.write(req.data);
}
request.end();
Expand Down
4 changes: 2 additions & 2 deletions geddy-core/scripts/runserv.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ process.addListener('uncaughtException', function (err) {
var msg = '';
msg += 'Error starting up application.\n';
msg += err.stack ? err.stack.toString() : '';
sys.debug(msg);
console.warn(msg);
// Die
sys.debug('###shutdown###');
console.warn('###shutdown###');
}
});

Expand Down
62 changes: 42 additions & 20 deletions geddy-model/lib/adapters/couchdb.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
var sys = require('sys');
if (geddy.config.environment == 'development') {
console.warn('Creating CouchDB client for DB adapter, connecting ...');
}

var Client = require('geddy-core/lib/clients/couchdb').Client;
var client = new Client(
var Client = require('geddy-core/lib/clients/couchdb').Client
, client
, postData;

client = new Client(
geddy.config.database.hostname,
geddy.config.database.port,
geddy.config.database.dbName);

// Try to connect to the database, make sure it's there
client.request({url: '/', method: 'GET'}, function (resp) {
if (resp.statusCode == 404) {
throw new Error(geddy.config.database.dbName +
' not found. Set up your DB with geddy-gen db:create.');
}
}
);

// Create the by-type and by-id views -- ignore conflicts,
// this means they're already there
var postData;
postData = {"_id": "_design/type", "views": {"all" : {"map": "function(doc){ if (doc.type) { emit(doc.type, doc) }}"}}};
postData = {"_id": "_design/type", "views": {"all":
{"map": "function(doc){ if (doc.type) { emit(doc.type, doc) }}"}}};
client.request({url: '/_design/type', method: 'PUT',
data: postData}, function (response) {
//sys.puts(sys.inspect(response));
data: postData}, function (resp) {
}
);
postData = {"_id": "_design/id", "views": {"all" : {"map": "function(doc){ if (doc.id) { emit(doc.id, doc) }}"}}};
postData = {"_id": "_design/id", "views": {"all":
{"map": "function(doc){ if (doc.id) { emit(doc.id, doc) }}"}}};
client.request({url: '/_design/id', method: 'PUT',
data: postData}, function (response) {
//sys.puts(sys.inspect(response));
data: postData}, function (resp) {
}
);

Expand Down Expand Up @@ -99,12 +112,15 @@ var adapter = new function () {
};

var _fetchItems = function (params, base) {
var ids;
var ids
, buildItemsFunc = function (resp) {
_buildItems(resp, params);
};
// All of a given datatype
if (!params.ids || params.ids[0] == 'all') {
client.request({url: '/_design/type/_view/all?key=%22' +
_escape(params.dataType) + '%22',
method: 'GET'}, _buildItems);
method: 'GET'}, buildItemsFunc);
}
// By id
else {
Expand All @@ -113,13 +129,13 @@ var adapter = new function () {
ids[i] = _escape(ids[i]);
}
client.request({url: '/_design/id/_view/all', method: 'POST',
data: {keys: ids}}, _buildItems);
data: {keys: ids}}, buildItemsFunc);
}

};

var _buildItems = function (response) {
var body = JSON.parse(response.body);
var _buildItems = function (resp, params) {
var body = JSON.parse(resp.body);
var rows = body.rows;
var data, parsed, resp = [];
// Create typed objects
Expand All @@ -128,11 +144,14 @@ var adapter = new function () {
data = rows[i].value;
if (data) {
parsed = GLOBAL[data.type].create(data);
// Add Couch-specific stuff to allow updates
parsed._id = data._id;
parsed._rev = data._rev;

if (!params.conditions || _matched(parsed, params.conditions)) {
// Add Couch-specific stuff to allow updates
parsed._id = data._id;
parsed._rev = data._rev;

resp.push(parsed);
resp.push(parsed);
}
}
}
_callback(null, resp);
Expand All @@ -150,8 +169,11 @@ var adapter = new function () {

this.all = function () {
var args = Array.prototype.slice.call(arguments);
var callback = args.pop();
// Datatype is first arg
var dataType = args.shift();
// Callback is last arg
var callback = args.pop();
// Optional filtering opts
var params = args.shift() || {};
var include, key;

Expand Down
6 changes: 4 additions & 2 deletions geddy-model/lib/adapters/sql_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,12 @@ var SQLBaseAdapter = function (conn) {
};

this.all = function () {

var args = Array.prototype.slice.call(arguments);
var callback = args.pop();
// Datatype is first arg
var dataType = args.shift();
// Callback is last arg
var callback = args.pop();
// Optional filtering opts
var params = args.shift() || {};
var include, key;

Expand Down

0 comments on commit d2a77f4

Please sign in to comment.