@@ -2,6 +2,7 @@ import { default as LoadingOutlined } from "@ant-design/icons/LoadingOutlined";
22import { default as Spin } from "antd/es/spin" ;
33import DataSourceIcon from "components/DataSourceIcon" ;
44import { ContextControlType , ContextJsonControl } from "comps/controls/contextCodeControl" ;
5+ import { FunctionControl } from "comps/controls/codeControl" ;
56import { trans } from "i18n" ;
67import {
78 CompAction ,
@@ -10,6 +11,7 @@ import {
1011 isMyCustomAction ,
1112 MultiBaseComp ,
1213 wrapChildAction ,
14+ evalFunc ,
1315} from "lowcoder-core" ;
1416import {
1517 Dropdown ,
@@ -34,6 +36,9 @@ import {
3436 ToInstanceType ,
3537} from "../generators/multi" ;
3638import { toQueryView } from "./queryCompUtils" ;
39+ import { getGlobalSettings } from "comps/utils/globalSettings" ;
40+ import { QUERY_EXECUTION_ERROR , QUERY_EXECUTION_OK } from "../../constants/queryConstants" ;
41+ import type { SandBoxOption } from "lowcoder-core/src/eval/utils/evalScript" ;
3742
3843const NoInputsWrapper = styled . div `
3944 color: ${ GreyTextColor } ;
@@ -133,13 +138,70 @@ export const LibraryQuery = class extends LibraryQueryBase {
133138 readonly isReady : boolean = false ;
134139
135140 private value : DataType | undefined ;
141+ private queryInfo : any = null ;
136142
137143 constructor ( params : CompParams < DataType > ) {
138144 super ( params ) ;
139145 this . value = params . value ;
140146 }
141147
142148 override getView ( ) {
149+ // Check if this is a JS query
150+ if ( this . queryInfo ?. query ?. compType === "js" ) {
151+ return async ( props : any ) => {
152+ try {
153+ const { orgCommonSettings } = getGlobalSettings ( ) ;
154+ const runInHost = ! ! orgCommonSettings ?. runJavaScriptInHost ;
155+ const timer = performance . now ( ) ;
156+ const script = this . queryInfo . query . comp . script || "" ;
157+ const options : SandBoxOption = { disableLimit : runInHost } ;
158+
159+ // Get input values from the inputs component and resolve any variables
160+ const inputValues = Object . entries ( this . children . inputs . children ) . reduce ( ( acc , [ name , input ] ) => {
161+ // Get the raw value from the input component's text property
162+ let value = input . children . text . getView ( ) ;
163+
164+ // Resolve any variables in the value
165+ if ( typeof value === 'string' ) {
166+ value = value . replace ( / \{ \{ ( [ ^ } ] + ) \} \} / g, ( match , path ) => {
167+ const parts = path . split ( '.' ) ;
168+ let current = props . args || { } ;
169+ for ( const part of parts ) {
170+ if ( current && typeof current === 'object' ) {
171+ current = current [ part ] ;
172+ } else {
173+ return match ; // Return original if path not found
174+ }
175+ }
176+ return current ?. value ?? match ;
177+ } ) ;
178+ }
179+
180+ acc [ name ] = value ;
181+ return acc ;
182+ } , { } as Record < string , any > ) ;
183+
184+ console . log ( "script: " + script ) ;
185+ console . log ( "inputValues: " , inputValues ) ;
186+
187+ const data = await evalFunc ( script , inputValues , undefined , options ) ;
188+ return {
189+ data : data ,
190+ code : QUERY_EXECUTION_OK ,
191+ success : true ,
192+ runTime : Number ( ( performance . now ( ) - timer ) . toFixed ( ) ) ,
193+ } ;
194+ } catch ( e ) {
195+ return {
196+ success : false ,
197+ data : "" ,
198+ code : QUERY_EXECUTION_ERROR ,
199+ message : ( e as any ) . message || "" ,
200+ } ;
201+ }
202+ } ;
203+ }
204+
143205 return toQueryView (
144206 Object . entries ( this . children . inputs . children ) . map ( ( [ name , input ] ) => ( {
145207 key : name ,
@@ -161,6 +223,7 @@ export const LibraryQuery = class extends LibraryQueryBase {
161223
162224 override reduce ( action : CompAction ) : this {
163225 if ( isMyCustomAction < QueryLibraryUpdateAction > ( action , "queryLibraryUpdate" ) ) {
226+ this . queryInfo = action . value ?. dsl ;
164227 const inputs = this . children . inputs . setInputs ( action . value ?. dsl ?. [ "inputs" ] ?? [ ] ) ;
165228 return setFieldsNoTypeCheck ( this , {
166229 children : { ...this . children , inputs : inputs } ,
@@ -258,7 +321,7 @@ const PropertyView = (props: { comp: InstanceType<typeof LibraryQuery> }) => {
258321 < QueryTutorialButton
259322 label = { trans ( "queryLibrary.viewQuery" ) }
260323 url = { `/query-library?forwardQueryId=${ queryId } ` }
261- styleName = { "dropdownRight" }
324+ styleName = "dropdownRight"
262325 />
263326 </ QueryConfigWrapper >
264327
@@ -284,4 +347,4 @@ const PropertyView = (props: { comp: InstanceType<typeof LibraryQuery> }) => {
284347const QueryLabelWrapper = styled . div `
285348 display: flex;
286349 align-items: center;
287- ` ;
350+ ` ;
0 commit comments