Skip to content

Commit 95e2ed1

Browse files
committed
setup data for the feature
1 parent 47d6ad9 commit 95e2ed1

File tree

6 files changed

+53
-20
lines changed

6 files changed

+53
-20
lines changed

packages/collection-model/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,11 @@ interface CollectionProps {
7878
index_size: number;
7979
isTimeSeries: boolean;
8080
isView: boolean;
81+
/** Only relevant for a view and identifies collection/view from which this view was created. */
8182
sourceName: string | null;
8283
source: Collection;
8384
properties: { id: string; options?: unknown }[];
85+
ns_source: 'provisioned' | 'privileges';
8486
}
8587

8688
type CollectionDataService = Pick<DataService, 'collectionStats' | 'collectionInfo' | 'listCollections' | 'isListSearchIndexesSupported'>;

packages/collection-model/lib/model.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ function pickCollectionInfo({
102102
validation,
103103
clustered,
104104
fle2,
105+
ns_source,
105106
}) {
106-
return { type, readonly, view_on, collation, pipeline, validation, clustered, fle2 };
107+
return { type, readonly, view_on, collation, pipeline, validation, clustered, fle2, ns_source };
107108
}
108109

109110
/**
@@ -124,6 +125,7 @@ const CollectionModel = AmpersandModel.extend(debounceActions(['fetch']), {
124125
statusError: { type: 'string', default: null },
125126

126127
// Normalized values from collectionInfo command
128+
ns_source: 'string',
127129
readonly: 'boolean',
128130
clustered: 'boolean',
129131
fle2: 'boolean',

packages/data-service/src/data-service.ts

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ import type {
5858
ConnectionFleOptions,
5959
ConnectionOptions,
6060
} from './connection-options';
61-
import type { InstanceDetails } from './instance-detail-helper';
61+
import type {
62+
CollectionDetails,
63+
DatabaseDetails,
64+
InstanceDetails,
65+
} from './instance-detail-helper';
6266
import {
6367
isNotAuthorized,
6468
isNotSupportedPipelineStage,
@@ -312,7 +316,7 @@ export interface DataService {
312316
| ConnectionStatusWithPrivileges['authInfo']['authenticatedUserPrivileges']
313317
| null;
314318
}
315-
): Promise<ReturnType<typeof adaptCollectionInfo>[]>;
319+
): Promise<CollectionDetails[]>;
316320

317321
/**
318322
* Returns normalized collection info provided by listCollection command for a
@@ -419,7 +423,7 @@ export interface DataService {
419423
roles?:
420424
| ConnectionStatusWithPrivileges['authInfo']['authenticatedUserRoles']
421425
| null;
422-
}): Promise<{ _id: string; name: string }[]>;
426+
}): Promise<Omit<DatabaseDetails, 'collections'>[]>;
423427

424428
/**
425429
* Get the stats for a database.
@@ -1291,7 +1295,16 @@ class DataServiceImpl extends WithLogContext implements DataService {
12911295
| ConnectionStatusWithPrivileges['authInfo']['authenticatedUserPrivileges']
12921296
| null;
12931297
} = {}
1294-
): Promise<ReturnType<typeof adaptCollectionInfo>[]> {
1298+
): Promise<CollectionDetails[]> {
1299+
const listCollections = async () => {
1300+
const colls = await this._listCollections(databaseName, filter, {
1301+
nameOnly,
1302+
});
1303+
return colls.map((coll) => ({
1304+
ns_source: 'provisioned' as const,
1305+
...coll,
1306+
}));
1307+
};
12951308
const getCollectionsFromPrivileges = async () => {
12961309
const databases = getPrivilegesByDatabaseAndCollection(
12971310
await this._getPrivilegesOrFallback(privileges),
@@ -1307,11 +1320,11 @@ class DataServiceImpl extends WithLogContext implements DataService {
13071320
// those registered as "real" collection names
13081321
Boolean
13091322
)
1310-
.map((name) => ({ name }));
1323+
.map((name) => ({ name, ns_source: 'privileges' as const }));
13111324
};
13121325

13131326
const [listedCollections, collectionsFromPrivileges] = await Promise.all([
1314-
this._listCollections(databaseName, filter, { nameOnly }),
1327+
listCollections(),
13151328
// If the filter is not empty, we can't meaningfully derive collections
13161329
// from privileges and filter them as the criteria might include any key
13171330
// from the listCollections result object and there is no such info in
@@ -1325,7 +1338,10 @@ class DataServiceImpl extends WithLogContext implements DataService {
13251338
// if they were fetched successfully
13261339
[...collectionsFromPrivileges, ...listedCollections],
13271340
'name'
1328-
).map((coll) => adaptCollectionInfo({ db: databaseName, ...coll }));
1341+
).map((coll) => ({
1342+
ns_source: coll.ns_source,
1343+
...adaptCollectionInfo({ db: databaseName, ...coll }),
1344+
}));
13291345

13301346
return collections;
13311347
}
@@ -1348,7 +1364,7 @@ class DataServiceImpl extends WithLogContext implements DataService {
13481364
roles?:
13491365
| ConnectionStatusWithPrivileges['authInfo']['authenticatedUserRoles']
13501366
| null;
1351-
} = {}): Promise<{ _id: string; name: string }[]> {
1367+
} = {}): Promise<Omit<DatabaseDetails, 'collections'>[]> {
13521368
const adminDb = this._database('admin', 'CRUD');
13531369

13541370
const listDatabases = async () => {
@@ -1363,7 +1379,10 @@ class DataServiceImpl extends WithLogContext implements DataService {
13631379
},
13641380
{ enableUtf8Validation: false }
13651381
);
1366-
return databases;
1382+
return databases.map((x) => ({
1383+
...x,
1384+
ns_source: 'provisioned' as const,
1385+
}));
13671386
} catch (err) {
13681387
// Currently Compass should not fail if listDatabase failed for any
13691388
// possible reason to preserve current behavior. We probably want this
@@ -1395,7 +1414,7 @@ class DataServiceImpl extends WithLogContext implements DataService {
13951414
// out
13961415
Boolean
13971416
)
1398-
.map((name) => ({ name }));
1417+
.map((name) => ({ name, ns_source: 'privileges' as const }));
13991418
};
14001419

14011420
const getDatabasesFromRoles = async () => {
@@ -1410,7 +1429,7 @@ class DataServiceImpl extends WithLogContext implements DataService {
14101429
// have custom privileges that we can't currently fetch.
14111430
['read', 'readWrite', 'dbAdmin', 'dbOwner']
14121431
);
1413-
return databases.map((name) => ({ name }));
1432+
return databases.map((name) => ({ name, ns_source: 'roles' as const }));
14141433
};
14151434

14161435
const [listedDatabases, databasesFromPrivileges, databasesFromRoles] =
@@ -1426,7 +1445,12 @@ class DataServiceImpl extends WithLogContext implements DataService {
14261445
[...databasesFromRoles, ...databasesFromPrivileges, ...listedDatabases],
14271446
'name'
14281447
).map((db) => {
1429-
return { _id: db.name, name: db.name, ...adaptDatabaseInfo(db) };
1448+
return {
1449+
_id: db.name,
1450+
name: db.name,
1451+
ns_source: db.ns_source,
1452+
...adaptDatabaseInfo(db),
1453+
};
14301454
});
14311455

14321456
return databases;

packages/data-service/src/instance-detail-helper.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type DataLakeDetails = {
5454
version: string | null;
5555
};
5656

57-
type CollectionDetails = {
57+
export type CollectionDetails = {
5858
_id: string;
5959
name: string;
6060
database: string;
@@ -76,9 +76,10 @@ type CollectionDetails = {
7676
validationAction: string;
7777
validationLevel: string;
7878
} | null;
79+
ns_source: 'provisioned' | 'privileges';
7980
};
8081

81-
type DatabaseDetails = {
82+
export type DatabaseDetails = {
8283
_id: string;
8384
name: string;
8485
collection_count: number;
@@ -88,6 +89,7 @@ type DatabaseDetails = {
8889
index_count: number;
8990
index_size: number;
9091
collections: CollectionDetails[];
92+
ns_source: 'provisioned' | 'privileges' | 'roles';
9193
};
9294

9395
export type InstanceDetails = {
@@ -358,7 +360,7 @@ function adaptBuildInfo(rawBuildInfo: Partial<BuildInfo>) {
358360

359361
export function adaptDatabaseInfo(
360362
databaseStats: Partial<DbStats> & Partial<DatabaseInfo>
361-
): Omit<DatabaseDetails, '_id' | 'collections' | 'name'> {
363+
): Omit<DatabaseDetails, '_id' | 'collections' | 'name' | 'ns_source'> {
362364
return {
363365
collection_count: databaseStats.collections ?? 0,
364366
document_count: databaseStats.objects ?? 0,
@@ -375,8 +377,10 @@ export function adaptCollectionInfo({
375377
info,
376378
options,
377379
type,
378-
}: CollectionInfoNameOnly &
379-
Partial<CollectionInfo> & { db: string }): CollectionDetails {
380+
}: CollectionInfoNameOnly & Partial<CollectionInfo> & { db: string }): Omit<
381+
CollectionDetails,
382+
'ns_source'
383+
> {
380384
const ns = toNS(`${db}.${name}`);
381385
const {
382386
collection,

packages/database-model/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ interface DatabaseProps {
1616
index_size: number;
1717
collectionsLength: number;
1818
collections: CollectionCollection;
19+
ns_source: 'provisioned' | 'privileges' | 'roles';
1920
}
2021

2122
interface Database extends DatabaseProps {

packages/database-model/lib/model.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ const DatabaseModel = AmpersandModel.extend(
105105
statusError: { type: 'string', default: null },
106106
collectionsStatus: { type: 'string', default: 'initial' },
107107
collectionsStatusError: { type: 'string', default: null },
108-
108+
ns_source: 'string',
109109
collection_count: 'number',
110110
document_count: 'number',
111111
storage_size: 'number',
@@ -236,7 +236,7 @@ const DatabaseCollection = AmpersandCollection.extend(
236236
roles: instanceModel.auth.roles
237237
});
238238

239-
this.set(dbs.map(({ _id, name }) => ({ _id, name })));
239+
this.set(dbs.map(({ _id, name, ns_source }) => ({ _id, name, ns_source })));
240240
},
241241

242242
toJSON(opts = { derived: true }) {

0 commit comments

Comments
 (0)