mirror of
https://github.com/Kong/insomnia.git
synced 2026-07-30 09:16:44 -04:00
refactor: migrate workspace-related database calls to services (#10200)
* refactor: migrate workspace-related database calls to services * optimize
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import * as crypto from 'node:crypto';
|
||||
|
||||
import type { Environment, Workspace } from 'insomnia-data';
|
||||
import type { Environment } from 'insomnia-data';
|
||||
import { database as db, models } from 'insomnia-data';
|
||||
|
||||
import * as projectService from './project';
|
||||
import * as workspaceService from './workspace';
|
||||
|
||||
const { type, prefix, vaultEnvironmentPath } = models.environment;
|
||||
const { EnvironmentKvPairDataType, EnvironmentType } = models.environment;
|
||||
@@ -12,7 +13,7 @@ const { EnvironmentKvPairDataType, EnvironmentType } = models.environment;
|
||||
export const removeAllSecrets = async (organizationIds: string[]) => {
|
||||
const allProjects = await projectService.listByOrganizationIds(organizationIds);
|
||||
const allProjectIds = allProjects.map(project => project._id);
|
||||
const allGlobalEnvironmentWorkspaces = await db.find<Workspace>(models.workspace.type, {
|
||||
const allGlobalEnvironmentWorkspaces = await workspaceService.list({
|
||||
parentId: { $in: allProjectIds },
|
||||
scope: models.workspace.WorkspaceScopeKeys.environment,
|
||||
});
|
||||
|
||||
@@ -36,7 +36,7 @@ export function getByParentId(parentId: string) {
|
||||
}
|
||||
|
||||
export async function findByProjectId(projectId: string) {
|
||||
const workspaces = await workspace.findByParentId(projectId);
|
||||
const workspaces = await workspace.listByParentId(projectId);
|
||||
return db.find<MockServer>(type, { parentId: { $in: workspaces.map(ws => ws._id) } });
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
import type { WorkspaceMeta } from 'insomnia-data';
|
||||
import type { Query, WorkspaceMeta } from 'insomnia-data';
|
||||
import { database as db, models } from 'insomnia-data';
|
||||
|
||||
const { type } = models.workspaceMeta;
|
||||
|
||||
export function list(query?: Query<WorkspaceMeta>, sort?: Record<string, any>, limit?: number) {
|
||||
return db.find<WorkspaceMeta>(type, query, sort, limit);
|
||||
}
|
||||
|
||||
export function create(patch: Partial<WorkspaceMeta> = {}) {
|
||||
if (!patch.parentId) {
|
||||
throw new Error(`New WorkspaceMeta missing parentId ${JSON.stringify(patch)}`);
|
||||
@@ -24,15 +28,7 @@ export async function getByParentId(parentId: string) {
|
||||
return db.findOne<WorkspaceMeta>(type, { parentId });
|
||||
}
|
||||
|
||||
export async function getByGitRepositoryId(gitRepositoryId: string) {
|
||||
return db.findOne<WorkspaceMeta>(type, { gitRepositoryId });
|
||||
}
|
||||
|
||||
export async function getOrCreateByParentId(parentId: string) {
|
||||
const doc = await getByParentId(parentId);
|
||||
return doc || create({ parentId });
|
||||
}
|
||||
|
||||
export function all() {
|
||||
return db.find<WorkspaceMeta>(type);
|
||||
}
|
||||
|
||||
@@ -1,40 +1,61 @@
|
||||
import type { Workspace } from 'insomnia-data';
|
||||
import type { Query, Workspace } from 'insomnia-data';
|
||||
import { database as db, models } from 'insomnia-data';
|
||||
|
||||
const { type } = models.workspace;
|
||||
|
||||
export function getById(id?: string) {
|
||||
return db.findOne<Workspace>(type, { _id: id });
|
||||
}
|
||||
const _getWorkspaceByIdOrWorkspace = async (idOrWorkspace: string | Workspace) => {
|
||||
const workspace = typeof idOrWorkspace === 'string' ? await getById(idOrWorkspace) : idOrWorkspace;
|
||||
if (!workspace) {
|
||||
throw new Error(
|
||||
`Workspace not found: ${typeof idOrWorkspace === 'string' ? idOrWorkspace : `_id=${idOrWorkspace._id}, name=${idOrWorkspace.name}`}`,
|
||||
);
|
||||
}
|
||||
return workspace;
|
||||
};
|
||||
|
||||
export function findByParentId(parentId: string) {
|
||||
return db.find<Workspace>(type, { parentId });
|
||||
}
|
||||
|
||||
export async function create(patch: Partial<Workspace> = {}) {
|
||||
expectParentToBeProject(patch.parentId);
|
||||
return db.docCreate<Workspace>(type, patch);
|
||||
}
|
||||
|
||||
export async function all() {
|
||||
return await db.find<Workspace>(type);
|
||||
}
|
||||
|
||||
export function count() {
|
||||
return db.count(type);
|
||||
}
|
||||
|
||||
export function update(workspace: Workspace, patch: Partial<Workspace>) {
|
||||
expectParentToBeProject(patch.parentId);
|
||||
return db.docUpdate(workspace, patch);
|
||||
}
|
||||
|
||||
export function remove(workspace: Workspace) {
|
||||
return db.remove(workspace);
|
||||
}
|
||||
|
||||
function expectParentToBeProject(parentId?: string | null) {
|
||||
function _expectParentToBeProject(parentId?: string | null) {
|
||||
if (parentId && !models.project.isProjectId(parentId)) {
|
||||
throw new Error('Expected the parent of a Workspace to be a Project');
|
||||
}
|
||||
}
|
||||
|
||||
export function list(query?: Query<Workspace>, sort?: Record<string, any>, limit?: number) {
|
||||
return db.find<Workspace>(type, query, sort, limit);
|
||||
}
|
||||
|
||||
export function get(query?: Query<Workspace>, sort?: Record<string, any>) {
|
||||
return db.findOne<Workspace>(type, query, sort);
|
||||
}
|
||||
|
||||
export function getById(id?: string) {
|
||||
return get({ _id: id });
|
||||
}
|
||||
|
||||
export function listByParentId(parentId: string) {
|
||||
return list({ parentId });
|
||||
}
|
||||
|
||||
export function count(query?: Query<Workspace>) {
|
||||
return db.count(type, query);
|
||||
}
|
||||
|
||||
export async function create(patch: Partial<Workspace> = {}) {
|
||||
_expectParentToBeProject(patch.parentId);
|
||||
return db.docCreate<Workspace>(type, patch);
|
||||
}
|
||||
|
||||
export async function update(idOrWorkspace: string | Workspace, patch: Partial<Workspace>) {
|
||||
const workspace = await _getWorkspaceByIdOrWorkspace(idOrWorkspace);
|
||||
_expectParentToBeProject(patch.parentId);
|
||||
return db.docUpdate(workspace, patch);
|
||||
}
|
||||
|
||||
export async function upsert(workspace: Workspace) {
|
||||
_expectParentToBeProject(workspace.parentId);
|
||||
return db.update(workspace as Workspace);
|
||||
}
|
||||
|
||||
export async function remove(idOrWorkspace: string | Workspace) {
|
||||
const workspace = await _getWorkspaceByIdOrWorkspace(idOrWorkspace);
|
||||
return db.remove(workspace);
|
||||
}
|
||||
|
||||
@@ -160,7 +160,7 @@ describe('importRaw()', () => {
|
||||
});
|
||||
|
||||
const workspacesCount = await services.workspace.count();
|
||||
const projectWorkspaces = await services.workspace.findByParentId(projectToImportTo._id);
|
||||
const projectWorkspaces = await services.workspace.listByParentId(projectToImportTo._id);
|
||||
const curlRequests = await services.request.findByParentId(projectWorkspaces[0]._id);
|
||||
|
||||
expect(workspacesCount).toBe(1);
|
||||
@@ -247,7 +247,7 @@ describe('importRaw()', () => {
|
||||
projectId: projectToImportTo._id,
|
||||
});
|
||||
|
||||
const projectWorkspaces = await services.workspace.findByParentId(projectToImportTo._id);
|
||||
const projectWorkspaces = await services.workspace.listByParentId(projectToImportTo._id);
|
||||
|
||||
const requestGroups = await services.requestGroup.findByParentId(projectWorkspaces[0]._id);
|
||||
const requests = await services.request.findByParentId(requestGroups[0]._id);
|
||||
@@ -325,7 +325,7 @@ describe('importRaw()', () => {
|
||||
projectId: projectToImportTo._id,
|
||||
});
|
||||
|
||||
const projectWorkspaces = await services.workspace.findByParentId(projectId);
|
||||
const projectWorkspaces = await services.workspace.listByParentId(projectId);
|
||||
const importedWorkspaceId = projectWorkspaces[0]._id;
|
||||
const requestBaseEnvironment = await services.environment.getByParentId(importedWorkspaceId);
|
||||
|
||||
|
||||
@@ -784,7 +784,7 @@ export async function findExistingImportedSpec(
|
||||
if (!incoming) continue;
|
||||
|
||||
for (const pid of projectIds) {
|
||||
const workspaces = await services.workspace.findByParentId(pid);
|
||||
const workspaces = await services.workspace.listByParentId(pid);
|
||||
const designWorkspaces = workspaces.filter(w => w.scope === 'design');
|
||||
|
||||
for (const ws of designWorkspaces) {
|
||||
|
||||
@@ -7,7 +7,6 @@ import {
|
||||
type Project,
|
||||
services,
|
||||
type Workspace,
|
||||
type WorkspaceMeta,
|
||||
type WorkspaceScope,
|
||||
} from 'insomnia-data';
|
||||
|
||||
@@ -89,8 +88,8 @@ const lockGenerator = () => {
|
||||
export const projectLock = lockGenerator();
|
||||
|
||||
export const checkSingleProjectSyncStatus = async (projectId: string) => {
|
||||
const projectWorkspaces = await services.workspace.findByParentId(projectId);
|
||||
const workspaceMetas = await database.find<WorkspaceMeta>(models.workspaceMeta.type, {
|
||||
const projectWorkspaces = await services.workspace.listByParentId(projectId);
|
||||
const workspaceMetas = await services.workspaceMeta.list({
|
||||
parentId: {
|
||||
$in: projectWorkspaces.map(w => w._id),
|
||||
},
|
||||
@@ -114,9 +113,9 @@ export const checkAllProjectSyncStatus = async (projects: Project[]) => {
|
||||
};
|
||||
|
||||
export async function getAllLocalFiles({ projectId }: { projectId: string }) {
|
||||
const projectWorkspaces = await services.workspace.findByParentId(projectId);
|
||||
const projectWorkspaces = await services.workspace.listByParentId(projectId);
|
||||
const [workspaceMetas, apiSpecs, mockServers] = await Promise.all([
|
||||
database.find<WorkspaceMeta>(models.workspaceMeta.type, {
|
||||
services.workspaceMeta.list({
|
||||
parentId: {
|
||||
$in: projectWorkspaces.map(w => w._id),
|
||||
},
|
||||
|
||||
@@ -527,7 +527,7 @@ async function syncServiceWorkspace(
|
||||
|
||||
/** Upserts the project-level environment workspace and syncs Konnect proxy URL vars into it. Returns the environment id. */
|
||||
async function upsertProjectEnvVars(controlPlane: KonnectControlPlane, project: Project): Promise<string> {
|
||||
const existingEnvWorkspaces = await db.find<Workspace>(models.workspace.type, {
|
||||
const existingEnvWorkspaces = await insoservices.workspace.list({
|
||||
parentId: project._id,
|
||||
scope: 'environment',
|
||||
});
|
||||
@@ -641,7 +641,7 @@ async function syncControlPlane(
|
||||
|
||||
// Load existing Konnect workspaces for this project once, keyed by service id
|
||||
const existingWorkspaces = (
|
||||
await db.find<Workspace>(models.workspace.type, {
|
||||
await insoservices.workspace.list({
|
||||
parentId: project._id,
|
||||
konnectServiceId: { $ne: null },
|
||||
})
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { RemoteProject, Workspace } from 'insomnia-data';
|
||||
import { database, models } from 'insomnia-data';
|
||||
import type { RemoteProject } from 'insomnia-data';
|
||||
import { database, models, services } from 'insomnia-data';
|
||||
|
||||
import type { VCS } from '~/main/cloud-sync/core/vcs';
|
||||
import { interceptAccessError } from '~/sync/access-error';
|
||||
@@ -28,7 +28,7 @@ export const pullBackendProject = async ({ vcs, backendProject, remoteProject }:
|
||||
// @TODO Revisit the UX for this. What should happen if there are other branches?
|
||||
// The default branch does not exist, so we create it and the workspace locally
|
||||
if (defaultBranchMissing) {
|
||||
const workspace = await database.update<Workspace>({
|
||||
const workspace = await services.workspace.upsert({
|
||||
...models.workspace.init(),
|
||||
_id: backendProject.rootDocumentId,
|
||||
name: backendProject.name,
|
||||
|
||||
@@ -247,7 +247,7 @@ function toPosixRelPath(relPath: string) {
|
||||
}
|
||||
|
||||
async function getProjectWorkspacesWithMeta(projectId: string) {
|
||||
const workspaces = await services.workspace.findByParentId(projectId);
|
||||
const workspaces = await services.workspace.listByParentId(projectId);
|
||||
const metas = await Promise.all(
|
||||
workspaces.map(async workspace => ({
|
||||
workspace,
|
||||
@@ -1160,7 +1160,7 @@ export const cloneGitRepoAction = async ({
|
||||
|
||||
if (insomniaFilesIds.length > 0) {
|
||||
// Check for existing workspaces with the same IDs (currently commented out)
|
||||
const existingWorkspaces = await database.find(models.workspace.type, {
|
||||
const existingWorkspaces = await services.workspace.list({
|
||||
_id: { $in: insomniaFilesIds },
|
||||
});
|
||||
|
||||
|
||||
@@ -14,12 +14,12 @@ type HarExport = Omit<InsomniaExport, 'format'>;
|
||||
|
||||
const getWorkspaces = (activeProjectId?: string) => {
|
||||
if (activeProjectId) {
|
||||
return services.workspace.findByParentId(activeProjectId);
|
||||
return services.workspace.listByParentId(activeProjectId);
|
||||
}
|
||||
// This code path was kept in case there was ever a time when the app wouldn't have an active project.
|
||||
// In over 5 months of monitoring in production, we never saw this happen.
|
||||
// Keeping it for defensive purposes, but it's not clear if it's necessary.
|
||||
return services.workspace.all();
|
||||
return services.workspace.list();
|
||||
};
|
||||
|
||||
// Only in the case of running unit tests from Inso can activeProjectId be undefined. This is because the concept of a project doesn't exist in git/insomnia sync or an export file
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { Organization } from 'insomnia-api';
|
||||
import type { Environment, GrpcRequest, Request, RequestGroup, WebSocketRequest, Workspace } from 'insomnia-data';
|
||||
import type { Environment, GrpcRequest, Request, RequestGroup, WebSocketRequest } from 'insomnia-data';
|
||||
import { database, models, services } from 'insomnia-data';
|
||||
|
||||
import { fuzzyMatch } from '~/common/misc';
|
||||
@@ -8,7 +8,7 @@ import { createFetcherLoadHook } from '~/ui/utils/router';
|
||||
|
||||
import type { Route } from './+types/commands';
|
||||
|
||||
const { environment, grpcRequest, project, request, requestGroup, workspace } = models;
|
||||
const { environment, grpcRequest, project, request, requestGroup } = models;
|
||||
|
||||
export async function clientLoader(args: Route.ClientLoaderArgs) {
|
||||
const searchParams = new URL(args.request.url).searchParams;
|
||||
@@ -42,7 +42,7 @@ export async function clientLoader(args: Route.ClientLoaderArgs) {
|
||||
|
||||
const allProjectIds = allProjects.map(project => project._id);
|
||||
|
||||
const allOrganizationWorkspaces = await database.find<Workspace>(workspace.type, {
|
||||
const allOrganizationWorkspaces = await services.workspace.list({
|
||||
parentId: { $in: allProjectIds },
|
||||
});
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { ApiSpec, GitRepository, MockServer, WorkspaceMeta } from 'insomnia-data';
|
||||
import type { ApiSpec, GitRepository, MockServer } from 'insomnia-data';
|
||||
import { models, services } from 'insomnia-data';
|
||||
import { href } from 'react-router';
|
||||
|
||||
@@ -14,9 +14,9 @@ import { createFetcherLoadHook } from '~/ui/utils/router';
|
||||
import type { Route } from './+types/organization.$organizationId.project.$projectId.list-workspaces';
|
||||
|
||||
async function getAllLocalFiles({ projectId }: { projectId: string }) {
|
||||
const projectWorkspaces = await services.workspace.findByParentId(projectId);
|
||||
const projectWorkspaces = await services.workspace.listByParentId(projectId);
|
||||
const [workspaceMetas, apiSpecs, mockServers] = await Promise.all([
|
||||
database.find<WorkspaceMeta>(models.workspaceMeta.type, {
|
||||
services.workspaceMeta.list({
|
||||
parentId: {
|
||||
$in: projectWorkspaces.map(w => w._id),
|
||||
},
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { createTeamProject, deleteTeamProject, isApiError, updateTeamProject } from 'insomnia-api';
|
||||
import type { WorkspaceMeta } from 'insomnia-data';
|
||||
import { models, services } from 'insomnia-data';
|
||||
import { href } from 'react-router';
|
||||
|
||||
@@ -271,9 +270,9 @@ export async function clientAction({ request, params }: Route.ClientActionArgs)
|
||||
selectedAuthorEmail,
|
||||
});
|
||||
|
||||
const projectWorkspaces = await services.workspace.findByParentId(project._id);
|
||||
const projectWorkspaces = await services.workspace.listByParentId(project._id);
|
||||
const bufferId = await database.bufferChanges();
|
||||
const workspaceMetas = await database.find<WorkspaceMeta>(models.workspaceMeta.type, {
|
||||
const workspaceMetas = await services.workspaceMeta.list({
|
||||
parentId: { $in: projectWorkspaces.map(w => w._id) },
|
||||
});
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import type { Workspace } from 'insomnia-data';
|
||||
import { database, models, services } from 'insomnia-data';
|
||||
import { services } from 'insomnia-data';
|
||||
import { href } from 'react-router';
|
||||
|
||||
import { invariant } from '~/common/utils/invariant';
|
||||
@@ -32,7 +31,7 @@ export async function clientLoader({ params }: Route.ClientLoaderArgs) {
|
||||
});
|
||||
|
||||
// Get all workspaces that are connected to backend projects and under the current project
|
||||
const workspacesWithBackendProjects = await database.find<Workspace>(models.workspace.type, {
|
||||
const workspacesWithBackendProjects = await services.workspace.list({
|
||||
_id: {
|
||||
$in: [...allPulledBackendProjectsForRemoteId, ...allFetchedRemoteBackendProjectsForRemoteId].map(
|
||||
p => p.rootDocumentId,
|
||||
|
||||
@@ -113,7 +113,7 @@ export async function clientLoader({ params, request }: Route.ClientLoaderArgs)
|
||||
(e1, e2) => e1.metaSortKey - e2.metaSortKey,
|
||||
);
|
||||
|
||||
const globalEnvironmentWorkspaces = await database.find<Workspace>(models.workspace.type, {
|
||||
const globalEnvironmentWorkspaces = await services.workspace.list({
|
||||
parentId: projectId,
|
||||
scope: 'environment',
|
||||
});
|
||||
@@ -293,7 +293,7 @@ export async function clientLoader({ params, request }: Route.ClientLoaderArgs)
|
||||
}
|
||||
}
|
||||
|
||||
const workspaces = await services.workspace.findByParentId(projectId);
|
||||
const workspaces = await services.workspace.listByParentId(projectId);
|
||||
|
||||
const collection = flattenTree();
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { Organization } from 'insomnia-api';
|
||||
import type { Project, Workspace } from 'insomnia-data';
|
||||
import { database, models, services } from 'insomnia-data';
|
||||
import { models, services } from 'insomnia-data';
|
||||
|
||||
import { createFetcherLoadHook } from '~/ui/utils/router';
|
||||
|
||||
@@ -22,7 +22,7 @@ export async function clientLoader(_args: Route.ClientLoaderArgs) {
|
||||
const untrackedProjects = [];
|
||||
|
||||
for (const project of projects) {
|
||||
const workspacesCount = await database.count('Workspace', {
|
||||
const workspacesCount = await services.workspace.count({
|
||||
parentId: project._id,
|
||||
});
|
||||
|
||||
@@ -32,7 +32,7 @@ export async function clientLoader(_args: Route.ClientLoaderArgs) {
|
||||
});
|
||||
}
|
||||
|
||||
const untrackedWorkspaces = await database.find<Workspace>('Workspace', {
|
||||
const untrackedWorkspaces = await services.workspace.list({
|
||||
parentId: null,
|
||||
});
|
||||
|
||||
|
||||
@@ -28,8 +28,8 @@ import path from 'node:path';
|
||||
|
||||
export type MigrationLogger = (level: 'info' | 'warn' | 'error', message: string) => void;
|
||||
|
||||
import type { GitRepository, Workspace, WorkspaceMeta } from 'insomnia-data';
|
||||
import { database as db, models } from 'insomnia-data';
|
||||
import type { GitRepository, WorkspaceMeta } from 'insomnia-data';
|
||||
import { database as db, models, services } from 'insomnia-data';
|
||||
|
||||
import { getInsomniaV5DataExport } from '../../common/insomnia-v5';
|
||||
import { CURRENT_MIGRATION_VERSION } from './git-migration-version';
|
||||
@@ -289,11 +289,11 @@ export async function migrateRepoStructureIfNeeded(
|
||||
* All workspaces are processed in parallel.
|
||||
*/
|
||||
async function flushWorkspacesToDisk(baseDir: string, projectId: string, logger?: MigrationLogger): Promise<void> {
|
||||
const workspaces = await db.find<Workspace>(models.workspace.type, { parentId: projectId });
|
||||
const workspaces = await services.workspace.listByParentId(projectId);
|
||||
|
||||
// Batch-fetch all workspace metadata to avoid N+1 queries.
|
||||
const workspaceIds = workspaces.map(w => w._id);
|
||||
const allWorkspaceMeta = await db.find<WorkspaceMeta>(models.workspaceMeta.type, {
|
||||
const allWorkspaceMeta = await services.workspaceMeta.list({
|
||||
parentId: { $in: workspaceIds },
|
||||
});
|
||||
const metaByWorkspaceId = Object.fromEntries(allWorkspaceMeta.map(m => [m.parentId, m]));
|
||||
@@ -356,17 +356,15 @@ async function flushWorkspacesToDisk(baseDir: string, projectId: string, logger?
|
||||
// the file but crashed before updating the DB.
|
||||
try {
|
||||
if (workspaceMeta && !workspaceMeta.gitFilePath) {
|
||||
await db.docUpdate<WorkspaceMeta>(workspaceMeta, { gitFilePath });
|
||||
await services.workspaceMeta.update(workspaceMeta, { gitFilePath });
|
||||
} else if (!workspaceMeta) {
|
||||
let meta = await db.findOne<WorkspaceMeta>(models.workspaceMeta.type, {
|
||||
parentId: workspace._id,
|
||||
});
|
||||
let meta = await services.workspaceMeta.getByParentId(workspace._id);
|
||||
if (!meta) {
|
||||
meta = await db.docCreate<WorkspaceMeta>(models.workspaceMeta.type, {
|
||||
meta = await services.workspaceMeta.create({
|
||||
parentId: workspace._id,
|
||||
});
|
||||
}
|
||||
await db.docUpdate<WorkspaceMeta>(meta, { gitFilePath });
|
||||
await services.workspaceMeta.update(meta, { gitFilePath });
|
||||
}
|
||||
} catch (err) {
|
||||
const metaMsg = err instanceof Error ? err.message : String(err);
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
import path from 'node:path';
|
||||
|
||||
import type { BaseModel } from 'insomnia-data';
|
||||
import { models } from 'insomnia-data';
|
||||
import { models, services } from 'insomnia-data';
|
||||
import type { PromiseFsClient } from 'isomorphic-git';
|
||||
import YAML from 'yaml';
|
||||
|
||||
@@ -209,7 +209,7 @@ export class NeDBClient {
|
||||
models.socketIORequest.type,
|
||||
];
|
||||
} else if (type !== null && id === null) {
|
||||
const workspace = await db.findOne(models.workspace.type, { _id: this._workspaceId });
|
||||
const workspace = await services.workspace.getById(this._workspaceId);
|
||||
const children = workspace ? await db.getWithDescendants(workspace, [type]) : [];
|
||||
docs = children.filter(d => d.type === type && !d.isPrivate);
|
||||
} else {
|
||||
|
||||
@@ -269,7 +269,7 @@ class RepoFileWatcher {
|
||||
* `importAllFiles` skips them (they are already up-to-date).
|
||||
*/
|
||||
private async flushNewerDbWorkspacesToDisk(): Promise<void> {
|
||||
const workspaces = await services.workspace.findByParentId(this.projectId);
|
||||
const workspaces = await services.workspace.listByParentId(this.projectId);
|
||||
|
||||
await Promise.all(
|
||||
workspaces.map(async workspace => {
|
||||
@@ -952,13 +952,11 @@ class RepoFileWatcher {
|
||||
filterIds?: Set<string>,
|
||||
): Promise<{ workspace: Workspace; meta: WorkspaceMeta | undefined }[]> {
|
||||
const query = filterIds ? { parentId: this.projectId, _id: { $in: [...filterIds] } } : { parentId: this.projectId };
|
||||
const workspaces = await db.find<Workspace>(models.workspace.type, query as Parameters<typeof db.find>[1]);
|
||||
const workspaces = await services.workspace.list(query);
|
||||
if (workspaces.length === 0) {
|
||||
return [];
|
||||
}
|
||||
const metas = await db.find<WorkspaceMeta>(models.workspaceMeta.type, {
|
||||
parentId: { $in: workspaces.map(w => w._id) },
|
||||
} as Parameters<typeof db.find>[1]);
|
||||
const metas = await services.workspaceMeta.list({ parentId: { $in: workspaces.map(w => w._id) } });
|
||||
const metaByParent = new Map(metas.map(m => [m.parentId, m]));
|
||||
return workspaces.map(workspace => ({ workspace, meta: metaByParent.get(workspace._id) }));
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import { getEncryptionKeys, getUserProfile, logout as logoutAPI } from 'insomnia-api';
|
||||
import type { GitRepository, WorkspaceMeta } from 'insomnia-data';
|
||||
import type { GitRepository } from 'insomnia-data';
|
||||
import { models, services } from 'insomnia-data';
|
||||
|
||||
import { getCurrentSessionId, type SessionData, setSessionData, unsetSessionData } from '~/common/account/session';
|
||||
import { AI_PLUGIN_NAME, LLM_BACKENDS } from '~/common/constants';
|
||||
import { database } from '~/common/database';
|
||||
import { getRuntime } from '~/runtimes';
|
||||
|
||||
// Re-export the isomorphic session core so renderer callers have a single
|
||||
@@ -137,7 +136,7 @@ async function _removeGitRepository(repo: GitRepository) {
|
||||
await services.project.update(p, { gitRepositoryId: models.project.EMPTY_GIT_PROJECT_ID });
|
||||
}
|
||||
|
||||
const workspaceMetas = await database.find<WorkspaceMeta>(models.workspaceMeta.type, { gitRepositoryId: repo._id });
|
||||
const workspaceMetas = await services.workspaceMeta.list({ gitRepositoryId: repo._id });
|
||||
for (const wsMeta of workspaceMetas) {
|
||||
await services.workspaceMeta.update(wsMeta, { gitRepositoryId: null });
|
||||
}
|
||||
|
||||
@@ -133,7 +133,7 @@ export const ProjectDropdown: FC<Props> = ({
|
||||
source: 'project',
|
||||
},
|
||||
});
|
||||
const workspacesForProject = await services.workspace.findByParentId(projectId);
|
||||
const workspacesForProject = await services.workspace.listByParentId(projectId);
|
||||
exportProjectToFile(projectName, workspacesForProject);
|
||||
},
|
||||
},
|
||||
|
||||
@@ -44,7 +44,7 @@ export const AddRequestToCollectionModal: FC<AddRequestModalProps> = ({ onHide }
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
const workspaces = await services.workspace.findByParentId(selectedProjectId);
|
||||
const workspaces = await services.workspace.listByParentId(selectedProjectId);
|
||||
const requestCollections = workspaces.filter(workspace => workspace.scope === 'collection');
|
||||
setWorkspaceOptions(requestCollections);
|
||||
setSelectedWorkspaceId(requestCollections[0]?._id || '');
|
||||
|
||||
@@ -415,7 +415,7 @@ export async function exportWorkspaceData({
|
||||
}
|
||||
|
||||
export async function exportAllData({ dirPath }: { dirPath: string }): Promise<void> {
|
||||
const workspaces = await database.find<Workspace>(models.workspace.type);
|
||||
const workspaces = await services.workspace.list();
|
||||
|
||||
const baseEnvironments = await database.find<Environment>(models.environment.type, {
|
||||
parentId: { $in: workspaces.map(w => w._id) },
|
||||
|
||||
@@ -10,10 +10,9 @@ import type {
|
||||
WebSocketRequest,
|
||||
WebSocketRequestMeta,
|
||||
Workspace,
|
||||
WorkspaceMeta,
|
||||
} from 'insomnia-data';
|
||||
import type { BaseModel } from 'insomnia-data';
|
||||
import { models } from 'insomnia-data';
|
||||
import { models, services } from 'insomnia-data';
|
||||
|
||||
import { database } from '~/common/database';
|
||||
import { fuzzyMatchAll } from '~/common/misc';
|
||||
@@ -57,10 +56,10 @@ export type WorkspaceWithSyncStatus = Workspace & {
|
||||
};
|
||||
|
||||
export async function getWorkspacesByProjectIds(projectIds: string[]) {
|
||||
const workspaces = await database.find<Workspace>(models.workspace.type, {
|
||||
const workspaces = await services.workspace.list({
|
||||
parentId: { $in: projectIds },
|
||||
});
|
||||
const workspaceMetas = await database.find<WorkspaceMeta>(models.workspaceMeta.type, {
|
||||
const workspaceMetas = await services.workspaceMeta.list({
|
||||
parentId: { $in: workspaces.map(w => w._id) },
|
||||
});
|
||||
const metaByWorkspaceId = new Map(workspaceMetas.map(meta => [meta.parentId, meta]));
|
||||
|
||||
@@ -394,7 +394,7 @@ const ProjectNavigationSidebarInner = (
|
||||
);
|
||||
const firstKonnectProject = sortedKonnectProjects[0];
|
||||
if (firstKonnectProject) {
|
||||
const workspaces = await services.workspace.findByParentId(firstKonnectProject._id);
|
||||
const workspaces = await services.workspace.listByParentId(firstKonnectProject._id);
|
||||
const envWorkspace = workspaces.find(w => w.scope === 'environment');
|
||||
if (envWorkspace) {
|
||||
// Show environment onboarding after first successful sync
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { createTeamProject, fetchTeamProjects, getCurrentPlan, getUserProfile, isApiError } from 'insomnia-api';
|
||||
import type { Project, Workspace } from 'insomnia-data';
|
||||
import { database, models, services } from 'insomnia-data';
|
||||
import type { Project } from 'insomnia-data';
|
||||
import { models, services } from 'insomnia-data';
|
||||
|
||||
import { projectLock } from '~/common/project';
|
||||
import { invariant } from '~/common/utils/invariant';
|
||||
@@ -70,9 +70,7 @@ export async function updateLocalProjectToRemote({
|
||||
});
|
||||
|
||||
// For each workspace in the local project
|
||||
const projectWorkspaces = await database.find<Workspace>('Workspace', {
|
||||
parentId: updatedProject._id,
|
||||
});
|
||||
const projectWorkspaces = await services.workspace.listByParentId(updatedProject._id);
|
||||
|
||||
for (const workspace of projectWorkspaces) {
|
||||
const workspaceMeta = await services.workspaceMeta.getOrCreateByParentId(workspace._id);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { database, models, services, type Workspace } from 'insomnia-data';
|
||||
import { services } from 'insomnia-data';
|
||||
|
||||
import { type InsomniaFile } from '~/common/project';
|
||||
|
||||
@@ -30,7 +30,7 @@ export async function getAllRemoteFiles({ projectId, organizationId }: { project
|
||||
`[getAllRemoteFiles] found allPulledBackendProjectsForRemoteId: ${allPulledBackendProjectsForRemoteId.length} and allFetchedRemoteBackendProjectsForRemoteId: ${allFetchedRemoteBackendProjectsForRemoteId.length} for remoteId: ${remoteId}`,
|
||||
);
|
||||
// Get all workspaces that are connected to backend projects and under the current project
|
||||
const workspacesWithBackendProjects = await database.find<Workspace>(models.workspace.type, {
|
||||
const workspacesWithBackendProjects = await services.workspace.list({
|
||||
_id: {
|
||||
$in: [...allPulledBackendProjectsForRemoteId, ...allFetchedRemoteBackendProjectsForRemoteId].map(
|
||||
p => p.rootDocumentId,
|
||||
|
||||
Reference in New Issue
Block a user