Playing Next Lesson In
seconds

Let's Learn AdonisJS 6 #4.10

Unmapped and Computed Model Properties

In This Lesson

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.

Created by
@tomgobich
Published

Join the Discussion 4 comments

Create a free account to join in on the discussion
  1. @rafal-olszewski

    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
    1. Responding to rafal-olszewski
      @tomgobich

      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
      1. Responding to tomgobich
        @rafal-olszewski

        Awesome thanks! Will try it out :)

        1
        1. Responding to rafal-olszewski
          @tomgobich

          Anytime!! 😊

          0