/
ncit
/
c4panelmodel
Обзор
Документация
Войти
/
ncit
/
c4panelmodel
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
src/lib/server/services/implied.ts
112 строк
3 KB
ncit
feat: implement full IcePanel feature clone (Phases 1-8)
09 июн 2026, 11:13
09 июн 2026, 11:13
858c15a
Код
Авторство
О чём код?
import { db } from '../db'; /** * Implied connections: when a connection exists at a lower C4 level (e.g. component), * we can automatically suggest/create the corresponding connection at the parent level * (e.g. container or system). This is a key IcePanel pattern. */ export interface ImpliedCandidate { sourceParentId: string; sourceParentName: string; targetParentId: string; targetParentName: string; viaConnectionId: string; viaLabel: string | null; exists: boolean; } /** * Find connections at lower levels that could be "promoted" to implied connections * at the given parent object level. */ export async function findImpliedCandidates(projectId: string, parentObjectId: string): Promise<ImpliedCandidate[]> { // Get all descendants of the parent object. const descendants = await db.modelObject.findMany({ where: { projectId, parentId: parentObjectId }, select: { id: true, name: true } }); const descIds = new Set(descendants.map((d) => d.id)); // Find connections between descendants. const connections = await db.connection.findMany({ where: { projectId, sourceId: { in: [...descIds] }, targetId: { in: [...descIds] } }, include: { source: { select: { id: true, name: true, parentId: true } }, target: { select: { id: true, name: true, parentId: true } } } }); const candidates: ImpliedCandidate[] = []; const seen = new Set<string>(); for (const conn of connections) { const srcParent = conn.source.parentId; const tgtParent = conn.target.parentId; if (!srcParent || !tgtParent || srcParent === tgtParent) continue; const key = `${srcParent}->${tgtParent}`; if (seen.has(key)) continue; seen.add(key); // Check if connection already exists at parent level. const existing = await db.connection.findFirst({ where: { projectId, sourceId: srcParent, targetId: tgtParent } }); const srcParentObj = await db.modelObject.findUnique({ where: { id: srcParent }, select: { name: true } }); const tgtParentObj = await db.modelObject.findUnique({ where: { id: tgtParent }, select: { name: true } }); candidates.push({ sourceParentId: srcParent, sourceParentName: srcParentObj?.name ?? srcParent, targetParentId: tgtParent, targetParentName: tgtParentObj?.name ?? tgtParent, viaConnectionId: conn.id, viaLabel: conn.label, exists: !!existing }); } return candidates; } /** * Create an implied connection at the parent level. * The connection references the lower-level connection via viaObjectId. */ export async function createImpliedConnection(projectId: string, sourceParentId: string, targetParentId: string, viaConnectionId: string, label?: string) { // Check if it already exists. const existing = await db.connection.findFirst({ where: { projectId, sourceId: sourceParentId, targetId: targetParentId } }); if (existing) return existing; const conn = await db.connection.create({ data: { projectId, sourceId: sourceParentId, targetId: targetParentId, label: label ?? 'implied', viaObjectId: null, metadata: { implied: true, viaConnectionId } } }); return conn; } /** * Add implied edge to a specific diagram. */ export async function addImpliedEdgeToDiagram(diagramId: string, connectionId: string) { return db.diagramEdge.upsert({ where: { diagramId_connectionId: { diagramId, connectionId } }, update: {}, create: { diagramId, connectionId, isImplied: true } }); }