Files
Kent Wang 6c9f1b60fc feat: Add app data service and enhance the sidebar cache logic (#10333)
* add package

remove workspaceCount from rootLoaderData

move unrelevant loaders out

add query context

adapt tanstack query

fix som issues

add new ways to load data

initial changes

clean code

remove and fix code

add a method to get organization data in main

refine the way to get data from tanstack

fix issue

fix issues

fix duplicate calling issue

fix unsubscribe issue

fix merge issue

fix listen issue

fix find organization issue for workspace

support workspatemeta in tanstack query

update types

move logic to insomnia-data services

remove things

change to use service

fix the loader bug

remove a file

fix type issues

refine code for workspace-children

change style import

fix main type issue

fix type issue

fix data fetching

fix delete project issue

cache the sidebar data in main

add unit tests

update cache and cache service

fix issues from comment

revert organization change

fix issues from comment

fix test failures

fix test failures

add comments and refine way to get workspace children

fix dedup and import issue

remove useless code

add duplicate id check

fix type failures

fix mock server issue

resolve conflict

1.remove main process cache

add renderer level app data

fix issues

* fix issues from review
2026-08-07 08:49:24 +08:00

46 lines
1.7 KiB
TypeScript

import type { GitRepository, OrganizationData } from 'insomnia-data';
import { database, models } from 'insomnia-data';
import * as projectService from '../project';
import * as workspaceService from '../workspace';
import * as workspaceMetaService from '../workspace-meta';
const isNotNullOrUndefined = <ValueType>(value: ValueType | null | undefined): value is ValueType => {
if (value === null || value === undefined) {
return false;
}
return true;
};
/**
* Get organization data by ID.
*/
export async function getOrganizationData(organizationId: string): Promise<OrganizationData> {
const projects = await projectService.listByOrganizationIds(organizationId);
const projectIds = projects.map(p => p._id);
const gitRepositoryIds = projects
.map(p => (models.project.isConnectedGitProject(p) ? models.project.getEffectiveRepoId(p) : null))
.filter(isNotNullOrUndefined);
const [gitRepositories, workspaces] = await Promise.all([
database.find<GitRepository>(models.gitRepository.type, {
_id: { $in: gitRepositoryIds },
}),
workspaceService.list({ parentId: { $in: projectIds } }),
]);
const workspaceMetas = await workspaceMetaService.list({ parentId: { $in: workspaces.map(w => w._id) } });
const projectsWithGitRepos = models.project.sortProjects(projects).map(project => {
const effectiveId = models.project.isConnectedGitProject(project)
? models.project.getEffectiveRepoId(project)
: null;
return {
...project,
gitRepository: gitRepositories.find((gr): gr is GitRepository => gr != null && gr._id === effectiveId),
};
});
return { projects: projectsWithGitRepos, workspaces, workspaceMetas };
}