You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
I don't really understand why, in particular, the BatchWriteRequest.put() method requires passing in the modelClazz argument before the array of models.
I would like to be able to pass in an array of mixed Model types, and have the class correctly determine where to write each one.
Describe the solution you'd like
It is quite straightforward to find the constructor from a JS object, using obj.constructor. Therefore, internally, the BatchWriteRequest class can determine the constructor object upon which to read metadata about the Model.
I've tested this alternative interface, and it works as such:
import*asDEfrom'@shiftcoders/dynamo-easy';// eslint-disable-next-line @typescript-eslint/ban-ts-comment// @ts-ignoreclassAutoBatchWriteextendsDE.BatchWriteRequest{protecteditemCount=0;putMixed(models: unknown[]){if(this.itemCount+models.length>DE.BATCH_WRITE_MAX_REQUEST_ITEM_COUNT){thrownewError(`batch write takes at max ${DE.BATCH_WRITE_MAX_REQUEST_ITEM_COUNT} items`);}for(constmodelofmodels){constmeta=Reflect.getMetadata('sc-reflect:model',model.constructor);// eslint-disable-next-line @typescript-eslint/ban-ts-comment// @ts-ignoreconstitem={PutRequest: {Item: DE.toDb(model,model.constructor)}};consttableName=meta.tableName;this.params.RequestItems[tableName]=this.params.RequestItems[tableName]||[];this.params.RequestItems[tableName].push(item);}this.itemCount+=models.length;returnthis;}}
Note that the ts/eslint comments are required here for this proof because:
a) the properties required to make this work are private inside BatchWriteRequest - this would be moot if this implementation was in BatchWriteRequest to start with
b) model.constructor for T = unknown type doesn't match ModelConstructor<T> - I'm not completely sure about the solution for this one. Particularly because there's no such thing as a base model type, and the type of object in the models array can be mixed.
Additional context
That's about it... I love this library, by the way. Even if this improvement is not considered, I can still make good use of it :)
Regards,
Doug.
The text was updated successfully, but these errors were encountered:
Is your feature request related to a problem? Please describe.
I don't really understand why, in particular, the BatchWriteRequest.put() method requires passing in the modelClazz argument before the array of models.
I would like to be able to pass in an array of mixed Model types, and have the class correctly determine where to write each one.
Describe the solution you'd like
It is quite straightforward to find the constructor from a JS object, using
obj.constructor
. Therefore, internally, the BatchWriteRequest class can determine the constructor object upon which to read metadata about the Model.I've tested this alternative interface, and it works as such:
Note that the ts/eslint comments are required here for this proof because:
a) the properties required to make this work are private inside BatchWriteRequest - this would be moot if this implementation was in BatchWriteRequest to start with
b)
model.constructor
forT = unknown
type doesn't matchModelConstructor<T>
- I'm not completely sure about the solution for this one. Particularly because there's no such thing as a base model type, and the type of object in the models array can be mixed.Additional context
That's about it... I love this library, by the way. Even if this improvement is not considered, I can still make good use of it :)
Regards,
Doug.
The text was updated successfully, but these errors were encountered: