-
Notifications
You must be signed in to change notification settings - Fork 8
feat(mongodb-constants): add new utils for views COMPASS:9700 #567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DarshanaVenkatesh
wants to merge
12
commits into
main
Choose a base branch
from
COMPASS-9700
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+289
−14
Open
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5ec3cbf
add pipeline utils
DarshanaVenkatesh 6f06f72
undo change to not related file
DarshanaVenkatesh 729dc1f
syntax fix
DarshanaVenkatesh 698426f
comment method
DarshanaVenkatesh d0c3e1e
export specific method
DarshanaVenkatesh 77fc67c
add missing dep
DarshanaVenkatesh 7fc5f83
update index.spec.ts
DarshanaVenkatesh 73e9e9e
add isVersionSearchCompatible methods
DarshanaVenkatesh d72e152
export as one
DarshanaVenkatesh 0a8de98
typo
DarshanaVenkatesh 3ba08d8
update view.spec file
DarshanaVenkatesh e2e36f9
change to peerDep and devDep
DarshanaVenkatesh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,7 @@ | |
"typescript": "^5.0.4" | ||
}, | ||
"dependencies": { | ||
"mongodb": "^6.18.0", | ||
"semver": "^7.7.1" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import { expect } from 'chai'; | ||
import { VIEW_PIPELINE_UTILS } from './views'; | ||
import type { Document } from 'mongodb'; | ||
|
||
describe('views', function () { | ||
describe('isPipelineSearchQueryable', function () { | ||
it('should return true for a valid pipeline with $addFields stage', function () { | ||
const pipeline: Document[] = [{ $addFields: { testField: 'testValue' } }]; | ||
expect(VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(pipeline)).to.equal( | ||
true, | ||
); | ||
}); | ||
|
||
it('should return true for a valid pipeline with $set stage', function () { | ||
const pipeline: Document[] = [{ $set: { testField: 'testValue' } }]; | ||
expect(VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(pipeline)).to.equal( | ||
true, | ||
); | ||
}); | ||
|
||
it('should return true for a valid pipeline with $match stage using $expr', function () { | ||
const pipeline: Document[] = [ | ||
{ $match: { $expr: { $eq: ['$field', 'value'] } } }, | ||
]; | ||
expect(VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(pipeline)).to.equal( | ||
true, | ||
); | ||
}); | ||
|
||
it('should return false for a pipeline with an unsupported stage', function () { | ||
const pipeline: Document[] = [{ $group: { _id: '$field' } }]; | ||
expect(VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(pipeline)).to.equal( | ||
false, | ||
); | ||
}); | ||
|
||
it('should return false for a $match stage without $expr', function () { | ||
const pipeline: Document[] = [{ $match: { nonExprKey: 'someValue' } }]; | ||
expect(VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(pipeline)).to.equal( | ||
false, | ||
); | ||
}); | ||
|
||
it('should return false for a $match stage with $expr and additional fields', function () { | ||
const pipeline: Document[] = [ | ||
{ | ||
$match: { | ||
$expr: { $eq: ['$field', 'value'] }, | ||
anotherField: 'value', | ||
}, | ||
}, | ||
]; | ||
expect(VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(pipeline)).to.equal( | ||
false, | ||
); | ||
}); | ||
|
||
it('should return true for an empty pipeline', function () { | ||
const pipeline: Document[] = []; | ||
expect(VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(pipeline)).to.equal( | ||
true, | ||
); | ||
}); | ||
|
||
it('should return false if any stage in the pipeline is invalid', function () { | ||
const pipeline: Document[] = [ | ||
{ $addFields: { testField: 'testValue' } }, | ||
{ $match: { $expr: { $eq: ['$field', 'value'] } } }, | ||
{ $group: { _id: '$field' } }, | ||
]; | ||
expect(VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(pipeline)).to.equal( | ||
false, | ||
); | ||
}); | ||
|
||
it('should handle a pipeline with multiple valid stages', function () { | ||
const pipeline: Document[] = [ | ||
{ $addFields: { field1: 'value1' } }, | ||
{ $match: { $expr: { $eq: ['$field', 'value'] } } }, | ||
{ $set: { field2: 'value2' } }, | ||
]; | ||
expect(VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(pipeline)).to.equal( | ||
true, | ||
); | ||
}); | ||
|
||
it('should return false for a $match stage with no conditions', function () { | ||
const pipeline: Document[] = [{ $match: {} }]; | ||
expect(VIEW_PIPELINE_UTILS.isPipelineSearchQueryable(pipeline)).to.equal( | ||
false, | ||
); | ||
}); | ||
}); | ||
|
||
describe('isVersionSearchCompatibleForViewsDataExplorer', function () { | ||
it('should return true for a version greater than or equal to 8.0.0', function () { | ||
expect( | ||
VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsDataExplorer( | ||
'8.0.0', | ||
), | ||
).to.equal(true); | ||
expect( | ||
VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsDataExplorer( | ||
'8.0.1', | ||
), | ||
).to.equal(true); | ||
expect( | ||
VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsDataExplorer( | ||
'8.1.0', | ||
), | ||
).to.equal(true); | ||
}); | ||
|
||
it('should return false for a version less than 8.0.0', function () { | ||
expect( | ||
VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsDataExplorer( | ||
'7.9.9', | ||
), | ||
).to.equal(false); | ||
expect( | ||
VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsDataExplorer( | ||
'7.0.0', | ||
), | ||
).to.equal(false); | ||
}); | ||
|
||
it('should handle invalid version format by returning false', function () { | ||
expect( | ||
VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsDataExplorer( | ||
'invalid-version', | ||
), | ||
).to.equal(false); | ||
expect( | ||
VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsDataExplorer(''), | ||
).to.equal(false); | ||
}); | ||
}); | ||
|
||
describe('isVersionSearchCompatibleForViewsCompass', function () { | ||
it('should return true for a version greater than or equal to 8.1.0', function () { | ||
expect( | ||
VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsCompass('8.1.0'), | ||
).to.equal(true); | ||
expect( | ||
VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsCompass('8.1.1'), | ||
).to.equal(true); | ||
expect( | ||
VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsCompass('8.2.0'), | ||
).to.equal(true); | ||
}); | ||
|
||
it('should return false for a version less than 8.1.0', function () { | ||
expect( | ||
VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsCompass('8.0.9'), | ||
).to.equal(false); | ||
expect( | ||
VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsCompass('8.0.0'), | ||
).to.equal(false); | ||
expect( | ||
VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsCompass('7.9.9'), | ||
).to.equal(false); | ||
}); | ||
|
||
it('should handle invalid version format by returning false', function () { | ||
expect( | ||
VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsCompass( | ||
'invalid-version', | ||
), | ||
).to.equal(false); | ||
expect( | ||
VIEW_PIPELINE_UTILS.isVersionSearchCompatibleForViewsCompass(''), | ||
).to.equal(false); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** utils related to view pipeline **/ | ||
|
||
import type { Document } from 'mongodb'; | ||
import semver from 'semver'; | ||
|
||
const MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_DE = '8.0.0'; | ||
const MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_COMPASS = '8.1.0'; | ||
|
||
/** | ||
* A view pipeline is searchQueryable (ie: a search index can be created on view) if | ||
* a pipeline consists of only addFields, set and match with expr stages | ||
* | ||
* @param pipeline the view pipeline | ||
* @returns whether pipeline is search queryable | ||
*/ | ||
const isPipelineSearchQueryable = (pipeline: Document[]): boolean => { | ||
for (const stage of pipeline) { | ||
const stageKey = Object.keys(stage)[0]; | ||
|
||
// Check if the stage is $addFields, $set, or $match | ||
if ( | ||
!( | ||
stageKey === '$addFields' || | ||
stageKey === '$set' || | ||
stageKey === '$match' | ||
) | ||
) { | ||
return false; | ||
} | ||
|
||
// If the stage is $match, check if it uses $expr | ||
if (stageKey === '$match') { | ||
const matchStage = stage['$match'] as Document; | ||
const matchKeys = Object.keys(matchStage || {}); | ||
|
||
if (matchKeys.length !== 1 || !matchKeys.includes('$expr')) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
/** | ||
* Views allow search indexes to be made on them in DE for server version 8.1+ | ||
* | ||
* @param serverVersion the server version | ||
* @returns whether serverVersion is search compatible for views in DE | ||
*/ | ||
const isVersionSearchCompatibleForViewsDataExplorer = ( | ||
serverVersion: string, | ||
): boolean => { | ||
try { | ||
return semver.gte( | ||
serverVersion, | ||
MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_DE, | ||
); | ||
} catch { | ||
return false; | ||
} | ||
}; | ||
|
||
/** | ||
* Views allow search indexes to be made on them in compass for mongodb version 8.0+ | ||
* | ||
* @param serverVersion the server version | ||
* @returns whether serverVersion is search compatible for views in Compass | ||
*/ | ||
const isVersionSearchCompatibleForViewsCompass = ( | ||
serverVersion: string, | ||
): boolean => { | ||
try { | ||
return semver.gte( | ||
serverVersion, | ||
MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_COMPASS, | ||
); | ||
} catch { | ||
return false; | ||
} | ||
}; | ||
|
||
export const VIEW_PIPELINE_UTILS = { | ||
MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_DE, | ||
MIN_VERSION_FOR_VIEW_SEARCH_COMPATIBILITY_COMPASS, | ||
isPipelineSearchQueryable, | ||
isVersionSearchCompatibleForViewsDataExplorer, | ||
isVersionSearchCompatibleForViewsCompass, | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have this instead in
peerDependencies
anddevDependencies
similar to how thedevtools-connect
is doing it?devtools-shared/packages/devtools-connect/package.json
Line 58 in f517a4a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, done!