Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing add Method in Upsert for Transactional Writes #301

Closed
misterjoshua opened this issue Sep 25, 2023 · 2 comments · Fixed by #305
Closed

Missing add Method in Upsert for Transactional Writes #301

misterjoshua opened this issue Sep 25, 2023 · 2 comments · Fixed by #305

Comments

@misterjoshua
Copy link

misterjoshua commented Sep 25, 2023

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!

@misterjoshua misterjoshua changed the title Missing add Method in Transactional Writes Following Update #296 Missing add Method in Upsert for Transactional Writes Following Update #296 Sep 25, 2023
@misterjoshua misterjoshua changed the title Missing add Method in Upsert for Transactional Writes Following Update #296 Missing add Method in Upsert for Transactional Writes Sep 25, 2023
@misterjoshua
Copy link
Author

misterjoshua commented Oct 1, 2023

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"
                }
            }
        }
    ]
}

@tywalch
Copy link
Owner

tywalch commented Oct 2, 2023

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

@tywalch tywalch linked a pull request Oct 2, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants