Created
June 21, 2018 15:51
-
-
Save eric-burel/cf2bf5735e5380c109a80e19d6e7be29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// my-service.hooks.js | |
module.exports = { | |
before:{ | |
find:[ | |
async findQuery(context){ | |
// get the client (Express global) | |
const { db } = context.app.get('knexClient') | |
// build and run the query | |
const q = db.raw(`SELECT * FROM TABLE`) | |
const res = await q | |
// when context.result is set, the next hooks will be ignored, | |
// so the service won't trigger its own automatically generated query | |
context.result = res | |
return context | |
} | |
] | |
} | |
// ...other hooks | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AMAZING!!! Exactly what I needed! I have a SQLite database that has a many-to-many relationship between
Students
andCourses
(AStudent
can be enrolled in manyCourses
, and aCourse
will have manyStudents
on a roster). There are three relevant tables:students
,courses
, andcourses_students
. I'm using thefeathers-knex
library, as I suspect you are. I found a couple of things:db
property on myknexClient
so your destructuring step didn't work for me, but it did give me enough clues to eventually figure out what to do. I don't know if the API has changed since you wrote that or what...context.result
directly (as I do below) I was able to avoid skipping the rest of the hooks. This allowed me to keep my hooks nice and cleanly separated and not have to do too many things at once.Here's a couple of relevant excerpts from my code:
and:
Thank you!!! I hope this helps you or the next person...