idb-ts
    Preparing search index...

    Function Calculated

    • Property decorator that derives the decorated field's value from the rest of the entity on every write.

      The compute function runs on each EntityRepository.create and EntityRepository.update before validation and timestamps, so:

      • any value assigned to the field by the caller is overwritten,
      • @Validate rules on the same field see the computed value,
      • the computed value is persisted and therefore works with indexes and the query builder like any ordinary field.

      Because the value is computed at write time, it reflects the entity state as of the last write - change an input field and the calculated field refreshes on the next update.

      Type Parameters

      • T = any

        The entity class the field belongs to.

      Parameters

      • compute: (item: T) => any

        Function deriving the field value from the entity.

      Returns PropertyDecorator

      @DataClass()
      class OrderLine {
      @KeyPath({ generator: 'uuid' })
      id!: string;

      quantity!: number;
      unitPrice!: number;

      @Calculated<OrderLine>((line) => line.quantity * line.unitPrice)
      total!: number;
      }