knex-hooks icon indicating copy to clipboard operation
knex-hooks copied to clipboard

How to install in Directus CMS

Open danieljwestman opened this issue 4 years ago • 2 comments

I'm trying to install and use this hooks on a Directus (v9 - the node version) project. Can't get it to load properly. I'm following the steps and import knex-hooks in the database file. When running it throws some errors:

npm run dev

[ERROR] 15:07:56 ⨯ Unable to compile TypeScript:

src/database/index.ts:53:10 - error TS2339: Property 'addHook' does not exist on type 'Knex<any, unknown[]>'.

53 database.addHook('before', '*', '*', (when, method, table, params) => {
            ~~~~~~~
src/database/index.ts:53:39 - error TS7006: Parameter 'when' implicitly has an 'any' type.

53 database.addHook('before', '*', '*', (when, method, table, params) => {
                                         ~~~~
src/database/index.ts:53:45 - error TS7006: Parameter 'method' implicitly has an 'any' type.

53 database.addHook('before', '*', '*', (when, method, table, params) => {
                                               ~~~~~~
src/database/index.ts:53:53 - error TS7006: Parameter 'table' implicitly has an 'any' type.

53 database.addHook('before', '*', '*', (when, method, table, params) => {
                                                       ~~~~~
src/database/index.ts:53:60 - error TS7006: Parameter 'params' implicitly has an 'any' type.

53 database.addHook('before', '*', '*', (when, method, table, params) => {

I guess Directus can't load the module. All the other node_modules have a different structure.

Anyone got this to work, in any project type?

danieljwestman avatar Oct 11 '20 13:10 danieljwestman

Hey, knex-hooks is missing typescript declarations and this is why TS can't compile your code because it does not know that knex is pacthed and have new method "addHook".

For now you have to manually declare knex method addHook(...) in your code.

alekbarszczewski avatar Oct 16 '20 09:10 alekbarszczewski

Hi @alekbarszczewski

Thanks for getting back to me in this issue. Sorry for my late response...

I'm not sure I understand. Could you please give me some more simple info/guides on how to setup these hooks (and helpers) in a Node (Express) project like Directus v9?

Big thanks!

danieljwestman avatar Oct 27 '20 02:10 danieljwestman