-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Event Handlers: does not support promises #301
Comments
Another issue I expect you will run into is that the If you intend to asynchronously log to some external service, i.e. not Postgres, you wouldn't have this issue, but it would still not be a great idea for performance. At the point that In my projects, I normally just log to stdout/stderr, via function onQueryStart(_query, formattedQuery) {
writeToRemoteLog(formattedQuery).catch(ex => {
console.error(`Failed to write to log: ${ex.message}`)
})
} The reason for enforcing
We could support the query modification use case by just relaxing the types. You could have something like: function getCurrentController(): string {
// TODO: Use AsyncLocalStorage to get the "controller" for the current request
return 'unknown'
}
function onQueryStart(_query, formattedQuery) {
// @ts-expect-error formattedQuery.text is marked as readonly
formattedQuery.text = formattedQuery.text + `/* controller='${getCurrentController()}' */`
} I'm not confident enough about the caching use case to know what the API should be yet. It's also possible we should add a hook that runs much earlier for this. Ideally, if you call |
@ForbesLindesay thanks for the reply! Turns out this situation I was just trying to force being able to do it through @databases instead of just using triggers.. I was against it but with a little bit of tweaks I was able to get triggers to work as needed. Still happen to work on a PR for this if it's something you're interested in having (so I can give back), let me know what your thoughts are though, don't want to create additional headache if not needed |
atdatabases/packages/pg/src/Driver.ts
Lines 409 to 415 in 97be50f
I'm trying to implement a sort of "Activity Log" middleware utilizing the
onQueryStart
andonQueryResults
event handlers, my idea is as follows:onQueryStart
parse and if query isUPDATE
make a query to get current value and store in classonQueryResults
parse and if query is sameUPDATE
query, get resulting value. Do insert intoactivity_log
table withbefore
andafter
entriesI have everything implemented for this, parsing the query using
node-sql-parser
everything is great no problems there.The problem i'm running into though is that all of the event handlers require the returned result to be
undefined
Would you be okay with me submitting a PR to update to add support event handlers to be promises that return undefined? Was there some other kind of functionality you were planning for event handlers that maybe I can help work on? Or do you have any other suggestions of an easier way to handle this?
note: I was trying to stay away from using triggers as there is some logic I need to do, like parsing the query for a specific "user" ID and only doing the insert for specific tables, when that ID has a value, etc
The text was updated successfully, but these errors were encountered: