-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start work on partial update builder, adding int tests
- Loading branch information
Showing
6 changed files
with
404 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,77 @@ | ||
export type ExpressionBuilderResult = { | ||
UpdateExpression: string; | ||
ExpressionAttributeNames: any; | ||
ExpressionAttributeValues: any; | ||
updateExpression: string; | ||
expressionAttributeNames: { [key: string]: string }; | ||
expressionAttributeValues: { [key: string]: any }; | ||
}; | ||
export const updateExpressionBuilder = (item: any): ExpressionBuilderResult => ({ | ||
UpdateExpression: internalUpdateBuilder(item), | ||
ExpressionAttributeNames: internalNameBuilder(item), | ||
ExpressionAttributeValues: internalValueBuilder(item), | ||
}); | ||
|
||
const internalUpdateBuilder = (item: any): string => { | ||
const itemKeys = Object.keys(item); | ||
export class UpdateExpressionsBuilder { | ||
private keyName: string; | ||
|
||
return `SET ${itemKeys.map((k, index) => `#field_${index} = :value_${index}`).join(', ')}`; | ||
}; | ||
constructor(keyName: string) { | ||
this.keyName = keyName; | ||
} | ||
|
||
const internalNameBuilder = (item: any): any => { | ||
const itemKeys = Object.keys(item); | ||
buildExpressionNames(item: any): { [key: string]: string } { | ||
const keylessItem = this.removeKey(item); | ||
const itemKeys = Object.keys(keylessItem); | ||
|
||
return itemKeys.reduce( | ||
(accumulator, k, index) => ({ ...accumulator, [`#field_${index}`]: k }), | ||
{}, | ||
); | ||
}; | ||
return itemKeys.reduce( | ||
(accumulator, k, index) => ({ ...accumulator, [`#prop_${index}`]: k }), | ||
{}, | ||
); | ||
} | ||
|
||
const internalValueBuilder = (item: any): any => { | ||
const itemKeys = Object.keys(item); | ||
buildExpressionValues(item: any): { [key: string]: any } { | ||
const keylessItem = this.removeKey(item); | ||
const itemKeys = Object.keys(keylessItem); | ||
|
||
return itemKeys.reduce( | ||
(accumulator, k, index) => ({ ...accumulator, [`:value_${index}`]: item[k] }), | ||
{}, | ||
); | ||
}; | ||
return itemKeys.reduce( | ||
(accumulator, k, index) => ({ ...accumulator, [`:value_${index}`]: item[k] }), | ||
{}, | ||
); | ||
} | ||
|
||
buildUpdateExpression(item: any) { | ||
const keylessItem = this.removeKey(item); | ||
const itemKeys = Object.keys(keylessItem); | ||
|
||
return `SET ${itemKeys.map((k, index) => `#prop_${index} = :value_${index}`).join(', ')}`; | ||
} | ||
|
||
private removeKey(item: any) { | ||
const itemCopy = { ...item }; | ||
delete itemCopy[this.keyName]; | ||
|
||
return itemCopy; | ||
} | ||
} | ||
|
||
// export const updateExpressionBuilder = (item: any): ExpressionBuilderResult => ({ | ||
// updateExpression: internalUpdateBuilder(item), | ||
// expressionAttributeNames: internalNameBuilder(item), | ||
// expressionAttributeValues: internalValueBuilder(item), | ||
// }); | ||
|
||
// const internalUpdateBuilder = (item: any): string => { | ||
// const itemKeys = Object.keys(item); | ||
|
||
// return `SET ${itemKeys.map((k, index) => `#prop_${index} = :value_${index}`).join(', ')}`; | ||
// }; | ||
|
||
// const internalNameBuilder = (item: any): any => { | ||
// const itemKeys = Object.keys(item); | ||
|
||
// return itemKeys.reduce( | ||
// (accumulator, k, index) => ({ ...accumulator, [`#prop_${index}`]: k }), | ||
// {}, | ||
// ); | ||
// }; | ||
|
||
// const internalValueBuilder = (item: any): any => { | ||
// const itemKeys = Object.keys(item); | ||
|
||
// return itemKeys.reduce( | ||
// (accumulator, k, index) => ({ ...accumulator, [`:value_${index}`]: item[k] }), | ||
// {}, | ||
// ); | ||
// }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.