Skip to content

Commit 3ffa2ce

Browse files
committed
make .toJs() stable. rethrow caught errors
1.6.0
1 parent a76b427 commit 3ffa2ce

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

dynadash.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export async function getTableRow<R>(
146146
TableName: GetItemInput['TableName'],
147147
keys: { [x: string]: any },
148148
params?: Omit<GetItemInput, 'TableName' | 'Key'> & { projection?: string[] },
149-
): Promise<(GetItemCommandOutput & { toJs: () => R | null }) | void | null> {
149+
): Promise<GetItemCommandOutput & { toJs: () => R | null }> {
150150
const { projection, ...rest } = params || {};
151151
const client = getDdbClient();
152152
try {
@@ -161,7 +161,7 @@ export async function getTableRow<R>(
161161
} catch (e) {
162162
consoleError(e);
163163
consoleError({ TableName, keys, params });
164-
return null;
164+
throw e;
165165
}
166166
}
167167

@@ -210,11 +210,12 @@ async function batchWriteTable(
210210
/**
211211
* @param request
212212
*/
213+
213214
function getBatchWriteRequest(request: 'PutRequest' | 'DeleteRequest') {
214-
return async function <R>(
215+
return async function <R, L extends R>(
215216
TableName: string,
216-
unmarshalledList: any[],
217-
predicate?: (item: any) => R | undefined | Promise<R | undefined>,
217+
unmarshalledList: L[],
218+
predicate?: <L>(item: L) => R | undefined | Promise<R | undefined>,
218219
): Promise<{ results: (BatchWriteItemCommandOutput | null)[]; actualList: R[] } | void> {
219220
const results = [];
220221
const actualList: R[] = [];
@@ -231,12 +232,12 @@ function getBatchWriteRequest(request: 'PutRequest' | 'DeleteRequest') {
231232
const requests: WriteRequest[] = [];
232233
for (const item of chunk) {
233234
try {
234-
let row = item;
235+
let row: R = item as unknown as R;
235236
if (predicate) {
236237
if (predicate.constructor.name === 'AsyncFunction') {
237238
row = await (predicate(item) as Promise<R>);
238239
} else {
239-
row = predicate(item);
240+
row = predicate(item) as R;
240241
}
241242
}
242243
// skip to next if row is falsey
@@ -456,7 +457,7 @@ async function handleQueryCommand<R>(query: QueryCommandInput) {
456457
} catch (e) {
457458
consoleError(e);
458459
consoleError({ query });
459-
return null;
460+
throw e;
460461
}
461462
}
462463

@@ -524,7 +525,7 @@ export async function updateTableRow<R>(
524525
ConditionExpression?: string;
525526
},
526527
ReturnValues = 'ALL_NEW',
527-
): Promise<(UpdateItemCommandOutput & { toJs: (predicate?: (row: R) => R) => R }) | null> {
528+
): Promise<UpdateItemCommandOutput & { toJs: (iterator?: (row: R) => R) => R }> {
528529
const { UpdateExpression, expressionAttributeValues, ExpressionAttributeNames } = params;
529530
const client = getDdbClient();
530531
try {
@@ -541,9 +542,9 @@ export async function updateTableRow<R>(
541542
const result = await client.send(new UpdateItemCommand(query));
542543
return {
543544
...result,
544-
toJs: (predicate) => {
545+
toJs: (iterator) => {
545546
const item = (result.Attributes ? unmarshall(result.Attributes) : {}) as R;
546-
return predicate ? predicate(item) : item;
547+
return iterator ? iterator(item) : item;
547548
},
548549
};
549550
} catch (e) {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dynadash",
3-
"version": "1.5.23",
3+
"version": "1.6.0",
44
"description": "DynamoDb helpers",
55
"main": "dist/dynadash.js",
66
"typings": "dist/types/dynadash.d.ts",

0 commit comments

Comments
 (0)