@rvoh/dream
    Preparing search index...

    Class Decorators<TD, T>

    Type Parameters

    Index

    Constructors

    Methods

    • AfterCreate decorator

      class User {
      @deco.AfterCreate()
      public doSomething() {
      console.log('hi!')
      }
      }

      Parameters

      Returns any

      The AfterCreate decorator

    • AfterCreateCommit decorator

      class User {
      @deco.AfterCreateCommit()
      public doSomething() {
      console.log('hi!')
      }
      }

      Parameters

      Returns any

      The AfterCreateCommit decorator

    • AfterDestroyCommit decorator

      class User {
      @deco.AfterDestroyCommit()
      public doSomething() {
      console.log('hi!')
      }
      }

      Parameters

      Returns any

      The AfterDestroyCommit decorator

    • AfterSave decorator

      class User {
      @deco.AfterSave()
      public doSomething() {
      console.log('hi!')
      }
      }

      Parameters

      Returns any

      The AfterSave decorator

    • AfterSaveCommit decorator

      class User {
      @deco.AfterSaveCommit()
      public doSomething() {
      console.log('hi!')
      }
      }

      Parameters

      Returns any

      The AfterSaveCommit decorator

    • AfterUpdate decorator

      class User {
      @deco.AfterUpdate()
      public doSomething() {
      console.log('hi!')
      }
      }

      Parameters

      Returns any

      The AfterUpdate decorator

    • AfterUpdateCommit decorator

      class User {
      @deco.AfterUpdateCommit()
      public doSomething() {
      console.log('hi!')
      }
      }

      Parameters

      Returns any

      The AfterUpdateCommit decorator

    • BeforeCreate decorator

      class User {
      @deco.BeforeCreate()
      public doSomething() {
      console.log('hi!')
      }
      }

      Parameters

      Returns any

      The BeforeCreate decorator

    • BeforeSave decorator

      class User {
      @deco.BeforeSave()
      public doSomething() {
      console.log('hi!')
      }
      }

      Parameters

      Returns any

      The BeforeSave decorator

    • BeforeUpdate decorator

      class User {
      @deco.BeforeUpdate()
      public doSomething() {
      console.log('hi!')
      }
      }

      Parameters

      Returns any

      The BeforeUpdate decorator

    • The Encrypted decorator automatically encrypts (upon setting) and decrypts (upon getting) so that the encrypted value is stored in the database.

      class User {
      @deco.Encrypted()
      // automatically sets `encryptedSsn` to the encrypted value that
      // `ssn` is set to in new/create/update, e.g., `await user.update({ ssn })`
      public ssn: string

      // automatically sets `myEncryptedPhone` to the encrypted value that
      // `phone` is set to new/create/update, e.g., `await user.update({ phone })`
      @deco.Encrypted('myEncryptedPhone)
      public phone: string
      }

      Parameters

      • this: Decorators<TD>
      • Optionalcolumn: keyof T["DB"][T["table"] & keyof T["DB"]] & string

        — if omitted, then 'encrypted' is prepended to the Pascal cased version of the decorated field

      Returns any

      An Encrypted decorator

    • The Scope decorator decorates a static method that accepts and returns a Dream Query.

      class Collar {
      @deco.Scope({ default: true })
      public static hideHiddenCollars(query: Query<Collar>) {
      return query.where({ hidden: false })
      }
      }

      Parameters

      • this: Decorators<TD>
      • opts: { default?: boolean } = {}

        — optional options

        • Optionaldefault?: boolean

          If true, this scope will be applied automatically to all queries involving this model. Defaults to false.

      Returns any

      A Scope decorator

    • The Sortable decorator automatically adjusts the value of the columns corresponding to the decorated field.

      NOTE: the Sortable decorator may not be used in STI child models (it may be used in the STI base class)

      class Balloon {
      @deco.Sortable({ scope: 'user' })
      public position: DreamColumn<Balloon, 'position'>
      }

      Parameters

      • this: Decorators<TD>
      • Optionalopts: SortableOptions<T>

        Configuration options for the sortable decorator

        • scope

          The column, association, or combination thereof which you would like to restrict the incrementing logic to. Can be a single column name, a single belongs-to association name, or an array of column/association names

      Returns any

      A Sortable decorator

    • The Validate decorator decorates a method to run before saving a model to the database.

      class Sandbag {
      @deco.Validate()
      public validateWeight(this: Sandbag) {
      if (!this.weight) return

      const undefinedOrNull: any[] = [undefined, null]
      if (!undefinedOrNull.includes(this.weightKgs))
      this.addError('weight', 'cannot include weightKgs AND weight')
      if (!undefinedOrNull.includes(this.weightTons))
      this.addError('weight', 'cannot include weightTons AND weight')
      }
      }

      Parameters

      Returns any

      A Validate decorator

    • The Validates decorator decorates a field to validate according to the specified validator and options.

      class Balloon {
      @deco.Validates('numericality', { min: 0, max: 100 })
      public volume: DreamColumn<Balloon, 'volume'>
      }

      Type Parameters

      • VT extends ValidationType
      • VTArgs extends string | RegExp | { max?: number; min?: number } | { max?: number; min: number }

      Parameters

      • this: Decorators<TD>
      • type: VT

        — the type of validation

      • Optionalargs: VTArgs

        — optional arguments specific to the type of validation

      Returns any

      A Validates decorator

    • The Virtual decorator enables setting of fields as if they corresponded to columns in the model's table so they can be passed to new, create, and update.

      For example, in the first example, below, one could call await bodyMeasurement.update({ lbs 180.1 }), and 180.1 will be passed into the lbs setter, which then translates lbs to grams to be stored in the grams column in the metrics table.

      And in the second example, below, one could call await user.update({ password }), and, in the BeforeSave lifecycle hook, the password would be hashed into hashedPassword. (This is just an example to illustrate using the Virtual decorator on a simple field; it might be better design to use the getter/setter pattern for password, with the getter simply returning undefined.)

      class BodyMeasurement {
      @deco.Virtual('number')
      public get lbs() {
      const self: User = this
      return gramsToLbs(self.getAttribute('grams') ?? 0)
      }

      public set lbs(lbs: number) {
      const self: User = this
      self.setAttribute('grams', lbsToGrams(lbs))
      }

      @deco.Virtual('number')
      public get kilograms() {
      const self: User = this
      return gramsToKilograms(self.getAttribute('grams') ?? 0)
      }

      public set kilograms(kg: number) {
      const self: User = this
      self.setAttribute('grams', kilogramsToGrams(kg))
      }
      }
      class User {
      @deco.Virtual('string')
      public password: string

      @deco.BeforeSave()
      public hasPassword() {
      this.setAttribute('hashedPassword', preferredHashingAlgorithm(this.password))
      }
      }

      Parameters

      Returns any

      A Virtual decorator