CouchDBRestStore.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. define("dojox/data/CouchDBRestStore", ["dojo", "dojox", "dojox/data/JsonRestStore"], function(dojo, dojox) {
  2. // A CouchDBRestStore is an extension of JsonRestStore to handle CouchDB's idiosyncrasies, special features,
  3. // and deviations from standard HTTP Rest.
  4. // NOTE: CouchDB is not designed to be run on a public facing network. There is no access control
  5. // on database documents, and you should NOT rely on client side control to implement security.
  6. dojo.declare("dojox.data.CouchDBRestStore",
  7. dojox.data.JsonRestStore,
  8. {
  9. save: function(kwArgs){
  10. var actions = this.inherited(arguments); // do the default save and then update for version numbers
  11. var prefix = this.service.servicePath;
  12. for(var i = 0; i < actions.length; i++){
  13. // need to update the item's version number after it has been committed
  14. (function(item,dfd){
  15. dfd.addCallback(function(result){
  16. if(result){
  17. item.__id = prefix + result.id; // update the object with the results of the post
  18. item._rev = result.rev;
  19. }
  20. return result;
  21. });
  22. })(actions[i].content,actions[i].deferred);
  23. }
  24. },
  25. fetch: function(args){
  26. // summary:
  27. // This only differs from JsonRestStore in that it, will put the query string the query part of the URL and it handles start and count
  28. args.query = args.query || '_all_docs?';
  29. if(args.start){
  30. args.query = (args.query ? (args.query + '&') : '') + 'startkey=' + args.start;
  31. delete args.start;
  32. }
  33. if(args.count){
  34. args.query = (args.query ? (args.query + '&') : '') + 'limit=' + args.count;
  35. delete args.count;
  36. }
  37. return this.inherited(arguments);
  38. },
  39. _processResults: function(results){
  40. var rows = results.rows;
  41. if(rows){
  42. var prefix = this.service.servicePath;
  43. var self = this;
  44. for(var i = 0; i < rows.length;i++){
  45. var realItem = rows[i].value;
  46. realItem.__id= prefix + rows[i].id;
  47. realItem._id= rows[i].id;
  48. realItem._loadObject= dojox.rpc.JsonRest._loader;
  49. rows[i] = realItem;
  50. }
  51. return {totalCount:results.total_rows, items:results.rows};
  52. }else{
  53. return {items:results};
  54. }
  55. }
  56. }
  57. );
  58. // create a set of stores
  59. dojox.data.CouchDBRestStore.getStores = function(couchServerUrl){
  60. var dfd = dojo.xhrGet({
  61. url: couchServerUrl+"_all_dbs",
  62. handleAs: "json",
  63. sync: true
  64. });
  65. var stores = {};
  66. dfd.addBoth(function(dbs){
  67. for(var i = 0; i < dbs.length; i++){
  68. stores[dbs[i]] = new dojox.data.CouchDBRestStore({target:couchServerUrl + dbs[i],idAttribute:"_id"});
  69. }
  70. return stores;
  71. });
  72. return stores;
  73. };
  74. return dojox.data.CouchDBRestStore;
  75. });