@@ -10,6 +10,7 @@ import {
1010 isMyCustomAction ,
1111 MultiBaseComp ,
1212 wrapChildAction ,
13+ evalFunc ,
1314} from "lowcoder-core" ;
1415import {
1516 Dropdown ,
@@ -34,6 +35,9 @@ import {
3435 ToInstanceType ,
3536} from "../generators/multi" ;
3637import { 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
3842const 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 } ,
0 commit comments