UNPKG

3.25 kBJavaScriptView Raw
1var MongoDBInstance = require('./model');
2var MongoDBCollection = require('mongodb-collection-model');
3var MongoDBCollectionCollection = require('mongodb-collection-model').Collection;
4var BaseDatabaseModel = require('mongodb-database-model');
5var BaseDatabaseCollection = require('mongodb-database-model').Collection;
6var filterableMixin = require('ampersand-collection-filterable');
7var toNS = require('mongodb-ns');
8var raf = require('raf');
9var result = require('lodash.result');
10var clone = require('lodash.clone');
11
12/**
13 * A user selectable collection of `MongoDBCollection`'s with `specialish`
14 * collections filtered out.
15 */
16var MongoDBCollectionOnInstanceCollection = MongoDBCollectionCollection.extend({
17 namespace: 'MongoDBCollectionOnInstanceCollection',
18 model: MongoDBCollection,
19 parse: function(res) {
20 return res.filter(function(d) {
21 return !toNS(d._id).system;
22 });
23 },
24 /**
25 * @param {Object} model you want to mark as selected.
26 * @return {Boolean} false if model already selected, true if incoming
27 * selected and previously selected toggled to true.
28 */
29 select: function(model) {
30 if (model.selected) {
31 return false;
32 }
33 raf(function selectableMarkModelSelected() {
34 var current = this.selected;
35 if (current) {
36 current.selected = false;
37 }
38 model.selected = true;
39 this.selected = model;
40 this.trigger('change:selected', this.selected);
41 }.bind(this));
42
43 return true;
44 },
45 unselectAll: function() {
46 if (this.selected) {
47 this.selected.set({
48 selected: false
49 });
50 }
51 this.selected = null;
52 this.trigger('change:selected', this.selected);
53 }
54}, filterableMixin);
55
56var DatabaseModel = BaseDatabaseModel.extend({
57 collections: {
58 collections: MongoDBCollectionOnInstanceCollection
59 }
60});
61
62var DatabaseCollection = BaseDatabaseCollection.extend({
63 model: DatabaseModel
64}, filterableMixin);
65
66/**
67 * Metadata for a MongoDB Instance, such as a `db.hostInfo()`, `db.listDatabases()`,
68 * `db.buildInfo()`, and more.
69 *
70 * @see http://npm.im/mongodb-instance-model
71 */
72module.exports = MongoDBInstance.extend({
73 namespace: 'MongoDBInstance',
74 collections: {
75 databases: DatabaseCollection,
76 collections: MongoDBCollectionOnInstanceCollection
77 },
78 url: '/instance',
79 fetch: function(options) {
80 var model = this;
81 var url = result(model, 'url');
82
83 options = options ? clone(options) : {};
84 if (!options.parse) {
85 options.parse = true;
86 }
87
88 var success = options.success;
89 options.success = function(resp) {
90 if (!model.set(model.parse(resp, options), options)) {
91 return false;
92 }
93 if (success) {
94 success(model, resp, options);
95 }
96 model.trigger('sync', model, resp, options);
97 };
98
99 var fn = options.error;
100 options.error = function(resp) {
101 if (fn) {
102 fn(model, resp, options);
103 }
104 model.trigger('error', model, resp, options);
105 };
106
107 var done = function(err, res) {
108 if (err) {
109 return options.error({}, 'error', err.message);
110 }
111 raf(function onClientSuccess() {
112 options.success(res, 'success', res);
113 });
114 };
115
116 options.dataService.instance({}, done);
117 }
118});