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

Can I remove (update in general) nested attributes? #358

Open
dls314 opened this issue Sep 1, 2023 · 2 comments
Open

Can I remove (update in general) nested attributes? #358

dls314 opened this issue Sep 1, 2023 · 2 comments

Comments

@dls314
Copy link

dls314 commented Sep 1, 2023

I'm trying to use EntityManager to write an update statement to remove a nested attribute, kind of like the following

import { Attribute, Entity, Table } from '@typedorm/common';
import { createConnection, getEntityManager } from '@typedorm/core';
import AWS from 'aws-sdk';

import DynamoDB = AWS.DynamoDB;

export class MyEntityAttribute {
  public readonly name: string | undefined;
  public readonly value: string | undefined;
}

@Entity<MyEntity>({
  name: 'MyEntity',
  primaryKey: {
    partitionKey: 'MyEntity#{{id}}',
  },
})
export class MyEntity {
  @Attribute()
  public id: string | undefined;

  @Attribute()
  public attribute: MyEntityAttribute | undefined;
}

export async function removeName(id: string): Promise<void> {
  const table = new Table({ name: 'MyTable', partitionKey: 'pk' });

  createConnection({ table: table, entities: [MyEntity], documentClient: new DynamoDB.DocumentClient() });

  const entityManager = getEntityManager();

  await entityManager.update(
    MyEntity,
    { id },
    {
      //
      // This doesn't work, the TSC error is the following
      //

      /*
      Type '{ name: { REMOVE: true; }; }' is not assignable to type '((SetValueType<MyEntity, "attribute"> | (Omit<{ ADD?: InvalidType<["number | any[]", "Update action 'ADD' can not be used for attribute", "attribute"]> | undefined; }, "ADD"> & Required<...> & Partial<...>) | (Omit<...> & ... 1 more ... & Partial<...>) | (Omit<...> & ... 1 more ... & Partial<...>)) & (SetValueType<....'.

      Type '{ name: { REMOVE: true; }; }' is not assignable to type 'Omit<{ DELETE?: InvalidType<[any[], "Update action 'DELETE' can not be used for attribute", "attribute"]> | undefined; }, "DELETE"> & Required<Pick<{ DELETE?: InvalidType<...> | undefined; }, "DELETE">> & Partial<...> & { ...; }'.

      Property 'DELETE' is missing in type '{ name: { REMOVE: true; }; }' but required in type 'Required<Pick<{ DELETE?: InvalidType<[any[], "Update action 'DELETE' can not be used for attribute", "attribute"]> | undefined; }, "DELETE">>'.ts(2322)

      update-body-type.d.ts(56, 5): 'DELETE' is declared here.

      example.ts(23, 5): The expected type comes from property 'attribute' which is declared here on type 'UpdateBody<MyEntity, { attribute: { name: { REMOVE: boolean; }; }; }>'
      */

      attribute: {
        name: { REMOVE: true },
      },
    },
  );
}

I don't think this is supported by TypeDORM at this time, but I hope I've missed something.

@dls314
Copy link
Author

dls314 commented Sep 1, 2023

I was hoping that TypeDORM would generate an update expression using DynamoDB's document path, like https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.Attributes.html#Expressions.Attributes.NestedElements.DocumentPathExamples

For an id like 1, I think the expression would end up as

{
  ExpressionAttributeNames: {
    '#attr0': 'attribute.name',
  },
  Key: {
    pk: 'MyEntity#1',
  },
  ReturnValues: 'ALL_NEW',
  TableName: 'MyTable',
  UpdateExpression:
    'REMOVE #attr0',
}

Or perhaps like this (sorry, my DynamoDB update expressions are weak)

{
  ExpressionAttributeNames: {
    '#attr0': 'attribute',
    '#attr1': 'name',
  },
  Key: {
    pk: 'MyEntity#1',
  },
  ReturnValues: 'ALL_NEW',
  TableName: 'MyTable',
  UpdateExpression:
    'REMOVE #attr0.#attr1',
}

@dls314 dls314 changed the title Can I remove nested attributes? Can I remove (update in general) nested attributes? Sep 5, 2023
@dls314
Copy link
Author

dls314 commented Oct 26, 2023

I'd still love to hear information from the community, it's likely I'm missing something.

But, to update from what I've found.
TypeDORM can't work with nested attributes through entity manager.

I ended up refactoring entities here, to avoid the necessity of working with nested attributes. It's not a great solution in general, I think, but it worked.

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

No branches or pull requests

1 participant