From 46f94a98812369c0b1ea1b7709d1a1cd6997b043 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Thu, 15 Aug 2024 11:26:59 +0200 Subject: [PATCH] Render queries on the server --- plugs/query/api.ts | 3 ++- plugs/query/query.plug.yaml | 1 + plugs/query/widget.ts | 13 ++++++++----- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/plugs/query/api.ts b/plugs/query/api.ts index 6dd160d7..7422d125 100644 --- a/plugs/query/api.ts +++ b/plugs/query/api.ts @@ -15,7 +15,8 @@ export async function query( } /** - * Renders a query either as a result array, or as a rendered template when the `render` clause is defined + * Renders a query either as a result array, or as a rendered template when the `render` clause is defined. + * Runs on the server side (except in sync mode) * @param parsedQuery * @param variables * @returns Promise: a string if the query has a `render` clause, or an array of results diff --git a/plugs/query/query.plug.yaml b/plugs/query/query.plug.yaml index b483d7c1..7749ad5f 100644 --- a/plugs/query/query.plug.yaml +++ b/plugs/query/query.plug.yaml @@ -7,6 +7,7 @@ functions: path: api.ts:query renderQuery: path: api.ts:renderQuery + env: server queryWidget: path: widget.ts:widget diff --git a/plugs/query/widget.ts b/plugs/query/widget.ts index f25d070f..f06bb9aa 100644 --- a/plugs/query/widget.ts +++ b/plugs/query/widget.ts @@ -17,7 +17,6 @@ import { parseQuery } from "../../plug-api/lib/parse_query.ts"; import { loadPageObject, replaceTemplateVars } from "../template/page.ts"; import type { CodeWidgetContent } from "../../plug-api/types.ts"; import { jsonToMDTable } from "../template/util.ts"; -import { renderQuery } from "./api.ts"; import type { ChangeSpec } from "@codemirror/state"; import { findNodeMatching, @@ -36,10 +35,14 @@ export async function widget( await replaceTemplateVars(bodyText, pageObject, config), ); - const results = await renderQuery(parsedQuery, { - page: pageObject, - config, - }); + const results = await system.invokeFunction( + "query.renderQuery", + parsedQuery, + { + page: pageObject, + config, + }, + ); if (Array.isArray(results)) { resultMarkdown = jsonToMDTable(results); } else {