Render queries on the server

pull/1039/head
Zef Hemel 2024-08-15 11:26:59 +02:00
parent 67f997258d
commit 46f94a9881
3 changed files with 11 additions and 6 deletions

View File

@ -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<any[] | string>: a string if the query has a `render` clause, or an array of results

View File

@ -7,6 +7,7 @@ functions:
path: api.ts:query
renderQuery:
path: api.ts:renderQuery
env: server
queryWidget:
path: widget.ts:widget

View File

@ -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 {