@paroicms/server
Version:
The ParoiCMS server
94 lines • 3.86 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 { 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,
});
return await loadFieldValues(siteContext, {
nodeId,
language,
fieldTypes,
publishedOnly,
preprocess,
});
}
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 n")
.select("n.id as nodeId")
.innerJoin("PaPartNode as p", "p.nodeId", "n.id")
.innerJoin("PaLNode as l", "l.nodeId", "n.id")
.where("n.parentId", parentNodeId)
.where("p.listName", listType.listName)
.where("l.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