awslabs / dynamodb-data-mapper-js

A schema-based data mapper for Amazon DynamoDB.
https://awslabs.github.io/dynamodb-data-mapper-js/
Apache License 2.0
816 stars 106 forks source link

Map always returns empty #192

Open fredspivock opened 4 years ago

fredspivock commented 4 years ago

class ViewedModuleItem { @attribute() completed?: boolean;

@attribute()
vidPosition?: number;

}

@table(TableName) export class ViewedModuleItems { @hashKey() partitionKey: string;

@rangeKey()
context: string;

@attribute()
courseId: string;

@attribute()
schoolId: string;

@attribute()
licenseId: string;

@attribute()
userId: string;

@attribute({memberType: embed(ViewedModuleItem)})
viewedModuleItems: Map<string, ViewedModuleItem>;

@attribute()
continueFromModuleItem?: string;

} The Map "viewedModuleItems" is in the return object but is always empty, not sure why. Anyone else got this issue? This is what is stored in DDB:

{ "context": "VIEWED_MODULE_ITEMS_e4e721a3-b440-58c3-a275-8057ba48ff2a_1592102815669", "continueFromModuleItem": "dd982796-e226-4f09-aff3-adbd8b7478ba", "courseId": "e4e721a3-b440-58c3-a275-8057ba48ff2a", "licenseId": "e4e721a3-b440-58c3-a275-8057ba48ff2a_1592102815669", "partitionKey": "dc7c874a-374a-40ec-8e79-50ed3fa17ce7_USER_d1c3da77-e9ee-42c7-9931-8d0c62559891", "schoolId": "dc7c874a-374a-40ec-8e79-50ed3fa17ce7", "updatedOn": 1592445746508, "userId": "d1c3da77-e9ee-42c7-9931-8d0c62559891", "viewedModuleItems": { "66a57a14-8977-4562-a14c-65a771e28451": { "completed": true }, "e02ddc1f-2d06-43fd-816f-236c275bd1d8": { "completed": true } } }

stvngrsh commented 4 years ago

Are you seeing an empty object {} in your API response for the map field? It's probably just b/c a Map in JS is not technically an Object. I ran into this problem when Express returned my data, it calls JSON.stringify() on the object which just turns any Map into an empty Object. You'll have to write custom logic to convert the Map data structure to a js object.

fredspivock commented 4 years ago

Are you seeing an empty object {} in your API response for the map field? It's probably just b/c a Map in JS is not technically an Object. I ran into this problem when Express returned my data, it calls JSON.stringify() on the object which just turns any Map into an empty Object. You'll have to write custom logic to convert the Map data structure to a js object.

This might have been the problem. I switched to Dynamo-Easy. Was a lot simpler to use. Thanks for your answer.

stvngrsh commented 4 years ago

Actually, was playing around with this and realized you can just not use the Map data structure in your TypeScript and dynamo still handles it the same way.

I simply went from:

@attribute({memberType: embed(SomeObj)}
someObj!: Map<string, SomeObj>;

To:

@attribute({memberType: embed(SomeObj)}
someObj!: { [key: string]: SomeObj};

Now the data is stringified by express as an object, but dynamo still stores it as a map in the platform. The takeaway here is Dynamo treats JS Maps & Objects as both Maps in Dynamo.