Transcript
-
So when we were reviewing our database schema, you might've been wondering why we left users as full name
-
and we split that full name into first name and last name for our Cinasts. Well, that was so that we could cover unmapped and computed columns inside of our models.
-
So if we take a look at our Cinasts model, we have our first name and last name as separate columns. But if we take a look at our user, we have just full name here.
-
And we can join our first name and our last name together within our Cinasts model to add in an additional column. So down here at the bottom,
-
we'll define a getter called full name. Scroll up a little bit here. And this will just return back this first name, space, this last name. So let's give this a save
-
and let's jump back into a REPL session to see what we get. So node ace REPL, let's await, load our models
-
and let's await models Cinasts dot query. And let's just get back to first result and let's hit run on that. Okay, so if we scroll up here,
-
we'll see that it was not added to our attributes. It's not a mapped column in any way. And if we scroll up a little bit more, we'll see it's not even on here. It's inside of our getters.
-
So it's not going to come through as a visible column. But if we plop this inside of a variable, so let's do let Cinasts equals
-
and we print out our Cinasts dot full name. Sure enough, we get that Cinasts full name. Their first name is Nathaniel
-
and their last name is Chaplin. Now, the way that we have this defined will allow us to access this so long as it is still a lucid model.
-
The second that we serialize this model, which would include returning it back as a JSON result from our server or passing it into client side code,
-
this column will no longer be included because we haven't specified it to be included. For example, if we do Cinasts dot serialize,
-
to serialize this model, again, this is the same behavior that's going to be used anytime that we return the model result here back as a JSON result from our server
-
or pass it into client side code, we'll get this serialized result here. And you'll see that we get back ID, first name, last name, headshot URL, created at and updated at,
-
but nowhere is our full name. If we wanted our full name to be included with this serialized data, we could jump back into that model
-
and we can decorate this getter with at computed to define this as a computed property. If we scroll up to see where that was imported from,
-
it's from AdonisJS Lucid ORM. So we'll scroll back down here. Don't forget to save and let's hide our text editor back away. And I'm pretty sure we'll need to reload our model here.
-
So let's just double check that. Yeah, sure enough. So let's dot exit, jump back into a REPL session, await, load models, and let's rerun our query.
-
So let's do let Cinasts equal our first one and then let's do Cinasts dot serialize. There we go. So unlike before where we just got back the ID, first name, last name, headshot URL,
-
created at and updated at, we now have in addition to those, a full name being serialized as well. So in turn, if you have a column outside the realms of your database
-
that you wanna return a synchronous result for, you can define it as a getter with whatever name you want it to have on the model for the property and then just return back what you need to return.
-
That can work directly with other properties inside of the model or it could return back something completely unrelated to your other model data. And then if you want it to be included with serialized data for your model,
-
you wanna add computed as a decorator on that property.
Unmapped and Computed Model Properties
We'll learn how to add unmapped and computed properties to our Lucid Models. We'll discuss the differences between a model column, unmapped property, and a computed property.
Join the Discussion 4 comments
-
Is there an existing equivalent of setters? For example, I'd like to have "active" boolean field on a model, and have a setter / method, called "deactivate" which by running model.deactivate(), sets it's field "active" to false.
Is that possible?1-
Responding to rafal-olszewski
Hi Rafal! Yeah, both are possible! Just like getters, setters can't be asynchronous. However, methods can be asynchronous.
export default class User extends BaseModel { // ... @column() declare active: boolean set isActive(value: boolean) { this.active = value } deactivate() { this.active = false } } const user = new User() user.isActive = false user.deactivate()
Copied!1-
Responding to tomgobich
-
Responding to rafal-olszewski
-
-
-