-
Hi, I'm pretty sure this problem is not related to ElectroDB, but still maybe someone can point me in the right direction.
I want to create an empty map for import { CustomAttributeType, Entity, Service } from "electrodb";
export const appEntity = new Entity(
{
model: {
entity: 'app',
version: '1',
service: 'chat',
},
attributes: {
appId: {
type: 'string',
required: true,
readOnly: true,
},
states: {
type: CustomAttributeType<{ [state: string]: boolean }>('any'),
required: false,
},
},
indexes: {
primary: {
scope: 'app',
pk: {
field: 'pk',
composite: [],
},
sk: {
field: 'sk',
composite: ['appId'],
},
},
},
},
{ table: 'electro' },
);
// const app = await appEntity
// .create({
// appId: '123',
// })
// .go();
const app = await appEntity
.patch({
appId: '123',
})
.data((attr, op) => {
op.ifNotExists(attr.states, {});
op.set(attr.states.APP_INSTALLED, true);
op.remove(attr.states.APP_NOT_INSTALLED);
})
.go();
console.log(app); That's the created update expression: {
"UpdateExpression": "SET #states = if_not_exists(#states, :states_u0), #states.#APP_INSTALLED = :states_u1, #appId = :appId_u0, #__edb_e__ = :__edb_e___u0, #__edb_v__ = :__edb_v___u0 REMOVE #states.#APP_NOT_INSTALLED",
"ExpressionAttributeNames": {
"#pk": "pk",
"#sk": "sk",
"#states": "states",
"#APP_INSTALLED": "APP_INSTALLED",
"#APP_NOT_INSTALLED": "APP_NOT_INSTALLED",
"#appId": "appId",
"#__edb_e__": "__edb_e__",
"#__edb_v__": "__edb_v__"
},
"ExpressionAttributeValues": {
":states_u0": {},
":states_u1": true,
":appId_u0": "123",
":__edb_e___u0": "app",
":__edb_v___u0": "1"
},
"TableName": "electro",
"Key": {
"pk": "$chat_app",
"sk": "$app_1#appid_123"
},
"ConditionExpression": "attribute_exists(#pk) AND attribute_exists(#sk)"
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @zirkelc Yeah, this is a DynamoDB constraint 👎 The overlap is |
Beta Was this translation helpful? Give feedback.
Hey @zirkelc
Yeah, this is a DynamoDB constraint 👎 The overlap is
op.ifNotExists(attr.states, {});
As written, it performs multiple operations on the map object: setting the map itself and adding/removing properties to it. You could use adefault
to set an empty map on item creation and omit theifNotExists
operation, something like this.