-
Notifications
You must be signed in to change notification settings - Fork 228
refactor(storage): refactor CompassQueryStorage to accept storage backend rather than userData COMPASS-9489 #7058
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
Draft
nvs119
wants to merge
1
commit into
main
Choose a base branch
from
COMPASS-9489
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.
+90
−40
Draft
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -12,69 +12,119 @@ export type QueryStorageOptions = { | |
basepath?: string; | ||
}; | ||
|
||
export interface QueryStorageBackend<TData> { | ||
loadAll(namespace?: string): Promise<TData[]>; | ||
export interface QueryDocumentBase { | ||
_id: string; | ||
_lastExecuted: Date; | ||
_ns?: string; | ||
} | ||
|
||
// this will be either UserDataBackend (for desktop filesystem storage) or | ||
// ApiBackend (for Compass-Web cloud storage) | ||
export interface QueryStorageBackend<TData extends QueryDocumentBase> { | ||
loadAll(): Promise<TData[]>; | ||
readOne(id: string): Promise<TData | undefined>; | ||
updateAttributes(id: string, data: Partial<TData>): Promise<TData>; | ||
delete(id: string): Promise<boolean>; | ||
saveQuery(data: Omit<TData, '_id' | '_lastExecuted'>): Promise<void>; | ||
saveQuery(data: TData): Promise<void>; | ||
} | ||
|
||
export abstract class CompassQueryStorage<TData extends QueryDocumentBase> | ||
implements QueryStorageBackend<TData> | ||
{ | ||
protected backend: QueryStorageBackend<TData>; | ||
|
||
constructor(backend: QueryStorageBackend<TData>) { | ||
this.backend = backend; | ||
} | ||
|
||
async loadAll(namespace?: string): Promise<TData[]> { | ||
const items = await this.backend.loadAll(); | ||
return items | ||
.sort((a, b) => b._lastExecuted.getTime() - a._lastExecuted.getTime()) | ||
.filter((x) => !namespace || x._ns === namespace); | ||
} | ||
|
||
async readOne(id: string): Promise<TData | undefined> { | ||
return this.backend.readOne(id); | ||
} | ||
|
||
async updateAttributes(id: string, patch: Partial<TData>): Promise<TData> { | ||
return this.backend.updateAttributes(id, patch); | ||
} | ||
|
||
async delete(id: string): Promise<boolean> { | ||
return this.backend.delete(id); | ||
} | ||
|
||
abstract saveQuery(data: Omit<TData, '_id' | '_lastExecuted'>): Promise<void>; | ||
} | ||
|
||
export abstract class CompassQueryStorage< | ||
// UserDataBackend preserves all major functionality and adapts UserData to the QueryStorageBackend interface | ||
export class UserDataBackend< | ||
TSchema extends z.Schema, | ||
TData extends z.output<TSchema> = z.output<TSchema> | ||
TData extends z.output<TSchema> & QueryDocumentBase | ||
> implements QueryStorageBackend<TData> | ||
{ | ||
protected readonly userData: UserData<TSchema>; | ||
constructor( | ||
schemaValidator: TSchema, | ||
protected readonly folder: string, | ||
protected readonly options: QueryStorageOptions | ||
) { | ||
this.userData = new UserData(schemaValidator, { | ||
subdir: folder, | ||
basePath: options.basepath, | ||
serialize: (content) => EJSON.stringify(content, undefined, 2), | ||
deserialize: (content) => EJSON.parse(content), | ||
}); | ||
private userData: UserData<TSchema>; | ||
|
||
constructor(userData: UserData<TSchema>) { | ||
this.userData = userData; | ||
} | ||
|
||
async loadAll(namespace?: string): Promise<TData[]> { | ||
async loadAll(): Promise<TData[]> { | ||
try { | ||
const { data } = await this.userData.readAll(); | ||
const sortedData = data | ||
.sort((a, b) => { | ||
return b._lastExecuted.getTime() - a._lastExecuted.getTime(); | ||
}) | ||
.filter((x) => !namespace || x._ns === namespace); | ||
return sortedData; | ||
return data as TData[]; | ||
} catch { | ||
return []; | ||
} | ||
} | ||
|
||
async updateAttributes(id: string, data: Partial<TData>): Promise<TData> { | ||
await this.userData.write(id, { | ||
...((await this.userData.readOne(id)) ?? {}), | ||
...data, | ||
}); | ||
return await this.userData.readOne(id); | ||
async readOne(id: string): Promise<TData | undefined> { | ||
try { | ||
const result = await this.userData.readOne(id); | ||
return result as TData | undefined; | ||
} catch { | ||
// not sure if this is what I should be returning here | ||
return undefined; | ||
} | ||
} | ||
|
||
async delete(id: string) { | ||
async updateAttributes(id: string, patch: Partial<TData>): Promise<TData> { | ||
// this is kind of a different approach than the original UserData | ||
// just more error handling | ||
const current = await this.readOne(id); | ||
if (!current) { | ||
// will throwing Errors here be bubbled up properly? | ||
// or should I return undefined or null? | ||
throw new Error(`Record not found for id ${id}`); | ||
} | ||
const updated = { ...current, ...patch }; | ||
await this.userData.write(id, updated as z.input<TSchema>); | ||
const after = await this.readOne(id); | ||
if (!after) { | ||
throw new Error(`Failed to update record for id ${id}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly we usually don't throw from these when write failed. Generally speaking I'd suggest to align with the current behavior of those without changing it |
||
} | ||
return after; | ||
} | ||
|
||
async delete(id: string): Promise<boolean> { | ||
return await this.userData.delete(id); | ||
} | ||
|
||
abstract saveQuery(data: any): Promise<void>; | ||
async saveQuery(data: TData): Promise<void> { | ||
await this.userData.write(data._id, data as z.input<TSchema>); | ||
} | ||
} | ||
|
||
export class CompassRecentQueryStorage | ||
extends CompassQueryStorage<typeof RecentQuerySchema, RecentQuery> | ||
extends CompassQueryStorage<RecentQuery> | ||
implements RecentQueryStorage | ||
{ | ||
private readonly maxAllowedQueries = 30; | ||
|
||
constructor(options: QueryStorageOptions = {}) { | ||
super(RecentQuerySchema, 'RecentQueries', options); | ||
constructor(backend: QueryStorageBackend<RecentQuery>) { | ||
super(backend); | ||
} | ||
|
||
async saveQuery( | ||
|
@@ -93,16 +143,16 @@ export class CompassRecentQueryStorage | |
_id, | ||
_lastExecuted: new Date(), | ||
}; | ||
await this.userData.write(_id, recentQuery); | ||
await this.backend.saveQuery(recentQuery); | ||
} | ||
} | ||
|
||
export class CompassFavoriteQueryStorage | ||
extends CompassQueryStorage<typeof FavoriteQuerySchema, FavoriteQuery> | ||
extends CompassQueryStorage<FavoriteQuery> | ||
implements FavoriteQueryStorage | ||
{ | ||
constructor(options: QueryStorageOptions = {}) { | ||
super(FavoriteQuerySchema, 'FavoriteQueries', options); | ||
constructor(backend: QueryStorageBackend<FavoriteQuery>) { | ||
super(backend); | ||
} | ||
|
||
async saveQuery( | ||
|
@@ -118,6 +168,6 @@ export class CompassFavoriteQueryStorage | |
_lastExecuted: new Date(), | ||
_dateSaved: new Date(), | ||
}; | ||
await this.userData.write(_id, favoriteQuery); | ||
await this.backend.saveQuery(favoriteQuery); | ||
} | ||
} |
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.
UserData and dependant services allow to use "update" as "create" in cases where the item doesn't exist yet, so this shouldn't throw