1 | var AmpersandModel = require('ampersand-model');
|
2 | var AmpersandCollection = require('ampersand-rest-collection');
|
3 | var IndexCollection = require('mongodb-index-model').Collection;
|
4 | var ns = require('mongodb-ns');
|
5 | var each = require('lodash.foreach');
|
6 |
|
7 | var 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 |
|
34 |
|
35 | flags_user: 'number',
|
36 | flags_system: 'number',
|
37 | |
38 |
|
39 |
|
40 | capped: {
|
41 | type: 'boolean',
|
42 | default: false
|
43 | },
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 | power_of_two: {
|
50 | type: 'boolean',
|
51 | default: true
|
52 | },
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | size: 'number',
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | index_details: 'object',
|
77 | |
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
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 |
|
162 | var CollectionCollection = AmpersandCollection.extend({
|
163 | comparator: '_id',
|
164 | model: Collection,
|
165 | modelType: 'CollectionCollection'
|
166 | });
|
167 |
|
168 | module.exports = Collection;
|
169 | module.exports.Collection = CollectionCollection;
|