tywalch/electrodb

Missing `add` Method in Upsert for Transactional Writes

misterjoshua opened this issue · 2 comments

Describe the bug
The update methods like add are not available for upsert when using transactional writes for ElectroDB entities. Using these methods results in a TypeScript error.

ElectroDB Version
2.10.0

ElectroDB Playground Link
ElectroDB Playground

Entity/Service Definitions

/* Model queries, see results, share with friends */

import { Entity, Service } from "electrodb";

const table = "your_table_name";

/* Tasks Entity */
const sum = new Entity(
  {
    model: {
      entity: "sum",
      version: "1",
      service: "someapp"
    },
    attributes: {
      name: {
        type: "string",
        required: true
      },
      value: {
        type: "number",
        required: true,
        default: 1,        
      }
    },
    indexes: {
      main: {
        pk: {
          field: "pk",
          composite: ["name"]
        }
      }
    }
  },
  { table }
);

const service = new Service({ sum });

// Works fine:
sum
    .upsert({ name: 'a' })
    .add({ value: 1 })
    .go();

// Error case:
service.transaction.write((entities) => [
  entities.sum
    .upsert({ name: 'a' })
    .add({ value: 1 }) // add doesn't exist here.
    .commit(),
]);

Expected behavior
After #296, I expected to be able to use update methods for transactional writes just like when using upsert on an entity directly.

Errors
Property 'add' does not exist on type 'UpsertRecordOperationOptionsTransaction<string, string, string, { model: { entity: string; version: string; service: string; }; attributes: { name: { type: "string"; required: true; }; value: { type: "number"; required: true; default: number; }; }; indexes: { ...; }; }, ResponseItem<...>>'.(2339)

Additional context
#296 introduced update methods to upsert.

Thank you!

I was messing around in the playground and I've discovered that the update methods work, but they're missing types.

For example, with the entity shown above, I can do this:

service.transaction.write((entities) => [
  // casting entities as any allows me to access the update methods.
  (entities as any).sum
    .upsert({ name: 'a' })
    .add({ value: 1 })
    .commit(),
]).go();

and here's the transact write, which looks right to me:

{
    "TransactItems": [
        {
            "Update": {
                "TableName": "your_table_name",
                "UpdateExpression": "SET #__edb_e__ = :__edb_e___u0, #__edb_v__ = :__edb_v___u0, #name = :name_u0 ADD #value :value_u0",
                "ExpressionAttributeNames": {
                    "#__edb_e__": "__edb_e__",
                    "#__edb_v__": "__edb_v__",
                    "#name": "name",
                    "#value": "value"
                },
                "ExpressionAttributeValues": {
                    ":__edb_e___u0": "sum",
                    ":__edb_v___u0": "1",
                    ":name_u0": "a",
                    ":value_u0": 1
                },
                "Key": {
                    "pk": "$someapp$sum_1#name_a"
                }
            }
        }
    ]
}

Thanks for putting this together, I hope to address the typing sometime this week 👍