| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | const log = require('./log'); | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  | const sql = require('./sql'); | 
					
						
							|  |  |  | const ScriptContext = require('./script_context'); | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-30 22:44:46 -05:00
										 |  |  | async function executeScript(dataKey, script, params) { | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  |     log.info('Executing script: ' + script); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-30 22:44:46 -05:00
										 |  |  |     const ctx = new ScriptContext(dataKey); | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const paramsStr = getParams(params); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |     let ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await sql.doInTransaction(async () => { | 
					
						
							|  |  |  |         ret = await (function() { return eval(`(${script})(${paramsStr})`); }.call(ctx)); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getParams(params) { | 
					
						
							| 
									
										
										
										
											2018-02-18 09:53:36 -05:00
										 |  |  |     return params.map(p => { | 
					
						
							|  |  |  |         if (typeof p === "string" && p.startsWith("!@#Function: ")) { | 
					
						
							|  |  |  |             return p.substr(13); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             return JSON.stringify(p); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }).join(","); | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |     executeScript | 
					
						
							|  |  |  | }; |