Skip to content
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

Configure hook order? #6799

Open
alxndrsn opened this issue Jun 25, 2019 · 6 comments
Open

Configure hook order? #6799

alxndrsn opened this issue Jun 25, 2019 · 6 comments
Labels
helpful info or workaround orm Related to models, datastores, orm config, Waterline, sails-hook-orm, etc.

Comments

@alxndrsn
Copy link

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 (#2517, #4338), but no response and nothing in the hooks docs (https://sailsjs.com/documentation/concepts/extending-sails/hooks/hook-specification).

@sailsbot
Copy link

@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:

  • look for a workaround. (Even if it's just temporary, sharing your solution can save someone else a lot of time and effort.)
  • tell us why this issue is important to you and your team. What are you trying to accomplish? (Submissions with a little bit of human context tend to be easier to understand and faster to resolve.)
  • make sure you've provided clear instructions on how to reproduce the bug from a clean install.
  • double-check that you've provided all of the requested version and dependency information. (Some of this info might seem irrelevant at first, like which database adapter you're using, but we ask that you include it anyway. Oftentimes an issue is caused by a confluence of unexpected factors, and it can save everybody a ton of time to know all the details up front.)
  • read the code of conduct.
  • if appropriate, ask your business to sponsor your issue. (Open source is our passion, and our core maintainers volunteer many of their nights and weekends working on Sails. But you only get so many nights and weekends in life, and stuff gets done a lot faster when you can work on it during normal daylight hours.)
  • let us know if you are using a 3rd party plugin; whether that's a database adapter, a non-standard view engine, or any other dependency maintained by someone other than our core team. (Besides the name of the 3rd party package, it helps to include the exact version you're using. If you're unsure, check out this list of all the core packages we maintain.)

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.

@johnabrams7 johnabrams7 added the orm Related to models, datastores, orm config, Waterline, sails-hook-orm, etc. label Jun 25, 2019
@whichking
Copy link
Contributor

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
Copy link

nahanil commented Jun 25, 2019

@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
Copy link
Author

@MadisonHicks that's a shame, but thanks for clarifying
@texh 😮 I think I'll just stick to modifying models in config/bootstrap.js 😉

@captainhusaynpenguin
Copy link

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 #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.

@sailsbot sailsbot removed the resolved label Jul 29, 2020
@eashaw
Copy link
Member

eashaw commented Sep 9, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
helpful info or workaround orm Related to models, datastores, orm config, Waterline, sails-hook-orm, etc.
Development

No branches or pull requests

7 participants