Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9f8b89b

Browse files
fixed edge cases in JS library queries
1 parent 587019b commit 9f8b89b

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

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

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import {
1212
MultiBaseComp,
1313
wrapChildAction,
1414
evalFunc,
15+
changeValueAction,
16+
multiChangeAction,
17+
isDynamicSegment,
1518
} from "lowcoder-core";
1619
import {
1720
Dropdown,
@@ -39,6 +42,9 @@ import { toQueryView } from "./queryCompUtils";
3942
import { getGlobalSettings } from "comps/utils/globalSettings";
4043
import { QUERY_EXECUTION_ERROR, QUERY_EXECUTION_OK } from "../../constants/queryConstants";
4144
import type { SandBoxOption } from "lowcoder-core/src/eval/utils/evalScript";
45+
import { QueryLibraryApi } from "@lowcoder-ee/api/queryLibraryApi";
46+
import { validateResponse } from "@lowcoder-ee/api/apiUtils";
47+
import { JSONValue } from "@lowcoder-ee/util/jsonTypes";
4248

4349
const NoInputsWrapper = styled.div`
4450
color: ${GreyTextColor};
@@ -126,6 +132,8 @@ type QueryLibraryUpdateAction = {
126132
const childrenMap = {
127133
libraryQueryId: valueComp<string>(""),
128134
libraryQueryRecordId: valueComp<string>("latest"),
135+
libraryQueryType: valueComp<string>(""),
136+
libraryQueryDSL: valueComp<JSONValue>(null),
129137
inputs: InputsComp,
130138
error: stateComp<string>(""),
131139
};
@@ -147,23 +155,26 @@ export const LibraryQuery = class extends LibraryQueryBase {
147155

148156
override getView() {
149157
// Check if this is a JS query
150-
if (this.queryInfo?.query?.compType === "js") {
158+
const queryInfo = this.children.libraryQueryDSL.getView() as any;
159+
const queryType = this.children.libraryQueryType.getView() as any;
160+
if (queryType === "js") {
151161
return async (props: any) => {
152162
try {
153163
const { orgCommonSettings } = getGlobalSettings();
154164
const runInHost = !!orgCommonSettings?.runJavaScriptInHost;
155165
const timer = performance.now();
156-
const script = this.queryInfo.query.comp.script || "";
166+
const script = queryInfo.query.comp.script || "";
157167
const options: SandBoxOption = { disableLimit: runInHost };
158168

159169
// Get input values from the inputs component and resolve any variables
160170
const inputValues = Object.entries(this.children.inputs.children).reduce((acc, [name, input]) => {
161171
// Get the raw value from the input component's text property
172+
let { unevaledValue } = input.children.text;
162173
let value = input.children.text.getView();
163-
174+
164175
// Resolve any variables in the value
165-
if (typeof value === 'string') {
166-
value = value.replace(/\{\{([^}]+)\}\}/g, (match, path) => {
176+
if (typeof unevaledValue === 'string') {
177+
unevaledValue = unevaledValue.replace(/\{\{([^}]+)\}\}/g, (match, path) => {
167178
const parts = path.split('.');
168179
let current = props.args || {};
169180
for (const part of parts) {
@@ -173,17 +184,14 @@ export const LibraryQuery = class extends LibraryQueryBase {
173184
return match; // Return original if path not found
174185
}
175186
}
176-
return current?.value ?? match;
187+
return current ?? match;
177188
});
178189
}
179190

180-
acc[name] = value;
191+
acc[name] = isDynamicSegment(unevaledValue) ? value : unevaledValue;
181192
return acc;
182193
}, {} as Record<string, any>);
183194

184-
console.log("script: " + script);
185-
console.log("inputValues: ", inputValues);
186-
187195
const data = await evalFunc(script, inputValues, undefined, options);
188196
return {
189197
data: data,
@@ -223,10 +231,13 @@ export const LibraryQuery = class extends LibraryQueryBase {
223231

224232
override reduce(action: CompAction): this {
225233
if (isMyCustomAction<QueryLibraryUpdateAction>(action, "queryLibraryUpdate")) {
226-
this.queryInfo = action.value?.dsl;
234+
const isJSQuery = this.children.libraryQueryType.getView() === 'js'
235+
const queryDSL = isJSQuery ? action.value?.dsl : null;
236+
const queryDSLValue = this.children.libraryQueryDSL.reduce(this.children.libraryQueryDSL.changeValueAction(queryDSL))
237+
227238
const inputs = this.children.inputs.setInputs(action.value?.dsl?.["inputs"] ?? []);
228239
return setFieldsNoTypeCheck(this, {
229-
children: { ...this.children, inputs: inputs },
240+
children: { ...this.children, inputs: inputs,libraryQueryDSL: queryDSLValue },
230241
isReady: true,
231242
});
232243
}
@@ -315,7 +326,17 @@ const PropertyView = (props: { comp: InstanceType<typeof LibraryQuery> }) => {
315326
value: meta.libraryQueryMetaView.id,
316327
}))}
317328
value={queryId ?? queryLibraryMeta[0]?.libraryQueryMetaView.id}
318-
onChange={(value) => dispatch(props.comp.changeChildAction("libraryQueryId", value))}
329+
onChange={(value) => {
330+
const queryDSL = queryLibraryMeta[value]?.libraryQueryMetaView || null;
331+
const { datasourceType } = queryDSL as any;
332+
333+
props.comp.dispatch(
334+
multiChangeAction({
335+
libraryQueryId: changeValueAction(value, false),
336+
libraryQueryType: changeValueAction(datasourceType, false),
337+
})
338+
)
339+
}}
319340
/>
320341
</div>
321342
<QueryTutorialButton

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /