CouchDBRestStore.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.data.CouchDBRestStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.data.CouchDBRestStore"] = true;
  8. dojo.provide("dojox.data.CouchDBRestStore");
  9. dojo.require("dojox.data.JsonRestStore");
  10. // A CouchDBRestStore is an extension of JsonRestStore to handle CouchDB's idiosyncrasies, special features,
  11. // and deviations from standard HTTP Rest.
  12. // NOTE: CouchDB is not designed to be run on a public facing network. There is no access control
  13. // on database documents, and you should NOT rely on client side control to implement security.
  14. dojo.declare("dojox.data.CouchDBRestStore",
  15. dojox.data.JsonRestStore,
  16. {
  17. save: function(kwArgs){
  18. var actions = this.inherited(arguments); // do the default save and then update for version numbers
  19. var prefix = this.service.servicePath;
  20. for(var i = 0; i < actions.length; i++){
  21. // need to update the item's version number after it has been committed
  22. (function(item,dfd){
  23. dfd.addCallback(function(result){
  24. if(result){
  25. item.__id = prefix + result.id; // update the object with the results of the post
  26. item._rev = result.rev;
  27. }
  28. return result;
  29. });
  30. })(actions[i].content,actions[i].deferred);
  31. }
  32. },
  33. fetch: function(args){
  34. // summary:
  35. // 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
  36. args.query = args.query || '_all_docs?';
  37. if(args.start){
  38. args.query = (args.query ? (args.query + '&') : '') + 'startkey=' + args.start;
  39. delete args.start;
  40. }
  41. if(args.count){
  42. args.query = (args.query ? (args.query + '&') : '') + 'limit=' + args.count;
  43. delete args.count;
  44. }
  45. return this.inherited(arguments);
  46. },
  47. _processResults: function(results){
  48. var rows = results.rows;
  49. if(rows){
  50. var prefix = this.service.servicePath;
  51. var self = this;
  52. for(var i = 0; i < rows.length;i++){
  53. var realItem = rows[i].value;
  54. realItem.__id= prefix + rows[i].id;
  55. realItem._id= rows[i].id;
  56. realItem._loadObject= dojox.rpc.JsonRest._loader;
  57. rows[i] = realItem;
  58. }
  59. return {totalCount:results.total_rows, items:results.rows};
  60. }else{
  61. return {items:results};
  62. }
  63. }
  64. }
  65. );
  66. // create a set of stores
  67. dojox.data.CouchDBRestStore.getStores = function(couchServerUrl){
  68. var dfd = dojo.xhrGet({
  69. url: couchServerUrl+"_all_dbs",
  70. handleAs: "json",
  71. sync: true
  72. });
  73. var stores = {};
  74. dfd.addBoth(function(dbs){
  75. for(var i = 0; i < dbs.length; i++){
  76. stores[dbs[i]] = new dojox.data.CouchDBRestStore({target:couchServerUrl + dbs[i],idAttribute:"_id"});
  77. }
  78. return stores;
  79. });
  80. return stores;
  81. };
  82. }