@paroicms/server
Version:
The ParoiCMS server
108 lines • 4.45 kB
JavaScript
import { getNodeTypeByName, PART_DEFAULT_LIST_NAME } from "@paroicms/internal-anywhere-lib";
import { encodeLNodeId, parseLNodeId, } from "@paroicms/public-anywhere-lib";
import { ApiError } from "@paroicms/public-server-lib";
import { type } from "arktype";
import { createNoRenderingContext } from "../../liquidjs-tools/liquidjs-rendering/rendering-context.js";
import { executeRenderingHook } from "../../plugin-services/rendering-hook.js";
import { getTypeNameOf } from "../node/node.queries.js";
import { getFieldTypesByNames, loadFieldValues } from "./load-fields.queries.js";
export async function getFieldValuesOfLNode(siteContext, options) {
const { lNodeId, fieldNames, preprocess, ignoreUnknownFieldNames, publishedOnly } = options;
const { nodeId, language } = parseLNodeId(lNodeId);
const typeName = await getTypeNameOf(siteContext, nodeId);
const fieldTypes = getFieldTypesByNames(siteContext, {
typeName,
fieldNames,
ignoreUnknownFieldNames,
});
const values = await loadFieldValues(siteContext, {
nodeId,
language,
fieldTypes,
publishedOnly,
});
if (preprocess) {
const renderingContext = createNoRenderingContext(siteContext);
const preprocessed = await executeRenderingHook(renderingContext, "fieldPreprocessor", {
fieldTypes,
values,
options: {
absoluteUrls: true,
},
});
await renderingContext.close();
return preprocessed;
}
return values;
}
export async function getFieldValuesOfOneList(siteContext, options) {
const { parentLNodeId, listName, fieldNames, preprocess, publishedOnly } = options;
const { nodeId: parentNodeId } = parseLNodeId(parentLNodeId);
const parentTypeName = await getTypeNameOf(siteContext, parentNodeId);
const parentNodeType = getNodeTypeByName(siteContext.siteSchema, parentTypeName);
if (parentNodeType.kind === "document") {
if (!listName)
throw new ApiError("listName is required for document parts", 400);
const listType = parentNodeType.lists?.find((item) => item.listName === listName);
if (!listType) {
throw new ApiError(`invalid listName '${listName}' for document type '${parentTypeName}'`, 400);
}
return await getFieldValuesOfList(siteContext, {
parentLNodeId,
listType,
fieldNames,
preprocess,
publishedOnly,
});
}
if (parentNodeType.kind === "part") {
if (listName && listName !== PART_DEFAULT_LIST_NAME) {
throw new ApiError(`invalid listName '${listName}' for part type (can be omitted)`, 400);
}
const listType = parentNodeType.list;
if (!listType) {
throw new ApiError(`missing sub-list for part type '${parentTypeName}'`, 400);
}
return await getFieldValuesOfList(siteContext, {
parentLNodeId,
listType,
fieldNames,
preprocess,
publishedOnly,
});
}
throw new ApiError(`invalid node kind '${parentNodeType.kind}'`, 400);
}
const FieldValuesOfListRowAT = type({
nodeId: "number",
"+": "reject",
}).pipe((r) => ({
nodeId: String(r.nodeId),
}));
async function getFieldValuesOfList(siteContext, options) {
const { parentLNodeId, listType, fieldNames, preprocess, publishedOnly } = options;
const { nodeId: parentNodeId, language } = parseLNodeId(parentLNodeId);
const rows = await siteContext
.cn("PaNode as l")
.select("l.id as nodeId")
.innerJoin("PaPartNode as p", "p.nodeId", "l.id")
.innerJoin("PaLNode as s", "s.nodeId", "l.id")
.where("l.parentId", parentNodeId)
.where("p.listName", listType.listName)
.where("s.language", language);
const partNodeIds = rows.map((row) => FieldValuesOfListRowAT.assert(row).nodeId);
const result = new Map();
for (const nodeId of partNodeIds) {
const lNodeId = encodeLNodeId({ nodeId, language });
const fieldValues = await getFieldValuesOfLNode(siteContext, {
lNodeId,
fieldNames,
preprocess,
ignoreUnknownFieldNames: true,
publishedOnly,
});
result.set(lNodeId, fieldValues);
}
return result;
}
//# sourceMappingURL=load-fields-of.queries.js.map