spincycle
Version:
A reactive message router and object manager that lets clients subscribe to object property changes on the server
250 lines (230 loc) • 7.81 kB
JavaScript
// Generated by CoffeeScript 1.8.0
(function() {
var CouchPersistence, OStore, couchdb, debug, defer, uuid,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
couchdb = require('felix-couchdb');
defer = require('node-promise').defer;
OStore = require('./OStore');
uuid = require('node-uuid');
debug = process.env["DEBUG"];
CouchPersistence = (function() {
function CouchPersistence() {
this.remove = __bind(this.remove, this);
this.set = __bind(this.set, this);
this.get = __bind(this.get, this);
this.all = __bind(this.all, this);
this.byProviderId = __bind(this.byProviderId, this);
this.getDbFor = __bind(this.getDbFor, this);
this.connect = __bind(this.connect, this);
this.dbs = [];
}
CouchPersistence.prototype.connect = function() {
return this.client = couchdb.createClient(5984, 'localhost', {
auth: {
username: 'admin',
password: process.env["COUCH_ADMIN_PW"]
}
});
};
CouchPersistence.prototype.getDbFor = function(_type) {
var db, q, type;
q = defer();
q.tag = uuid.v4();
type = _type.toLowerCase();
db = this.dbs[type];
if (!db) {
console.log('no cached db found for ' + type + ' q = ' + q.tag);
db = this.client.db(type);
db.exists((function(_this) {
return function(er, exists) {
console.log('exists returned ' + exists + ' for db ' + type + ' q = ' + q.tag);
if (er) {
console.log('ERROR ---------------- ' + er);
console.dir(er);
}
if (exists) {
console.log('database ' + type + ' exists, so returning that' + ' q = ' + q.tag);
_this.dbs[type] = db;
if (!q.done) {
q.resolve(db);
}
return q.done = true;
} else {
console.log('database ' + type + ' does not exists. creating as we speak...' + ' q = ' + q.tag);
return db.create(function(er) {
if (er) {
console.log('DB create error: ' + JSON.stringify(er));
}
db.saveDesign(type, {
views: {
'all': {
map: function(doc) {
if (doc.id && doc.type.toLowerCase() === type) {
return emit(doc.id, doc);
}
}
},
'providerid': {
map: function(doc) {
if (doc.providerId) {
return emit(doc.providerId, doc);
}
}
}
}
});
console.log('new database ' + type + ' created' + ' q = ' + q.tag);
_this.dbs[type] = db;
if (!q.done) {
q.resolve(db);
}
return q.done = true;
});
}
};
})(this));
} else {
q.resolve(db);
}
return q;
};
CouchPersistence.prototype.dot = function(attr) {
return function(obj) {
return obj[attr];
};
};
CouchPersistence.prototype.byProviderId = function(_type, pid) {
var q, type;
console.log('byProviderId called for pid ' + pid + ' and type ' + _type);
q = defer();
type = _type.toLowerCase();
this.getDbFor(type).then((function(_this) {
return function(db) {
console.log('got db ' + db);
return db.view(type, 'providerid', {
key: pid
}, function(err, matches) {
console.log('err = ' + err + ' matches = ' + matches);
console.dir(matches);
return q.resolve(matches.rows.map(_this.dot('value')));
});
};
})(this));
return q;
};
CouchPersistence.prototype.all = function(_type, query, cb) {
var rv, type;
rv = [];
type = _type.toLowerCase();
return this.getDbFor(type).then((function(_this) {
return function(db) {
return db.allDocs(function(err, res) {
var count;
if (err) {
console.log('CouchPersistence fetch all ERROR: ' + err);
console.dir(err);
return cb([]);
} else {
count = res.rows.length;
if (count === 0) {
return cb(rv);
} else {
return res.rows.forEach(function(row) {
return db.getDoc(row.id, function(verr, value) {
if (row.id.indexOf('_') === -1) {
rv.push(value);
}
if (--count === 0) {
return cb(rv);
}
});
});
}
}
});
};
})(this));
};
CouchPersistence.prototype.get = function(_type, id, cb) {
var type;
if (_type) {
type = _type.toLowerCase();
return this.getDbFor(type).then((function(_this) {
return function(db) {
return db.getDoc(id, function(err, res) {
if (err) {
console.log('** Couch Get ERROR for type ' + type + ' id ' + id + ': ' + err);
console.dir(err);
return cb(null);
} else {
return cb(res);
}
});
};
})(this), (function(_this) {
return function(err) {
console.log('getDbFor Couch ERROR: ' + err);
console.dir(err);
return cb(null);
};
})(this));
} else {
console.log('...EEEEhh trying to get DB object with no type + WTF!');
return xyzzy;
}
};
CouchPersistence.prototype.set = function(_type, obj, cb) {
var type;
type = _type.toLowerCase();
return this.getDbFor(type).then((function(_this) {
return function(db) {
var onSave;
onSave = function(err, res, cb) {
var oo;
if (err) {
console.log('** Couch Set ERROR: ' + err);
console.dir(err);
console.dir(obj);
} else {
OStore = require('./OStore');
oo = OStore.objects[obj.id];
if (debug) {
console.log('--------------------------------------------------------------------------------------------- couchpersistence.set setting _rev to ' + res.rev + ' on ' + type + ' ' + obj.id);
}
if (!res.rev) {
console.dir(res);
}
oo._rev = res.rev;
}
if (cb) {
return cb(res);
}
};
return db.saveDoc(obj.id, obj, onSave);
};
})(this));
};
CouchPersistence.prototype.remove = function(_type, obj, cb) {
var type;
type = _type.toLowerCase();
return this.getDbFor(type).then((function(_this) {
return function(db) {
return db.removeDoc(obj.id, obj._rev, function(err, res) {
if (err) {
console.log('** Couch Remove ERROR: ' + err);
console.dir(err);
return console.dir(obj);
} else {
if (cb) {
return cb(res);
}
}
});
};
})(this));
};
return CouchPersistence;
})();
module.exports = CouchPersistence;
}).call(this);
//# sourceMappingURL=CouchPersistence.js.map