summaryrefslogtreecommitdiff
path: root/src/fauxton/app/modules/databases/resources.js
blob: a716ea111ff72c57b07d087ee3937363e743aa3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

define([
  "app",

  "api",

  // Modules
  "modules/documents/resources"
],

function(app, FauxtonAPI, Documents) {
  var Databases = FauxtonAPI.module();

  Databases.DocLimit = 20;

  Databases.Model = Backbone.Model.extend({
    initialize: function(options) {
      this.status = new Databases.Status({
        database: this
      });
    },

    documentation: function(){
      return "all_dbs";
    },
    
    buildAllDocs: function(params) {
      this.allDocs = new Documents.AllDocs(null, {
        database: this,
        params: params
      });

      return this.allDocs;
    },

    isNew: function(){
      // Databases are never new, to make Backbone do a PUT
      return false;
    },

    url: function(context) {
      if (context === "index") {
        return "/database/" + this.safeID() + "/_all_docs";
      } else if (context === "web-index") {
        return "#/database/"+ this.safeID() + "/_all_docs?limit=" + Databases.DocLimit;
      } else if (context === "apiurl") { 
        return window.location.origin + "/database/" + this.safeID() + "/_all_docs";
      } else if (context === "changes") {
        return "/database/" + this.safeID() + "/_changes?descending=true&limit=100&include_docs=true";
      } else if (context === "changes-apiurl") { 
        return window.location.origin + "/database/" + this.safeID() + "/_changes?descending=true&limit=100&include_docs=true";
      } else if (context === "app") {
        return "/database/" + this.safeID();
      } else {
        return app.host + "/" + this.safeID();
      }
    },
    safeName: function(){
      return app.mixins.safeURLName(this.get("name"));
    },
    safeID: function() {
      return app.mixins.safeURLName(this.id);
    },
    buildChanges: function (params) {
      this.changes = new Databases.Changes({
        database: this,
        params: params
      });

      return this.changes;
    }
  });

  Databases.Changes = Backbone.Collection.extend({

    initialize: function(options) {
      this.database = options.database;
      this.params = options.params;
    },
    documentation: function(){
      return "changes";
    },
    url: function (context) {
      var query = "";
      if (this.params) {
        query = "?" + $.param(this.params);
      }
      if (context === "apiurl") { 
        return window.location.origin + '/' + this.database.safeID() + '/_changes' + query;
      } else {

      return app.host + '/' + this.database.safeID() + '/_changes' + query;
      }
    },

    parse: function (resp) {
      this.last_seq = resp.last_seq;
      return resp.results;
    }
  });

  Databases.Status = Backbone.Model.extend({
    url: function() {
      return app.host + "/" + this.database.safeID();
    },

    initialize: function(options) {
      this.database = options.database;
    },

    numDocs: function() {
      return this.get("doc_count");
    },

    updateSeq: function(full) {
      var updateSeq = this.get("update_seq");
      if (full || (typeof(updateSeq) === 'number')) {
        return updateSeq;
      } else if (updateSeq) {
        return updateSeq.split('-')[0];
      } else {
        return 0;
      }
    },

    humanSize: function() {
      // cribbed from http://stackoverflow.com/questions/10420352/converting-file-size-in-bytes-to-human-readable
      var i = -1;
      var byteUnits = [' kB', ' MB', ' GB', ' TB', 'PB', 'EB', 'ZB', 'YB'];
      var fileSizeInBytes = this.dataSize();

      if (!fileSizeInBytes) {
        return 0;
      }

      do {
          fileSizeInBytes = fileSizeInBytes / 1024;
          i++;
      } while (fileSizeInBytes > 1024);

      return Math.max(fileSizeInBytes, 0.1).toFixed(1) + byteUnits[i];
    },
    diskSize: function () {
      return this.get("disk_size");
    },

    dataSize: function () {
      return this.get("other").data_size;
    }
  });

  // TODO: shared databases - read from the user doc
  Databases.List = Backbone.Collection.extend({
    model: Databases.Model,
    documentation: function(){
      return "all_dbs";
    },
    url: function(context) {
      if (context === "apiurl") { 
        return window.location.origin + "/_all_dbs";
      } else {
        return app.host + "/_all_dbs";
      }
    },

    parse: function(resp) {
      // TODO: pagination!
      return _.map(resp, function(database) {
        return {
          id: app.mixins.safeURLName(database),
          name: database
        };
      });
    }
  });

  return Databases;
});