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.

Published
Mar 05, 24
Duration
3m 24s

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

Join The Discussion! (4 Comments)

Please sign in or sign up for free to join in on the dicussion.

  1. Commented 25 days ago

    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

    Please sign in or sign up for free to reply

    1. Commented 25 days ago

      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

      Please sign in or sign up for free to reply

      1. Commented 24 days ago

        Awesome thanks! Will try it out :)

        1

        Please sign in or sign up for free to reply

        1. Commented 19 days ago

          Anytime!! 😊

          0

          Please sign in or sign up for free to reply