Skip to content

database handlers #8592

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
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions packages/insomnia/src/main.development.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { SegmentEvent, trackSegmentEvent } from './main/analytics';
import { registerInsomniaProtocols } from './main/api.protocol';
import { backupIfNewerVersionAvailable } from './main/backup';
import { registerGitServiceAPI } from './main/git-service';
import { registerDataBaseHandlers } from './main/ipc/database';
import { ipcMainOn, ipcMainOnce, registerElectronHandlers } from './main/ipc/electron';
import { registergRPCHandlers } from './main/ipc/grpc';
import { registerMainHandlers } from './main/ipc/main';
Expand Down Expand Up @@ -64,6 +65,7 @@ app.on('web-contents-created', (_, contents) => {
app.on('ready', async () => {
registerElectronHandlers();
// @TODO - Maybe move the register stuff in the registerMainHandlers function
registerDataBaseHandlers();
registerMainHandlers();
registergRPCHandlers();
registerGitServiceAPI();
Expand Down
22 changes: 22 additions & 0 deletions packages/insomnia/src/main/ipc/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { database } from '../../common/database';
import type { ModelTypes } from "../../models";
import * as models from '../../models';
import { ipcMainHandle } from "./electron";
export interface DatabaseBridgeAPI {
getWhere: <T>(type: ModelTypes, options: Record<string, unknown>) => Promise<T>;
find: <T>(type: ModelTypes, options: Record<string, unknown>) => Promise<T[]>;
caCertificate: {
create: (options: { parentId: string; path: string }) => Promise<string>;
};
}
export const registerDataBaseHandlers = () => {
ipcMainHandle('database.caCertificate.create', async (_, options: { parentId: string; path: string }) => {
return models.caCertificate.create(options);
});
ipcMainHandle('database.getWhere', async (_, options: { type: ModelTypes; query: Record<string, unknown> }) => {
return (await database.find(options.type, options.query))?.[0];
})
ipcMainHandle('database.find', async (_, options: { type: ModelTypes; query: Record<string, unknown> }) => {
return database.find(options.type, options.query);
})
}
2 changes: 2 additions & 0 deletions packages/insomnia/src/main/ipc/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export type HandleChannels =
| 'curl.readyState'
| 'curlRequest'
| 'database.caCertificate.create'
| 'database.getWhere'
| 'database.find'
| 'getExecution'
| 'grpc.loadMethods'
| 'grpc.loadMethodsFromReflection'
Expand Down
12 changes: 3 additions & 9 deletions packages/insomnia/src/main/ipc/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import iconv from 'iconv-lite';

import { APP_START_TIME, LandingPage, SentryMetrics } from '../../common/sentry';
import type { HiddenBrowserWindowBridgeAPI } from '../../hidden-window';
import * as models from '../../models';
import { SegmentEvent, trackPageView, trackSegmentEvent } from '../analytics';
import { authorizeUserInWindow } from '../authorizeUserInWindow';
import { backup, restoreBackup } from '../backup';
Expand All @@ -17,6 +16,7 @@ import type { CurlBridgeAPI } from '../network/curl';
import { cancelCurlRequest, curlRequest } from '../network/libcurl-promise';
import { addExecutionStep, completeExecutionStep, getExecution, startExecution, type TimingStep, updateLatestStepName } from '../network/request-timing';
import type { WebSocketBridgeAPI } from '../network/websocket';
import type { DatabaseBridgeAPI } from './database';
import { ipcMainHandle, ipcMainOn, ipcMainOnce, type RendererOnChannels } from './electron';
import extractPostmanDataDumpHandler from './extractPostmanDataDump';
import type { gRPCBridgeAPI } from './grpc';
Expand Down Expand Up @@ -48,11 +48,7 @@ export interface RendererToMainBridgeAPI {
showNunjucksContextMenu: (options: { key: string; nunjucksTag?: { template: string; range: MarkerRange } }) => void;
showContextMenu: (options: { key: string; menuItems: MenuItemConstructorOptions[]; extra?: Record<string, any> }) => void;

database: {
caCertificate: {
create: (options: { parentId: string; path: string }) => Promise<string>;
};
};
database: DatabaseBridgeAPI;
hiddenBrowserWindow: HiddenBrowserWindowBridgeAPI;
getExecution: (options: { requestId: string }) => Promise<TimingStep[]>;
addExecutionStep: (options: { requestId: string; stepName: string }) => void;
Expand All @@ -78,9 +74,7 @@ export function registerMainHandlers() {
ipcMainHandle('getExecution', (_, options: { requestId: string }) => {
return getExecution(options.requestId);
});
ipcMainHandle('database.caCertificate.create', async (_, options: { parentId: string; path: string }) => {
return models.caCertificate.create(options);
});

ipcMainOn('loginStateChange', async () => {
BrowserWindow.getAllWindows().forEach(w => {
w.webContents.send('loggedIn');
Expand Down
2 changes: 1 addition & 1 deletion packages/insomnia/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export function all() {
userSession,
] as const;
}

export type ModelTypes = "CaCertificate" | "Workspace" | "GitRepository" | "ApiSpec" | "Environment" | "CookieJar" | "Stats" | "Settings" | "Project" | "WorkspaceMeta" | "GitCredentials" | "RequestGroup" | "UserSession" | "RequestGroupMeta" | "Request" | "RequestVersion" | "RequestMeta" | "Response" | "MockServer" | "MockRoute" | "OAuth2Token" | "ClientCertificate" | "PluginData" | "UnitTestSuite" | "UnitTestResult" | "UnitTest" | "ProtoFile" | "ProtoDirectory" | "GrpcRequest" | "GrpcRequestMeta" | "RunnerTestResult" | "WebSocketPayload" | "WebSocketRequest" | "WebSocketResponse";
export function types() {
return all().map(model => model.type);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/insomnia/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ const main: Window['main'] = {
showNunjucksContextMenu: options => ipcRenderer.send('show-nunjucks-context-menu', options),
showContextMenu: options => ipcRenderer.send('showContextMenu', options),
database: {
getWhere: options => ipcRenderer.invoke('database.getWhere', options),
find: options => ipcRenderer.invoke('database.find', options),
caCertificate: {
create: options => ipcRenderer.invoke('database.caCertificate.create', options),
},
Expand Down
5 changes: 2 additions & 3 deletions packages/insomnia/src/ui/routes/organization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import { useLocalStorage } from 'react-use';

import * as session from '../../account/session';
import { getAppWebsiteBaseURL } from '../../common/constants';
import { database } from '../../common/database';
import { SentryMetrics } from '../../common/sentry';
import { userSession } from '../../models';
import { updateLocalProjectToRemote } from '../../models/helpers/project';
Expand Down Expand Up @@ -212,7 +211,7 @@ export const syncOrgsAndProjectsAction: ActionFunction = async ({ request }) =>

// When user switch to a new organization, there is no project in db cache, we need to redirect to the first project after sync project
if (!projectId && asyncTaskList.includes(AsyncTask.SyncProjects)) {
const firstProject = await database.getWhere<Project>(ProjectType, { parentId: organizationId });
const firstProject = await window.main.database.getWhere<Project>(ProjectType, { parentId: organizationId });
if (firstProject?._id) {
return redirect(`/organization/${organizationId}/project/${firstProject?._id}`);
}
Expand All @@ -235,7 +234,7 @@ async function migrateProjectsUnderOrganization(personalOrganizationId: string,

const preferredProjectType = localStorage.getItem('prefers-project-type');
if (preferredProjectType === 'remote') {
const localProjects = await database.find<Project>('Project', {
const localProjects = await window.main.database.find<Project>('Project', {
parentId: personalOrganizationId,
remoteId: null,
});
Expand Down
Loading