beforeScriptListener custom extension
-
Hi,
We are trying to migrate to v3 from v2.11 and there is one piece that doesn't quite fit.
We have a custom extension that allow us to run some queries internally and return the data.
On version v2 we have the followingjsreport.config.ts
module.exports = { name: "dataLoader", main: "index.js", dependencies: ["templates", "data", "scripts"] }
It seems that something change on v3 and dependencies is not longer there, not quite sure if we need to do something to adapt that.
The main issue I think is on the actual extension code, we had
reporter.initializeListeners.add("dataLoader", async () => { reporter.beforeScriptListeners.insert( { after: "assets" }, "dataLoader", reporter, async (scriptDef, req, done) => {
We use the
beforeScriptListeners
. It's not documented but it does what we need.
To add some context:
We have a jsreport script that adds some queries toreq.data.queries
req.data.queries = [QUERIES]
Then on our extension we check the
req.data.queries
, we run the queries internally and we store the results onreq.data[tableName]
If we try run the code as we have it on v3 the first error comes loading jsreport trying to access to
beforeScriptListeners
because it doesn't exist on the reporter. If we switch this tobeforeRenderListeners
when we reach the code it doesn't contain the data we need, my guess is because it actually run before the render and not before the scripts so at that point our script that set the queries onreq.data.queries
hasn't even run yet.What can we do? How we can run our extension to being able to capture that data?
Thanks!
-
Some documentation:
https://jsreport.net/learn/custom-extension#server-sideSince v3, we split extension's code to the
main
part running in the main thread andworker
part running in the safe worker thread. You just need to registerbeforeScriptListeners
in the worker part and that should be all.This could be a simple example for you
https://github.com/jsreport/jsreport/tree/master/packages/jsreport-localization
-
Awesome, it worked like a charm.
Thanks!