/
ncit
/
c4panelmodel
Обзор
Документация
Войти
/
ncit
/
c4panelmodel
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
src/lib/server/services/model.ts
100 строк
3 KB
ncit
feat: rewrite hooks.server.ts to use Supabase Auth
07 июн 2026, 19:32
07 июн 2026, 19:32
b6162ab
Код
Авторство
О чём код?
import { db } from '../db'; import type { ObjectType, ObjectStatus } from '$lib/constants/c4'; export interface CreateObjectInput { projectId: string; type: ObjectType; name: string; parentId?: string | null; displayDescription?: string | null; isExternal?: boolean; } /** Ensure a unique name within (projectId, parentId) scope by appending a counter. */ async function uniqueName(projectId: string, parentId: string | null, base: string) { let name = base; let n = 2; while ( await db.modelObject.findFirst({ where: { projectId, parentId: parentId ?? null, name } }) ) { name = `${base} ${n++}`; } return name; } export async function createObject(input: CreateObjectInput) { const name = await uniqueName(input.projectId, input.parentId ?? null, input.name); return db.modelObject.create({ data: { projectId: input.projectId, type: input.type, name, parentId: input.parentId ?? null, displayDescription: input.displayDescription ?? null, isExternal: input.isExternal ?? false } }); } export async function updateObject( id: string, data: Partial<{ name: string; displayDescription: string | null; detailedDescription: string | null; status: ObjectStatus; isExternal: boolean; ownerTeamId: string | null; ownerUserId: string | null; domainId: string | null; metadata: unknown; }> ) { return db.modelObject.update({ where: { id }, data: data as any }); } export async function deleteObject(id: string) { // Connections cascade via FK; children cascade too. return db.modelObject.delete({ where: { id } }); } export async function getProjectModel(projectId: string) { const [objects, connections] = await Promise.all([ db.modelObject.findMany({ where: { projectId }, include: { technologies: { include: { technology: true } }, tags: { include: { tag: { include: { group: true } } } }, links: true }, orderBy: { createdAt: 'asc' } }), db.connection.findMany({ where: { projectId }, orderBy: { createdAt: 'asc' } }) ]); return { objects, connections }; } export interface CreateConnectionInput { projectId: string; sourceId: string; targetId: string; label?: string | null; technology?: string | null; } export async function createConnection(input: CreateConnectionInput) { // Prevent duplicates (same source/target). const existing = await db.connection.findFirst({ where: { projectId: input.projectId, sourceId: input.sourceId, targetId: input.targetId } }); if (existing) return existing; return db.connection.create({ data: { projectId: input.projectId, sourceId: input.sourceId, targetId: input.targetId, label: input.label ?? null, technology: input.technology ?? null } }); }