UNPKG

@paroicms/server

Version:
138 lines 4.57 kB
import { parseSqliteDateTime } from "@paroicms/internal-server-lib"; import { ApiError } from "@paroicms/public-server-lib"; import { type } from "arktype"; const DocumentRowAT = type({ nodeId: "number", language: "string", ready: "number", updatedAt: "string|number|Date", title: "string|null", slug: "string|null", metaDescription: "string|null", metaKeywords: "string|null", overrideLanguage: "string|null", "+": "reject", }).pipe((data) => ({ nodeId: String(data.nodeId), language: data.language, ready: Boolean(data.ready), updatedAt: parseSqliteDateTime(data.updatedAt), title: data.title ?? undefined, slug: data.slug ?? undefined, metaDescription: data.metaDescription ?? undefined, metaKeywords: data.metaKeywords ?? undefined, overrideLanguage: data.overrideLanguage ?? undefined, })); export async function document({ cn }, documentId) { const document = await cn("PaDocument as d") .innerJoin("PaLNode as l", { "l.nodeId": "d.nodeId", "l.language": "d.language", }) .select("d.nodeId", "d.language", "l.ready", "l.updatedAt", "d.title", "d.slug", "d.metaDescription", "d.metaKeywords", "d.overrideLanguage") .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 n") .select("d.title", "d.language") .leftJoin("PaDocument as d", "d.nodeId", "n.id") .where("n.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 n") .select("n2.id as parentId") .innerJoin("PaLNode as l", "l.nodeId", "n.id") .innerJoin("PaNode as n2", "n2.id", "n.parentId") .innerJoin("PaLNode as l2", "l2.nodeId", "n2.id") .where("n.id", nodeId) .andWhere("l.language", language) .andWhere("l2.language", language) .first(); if (!row) return; 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 n") .select("n.parentId as id") .where("n.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 n") .select("n.parentId", "d.language") .innerJoin("PaDocument as d", "d.nodeId", "n.id") .where("n.id", options.nodeId) .whereIn("d.language", languages) .whereNotNull("n.parentId"); return rows.map((row) => { const validated = ParentDocumentRowAT.assert(row); return { language: validated.language, nodeId: validated.parentId, }; }); } //# sourceMappingURL=load-documents.queries.js.map