Skip to content

Commit

Permalink
improve database types to allow for querying per model fields
Browse files Browse the repository at this point in the history
  • Loading branch information
gatzjames authored and jackkav committed Jun 7, 2024
1 parent ff972ee commit 5efaa33
Show file tree
Hide file tree
Showing 12 changed files with 18 additions and 30 deletions.
26 changes: 9 additions & 17 deletions packages/insomnia/src/common/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,9 @@ import * as models from '../models/index';
import type { Workspace } from '../models/workspace';
import { generateId } from './misc';

export interface Query {
_id?: string | SpecificQuery;
parentId?: string | SpecificQuery | null;
remoteId?: string | SpecificQuery | null;
plugin?: string;
key?: string;
environmentId?: string | null;
protoFileId?: string;
name?: string | SpecificQuery;
}
export type Query<T extends BaseModel = BaseModel> = {
[key in keyof T]?: string | SpecificQuery | null | undefined;
};

type Sort = Record<string, any>;

Expand All @@ -38,7 +31,6 @@ export interface SpecificQuery {
$ne?: string | null;
}

export type ModelQuery<T extends BaseModel> = Partial<Record<keyof T, SpecificQuery>>;
export type ChangeType = 'insert' | 'update' | 'remove';
export const database = {
all: async function<T extends BaseModel>(type: string) {
Expand Down Expand Up @@ -80,7 +72,7 @@ export const database = {
return ++bufferChangesId;
},

count: async function<T extends BaseModel>(type: string, query: Query = {}) {
count: async function <T extends BaseModel>(type: string, query: Query<T> = {}) {
if (db._empty) {
return _send<number>('count', ...arguments);
}
Expand Down Expand Up @@ -168,7 +160,7 @@ export const database = {

find: async function<T extends BaseModel>(
type: string,
query: Query | string = {},
query: Query<T> | string = {},
sort: Sort = { created: 1 },
) {
if (db._empty) {
Expand Down Expand Up @@ -197,7 +189,7 @@ export const database = {

findMostRecentlyModified: async function<T extends BaseModel>(
type: string,
query: Query = {},
query: Query<T> = {},
limit: number | null = null,
) {
if (db._empty) {
Expand Down Expand Up @@ -280,15 +272,15 @@ export const database = {
}
},

getMostRecentlyModified: async function<T extends BaseModel>(type: string, query: Query = {}) {
getMostRecentlyModified: async function <T extends BaseModel>(type: string, query: Query<T> = {}) {
if (db._empty) {
return _send<T>('getMostRecentlyModified', ...arguments);
}
const docs = await database.findMostRecentlyModified<T>(type, query, 1);
return docs.length ? docs[0] : null;
},

getWhere: async function<T extends BaseModel>(type: string, query: ModelQuery<T> | Query) {
getWhere: async function <T extends BaseModel>(type: string, query: Query<T>) {
if (db._empty) {
return _send<T>('getWhere', ...arguments);
}
Expand Down Expand Up @@ -479,7 +471,7 @@ export const database = {
await database.flushChanges(flushId);
},

removeWhere: async function<T extends BaseModel>(type: string, query: Query) {
removeWhere: async function <T extends BaseModel>(type: string, query: Query<T>) {
if (db._empty) {
return _send<void>('removeWhere', ...arguments);
}
Expand Down
1 change: 0 additions & 1 deletion packages/insomnia/src/models/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ export async function duplicate(environment: Environment) {
$gt: environment.metaSortKey,
},
};
// @ts-expect-error -- TSCONVERSION appears to be a genuine error
const [nextEnvironment] = await db.find<Environment>(type, q, { metaSortKey: 1 });
const nextSortKey = nextEnvironment ? nextEnvironment.metaSortKey : environment.metaSortKey + 100;
// Calculate new sort key
Expand Down
2 changes: 1 addition & 1 deletion packages/insomnia/src/models/grpc-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export async function duplicate(request: GrpcRequest, patch: Partial<GrpcRequest
$gt: request.metaSortKey,
},
};
// @ts-expect-error -- TSCONVERSION

const [nextRequest] = await db.find<GrpcRequest>(type, q, {
metaSortKey: 1,
});
Expand Down
4 changes: 2 additions & 2 deletions packages/insomnia/src/models/plugin-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ export async function upsertByKey(plugin: string, key: string, value: string) {
}

export async function removeByKey(plugin: string, key: string) {
return db.removeWhere(type, { plugin, key });
return db.removeWhere<PluginData>(type, { plugin, key });
}

export async function all(plugin: string) {
return db.find<PluginData>(type, { plugin });
}

export async function removeAll(plugin: string) {
return db.removeWhere(type, { plugin });
return db.removeWhere<PluginData>(type, { plugin });
}

export async function getByKey(plugin: string, key: string) {
Expand Down
1 change: 0 additions & 1 deletion packages/insomnia/src/models/request-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export async function duplicate(requestGroup: RequestGroup, patch: Partial<Reque
},
};

// @ts-expect-error -- TSCONVERSION appears to be a genuine error
const [nextRequestGroup] = await db.find<RequestGroup>(type, q, {
metaSortKey: 1,
});
Expand Down
1 change: 0 additions & 1 deletion packages/insomnia/src/models/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,6 @@ export async function duplicate(request: Request, patch: Partial<Request> = {})
},
};

// @ts-expect-error -- TSCONVERSION appears to be a genuine error
const [nextRequest] = await db.find<Request>(type, q, {
metaSortKey: 1,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/insomnia/src/models/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ async function _findRecentForRequest(
environmentId: string | null,
limit: number,
) {
const query: Query = {
const query: Query<Response> = {
parentId: requestId,
};

Expand Down
2 changes: 1 addition & 1 deletion packages/insomnia/src/models/websocket-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export async function duplicate(request: WebSocketRequest, patch: Partial<WebSoc
$gt: request.metaSortKey,
},
};
// @ts-expect-error -- Database TSCONVERSION

const [nextRequest] = await database.find<WebSocketRequest>(type, q, {
metaSortKey: 1,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/insomnia/src/models/websocket-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ async function _findRecentForRequest(
environmentId: string | null,
limit: number,
) {
const query: Query = {
const query: Query<WebSocketResponse> = {
parentId: requestId,
};

Expand Down
1 change: 0 additions & 1 deletion packages/insomnia/src/models/workspace-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export async function getByParentId(parentId: string) {
}

export async function getByGitRepositoryId(gitRepositoryId: string) {
// @ts-expect-error -- TSCONVERSION needs generic for query
return db.getWhere<WorkspaceMeta>(type, { gitRepositoryId });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import { Project, RemoteProject } from '../../models/project';

export const shouldMigrateProjectUnderOrganization = async () => {
const [localProjectCount, legacyRemoteProjectCount] = await Promise.all([
database.count(models.project.type, {
database.count<Project>(models.project.type, {
remoteId: null,
parentId: null,
}),
database.count(models.project.type, {
database.count<Project>(models.project.type, {
remoteId: { $ne: null },
parentId: null,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const ResponseMultipartViewer: FC<Props> = ({
};
const { canceled, filePath } = await window.dialog.showSaveDialog(options);

if (canceled) {
if (canceled || !filePath) {
return;
}

Expand Down

0 comments on commit 5efaa33

Please sign in to comment.