Skip to content

Commit

Permalink
chore(instance-model, database-model, collection-model): Refactor ins…
Browse files Browse the repository at this point in the history
…tance 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
  • Loading branch information
gribnoysup authored Nov 2, 2021
1 parent a8d8731 commit c0f3789
Show file tree
Hide file tree
Showing 56 changed files with 2,235 additions and 4,224 deletions.
3,539 changes: 1,136 additions & 2,403 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions packages/collection-model/.depcheckrc
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
ignores: [
"lodash.result",
"electron"
]
ignores: ['@mongodb-js/prettier-config-compass']
9 changes: 0 additions & 9 deletions packages/collection-model/.eslintrc

This file was deleted.

4 changes: 4 additions & 0 deletions packages/collection-model/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: ['@mongodb-js/eslint-config-compass']
};
1 change: 1 addition & 0 deletions packages/collection-model/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"@mongodb-js/prettier-config-compass"
4 changes: 2 additions & 2 deletions packages/collection-model/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var ExtendedCollectionModel = require('./lib/extended-model');
var ExtendedCollectionModelCollection = require('./lib/extended-model').Collection;
var ExtendedCollectionModel = require('./lib/model');
var ExtendedCollectionModelCollection = require('./lib/model').Collection;

module.exports = ExtendedCollectionModel;
module.exports.Collection = ExtendedCollectionModelCollection;
168 changes: 0 additions & 168 deletions packages/collection-model/lib/collection-model.js

This file was deleted.

89 changes: 0 additions & 89 deletions packages/collection-model/lib/extended-model.js

This file was deleted.

105 changes: 105 additions & 0 deletions packages/collection-model/lib/model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const AmpersandModel = require('ampersand-model');
const AmpersandCollection = require('ampersand-collection');
const { promisify } = require('util');
const toNs = require('mongodb-ns');

const CollectionModel = AmpersandModel.extend({
modelType: 'Collection',
idAttribute: '_id',
props: {
_id: 'string',

// Normalized values from listCollections command
name: { type: 'string', required: true },
database: { type: 'string', required: true },
type: { type: 'string', required: true },
system: { type: 'boolean', required: true },
oplog: { type: 'boolean', required: true },
command: { type: 'boolean', required: true },
special: { type: 'boolean', required: true },
specialish: { type: 'boolean', required: true },
normal: { type: 'boolean', required: true },
readonly: 'boolean',
collation: 'object',
pipeline: 'array',
validation: 'object',

// Normalized values from collStats command
ns: 'string',
is_capped: 'boolean',
max: 'number',
is_power_of_two: 'boolean',
index_sizes: 'object',
document_count: 'number',
document_size: 'number',
storage_size: 'number',
index_count: 'number',
index_size: 'number',
padding_factor: 'number',
extent_count: 'number',
extent_last_size: 'number',
flags_user: 'number',
max_document_size: 'number',
size: 'number',
index_details: 'object',
wired_tiger: 'object',
},
/**
* @param {{ dataService: import('mongodb-data-service').DataService }} dataService
* @returns
*/
async fetch({ dataService }) {
const collectionStatsAsync = promisify(
dataService.collectionStats.bind(dataService)
);
const [collStats, collectionInfo] = await Promise.all([
collectionStatsAsync(this.getId()),
dataService.collectionInfo(this.database, this.name),
]);
return this.set({ ...collStats, ...collectionInfo });
},
});

const CollectionCollection = AmpersandCollection.extend({
modelType: 'CollectionCollection',
mainIndex: '_id',
indexes: ['name'],
comparator: '_id',
model: CollectionModel,
/**
* @param {{ dataService: import('mongodb-data-service').DataService }} dataService
* @returns
*/
fetch({ dataService }) {
return new Promise((resolve, reject) => {
const databaseName = this.parent && this.parent.getId();

if (!databaseName) {
throw new Error(
"Trying to fetch MongoDBCollectionCollection that doesn't have the parent model"
);
}

dataService.listCollectionsNamesOnly(databaseName, (err, collections) => {
if (err) {
reject(err);
return;
}
resolve(
this.set(
collections.filter((coll) => {
// TODO: This is not the best place to do this kind of
// filtering, but for now this preserves the current behavior
// and changing it right away will expand the scope of the
// refactor significantly. We can address this in COMPASS-5211
return toNs(`${databaseName}.${coll.name}`).system === false;
})
)
);
});
});
},
});

module.exports = CollectionModel;
module.exports.Collection = CollectionCollection;
Loading

0 comments on commit c0f3789

Please sign in to comment.