Skip to content

Commit c0f3789

Browse files
authored
chore(instance-model, database-model, collection-model): Refactor instance db coll models COMPASS-5208 COMPASS-5209 COMPASS-5210 (#2558)
* chore(collection-model, database-model, instance-model): Update mocha and eslint * refactor(mongodb-data-service): Change and provide new methods for databases/collections info fetching - Remove db/coll fetching from instance method - Add databaseStats public method to fetch dbStats - Add listCollectionsNamesOnly that safely fetches collection names with priviliges fallback - Add collectionInfo method that gets coll info for a single collection from listCollections - Allow nameOnly option in listDatabases * chore(compass-schema-validation): Refactor schema-validation to use new dataService.collectionInfo method * feat(collection-model): Add fetch method to the collection model and collection so it can handle it's own data loading * feat(database-model): Add fetch method to the database model and collection so it can handle it's own data loading * refactor(instance-model): Refactor instance fetch method * refactor(compass-app-stores): Refactor instance store to use new instance/db/coll fetch methods * fix(compass-home): Do not show overlay from compass-home It races with the one in app-stores plugin, no need for that * refactor(compass-sidebar): Refactor sidebar instance state handling to match new app-stores behaviour, update mocks in tests * chore: Update package-lock * chore: Address review feedback - Use async methods in data-service instead of callbacks - Rename extended-model to model * chore(sidebar): Destruct arguments * chore: Mention relevant NODE ticket * test(data-service): Don't forget to call done in tests * test(instance-model): Update instance method mock * test(app-stores): Update instance method mock
1 parent a8d8731 commit c0f3789

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2235
-4224
lines changed

package-lock.json

+1,136-2,403
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/collection-model/.depcheckrc

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
ignores: [
2-
"lodash.result",
3-
"electron"
4-
]
1+
ignores: ['@mongodb-js/prettier-config-compass']

packages/collection-model/.eslintrc

-9
This file was deleted.
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
root: true,
3+
extends: ['@mongodb-js/eslint-config-compass']
4+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"@mongodb-js/prettier-config-compass"

packages/collection-model/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var ExtendedCollectionModel = require('./lib/extended-model');
2-
var ExtendedCollectionModelCollection = require('./lib/extended-model').Collection;
1+
var ExtendedCollectionModel = require('./lib/model');
2+
var ExtendedCollectionModelCollection = require('./lib/model').Collection;
33

44
module.exports = ExtendedCollectionModel;
55
module.exports.Collection = ExtendedCollectionModelCollection;

packages/collection-model/lib/collection-model.js

-168
This file was deleted.

packages/collection-model/lib/extended-model.js

-89
This file was deleted.
+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
const AmpersandModel = require('ampersand-model');
2+
const AmpersandCollection = require('ampersand-collection');
3+
const { promisify } = require('util');
4+
const toNs = require('mongodb-ns');
5+
6+
const CollectionModel = AmpersandModel.extend({
7+
modelType: 'Collection',
8+
idAttribute: '_id',
9+
props: {
10+
_id: 'string',
11+
12+
// Normalized values from listCollections command
13+
name: { type: 'string', required: true },
14+
database: { type: 'string', required: true },
15+
type: { type: 'string', required: true },
16+
system: { type: 'boolean', required: true },
17+
oplog: { type: 'boolean', required: true },
18+
command: { type: 'boolean', required: true },
19+
special: { type: 'boolean', required: true },
20+
specialish: { type: 'boolean', required: true },
21+
normal: { type: 'boolean', required: true },
22+
readonly: 'boolean',
23+
collation: 'object',
24+
pipeline: 'array',
25+
validation: 'object',
26+
27+
// Normalized values from collStats command
28+
ns: 'string',
29+
is_capped: 'boolean',
30+
max: 'number',
31+
is_power_of_two: 'boolean',
32+
index_sizes: 'object',
33+
document_count: 'number',
34+
document_size: 'number',
35+
storage_size: 'number',
36+
index_count: 'number',
37+
index_size: 'number',
38+
padding_factor: 'number',
39+
extent_count: 'number',
40+
extent_last_size: 'number',
41+
flags_user: 'number',
42+
max_document_size: 'number',
43+
size: 'number',
44+
index_details: 'object',
45+
wired_tiger: 'object',
46+
},
47+
/**
48+
* @param {{ dataService: import('mongodb-data-service').DataService }} dataService
49+
* @returns
50+
*/
51+
async fetch({ dataService }) {
52+
const collectionStatsAsync = promisify(
53+
dataService.collectionStats.bind(dataService)
54+
);
55+
const [collStats, collectionInfo] = await Promise.all([
56+
collectionStatsAsync(this.getId()),
57+
dataService.collectionInfo(this.database, this.name),
58+
]);
59+
return this.set({ ...collStats, ...collectionInfo });
60+
},
61+
});
62+
63+
const CollectionCollection = AmpersandCollection.extend({
64+
modelType: 'CollectionCollection',
65+
mainIndex: '_id',
66+
indexes: ['name'],
67+
comparator: '_id',
68+
model: CollectionModel,
69+
/**
70+
* @param {{ dataService: import('mongodb-data-service').DataService }} dataService
71+
* @returns
72+
*/
73+
fetch({ dataService }) {
74+
return new Promise((resolve, reject) => {
75+
const databaseName = this.parent && this.parent.getId();
76+
77+
if (!databaseName) {
78+
throw new Error(
79+
"Trying to fetch MongoDBCollectionCollection that doesn't have the parent model"
80+
);
81+
}
82+
83+
dataService.listCollectionsNamesOnly(databaseName, (err, collections) => {
84+
if (err) {
85+
reject(err);
86+
return;
87+
}
88+
resolve(
89+
this.set(
90+
collections.filter((coll) => {
91+
// TODO: This is not the best place to do this kind of
92+
// filtering, but for now this preserves the current behavior
93+
// and changing it right away will expand the scope of the
94+
// refactor significantly. We can address this in COMPASS-5211
95+
return toNs(`${databaseName}.${coll.name}`).system === false;
96+
})
97+
)
98+
);
99+
});
100+
});
101+
},
102+
});
103+
104+
module.exports = CollectionModel;
105+
module.exports.Collection = CollectionCollection;

0 commit comments

Comments
 (0)