Function Flatten

  • @Flatten decorator defines a relation and extract a single property from the nested relation. The decorator 'flatten' the structure during the reading phase and reconstruct it during the writing phase.

    Type Parameters

    • This extends object

      The type of the class the decorator is applied to.

    • Relation

      The relation class type.

    • Value

      The type of the decorated property.

    • Args extends string

    Parameters

    • relation: new (args?: any) => Relation

      The relation type that contains the nested property.

    • property: keyof Relation

      The property inside the relation that should be extracted when reading and re-encapsulated when writing.

    • Optionalargs: RelationParameters<This, Args>

    Returns DecoratorType<This, Value>

    The property decorator function.

    In the following example @Flatten is used to create array of string while also applying operation to that inner in string in the ProtocolString definition. The ProtocolString parse null terminated strings but ensure the size of a string block is always of 64 bytes.

    class ProtocolString {
    @EnsureSize(64)
    @NullTerminatedString()
    data: string
    }

    class Protocol {
    @Until(EOF)
    @Flatten(ProtocolString, 'data')
    strings: string[]
    }