-
Notifications
You must be signed in to change notification settings - Fork 237
feat(data-modeling): implement data model storage that uses atlas user data COMPASS-9949 #7459
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
gribnoysup
wants to merge
4
commits into
main
Choose a base branch
from
COMPASS-9949
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ed88fb9
feat(data-modeling): implement data model storage that uses atlas use…
gribnoysup 2e013e5
chore(components): force hide file input
gribnoysup b0749c6
chore(data-modeling): fix logid
gribnoysup 0229c81
chore(data-modeling): add missing dependency
gribnoysup 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
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
119 changes: 119 additions & 0 deletions
119
packages/compass-data-modeling/src/services/data-model-storage-atlas.tsx
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,119 @@ | ||
import { AtlasUserData } from '@mongodb-js/compass-user-data'; | ||
import type { | ||
DataModelStorage, | ||
MongoDBDataModelDescription, | ||
} from './data-model-storage'; | ||
import { MongoDBDataModelDescriptionSchema } from './data-model-storage'; | ||
import dataModelStorageInMemory from './data-model-storage-in-memory'; | ||
import { | ||
atlasServiceLocator, | ||
type AtlasService, | ||
} from '@mongodb-js/atlas-service/provider'; | ||
import { createServiceProvider } from '@mongodb-js/compass-app-registry'; | ||
import { DataModelStorageServiceProvider } from '../provider'; | ||
import React, { useRef } from 'react'; | ||
import { mongoLogId, useLogger } from '@mongodb-js/compass-logging/provider'; | ||
|
||
class DataModelStorageAtlas implements DataModelStorage { | ||
private readonly userData: AtlasUserData< | ||
typeof MongoDBDataModelDescriptionSchema | ||
>; | ||
constructor(orgId: string, projectId: string, atlasService: AtlasService) { | ||
this.userData = new AtlasUserData( | ||
MongoDBDataModelDescriptionSchema, | ||
'dataModelDescriptions', | ||
{ | ||
orgId, | ||
projectId, | ||
getResourceUrl(path) { | ||
// TODO(COMPASS-9960): this is copied from compass-web entrypoint for | ||
// brevity, but shouldn't be defined outside of AtlasUserData, this | ||
// logic is literally the same between all user data instances and can | ||
// be encapsulated inside of the AtlasUserData class implementation | ||
const [type, pathOrgId, pathProjectId, id] = | ||
path?.split('/').filter(Boolean) || []; | ||
|
||
if ( | ||
!type || | ||
!pathOrgId || | ||
!pathProjectId || | ||
type !== 'dataModelDescriptions' | ||
) { | ||
throw new Error( | ||
'DataModelStorageAtlas is used outside of Atlas Cloud context' | ||
); | ||
} | ||
|
||
return atlasService.userDataEndpoint( | ||
pathOrgId, | ||
pathProjectId, | ||
type, | ||
id | ||
); | ||
}, | ||
authenticatedFetch: atlasService.authenticatedFetch.bind(atlasService), | ||
} | ||
); | ||
} | ||
save(description: MongoDBDataModelDescription) { | ||
return this.userData.write(description.id, description); | ||
} | ||
delete(id: MongoDBDataModelDescription['id']) { | ||
return this.userData.delete(id); | ||
} | ||
async loadAll(): Promise<MongoDBDataModelDescription[]> { | ||
try { | ||
const res = await this.userData.readAll(); | ||
return res.data; | ||
} catch { | ||
return []; | ||
} | ||
} | ||
async load(id: string): Promise<MongoDBDataModelDescription | null> { | ||
return ( | ||
(await this.loadAll()).find((item) => { | ||
return item.id === id; | ||
}) ?? null | ||
); | ||
} | ||
} | ||
|
||
export const DataModelStorageServiceProviderAtlas = createServiceProvider( | ||
function DataModelStorageServiceProviderAtlas({ | ||
children, | ||
orgId, | ||
projectId, | ||
}: { | ||
children?: React.ReactNode; | ||
orgId?: string; | ||
projectId?: string; | ||
}) { | ||
const storageRef = useRef<DataModelStorage>(); | ||
const atlasService = atlasServiceLocator(); | ||
const logger = useLogger('DATA-MODEL-STORAGE'); | ||
|
||
if (!storageRef.current) { | ||
if (orgId && projectId) { | ||
storageRef.current = new DataModelStorageAtlas( | ||
orgId, | ||
projectId, | ||
atlasService | ||
); | ||
} else { | ||
logger.log.warn( | ||
mongoLogId(1_001_000_379), | ||
'DataModelStorageServiceProviderAtlas', | ||
'Falling back to in memory storage because orgId or projectId is missing' | ||
); | ||
// Fallback to in-memory if we're outside of Atlas Cloud | ||
storageRef.current = dataModelStorageInMemory; | ||
} | ||
} | ||
|
||
return ( | ||
<DataModelStorageServiceProvider storage={storageRef.current}> | ||
{children} | ||
</DataModelStorageServiceProvider> | ||
); | ||
} | ||
); |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export * from './dist/services/data-model-storage-in-memory.d'; | ||
export * from './dist/services/data-model-storage-atlas.d'; |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
'use strict'; | ||
module.exports = require('./dist/services/data-model-storage-in-memory'); | ||
module.exports = require('./dist/services/data-model-storage-atlas'); |
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
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.
This clean-up will touch a lot of unrelated code, so I'm splitting it to a separate PR