How To Add A Custom Method to the Model Query Builder in AdonisJS

preview_player
Показать описание
In this lesson, we'll learn how to define a macro for the Model Query Builder within an AdonisJS project to provide a new method onto all our Model's Query Builder. We'll also add a custom method inspired by Laravel called "firstOr", which will allow us to define our own fallback value if a first query result cannot be found.

Social Media:
Рекомендации по теме
Комментарии
Автор

An improvement for the firstOr method's TypeScript has been shared and explained to me, big thanks to Aman Virk, that allows the callback function's return type to be inferred instead of manually defined. When I can I'll be sure to get an updated video or a follow up video including this approach. You can find it below:

firstOr<T = undefined>(orFunction: () => T): Promise<Result> | T

Here we’re defining our firstOr method and defining a type argument, T, and we’ll default that to undefined. We state that it accepts a callback function as its only argument, and we’ll have TypeScript infer the return type as T for that function. Lastly, it returns a promise of type Result or T, the type argument.

Adocasts
Автор

Thank you so much! Your videos have been really helpful.
Can you do a video on the lifecycle of an Adonis app? That would mean a lot.

leorai
Автор

Please make a dashboard application with Adonis

hasiburrahman
visit shbcf.ru