Skip to content

Commit

Permalink
Replace {counterTable,table}AttributeName with attributeName
Browse files Browse the repository at this point in the history
In practice, the version or counter attribute has the same name in
both tables. Forcing that to be the case will make it simpler to
implement versioning (#20).
  • Loading branch information
lpsinger committed Oct 31, 2023
1 parent 7d879b7 commit 6093c36
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
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

0 comments on commit 6093c36

Please sign in to comment.