dynamodb-toolbox
dynamodb-toolbox copied to clipboard
Entity update requires full object and complains about partial updates
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,
});
Hey @ahmad-ali14, can you please share your schemas?
@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,
});
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 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>;
- using
InferEntityItem
export type PropertyTT = InferEntityItem<typeof propertiesEntity>;
- unfortunately,
InferEntityItem
is not exported and I can't use it except throughEntityItem
which does not help in this case
the definition of InferIdentityItem
is not exported from Entity class for some reason in the build
Must be something I'm missing 😅
@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 I'll create a PR that exports these types so you won't need to use imports from the dist explicitly :)