Updating a map with dynamic properties #455
-
I'm trying to model a relationship between users and shops using maps with dynamic properties. I couldn't really find anything in the docs about maps with arbitrary properties, but I figured it's possible with custom types: type ShopUsers = Record<string, { name: string; role: ShopRole }>;
const ShopEntity = new Entity({
attributes: {
// map of user ids to shops
users: { type: CustomAttributeType<ShopUsers>('any') },
},
});
type UserShops = Record<string, { name: string; role: ShopRole }>;
const UserEntity = new Entity({
attributes: {
// map of shop ids to users
shops: { type: CustomAttributeType<UserShops>('any') },
},
}); I thought this structure would be better than using lists, because it doesn't allow duplicate keys, and should allow me to efficiently update/remove relationships as long as I know the user id / shop id (i.e. during denormalization in a stream consumer). But I'm stuck actually updating/removing properties in these maps: await ShopEntity.patch(key).set({ [`users.${userId}`]: { ... } }).go(); Error: Attribute "users.jsmith" does not exist on model. - For more detail on this error reference: https://electrodb.dev/en/reference/errors/#invalid-attribute await ShopEntity.patch(key).remove([`users.${userId}`]).go(); TS2322: Type Any help would be appreciated! Am I going in a bad direction by using custom attributes instead of lists? Or is there some other syntax to patch dynamic map properties? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I'm AFK at the moment, but checkout the data method, it should allow for operations like the one you're trying to perform 👍 |
Beta Was this translation helpful? Give feedback.
I'm AFK at the moment, but checkout the data method, it should allow for operations like the one you're trying to perform 👍