Skip to content

Commit 128d31f

Browse files
author
FalkWolsky
committed
Updating Query Library Function
1 parent 4ac60a2 commit 128d31f

File tree

4 files changed

+83
-2
lines changed

4 files changed

+83
-2
lines changed

client/packages/lowcoder/src/comps/queries/libraryQuery.tsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
isMyCustomAction,
1111
MultiBaseComp,
1212
wrapChildAction,
13+
evalFunc,
1314
} from "lowcoder-core";
1415
import {
1516
Dropdown,
@@ -34,6 +35,9 @@ import {
3435
ToInstanceType,
3536
} from "../generators/multi";
3637
import { toQueryView } from "./queryCompUtils";
38+
import { getGlobalSettings } from "comps/utils/globalSettings";
39+
import { QUERY_EXECUTION_ERROR, QUERY_EXECUTION_OK } from "../../constants/queryConstants";
40+
import type { SandBoxOption } from "lowcoder-core/src/eval/utils/evalScript";
3741

3842
const NoInputsWrapper = styled.div`
3943
color: ${GreyTextColor};
@@ -133,13 +137,60 @@ export const LibraryQuery = class extends LibraryQueryBase {
133137
readonly isReady: boolean = false;
134138

135139
private value: DataType | undefined;
140+
private queryInfo: any = null;
136141

137142
constructor(params: CompParams<DataType>) {
138143
super(params);
139144
this.value = params.value;
140145
}
141146

142147
override getView() {
148+
// Check if this is a JS query
149+
if (this.queryInfo?.query?.compType === "js") {
150+
return async (props: any) => {
151+
try {
152+
const { orgCommonSettings } = getGlobalSettings();
153+
const runInHost = !!orgCommonSettings?.runJavaScriptInHost;
154+
const timer = performance.now();
155+
const script = this.queryInfo.query.comp.script || "";
156+
const options: SandBoxOption = { disableLimit: runInHost };
157+
158+
// Get input values from the inputs component
159+
const inputValues = Object.entries(this.children.inputs.children).reduce((acc, [name, input]) => {
160+
// Get the actual value from the input component's text property
161+
const value = input.children.text.getView();
162+
acc[name] = value;
163+
return acc;
164+
}, {} as Record<string, any>);
165+
166+
// Combine props.args with input values
167+
const context = {
168+
...props.args,
169+
...inputValues,
170+
};
171+
172+
console.log("script: " + script);
173+
console.log("context: ", context);
174+
175+
// Pass script directly to evalFunc without wrapping
176+
const data = await evalFunc(script, context, undefined, options);
177+
return {
178+
data: data,
179+
code: QUERY_EXECUTION_OK,
180+
success: true,
181+
runTime: Number((performance.now() - timer).toFixed()),
182+
};
183+
} catch (e) {
184+
return {
185+
success: false,
186+
data: "",
187+
code: QUERY_EXECUTION_ERROR,
188+
message: (e as any).message || "",
189+
};
190+
}
191+
};
192+
}
193+
143194
return toQueryView(
144195
Object.entries(this.children.inputs.children).map(([name, input]) => ({
145196
key: name,
@@ -161,6 +212,7 @@ export const LibraryQuery = class extends LibraryQueryBase {
161212

162213
override reduce(action: CompAction): this {
163214
if (isMyCustomAction<QueryLibraryUpdateAction>(action, "queryLibraryUpdate")) {
215+
this.queryInfo = action.value?.dsl;
164216
const inputs = this.children.inputs.setInputs(action.value?.dsl?.["inputs"] ?? []);
165217
return setFieldsNoTypeCheck(this, {
166218
children: { ...this.children, inputs: inputs },

client/packages/lowcoder/src/comps/queries/queryComp.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ QueryCompTmp = class extends QueryCompTmp {
487487
args: action.args,
488488
variables: action.args,
489489
timeout: this.children.timeout,
490-
callback: (result) => this.processResult(result, action, startTime)
490+
callback: (result: QueryResult) => this.processResult(result, action, startTime)
491491
});
492492
}, getTriggerType(this) === "manual")
493493
.then(

client/packages/lowcoder/src/comps/queries/queryCompUtils.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { Fragment } from "react";
77
import { ParamsControlType, ValueFunction } from "../controls/paramsControl";
88
import { getGlobalSettings } from "comps/utils/globalSettings";
99
import { ResourceType } from "@lowcoder-ee/constants/queryConstants";
10+
import { evalFunc } from "lowcoder-core";
11+
import { QUERY_EXECUTION_ERROR, QUERY_EXECUTION_OK } from "../../constants/queryConstants";
12+
import type { SandBoxOption } from "lowcoder-core/src/eval/utils/evalScript";
1013

1114
export type FunctionProperty = {
1215
key: string;
@@ -30,6 +33,32 @@ export function toQueryView(params: FunctionProperty[]) {
3033
}): Promise<QueryResult> => {
3134
const { applicationId, isViewMode } = getGlobalSettings();
3235

36+
// Check if this is a JS query
37+
const isJsQuery = props.queryId?.startsWith("js:");
38+
if (isJsQuery) {
39+
try {
40+
const { orgCommonSettings } = getGlobalSettings();
41+
const runInHost = !!orgCommonSettings?.runJavaScriptInHost;
42+
const timer = performance.now();
43+
const script = props.args?.script || "";
44+
const options: SandBoxOption = { disableLimit: runInHost };
45+
const data = await evalFunc(`return (${script}\n);`, props.args || {}, undefined, options);
46+
return {
47+
data: data,
48+
code: QUERY_EXECUTION_OK,
49+
success: true,
50+
runTime: Number((performance.now() - timer).toFixed()),
51+
};
52+
} catch (e) {
53+
return {
54+
success: false,
55+
data: "",
56+
code: QUERY_EXECUTION_ERROR,
57+
message: (e as any).message || "",
58+
};
59+
}
60+
}
61+
3362
let mappedVariables: Array<{key: string, value: string}> = [];
3463
Object.keys(props.variables)
3564
.filter(k => k !== "$queryName")

client/packages/lowcoder/src/util/context/SimpleSubscriptionContext.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export const SimpleSubscriptionContextProvider = (props: {
9696
};
9797

9898
if (!productsLoaded && !subscriptionProductsLoading && !user.isAnonymous) {
99-
console.log("Outer context: Fetching products...");
99+
// console.log("Outer context: Fetching products...");
100100
setSubscriptionProductsLoading(true);
101101
fetchProducts();
102102
}

0 commit comments

Comments
 (0)