dynamodb-toolbox icon indicating copy to clipboard operation
dynamodb-toolbox copied to clipboard

Entity update requires full object and complains about partial updates

Open ahmad-ali14 opened this issue 1 year ago • 1 comments

version 0.3.5

  • in the previous version, this worked fine:
  await UserEntity.update({
    id: user.id,
    postIds: updatedPostIds,
  });

version 0.4.3

  • typescript complains about the above operation and requires the whole user object.
  • so the previous operation needs to be transformed as such to suppress the typescript error:
  await UserEntity.update({
    ...user,
    postIds: updatedPostIds,
  });

ahmad-ali14 avatar Aug 06 '22 09:08 ahmad-ali14

Hey @ahmad-ali14, can you please share your schemas?

naorpeled avatar Aug 06 '22 10:08 naorpeled

@naorpeled thank you for your answer; I can not reveal the schema, but here is an example it is very basic, nothing fancy :)

  • table definition:
import { Table, Entity } from "dynamodb-toolbox";
import DynamoDB from "aws-sdk/clients/dynamodb";

export const DocumentClient = new DynamoDB.DocumentClient();
const tablePrefix = "table_";

type UserT = {
id: string;
craetedAt: string;
updatedAt: string;
postIds?: string;
}

export const UsersTable = new Table({
  name: `${tablePrefix}-Users`,
  partitionKey: "id",
  DocumentClient,
  entityField: "table",
});
  • entity definition:
export const UserEntity = new Entity<UserT>({
  name: "Users",
  table: UsersTable,
  timestamps: true,
  attributes: {
    id: {
      partitionKey: true,
      type: "string",
    },
    postIds: { type: "string" },
    createdAt: {
      type: "string",
      dependsOn: ["created"],
      default: (data) => `${data.created}`,
    },
    updatedAt: {
      type: "string",
      dependsOn: ["modified"],
      default: (data) => `${data.modified}`,
      onUpdate: true,
    },
  },
});

  • using 0.3.5 (always works)
  await UserEntity.update({
    id: user.id,
    postIds: updatedPostIds,
  });
  • using 0.4.3 (error: id, postId can not be assigned to type UserT, createdAt, updatedAt required )
  await UserEntity.update({
    id: user.id,
    postIds: updatedPostIds,
  });

ahmad-ali14 avatar Aug 24 '22 18:08 ahmad-ali14

Hey @ahmad-ali14, hope you're doing well 😎

You no longer need to provide the generic parameter when creating a new entity in v0.4 of the library for type definitions, now they are included out of the box. A lot of the type inference logic was changed since v0.35.

Try to remove it and let me know if you're still encountering issues :)

naorpeled avatar Aug 24 '22 19:08 naorpeled

@naorpeled Thank you very much; one last thing

  • I have this entity, and I want to use the type inferred by its schema everywhere in the App.
export const propertiesEntity = new Entity({
  name: 'Property',
  table: propertiesTable,
  timestamps: true,
  attributes: {
    id: { partitionKey: true, type: 'string' },
    companyId: { type: 'string', sortKey: true },
    name: { type: 'string' },
    //TODO: ...add rest of attributes
  },
} as const);
  • here are some interesting findings.
const  T = propertiesEntity._typesOnly._entityItemOverlay // undefined since I don't supply an Overlay
  • using EntityItem (give undefined, since it is looking for the Overlay that I don't supply).
export type PropertyTTT = EntityItem<typeof propertiesEntity>;

Screen Shot 2022-08-25 at 08 06 13

  • using InferEntityItem
export type PropertyTT = InferEntityItem<typeof propertiesEntity>;

Screen Shot 2022-08-25 at 08 09 17

  • unfortunately, InferEntityItem is not exported and I can't use it except through EntityItem which does not help in this case

ahmad-ali14 avatar Aug 25 '22 07:08 ahmad-ali14

the definition of InferIdentityItem is not exported from Entity class for some reason in the build

Screen Shot 2022-08-25 at 08 21 39

Must be something I'm missing 😅

ahmad-ali14 avatar Aug 25 '22 07:08 ahmad-ali14

@naorpeled just used https://www.npmjs.com/package/dynamodb-toolbox/v/0.5.0-beta.0 and looks all fine, thank you very much

I'm using this import

import type { EntityItem, InferEntityItem } from 'dynamodb-toolbox/dist/classes/Entity/types';

ahmad-ali14 avatar Aug 25 '22 07:08 ahmad-ali14

@ahmad-ali14 I'll create a PR that exports these types so you won't need to use imports from the dist explicitly :)

naorpeled avatar Aug 25 '22 08:08 naorpeled