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

Replace {counterTable,table}AttributeName with attributeName #28

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ export interface DynamoDBAutoIncrementProps {
counterTableKey: Record<string, NativeAttributeValue>

/** the name of the attribute in the table in which to store the last value of the counter */
counterTableAttributeName: string
attributeName: string

/** the name of the table in which to store items */
tableName: string

/** the name of the attribute used as the auto-incrementing partition key in the table in which to store items */
tableAttributeName: string

/** the initial value of the counter */
initialValue: number

Expand Down Expand Up @@ -93,11 +90,11 @@ export class DynamoDBAutoIncrement extends BaseDynamoDBAutoIncrement {
return (
(
await this.props.doc.get({
AttributesToGet: [this.props.counterTableAttributeName],
AttributesToGet: [this.props.attributeName],
Key: this.props.counterTableKey,
TableName: this.props.counterTableName,
})
).Item?.[this.props.counterTableAttributeName] ?? undefined
).Item?.[this.props.attributeName] ?? undefined
)
}

Expand All @@ -120,21 +117,21 @@ export class DynamoDBAutoIncrement extends BaseDynamoDBAutoIncrement {
{
ConditionExpression,
ExpressionAttributeNames: {
'#counter': this.props.counterTableAttributeName,
'#counter': this.props.attributeName,
},
ExpressionAttributeValues,
Item: {
...this.props.counterTableKey,
[this.props.counterTableAttributeName]: nextCounter,
[this.props.attributeName]: nextCounter,
},
TableName: this.props.counterTableName,
},
{
ConditionExpression: 'attribute_not_exists(#counter)',
ExpressionAttributeNames: {
'#counter': this.props.tableAttributeName,
'#counter': this.props.attributeName,
},
Item: { [this.props.tableAttributeName]: nextCounter, ...item },
Item: { [this.props.attributeName]: nextCounter, ...item },
TableName: this.props.tableName,
},
]
Expand Down
7 changes: 3 additions & 4 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ beforeAll(async () => {
doc,
counterTableName: 'autoincrement',
counterTableKey: { tableName: 'widgets' },
counterTableAttributeName: 'counter',
tableName: 'widgets',
tableAttributeName: 'widgetID',
attributeName: 'widgetID',
initialValue: 1,
}
autoincrement = new DynamoDBAutoIncrement(options)
Expand Down Expand Up @@ -69,7 +68,7 @@ describe('dynamoDBAutoIncrement', () => {
} else {
await doc.put({
TableName: 'autoincrement',
Item: { tableName: 'widgets', counter: lastID },
Item: { tableName: 'widgets', widgetID: lastID },
})
nextID = lastID + 1
}
Expand All @@ -89,7 +88,7 @@ describe('dynamoDBAutoIncrement', () => {
expect(autoincrementItems).toEqual([
{
tableName: 'widgets',
counter: nextID,
widgetID: nextID,
},
])
}
Expand Down