UNPKG

@npio/internals

Version:

A free visual website editor, powered with your own SolidJS components.

109 lines (97 loc) 1.96 kB
import { useDatabase } from "../prisma"; export const deleteElements = async ( ids: string[], { history: history = true, historyDeep, }: { history?: boolean; historyDeep?: boolean; } = {}, ) => { const db = useDatabase(); historyDeep = historyDeep ?? history; if (!ids.length) return; const elements = await db.nitroElement.findMany({ select: { id: true, slots: { select: { id: true, elements: { select: { id: true, }, }, }, }, history: { select: { id: true, revisions: { select: { id: true, }, where: { id: { notIn: ids, }, }, }, }, }, }, where: { id: { in: ids, }, }, }); if (!elements.length) return; const historyIds: string[] = []; const revisionIds: string[] = []; const childrenIds: string[] = []; const slotIds: string[] = []; for (const element of elements) { if (history) { historyIds.push(element.history.id); for (const rev of element.history.revisions) { revisionIds.push(rev.id); } } for (const slot of element.slots) { slotIds.push(slot.id); for (const el of slot.elements) { childrenIds.push(el.id); } } } await deleteElements(childrenIds, { history: historyDeep, }); await db.nitroElementSlot.deleteMany({ where: { id: { in: slotIds, }, }, }); await db.nitroElement.deleteMany({ where: { id: { in: ids }, }, }); if (history) { await deleteElements(revisionIds, { history: false, historyDeep: true, }); await db.nitroElementHistory.deleteMany({ where: { id: { in: historyIds, }, }, }); } };