How to increment the number in map by add #214
Answered
by
tywalch
Derek-X-Wang
asked this question in
Q&A
-
Hi @tywalch, Is there any way to increment the number inside of a map object? For example export const UserEntity = new Entity(
{
model: {
version: "1",
entity: "User",
service: "Demo",
},
attributes: {
userId: {
type: "string",
required: true,
},
statistics: {
type: "map",
properties: {
storeCount: {
type: "number",
},
orderCount: {
type: "number",
},
},
},
createdAt: {
type: "number",
default: () => Date.now(),
// cannot be modified after created
readOnly: true
},
updatedAt: {
type: "number",
// watch for changes to any attribute
watch: "*",
// set current timestamp when updated
set: () => Date.now(),
readOnly: true
}
},
indexes: {
user: {
collection: "user",
pk: {
field: "pk",
composite: ["userId"],
},
sk: {
field: "sk",
composite: [],
},
},
},
},
Dynamo.Configuration
); and I want to increment await UserEntity.update({
userId,
})
.add({
"statistics.storeCount": by, // something like this. not sure if electrodb has this feature.
})
.go(); |
Beta Was this translation helpful? Give feedback.
Answered by
tywalch
Feb 4, 2023
Replies: 1 comment 1 reply
-
You can use the UserEntity.update({ userId })
.data((attr, op) => {
op.add(attr.statistics.storeCount, 1)
})
.go(); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Derek-X-Wang
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use the
.data()
method for this:Full example in playground