Ready to get started?
Join Adocasts Plus for $8.00/mo, or sign into your account to get access to all of our lessons.
A Deep Dive Into Mixins & Compose
In this lesson, we'll walk through what mixins are and how they work by first inspecting them as plain objects then working our way into classes then finally TypeScript. We'll then implement mixins via AdonisJS' compose utility.
- Author
- Tom Gobich
- Published
- Dec 17, 23
- Duration
- 11m 21s
Developer, dog lover, and burrito eater. Currently teaching AdonisJS, a fully featured NodeJS framework, and running Adocasts where I post new lessons weekly. Professionally, I work with JavaScript, .Net C#, and SQL Server.
Adocasts
Burlington, KY
Ready to get started?
Join Adocasts Plus for $8.00/mo, or sign into your account to get access to all of our lessons.
Related Content
-
1How To Use Vue 3 with TypeScript in an AdonisJS Project10m 14s
-
Creating A Lucid Model Mixin for our Organization Relationship8m 29s
-
3How To Do Multi Model Authentication with AdonisJS and Lucid ORM18m 29s
-
4Getting Data by the User's Timezone with Luxon's DateTime5m 54s
-
5Easy Imports with NodeJS Subpath Imports8m 40s
-
Typing Lucid Models in Inertia with DTOs10m 58s
-
7How To Serialize All AdonisJS Lucid Model Properties As Camel Case6m 23s
-
8How To Add A Custom Method to the Model Query Builder in AdonisJS9m 50s
-
9How To Use AdonisJS Model Hooks To Log All User Actions9m 59s
-
10Debugging, Inspecting, and Freezing Code Execution11m 16s
-
11Filtering By User's Watched Status6m 7s
-
12An Alternative Approach to Many-To-Many Relationships4m 56s
-
13Let's Learn Adonis 5: Installing and Configuring Lucid6m 49s
-
14Limiting, Sorting, & Pagination with the Query Builder4m 57s
-
15Defining Our Models6m 49s
Join The Discussion! (6 Comments)
Please sign in or sign up for free to join in on the dicussion.
ArthurFranckPat
Thank you for the video. I have few questions : Can we use decorators inside the mixin ? For exemple if we want to define a relationship between the model and itself (a recursive relationship)
Please sign in or sign up for free to reply
tomgobich
Hey Arthur! I just gave it a go and everything seems to be working a-okay with using decorators in a mixin! Only caveat is, it looks like you have to give the class a name and return it via that name on a separate line for TypeScript to be happy. Below is what I tried, note the imports are for AdonisJS 6, but the functionality would be the same between 5 & 6.
As a heads up, you can define a parent/child relationship directly on the model without using compose. It'll look like the below.
Hope this helps!
—-
Edit: Updated to account for the TypeScript requirement of the mixin class needing a name and for the return to be on a separate line. I missed the small red squiggly on the decorator when I first attempted this.
Please sign in or sign up for free to reply
danilesky17
Hello, what about this in adonisJS v6 , there is no such a Normalizing class or maybe i cannot find it in documentation ? Thanks for answear :)
Please sign in or sign up for free to reply
tomgobich
Hi Danilesky17! Hmm, yeah it seems to have slipped from the documentation. Luckily though, it is still within AdonisJS, it's just now under the core helpers. Below is the example from this lesson updated for AdonisJS 6!
Hope this helps!
Please sign in or sign up for free to reply
danilesky17
Tom, thank you for quick response! It helped me a lot. I couldn't find this constructor before and VS code didn't offer me when i was trying to find it. Now it seems to be working correctly. Thanks.
Please sign in or sign up for free to reply
tomgobich
Anytime!! Yeah, I tried the same thing and had the same result. It's a hidden little bugger lol 😊
Please sign in or sign up for free to reply