Skip to content

Commit 93ed66a

Browse files
committed
feat(sidebar): filter both databasees and collections with dot notation
1 parent 12e2aa3 commit 93ed66a

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

packages/compass-sidebar/src/components/use-filtered-connections.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ const sidebarConnections: SidebarConnection[] = [
5353
pipeline: [],
5454
isNonExistent: false,
5555
},
56+
{
57+
_id: 'coll_ready_1_1_2',
58+
name: 'coll_ready_shared_name',
59+
type: 'collection',
60+
sourceName: '',
61+
pipeline: [],
62+
isNonExistent: false,
63+
},
5664
],
5765
collectionsLength: 1,
5866
collectionsStatus: 'ready',
@@ -70,6 +78,14 @@ const sidebarConnections: SidebarConnection[] = [
7078
pipeline: [],
7179
isNonExistent: false,
7280
},
81+
{
82+
_id: 'coll_ready_1_2_2',
83+
name: 'coll_ready_shared_name',
84+
type: 'collection',
85+
sourceName: '',
86+
pipeline: [],
87+
isNonExistent: false,
88+
},
7389
],
7490
collectionsLength: 1,
7591
collectionsStatus: 'ready',
@@ -626,6 +642,27 @@ describe('useFilteredConnections', function () {
626642
});
627643
});
628644

645+
it('should filter collection items and database items using dot notation', async function () {
646+
const { result } = renderHookWithContext(useFilteredConnections, {
647+
initialProps: {
648+
connections: mockSidebarConnections,
649+
filter: {
650+
regex: new RegExp('ready_1_1.coll_ready_shared_name', 'i'), // this matches only coll_ready_shared_name collection in ready_1_1 database
651+
excludeInactive: false,
652+
},
653+
fetchAllCollections: fetchAllCollectionsStub,
654+
onDatabaseExpand: onDatabaseExpandStub,
655+
},
656+
});
657+
658+
await waitFor(() => {
659+
expect(
660+
(result.current.filtered?.[0] as SidebarConnectedConnection)
661+
.databases[0].collections
662+
).to.have.length(1); // the result has 1 collection
663+
});
664+
});
665+
629666
it('as expanded, it should return an object containing an expanded object for the matching items', async function () {
630667
const { result } = renderHookWithContext(useFilteredConnections, {
631668
initialProps: {

packages/compass-sidebar/src/components/use-filtered-connections.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const filterDatabases = (
8585
const results: FilteredDatabase[] = [];
8686
for (const db of databases) {
8787
const isMatch = !regex || regex.test(db.name);
88-
const childMatches = filterCollections(db.collections, regex);
88+
const childMatches = filterCollections(db.collections, regex, db.name);
8989

9090
if (isMatch || childMatches.length) {
9191
// If the db doesn't match, we want to use just the matching collections.
@@ -111,10 +111,14 @@ const filterDatabases = (
111111

112112
const filterCollections = (
113113
collections: SidebarCollection[],
114-
regex: RegExp | null
114+
regex: RegExp | null,
115+
dbName: string
115116
): FilteredCollection[] => {
116117
return collections
117-
.filter(({ name }) => !regex || regex.test(name))
118+
.filter(
119+
({ name }) =>
120+
!regex || regex.test(name) || regex.test(`${dbName}.${name}`)
121+
)
118122
.map((collection) => ({ ...collection, isMatch: true }));
119123
};
120124

0 commit comments

Comments
 (0)