PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/fauxton/app/modules/databases/resources.js

https://github.com/benoitc/couchdb
JavaScript | 148 lines | 106 code | 26 blank | 16 comment | 13 complexity | 891942627772435780bed0c54fbff365 MD5 | raw file
  1. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  2. // use this file except in compliance with the License. You may obtain a copy of
  3. // the License at
  4. //
  5. // http://www.apache.org/licenses/LICENSE-2.0
  6. //
  7. // Unless required by applicable law or agreed to in writing, software
  8. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. // License for the specific language governing permissions and limitations under
  11. // the License.
  12. define([
  13. "app",
  14. "api",
  15. // Modules
  16. "modules/documents/resources"
  17. ],
  18. function(app, FauxtonAPI, Documents) {
  19. var Databases = FauxtonAPI.module();
  20. Databases.Model = Backbone.Model.extend({
  21. initialize: function(options) {
  22. this.status = new Databases.Status({
  23. database: this
  24. });
  25. },
  26. buildAllDocs: function(params) {
  27. this.allDocs = new Documents.AllDocs(null, {
  28. database: this,
  29. params: params
  30. });
  31. return this.allDocs;
  32. },
  33. isNew: function(){
  34. // Databases are never new, to make Backbone do a PUT
  35. return false;
  36. },
  37. url: function(context) {
  38. if (context === "index") {
  39. return "/database/" + this.id + "/_all_docs";
  40. } else if (context === "changes") {
  41. return "/database/" + this.id + "/_changes?descending=true&limit=100";
  42. } else if (context === "app") {
  43. return "/database/" + this.id;
  44. } else {
  45. return app.host + "/" + this.id;
  46. }
  47. },
  48. buildChanges: function (params) {
  49. this.changes = new Databases.Changes({
  50. database: this,
  51. params: params
  52. });
  53. return this.changes;
  54. }
  55. });
  56. Databases.Changes = Backbone.Collection.extend({
  57. initialize: function(options) {
  58. this.database = options.database;
  59. this.params = options.params;
  60. },
  61. url: function () {
  62. var query = "";
  63. if (this.params) {
  64. query = "?" + $.param(this.params);
  65. }
  66. return app.host + '/' + this.database.id + '/_changes' + query;
  67. },
  68. parse: function (resp) {
  69. this.last_seq = resp.last_seq;
  70. return resp.results;
  71. }
  72. });
  73. Databases.Status = Backbone.Model.extend({
  74. url: function() {
  75. return app.host + "/" + this.database.id;
  76. },
  77. initialize: function(options) {
  78. this.database = options.database;
  79. },
  80. numDocs: function() {
  81. return this.get("doc_count");
  82. },
  83. updateSeq: function(full) {
  84. var updateSeq = this.get("update_seq");
  85. if (full || (typeof(updateSeq) === 'number')) {
  86. return updateSeq;
  87. } else if (updateSeq) {
  88. return updateSeq.split('-')[0];
  89. } else {
  90. return 0;
  91. }
  92. },
  93. humanSize: function() {
  94. // cribbed from http://stackoverflow.com/questions/10420352/converting-file-size-in-bytes-to-human-readable
  95. var i = -1;
  96. var byteUnits = [' kB', ' MB', ' GB', ' TB', 'PB', 'EB', 'ZB', 'YB'];
  97. var fileSizeInBytes = this.get("disk_size");
  98. do {
  99. fileSizeInBytes = fileSizeInBytes / 1024;
  100. i++;
  101. } while (fileSizeInBytes > 1024);
  102. return Math.max(fileSizeInBytes, 0.1).toFixed(1) + byteUnits[i];
  103. }
  104. });
  105. // TODO: shared databases - read from the user doc
  106. Databases.List = Backbone.Collection.extend({
  107. model: Databases.Model,
  108. url: function() {
  109. return app.host + "/_all_dbs";
  110. },
  111. parse: function(resp) {
  112. // TODO: pagination!
  113. return _.map(_.first(resp, 10), function(database) {
  114. return {
  115. id: encodeURIComponent(database),
  116. name: database
  117. };
  118. });
  119. }
  120. });
  121. return Databases;
  122. });