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

Improve typing for update operation #464

Open
LouisPinsard opened this issue Apr 6, 2023 · 1 comment
Open

Improve typing for update operation #464

LouisPinsard opened this issue Apr 6, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@LouisPinsard
Copy link

Hello !

When doing an update operation it would be awesome to be forced to fill the fields that are used in the key attributes.
For instance given this schema for a User entity:

{
            pk: {type: 'string', value: '${_type}#'},
            sk: {type: 'string', value: '${_type}#${id}'},

            gs1pk: {type: 'string', value: '${_type}#'},
            gs1sk: {type: 'string', value: '${_type}#${id}'},
            renderConfiguration: {
                type: Object,
                schema: {
                    pages: {
                        type: Array,
                        items: {type: String},
                    },
                } as const,
            },
            name: {type: 'string'},
            email: {type: 'string'},
            id: {type: 'string', generate: 'ulid'},
        }

The update typing on the User model should force me to fill the id attribute.

It could be achived with something like this:

export type Templated<Template> =
  Template extends `${string}$\{${infer PartA}}${infer PartB}`
    ? PartA | Templated<PartB>
    : never;

export type ExtractCompositeKeyAttribute<T extends OneModel> =
  | Templated<T['PK']['value']>
  | Templated<T['SK']['value']>;

export type AttributeForUpdate<T extends OneModel> = AttributeForGet<T> &
  Partial<Entity<T>>;

What do you think? Happy to try a PR if it seems right!

@mobsense
Copy link
Contributor

mobsense commented Apr 7, 2023

Would be awesome if you could do a PR.

Be aware, that the TS magic is pretty fragile and you need to really test that we're not breaking other typings.

That being said, .... have at it.

Michael

@mobsense mobsense added the enhancement New feature or request label Apr 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants