balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.84k stars 1.95k forks source link

Configure hook order? #6799

Open alxndrsn opened 5 years ago

alxndrsn commented 5 years ago

I'd like to define (and potentially release) a couple of hooks which modify the models and associated functions created by sails-hook-orm. Is there a way to configure the order of hook loading/execution?

I've noticed a couple of issues asking the same question (https://github.com/balderdashy/sails/issues/2517, https://github.com/balderdashy/sails/issues/4338), but no response and nothing in the hooks docs (https://sailsjs.com/documentation/concepts/extending-sails/hooks/hook-specification).

sailsbot commented 5 years ago

@alxndrsn Thanks for posting! We'll take a look as soon as possible.

In the mean time, there are a few ways you can help speed things along:

Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly.

For help with questions about Sails, click here.

whichking commented 5 years ago

Hi @alxndrsn! Unfortunately, as of Sails v1.0, the load order of custom hooks is not guaranteed (see the relevant documentation). We take user interest into consideration when planning updates to Sails, so this might be a feature to include in a future version.

I'll leave this open in case anybody else has thoughts or suggestions related to this topic. 🌊

nahanil commented 5 years ago

@alxndrsn You might be able to get away with waiting for the ORM hook to load, doing what you need, then forcing it to reload. Depends on your use case.

I do something like the following very cut-down example in the initialize part of some of my hooks to retrofit and tweak some models (not suggesting this is even a remotely good idea, but hey)

sails.after('hook:orm:loaded', () => {
   // Do some hacky voodoo and retrofit/tweak models
  sails.hooks.orm.reload((err) => { /* handle explosions */ })
})

Actually calling sails.hooks.orm.reload seems to crap itself if you have the default "Archive" model enabled (I've set archiveModelIdentity: false in my project-level config/models.js where project-level != inside my hooks), but I think that would warrant it's own issue being created. For now I've disabled it until I can find a better way.

Edit: https://github.com/balderdashy/sails-hook-orm#sailshooksormreload says you shouldn't do this 😆

alxndrsn commented 5 years ago

@madisonhicks that's a shame, but thanks for clarifying @texh :open_mouth: I think I'll just stick to modifying models in config/bootstrap.js :wink:

captainhusaynpenguin commented 4 years ago

This is how I propose to incorporate this feature in the next release, and would really appreciate it [and believe many others would too].

Description

Define an additional input when one can input change the automatic naming convention by sails: (package.json)

  "sails": {
    "isHook": true,
    "hookName": "xyz",
    "hookOrder": "m"
  },

Here, m could take any integer value. Similar principle as with z-index in CSS. So, when sails is loading hooks, it checks for the order number and loads hooks afterwards alphabetically.

Reasoning

Use-case | generally speaking, defining an order for hooks is quite complicated for big projects. Nobody can really keep tracks of what should come after what and why! However, there are packages which need to be loaded prior to others: ORM, View Engines, etc. Hence, it is best to define an "order" hierarchy instead of requesting the developer to provide the order via an array or json or etc.

Precedent | This is practically the "hack" that @emahuni suggests in https://github.com/balderdashy/sails/issues/2517.

Development | Though, I don't know how the Sails core does it, but I do believe that this is going to be quite easy to implement.


PS. Thanks for developing, maintaining and keeping Sails relevant. Hope one day Sails would be what Laravel or Rails are to their respective languages.

eashaw commented 4 years ago

Hello @alxndrsn & @captainhusaynpinguin, Sails handles hook order in default-hooks.js. An example of how one of these hooks handles loading at the right time. can be found in the http hook's initialize.js. To specify the order of custom hooks, you can do something similar in your hook's initialize function.