'Typescript - Casting object that I receive from dynamo doesn't save the custom property

When I do query to dynamo db it returns object with all correct fields:

  result.Item = {
    refId: 'nameA', 
    count: 3,
    Connection: { 
       Managed: true, 
       Name: 'connection-app' 
    }
  }

CustomerGroup.ts:

@table(CUSTOMER_GROUP_TABLE)
export class CustomerGroup {
    @hashKey()
      ReferenceID: string = '';

    @attribute()
      Name: string = '';

    get getConnection() {
      if (this.Connection) {
        return this.Connection;
      }
      // some more logic to build connection string
    }
}

GetCustomerGroup.ts:

export class GetCustomerGroup {
  async invoke(_, args, context: AppContext) {
    if (!isNil(args.ReferenceID)) {
      try {
        const result = await context.getAwsFactory().getDynamoDocClient().get({
          TableName: CUSTOMER_GROUP_TABLE,
          Key: {
            ReferenceID: args.ReferenceID,
          },
        }).promise();
  
        return result.Item as CustomerGroup;
      } catch (err) {
        console.log(err);
      }
    }
    return null;
  }
}

When I try to access that property in a code like this const connection = group.getConnection.Name; I get a runtime error:

TypeError: Cannot read property 'Name' of undefined

How can I make sure that property of that class works after casting?



Solution 1:[1]

the invoke method should use predicate and getObjectManager and it looks like this:

async invoke(_, args, context: AppContext) {
    const predicate = construct<CustomerGroup>(CustomerGroup, { ReferenceID: args.ReferenceID });
    const customerGroup = await context.getObjectManager().get<CustomerGroup>(predicate);
    if (customerGroup === null) {
      return null;
    }
    return customerGroup;
 }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrei