Options
All
  • Public
  • Public/Protected
  • All
Menu

Module "src/replica/Entity"

Index

Classes

Variables

Functions

Variables

Const field

field: typeof generic_field = generic_field

Field decorator. The type signature is:

field : <T extends typeof Field = typeof Field> (fieldConfig? : Partial<InstanceType<T>>, fieldCls : T | typeof Field = Field) => PropertyDecorator

Its a function, that accepts field config object and optionally a field class (default is Field) and returns a property decorator.

Example:

const ignoreCaseCompare = (a : string, b : string) : boolean => a.toUpperCase() === b.toUpperCase()

class MyField extends Field {}

class Author extends Entity.mix(Base) {
    @field({ equality : ignoreCaseCompare })
    firstName       : string

    @field({ lazy : true }, MyField)
    lastName       : string
}

For every field, there are generated get and set accessors, with which you can read/write the data:

const author     = Author.new({ firstName : 'Mark' })

author.firstName // Mark
author.lastName  = 'Twain'

The getters are basically using Replica.get and setters Replica.write.

Note, that if the identifier is asynchronous, reading from it will return a promise. But, immediately after the Replica.commit call, getter will return plain value. This is a compromise between the convenience and correctness and this behavior may change (or made configurable) in the future.

Additionally to the accessors, the getter and setter methods are generated. The getter method's name is formed as get followed by the field name with upper-cased first letter. The setter's name is formed in the same way, with set prefix.

The getter method is an exact equivalent of the get accessor. The setter method, in addition to data write, immediately after that performs a call to Replica.commit (or Replica.commitAsync, depending from the Replica.autoCommitMode option) and return its result.

const author     = Author.new({ firstName : 'Mark' })

author.getFirstName() // Mark
await author.setLastName('Twain') // issues asynchronous commit

Functions

Const calculate

  • calculate(fieldName: Name): MethodDecorator
  • Decorator for the method, that calculates a value of some field

    
    @entity()
    class Author extends Entity.mix(Base) {
        @field()
        firstName       : string
        @field()
        lastName        : string
        @field()
        fullName        : string
    
        @calculate('fullName')
        calculateFullName () : string {
            return this.firstName + ' ' + this.lastName
        }
    }

    Parameters

    • fieldName: Name

      The name of the field the decorated method should be "tied" to.

    Returns MethodDecorator

Generated using TypeDoc