1 /** 
  2  * @namespace 
  3  * @name couchlib 
  4 */
  5 
  6 /** @lends couchlib */
  7 
  8 var cradle = require('cradle');
  9 
 10 /** Get a CouchDB connection. */
 11 var get_couchdb_connection = function (couchdb_opts) {
 12   var my_opts = {
 13     host: couchdb_opts.host,
 14     port: couchdb_opts.port,
 15     options: {cache: couchdb_opts.cache, raw: false}
 16   };
 17   if (couchdb_opts.user.length > 0 && couchdb_opts.pass.length > 0) {
 18     my_opts['auth'] = {username: couchdb_opts.user, password: couchdb_opts.pass};
 19   }
 20   return new cradle.Connection(my_opts);
 21 };
 22 exports.get_couchdb_connection = get_couchdb_connection;
 23 if (typeof process.singleton_schemas == 'undefined') {
 24   process.singleton_schemas = {};
 25 }
 26 /** Get a object for a CouchDB namespace. */
 27 var get_couchdb_schema = function (couchdb_opts, schema) {
 28   if (couchdb_opts.prefix.length > 0) {
 29     var prefix = couchdb_opts.prefix + '_';
 30   } else {
 31     var prefix = '';
 32   }
 33   var k = couchdb_opts.host + '_' + couchdb_opts.port + '_' + prefix + schema;
 34   if (!process.singleton_schemas.hasOwnProperty(k)) {
 35     var c = get_couchdb_connection(couchdb_opts);
 36     process.singleton_schemas[k] = c.database(prefix + schema);
 37     process.singleton_schemas[k].exists(function (err, exist) {
 38       if (err) console.log('exists err: ' + err.toString());
 39       if (exist == false) {
 40         process.singleton_schemas[k].create(function (err, resp) {
 41           if (err) console.log('create err: ' + err.toString());
 42           else {
 43             if (couchdb_opts.views.hasOwnProperty(schema)) {
 44               for(var i in couchdb_opts.views[schema]) {
 45                 (function () {
 46                   var view = couchdb_opts.views[schema][i];
 47                   process.singleton_schemas[k].save(view.uri, view.design, function (err, doc) {
 48                     console.log(view.uri);
 49                     console.log(err);
 50                     console.log(doc);
 51                   })
 52                 })();
 53               }
 54             }
 55             
 56           }
 57         }); // create
 58       }
 59     }); // exists
 60   }
 61   return process.singleton_schemas[k];
 62 };
 63 exports.get_couchdb_schema = get_couchdb_schema;
 64 
 65 /** Checks if a document exists. */
 66 var check_object_exists = function (couchdb_opts, schema, name, cb) {
 67   var db = get_couchdb_schema(couchdb_opts, schema);
 68   db.get(name, function (err, doc) {
 69     if (err) {
 70       if (err.error == 'not_found') {
 71         cb(undefined, false);
 72       } else {
 73         cb(err, undefined);
 74       }
 75     } else {
 76       cb(undefined, true);
 77     }
 78   });
 79 };
 80 exports.check_object_exists = check_object_exists;
 81 
 82 /** Saves a document after replacing a n properties. */
 83 var merge_object = function (couchdb_opts, schema, name, props, cb) {
 84   var db = get_couchdb_schema(couchdb_opts, schema);
 85   db.get(name, function (err, doc) {
 86     if (err) {
 87       if (err.error == 'not_found') {
 88         cb({func: 'merge_object db.get_not_found', err: err}, undefined);
 89       } else {
 90         cb({func: 'merge_object db.get ', err: err}, undefined);
 91       }
 92     } else {
 93       var rev = doc._rev;
 94       delete doc._rev;
 95       delete doc._id;
 96       for (var i in props) {
 97         if (props.hasOwnProperty(i)) {
 98           doc[i] = props[i];
 99         }
100       };
101       db.save(name, rev, doc, function (err, resp) {
102         if (err) {
103           cb({func: 'merge_object db.save', err: err}, undefined);
104         } else {
105           cb(undefined, resp);
106         }
107       });
108     }
109   });
110 };
111 exports.merge_object = merge_object;
112 
113 /** Saves a document after applying a method to the document. */
114 var meth_object = function (couchdb_opts, schema, name, meth, cb) {
115   var db = get_couchdb_schema(couchdb_opts, schema);
116   db.get(name, function (err, doc) {
117     if (err) {
118       if (err.error == 'not_found') {
119         cb({func: 'meth_object db.get_not_found', err: err}, undefined);
120       } else {
121         cb({func: 'meth_object db.get ', err: err}, undefined);
122       }
123     } else {
124       var rev = doc._rev;
125       delete doc._id;
126       delete doc._rev;
127       db.save(name, rev, meth (doc), function (err, resp) {
128         if (err) {
129           cb({func: 'meth_object db.save', err: err}, undefined);
130         } else {
131           cb(undefined, resp);
132         }
133       });
134     }
135   });
136 };
137 exports.meth_object = meth_object;