Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions packages/my-queries-storage/src/compass-query-storage.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import { UUID, EJSON } from 'bson';
import { UserData, type z } from '@mongodb-js/compass-user-data';
import { RecentQuerySchema, FavoriteQuerySchema } from './query-storage-schema';
import {
RecentQuerySchema,
FavoriteQuerySchema,
type RecentQuery,
type FavoriteQuery,
} from './query-storage-schema';
import type { FavoriteQueryStorage, RecentQueryStorage } from './query-storage';

export type QueryStorageOptions = {
basepath?: string;
};

export abstract class CompassQueryStorage<T extends typeof RecentQuerySchema> {
protected readonly userData: UserData<T>;
export interface QueryStorageBackend<TData> {
loadAll(namespace?: string): Promise<TData[]>;
updateAttributes(id: string, data: Partial<TData>): Promise<TData>;
delete(id: string): Promise<boolean>;
saveQuery(data: Omit<TData, '_id' | '_lastExecuted'>): Promise<void>;
}

export abstract class CompassQueryStorage<
TSchema extends z.Schema,
TData = z.output<TSchema>
Copy link
Collaborator

@gribnoysup gribnoysup Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should either restrict the type here or not make TData a generic at all (and I kinda thing maybe the latter even if it's a bit more verbose), schema and data are connected, so leaving the option to provide data that is not matching the schema for implementations is something we should avoid

Suggested change
TData = z.output<TSchema>
TData extends z.output<TSchema> = z.output<TSchema>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, changed thank you!

> implements QueryStorageBackend<TData>
{
protected readonly userData: UserData<TSchema>;
constructor(
schemaValidator: T,
schemaValidator: TSchema,
protected readonly folder: string,
protected readonly options: QueryStorageOptions
) {
Expand All @@ -22,7 +38,7 @@ export abstract class CompassQueryStorage<T extends typeof RecentQuerySchema> {
});
}

async loadAll(namespace?: string): Promise<z.output<T>[]> {
async loadAll(namespace?: string): Promise<TData[]> {
try {
const { data } = await this.userData.readAll();
const sortedData = data
Expand All @@ -36,10 +52,7 @@ export abstract class CompassQueryStorage<T extends typeof RecentQuerySchema> {
}
}

async updateAttributes(
id: string,
data: Partial<z.input<T>>
): Promise<z.output<T>> {
async updateAttributes(id: string, data: Partial<TData>): Promise<TData> {
await this.userData.write(id, {
...((await this.userData.readOne(id)) ?? {}),
...data,
Expand All @@ -50,10 +63,12 @@ export abstract class CompassQueryStorage<T extends typeof RecentQuerySchema> {
async delete(id: string) {
return await this.userData.delete(id);
}

abstract saveQuery(data: any): Promise<void>;
}

export class CompassRecentQueryStorage
extends CompassQueryStorage<typeof RecentQuerySchema>
extends CompassQueryStorage<typeof RecentQuerySchema, RecentQuery>
implements RecentQueryStorage
{
private readonly maxAllowedQueries = 30;
Expand Down Expand Up @@ -83,7 +98,7 @@ export class CompassRecentQueryStorage
}

export class CompassFavoriteQueryStorage
extends CompassQueryStorage<typeof FavoriteQuerySchema>
extends CompassQueryStorage<typeof FavoriteQuerySchema, FavoriteQuery>
implements FavoriteQueryStorage
{
constructor(options: QueryStorageOptions = {}) {
Expand Down