UNPKG

@paroicms/server

Version:
127 lines 4.12 kB
import { ApiError } from "@paroicms/public-server-lib"; import { type } from "arktype"; const DocumentRowAT = type({ nodeId: "number", language: "string", title: "string|null", slug: "string|null", metaDescription: "string|null", metaKeywords: "string|null", "+": "reject", }).pipe((data) => ({ nodeId: String(data.nodeId), language: data.language, title: data.title ?? undefined, slug: data.slug ?? undefined, metaDescription: data.metaDescription ?? undefined, metaKeywords: data.metaKeywords ?? undefined, })); export async function document({ cn }, documentId) { const document = await cn("PaDocument as d") .select("d.nodeId", "d.language", "d.title", "d.slug", "d.metaDescription", "d.metaKeywords") .where({ "d.nodeId": documentId.nodeId, "d.language": documentId.language, }) .first(); if (!document) return; return DocumentRowAT.assert(document); } export async function getOneDocument(siteContext, id) { const doc = await document(siteContext, id); if (!doc) { throw new ApiError(`can't find document '${id.language}:${id.nodeId}'`, 404); } return doc; } export async function findOneDocument(siteContext, id) { const doc = await document(siteContext, id); return doc ? doc : undefined; } const TermDocumentRowAT = type({ language: "string", title: "string|null", "+": "reject", }).pipe((data) => ({ language: data.language, title: data.title ?? undefined, })); export async function findOneTermDocument({ cn }, { language, nodeId }) { const rows = await cn("PaNode as l") .select("d.title", "d.language") .leftJoin("PaDocument as d", "d.nodeId", "l.id") .where("l.id", nodeId); const candidates = rows.map((row) => TermDocumentRowAT.assert(row)); let found = candidates.find((row) => row.language === language); if (!found) { found = candidates[0]; } return found; } const ParentLNodeRowAT = type({ parentId: "number", "+": "reject", }).pipe((r) => ({ parentId: String(r.parentId), })); export async function getParentLNodeIdOf(siteContext, { language, nodeId }) { const row = await siteContext .cn("PaNode as l") .select("l2.id as parentId") .innerJoin("PaLNode as s", "s.nodeId", "l.id") .innerJoin("PaNode as l2", "l2.id", "l.parentId") .innerJoin("PaLNode as s2", "s2.nodeId", "l2.id") .where("l.id", nodeId) .andWhere("s.language", language) .andWhere("s2.language", language) .first(); if (!row) return undefined; return { language, nodeId: ParentLNodeRowAT.assert(row).parentId, }; } const ParentNodeRowAT = type({ id: "number|null", "+": "reject", }).pipe((r) => ({ id: r.id === null ? undefined : String(r.id), })); export async function getParentNodeIdOf(siteContext, nodeId) { const row = await siteContext .cn("PaNode as l") .select("l.parentId as id") .where("l.id", nodeId) .first(); if (!row) throw new Error(`cannot find parent node of '${nodeId}'`); return ParentNodeRowAT.assert(row).id ?? undefined; } const ParentDocumentRowAT = type({ language: "string", parentId: "number", "+": "reject", }).pipe((r) => ({ language: r.language, parentId: String(r.parentId), })); export async function getParentDocumentIdsOf(siteContext, options) { const languages = options.languages ?? siteContext.siteSchema.languages; const rows = await siteContext .cn("PaNode as l") .select("l.parentId", "d.language") .innerJoin("PaDocument as d", "d.nodeId", "l.id") .where("l.id", options.nodeId) .whereIn("d.language", languages) .whereNotNull("l.parentId"); return rows.map((row) => { const validated = ParentDocumentRowAT.assert(row); return { language: validated.language, nodeId: validated.parentId, }; }); } //# sourceMappingURL=load-documents.queries.js.map