UNPKG

4.28 kBJavaScriptView Raw
1var AmpersandModel = require('ampersand-model');
2var AmpersandCollection = require('ampersand-rest-collection');
3var IndexCollection = require('mongodb-index-model').Collection;
4var ns = require('mongodb-ns');
5var each = require('lodash.foreach');
6
7var Collection = AmpersandModel.extend({
8 modelType: 'Collection',
9 idAttribute: '_id',
10 props: {
11 _id: {
12 type: 'string',
13 required: true
14 },
15 database: 'string',
16 document_count: 'number',
17 document_size: 'number',
18 storage_size: 'number',
19 index_count: 'number',
20 index_size: 'number',
21 padding_factor: 'number',
22 extent_count: 'number',
23 extent_last_size: 'number',
24 readonly: {
25 type: 'boolean',
26 default: false
27 },
28 collation: {
29 type: 'object',
30 default: null
31 },
32 /**
33 * http://docs.mongodb.org/manual/reference/command/collStats/#collStats.userFlags
34 */
35 flags_user: 'number',
36 flags_system: 'number',
37 /**
38 * Is this a capped collection?
39 */
40 capped: {
41 type: 'boolean',
42 default: false
43 },
44 /**
45 * Is this collection using power of 2 allocation?
46 *
47 * http://docs.mongodb.org/manual/core/storage/#power-of-2-allocation
48 */
49 power_of_two: {
50 type: 'boolean',
51 default: true
52 },
53 /**
54 * The total size in memory of all records in a collection. This value does
55 * not include the record header, which is 16 bytes per record, but does
56 * include the record’s padding. Additionally size does not include the
57 * size of any indexes associated with the collection, which the
58 * totalIndexSize field reports..
59 *
60 * http://docs.mongodb.org/manual/reference/command/collStats/#collStats.size
61 */
62 size: 'number',
63 /**
64 * New in version 3.0.0.
65 *
66 * A document that reports data from the storage engine for each index
67 * in the collection.
68 *
69 * The fields in this document are the names of the indexes, while the
70 * values themselves are documents that contain statistics for the index
71 * provided by the storage engine. These statistics are for
72 * internal diagnostic use.
73 *
74 * http://docs.mongodb.org/manual/reference/command/collStats/#collStats.indexDetails
75 */
76 index_details: 'object',
77 /**
78 * New in version 3.0.0.
79 *
80 * wiredTiger only appears when using the wiredTiger storage engine. This
81 * document contains data reported directly by the WiredTiger engine and
82 * other data for internal diagnostic use.
83 *
84 * http://docs.mongodb.org/manual/reference/command/collStats/#collStats.wiredTiger
85 */
86 wired_tiger: 'object',
87 type: {
88 type: 'string',
89 default: 'collection'
90 },
91 view_on: {
92 type: 'string',
93 default: undefined
94 },
95 pipeline: {
96 type: 'array',
97 default: undefined
98 }
99 },
100 collections: {
101 indexes: IndexCollection
102 },
103 derived: {
104 document_size_average: {
105 deps: ['document_size', 'document_count'],
106 fn: function() {
107 return this.document_size / this.document_count;
108 }
109 },
110 index_size_average: {
111 deps: ['index_size', 'index_count'],
112 fn: function() {
113 return this.index_size / this.index_count;
114 }
115 },
116 name: {
117 deps: ['_id'],
118 fn: function() {
119 if (!this._id) {
120 return undefined;
121 }
122 return ns(this._id).collection;
123 }
124 },
125 specialish: {
126 deps: ['_id'],
127 fn: function() {
128 if (!this._id) {
129 return undefined;
130 }
131 return ns(this._id).specialish;
132 }
133 }
134 },
135 serialize: function() {
136 var res = this.getAttributes(
137 {
138 props: true,
139 derived: true
140 },
141 true
142 );
143
144 each(
145 this._children,
146 function(value, key) {
147 res[key] = this[key].serialize();
148 },
149 this
150 );
151 each(
152 this._collections,
153 function(value, key) {
154 res[key] = this[key].serialize();
155 },
156 this
157 );
158 return res;
159 }
160});
161
162var CollectionCollection = AmpersandCollection.extend({
163 comparator: '_id',
164 model: Collection,
165 modelType: 'CollectionCollection'
166});
167
168module.exports = Collection;
169module.exports.Collection = CollectionCollection;