我目前正在与dynamodb-data-mapper
和dynamodb-data-mapper-annotation
作斗争。
我有一个对应于表的类,每次保存对象时都必须重新计算一些属性。
我已经能够将marshall
和unmarshall
用于updatedAt
属性,因为它完全独立,但当我需要它们之间的交互时,我找不到任何成功的解决方法。
我希望这是一个完全内部的东西,一个我必须调用的方法(更好地避免以后出错(。
你能找到解决方案吗?
@table('my-class-table')
export class MyClass {
@autoGeneratedHashKey()
readonly id: string;
@attribute({ defaultProvider: () => new Date() })
readonly createdAt: Date;
@attribute({
type: 'Custom',
defaultProvider: () => new Date(),
marshall: (): AttributeValue => ({ S: new Date().toISOString() }),
unmarshall: (persistedValue: AttributeValue): Date => new Date(persistedValue.S!),
})
readonly updatedAt: Date = new Date();
@attribute()
alerts: VehicleAlert[];
// This attribute depends on the alerts attribute
@attribute()
status: string;
}
我不确定你的确切用例(也许你已经弄清楚了(,但我通过使用getter解决了类似的问题。对于您的更新示例:
class MyClass {
get updatedAt() {
return new Date().toISOString();
}
}
每次保存文档时,数据映射器都会访问该属性,并使用从getter获得的值更新数据库中的记录。