UNPKG

@paroicms/server

Version:
79 lines 3.61 kB
import { getDocumentTypeByName, } from "@paroicms/internal-anywhere-lib"; import { createSimpleTranslator } from "@paroicms/public-server-lib"; import { manageClusterRoutingDocuments } from "../../admin-backend/routing-cluster/cluster-management.js"; import { shouldSkipAuthorsTaxonomy } from "../site-schema/site-schema-helpers.js"; import { mainDbSchemaName } from "./db-constants.js"; import { createNode } from "./init-node-queries.js"; export async function initializeSiteStructure(cn, siteSchema, logger, fqdn) { logger.info(`Initialize '${mainDbSchemaName}' database with minimal structure…`); const schemaI18n = createSimpleTranslator({ labels: siteSchema.l10n, logger }); const homeNodeId = await createInitialSiteStructure(cn, siteSchema, schemaI18n); await autoCreateCluster({ cn, siteSchema, logger, fqdn }, { nodeId: homeNodeId, documentType: siteSchema.nodeTypes.home, }); } async function createInitialSiteStructure(cn, siteSchema, schemaI18n) { const documentType = siteSchema.nodeTypes.home; const makeTitle = (language) => schemaI18n.translate({ key: `nodeTypes.${documentType.typeName}.label`, language, defaultValue: documentType.typeName, }); const homeNodeId = await cn.transaction(async (tx) => { const siteNode = await createNode(siteSchema, tx, { typeName: "_site", }); const homeNode = await createNode(siteSchema, tx, { typeName: documentType.typeName, parentId: siteNode.id, }); for (const language of siteSchema.languages) { await tx("PaLNode").insert({ language, nodeId: homeNode.id, ready: true, updatedAt: tx.raw("current_timestamp"), }); await tx("PaDocument").insert({ language, nodeId: homeNode.id, title: makeTitle(language) }); } return homeNode.id; }); return homeNodeId; } export async function autoCreateCluster(siteContext, rootNode) { if (rootNode.documentType.cluster?.autoCreate && rootNode.documentType.routingChildren && rootNode.documentType.routingChildren.length > 0) { const request = buildClusterManagementRequest(siteContext.siteSchema, rootNode); await manageClusterRoutingDocuments(siteContext, request); } } function buildClusterManagementRequest(siteSchema, rootNode) { const { nodeId, documentType } = rootNode; const skipAuthors = shouldSkipAuthorsTaxonomy(siteSchema); const routingChildren = skipAuthors ? documentType.routingChildren?.filter((typeName) => typeName !== "authors") : documentType.routingChildren; const children = routingChildren?.map((typeName) => buildClusterManagementNode(siteSchema, typeName, { skipAuthors })); return { rootTypeName: documentType.typeName, rootNodeId: nodeId, children, }; } function buildClusterManagementNode(siteSchema, typeName, { skipAuthors, }) { const nodeType = getDocumentTypeByName(siteSchema, typeName); const node = { typeName, action: "insert", }; const routingChildren = skipAuthors ? nodeType.routingChildren?.filter((childTypeName) => childTypeName !== "authors") : nodeType.routingChildren; if (routingChildren && routingChildren.length > 0) { node.children = routingChildren.map((childTypeName) => buildClusterManagementNode(siteSchema, childTypeName, { skipAuthors })); } return node; } //# sourceMappingURL=populate-routing-documents.js.map