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

Feature: Add types in batch get

Open mobsense opened this issue 3 years ago • 13 comments

Discussed in https://github.com/sensedeep/dynamodb-onetable/discussions/337

Originally posted by ShivamJoker May 21, 2022 It would be great to get typescript types in the batchGet response as well.

Right now it returns an array of unknown which is not helpful at all.

image

mobsense avatar Jul 01 '22 09:07 mobsense

I believe I have this one sorted as well. I will open a PR after #369 cause I need those changes

loetjvr avatar Jul 23 '22 13:07 loetjvr

Great! Thank you.

mobsense avatar Jul 24 '22 02:07 mobsense

One note on this typing, I've found that batchGet returns a Responses key normally, but when parse: true is set as an option, it returns an array of initialized models. Its a little confusing that setting an option changes the response type so drastically.

sdemjanenko avatar Sep 19 '22 16:09 sdemjanenko

Thanks for raising the issue.

That is the case for all APIs. Parse has a big impact.

When you use parse() which is the default for the low level APIs, the returned data is parsed as models.

If not, then it is useful to get the raw responses.

Can you point out where in the doc you found this confusing and we can clarify.

mobsense avatar Sep 20 '22 05:09 mobsense

is there any update on above issue?

martinmicunda avatar Dec 01 '23 13:12 martinmicunda

Sorry, nothing yet. Do you want to try to propose a solution.

mobsense avatar Dec 06 '23 22:12 mobsense

@mobsense one solution would be to use Generic type:

batchGet<T = {}>(batch: any, params?: OneParams): Promise<T[]>

so I could use:

const users = await this.table.batchGet<User>(params.batch, params)

martinmicunda avatar Dec 07 '23 20:12 martinmicunda

Sorry for the slow answer.

But batchGet is typically used to fetch multiple different types. I suppose you could provide a union as the generic type: e.g. User | Account | Item ....

If you just want to fetch users, then find() would be a better choice.

mobsense avatar Jan 07 '24 22:01 mobsense

The downside of that change to use a generic type would be that it would break existing TS code would it not?

mobsense avatar Jan 07 '24 22:01 mobsense

@mobsense

I have use case sometimes where I need to use batchGet instead of find e.g. to get multiple users by ids.

Passing User | Account | Item is fine because we would have generic function

Sorry for the slow answer.

But batchGet is typically used to fetch multiple different types. I suppose you could provide a union as the generic type: e.g. User | Account | Item ....

If you just want to fetch users, then find() would be a better choice.

martinmicunda avatar Jan 08 '24 17:01 martinmicunda

The downside of that change to use a generic type would be that it would break existing TS code would it not?

it shouldn't (depends on the ts-config) as generic type would be optional because of default initialisation <T = {}>

batchGet<T = {}>(batch: any, params?: OneParams): Promise<T[]>

use type

const users = await this.table.batchGet<User>(params.batch, params)

or without type

const users = await this.table.batchGet(params.batch, params)

martinmicunda avatar Jan 08 '24 17:01 martinmicunda

We'll commit that change. If you and other could test and if okay, we'll add to the next point release.

mobsense avatar Feb 06 '24 01:02 mobsense

@mobsense yeah I can test it :)

martinmicunda avatar Feb 06 '24 14:02 martinmicunda