Skip to content

Commit 16cc7e7

Browse files
authored
chore: deduplicate storage path access and create compass-utils (#3431)
In a number of places, we accessed the `@electron/remote` package for fetching storage paths. However, with the enterprise improvements project, we will now also require some of the packages which do this to work in the main process, not just a renderer, so this needs to be updated to conditionally use the `electron` package itself rather than `@electron/remote`. Instead of manually performing this change in every location, extract this shared code and implement fallback handling there. Also move the `promisifyAmpersandMethod` utility there, which (as I understand it) only lived in data-service because we did not have a better place for it at the time. I know adding something named `utils` to a codebase can be a pretty slippery slope, but I think it’s the best choice for this particular problem.
1 parent 09a922b commit 16cc7e7

39 files changed

+816
-181
lines changed

package-lock.json

Lines changed: 585 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-preferences-model/lib/model.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@ var Model = require('ampersand-model');
22
var storageMixin = require('storage-mixin');
33
var get = require('lodash.get');
44
var format = require('util').format;
5-
6-
var electronApp;
7-
try {
8-
electronApp = require('@electron/remote').app;
9-
} catch (e) {
10-
/* eslint no-console: 0 */
11-
console.log('Could not load @electron/remote', e.message);
12-
}
5+
var compassUtils = require('@mongodb-js/compass-utils');
6+
var basepath = (compassUtils.getStoragePaths() || {}).basepath;
137

148
var debug = require('debug')('mongodb-compass:models:preferences');
159

@@ -276,7 +270,7 @@ var Preferences = Model.extend(storageMixin, {
276270
namespace: 'AppPreferences',
277271
storage: {
278272
backend: 'disk',
279-
basepath: electronApp ? electronApp.getPath('userData') : undefined
273+
basepath: basepath
280274
},
281275
initialize: function() {
282276
this.on('page-refresh', this.onPageRefresh.bind(this));

packages/compass-preferences-model/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"test-ci": "npm run test"
3535
},
3636
"dependencies": {
37+
"@mongodb-js/compass-utils": "^0.1.0",
3738
"ampersand-collection-filterable": "^0.3.0",
3839
"ampersand-model": "^8.0.1",
3940
"ampersand-rest-collection": "^6.0.0",
@@ -47,9 +48,7 @@
4748
"storage-mixin": "^5.0.0"
4849
},
4950
"devDependencies": {
50-
"@electron/remote": "^2.0.8",
5151
"depcheck": "^1.4.1",
52-
"electron": "^15.5.7",
5352
"eslint": "^7.25.0",
5453
"eslint-config-mongodb-js": "^3.0.1",
5554
"lodash.result": "^4.5.2",

packages/compass-query-history/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,22 @@
5757
"dependencies": {
5858
"@mongodb-js/compass-components": "^1.0.0",
5959
"@mongodb-js/compass-logging": "^1.0.0",
60+
"@mongodb-js/compass-utils": "^0.1.0",
6061
"bson": "^4.4.1",
6162
"hadron-react-components": "^6.0.0",
62-
"mongodb-data-service": "^22.0.0",
6363
"react": "^16.14.0",
6464
"storage-mixin": "^5.0.0"
6565
},
6666
"peerDependencies": {
6767
"@mongodb-js/compass-components": "^1.0.0",
6868
"@mongodb-js/compass-logging": "^1.0.0",
69+
"@mongodb-js/compass-utils": "^0.1.0",
6970
"bson": "^4.4.1",
7071
"hadron-react-components": "^6.0.0",
71-
"mongodb-data-service": "^22.0.0",
7272
"react": "^16.14.0",
7373
"storage-mixin": "^5.0.0"
7474
},
7575
"devDependencies": {
76-
"@electron/remote": "^2.0.8",
7776
"@mongodb-js/eslint-config-compass": "^1.0.0",
7877
"@mongodb-js/mocha-config-compass": "^1.0.0",
7978
"@mongodb-js/prettier-config-compass": "^1.0.0",

packages/compass-query-history/src/models/favorite-query-collection.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import Collection from 'ampersand-rest-collection';
22
import FavoriteQuery from './favorite-query';
33
import storageMixin from 'storage-mixin';
4-
5-
let remote;
6-
try {
7-
remote = require('@electron/remote');
8-
} catch (e) {
9-
console.error('Could not load @electron/remote', e.message);
10-
}
4+
import { getStoragePaths } from '@mongodb-js/compass-utils';
5+
const { basepath } = getStoragePaths() || {};
116

127
/**
138
* Represents a collection of favorite queries.
@@ -23,7 +18,7 @@ const FavoriteQueryCollection = Collection.extend(storageMixin, {
2318
namespace: 'FavoriteQueries',
2419
storage: {
2520
backend: 'disk',
26-
basepath: remote ? remote.app.getPath('userData') : undefined,
21+
basepath,
2722
},
2823
mainIndex: '_id',
2924
comparator: (favorite) => {

packages/compass-query-history/src/models/favorite-query.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import Query from './query';
22
import storageMixin from 'storage-mixin';
3-
4-
let remote;
5-
try {
6-
remote = require('@electron/remote');
7-
} catch (e) {
8-
console.error('Could not load @electron/remote', e.message);
9-
}
3+
import { getStoragePaths } from '@mongodb-js/compass-utils';
4+
const { basepath } = getStoragePaths() || {};
105

116
/**
127
* A model that represents a favorite MongoDB query.
@@ -16,7 +11,7 @@ const FavoriteQuery = Query.extend(storageMixin, {
1611
namespace: 'FavoriteQueries',
1712
storage: {
1813
backend: 'disk',
19-
basepath: remote ? remote.app.getPath('userData') : undefined,
14+
basepath,
2015
},
2116
props: {
2217
/**

packages/compass-query-history/src/models/recent-query-collection.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import Collection from 'ampersand-rest-collection';
22
import RecentQuery from './recent-query';
33
import storageMixin from 'storage-mixin';
4-
5-
let remote;
6-
try {
7-
remote = require('@electron/remote');
8-
} catch (e) {
9-
console.error('Could not load @electron/remote', e.message);
10-
}
4+
import { getStoragePaths } from '@mongodb-js/compass-utils';
5+
const { basepath } = getStoragePaths() || {};
116

127
/**
138
* Represents a collection of recent queries.
@@ -23,7 +18,7 @@ const RecentQueryCollection = Collection.extend(storageMixin, {
2318
namespace: 'RecentQueries',
2419
storage: {
2520
backend: 'disk',
26-
basepath: remote ? remote.app.getPath('userData') : undefined,
21+
basepath,
2722
},
2823
mainIndex: '_id',
2924
comparator: (recent) => {

packages/compass-query-history/src/models/recent-query.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import Query from './query';
22
import storageMixin from 'storage-mixin';
3-
4-
let remote;
5-
try {
6-
remote = require('@electron/remote');
7-
} catch (e) {
8-
console.error('Could not load @electron/remote', e.message);
9-
}
3+
import { getStoragePaths } from '@mongodb-js/compass-utils';
4+
const { basepath } = getStoragePaths() || {};
105

116
/**
127
* A model that represents a recent MongoDB query.
@@ -16,7 +11,7 @@ const RecentQuery = Query.extend(storageMixin, {
1611
namespace: 'RecentQueries',
1712
storage: {
1813
backend: 'disk',
19-
basepath: remote ? remote.app.getPath('userData') : undefined,
14+
basepath,
2015
},
2116
});
2217

packages/compass-query-history/src/utils/favorite-query-storage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { promisifyAmpersandMethod } from 'mongodb-data-service';
1+
import { promisifyAmpersandMethod } from '@mongodb-js/compass-utils';
22
import { FavoriteQueryCollection, FavoriteQuery } from '../models';
33

44
export class FavoriteQueryStorage {

packages/compass-query-history/src/utils/favorite-query-storage.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { expect } from 'chai';
33
import fs from 'fs';
44
import path from 'path';
55
import os from 'os';
6-
import { promisifyAmpersandMethod } from 'mongodb-data-service';
6+
import { promisifyAmpersandMethod } from '@mongodb-js/compass-utils';
77

88
import { FavoriteQueryStorage } from '.';
99
import { FavoriteQuery, FavoriteQueryCollection } from '../models';

0 commit comments

Comments
 (0)