Backend hook 🪝

Hello, about this…

Hook files are regular JavaScript files that have the following format:

   "workflow": {
       "activate": [
           async function (workflowData) {
               const activeWorkflows = await this.dbCollections.Workflow.count({ active: true });

               if (activeWorkflows > 1) {
                   throw new Error(
                       'Active workflow limit reached.'
                   );
               }
           }
       ]
   }

I’ve spent 2 days looking through the source code and can’t find a solution (I’ve been trying some combinations in PostgreSQL)… so I’ll be direct… the idea is not only to limit the workflow activation globally for the entire instance, but to do it per individual user;

how do I find or declare the variable that gives me the logged in user and then compare that user id with the project id each workflow Id belongs to?

If the project id of the user trying to activate the workflow exceeds the limit, only that user should get the error notificación…
** is it possible to do this in this or another way?**

Note: I have already tested the original script and it works very well, I would just like to make the mentioned addition; and suggest that there should be more documentation with examples of Backend and Frontend Hooks.

Information on your n8n setup

  • latest:
  • Database (default: PostgreSQL):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via Docker:
  • Operating system: Ubuntu 24

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:
  • n8n version: 1.69.2
  • Database: PostgreSQL
  • n8n EXECUTIONS_PROCESS setting (default: own, main): main
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Ubuntu 24

Storytime
Since nobody answers :upside_down_face:, I will leave here a simple explanation of what I did thinking outside the box…

I had to combine:
a workflow that accesses postgress and exports a . json file with the workflowId and its respective projectid (from the shared_workflow table, its trigger is “postgresql” every time a new workflow is created it generates the updated file) …

Then to get the projectid of the workflow that is trying to be activated (workflowData.id, with this I get my “binding element” to the account) and that in turn filters all the workflows belonging to the projectId, I had to do this by consulting the Rest API, since dbCollections does not filter “workflowsIds” (an array of only the ids of a project, I noticed that the API does, I feel that dbCollections is very limited, you should put more documentation)…

With all this I managed to filter that only by project (assigned user) consult the workflows that are activated respectively…

Thus allowing not only global limits but also specific users (in this way it is more equitable and I allow, for example, if I have 3 users, that each one can activate 2 workflows, without just one activating 6 and consuming the limit)…

Something similar, I will try to limit the creation so that they can only create 5 but only activate 3 simultaneously).

I hope it helps someone :santa:
:pray:Thank you for your attention.

Hey @Santa_Claus,

That seems like a nice solution although I do wonder if there is a better way, Assuming you are using embed did you try reaching out to your sales contact about this as they may have been able to find the answer for you.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.