@jupiterone/dynamodb-dao
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -79,2 +79,11 @@ import { DocumentClient } from 'aws-sdk/clients/dynamodb'; | ||
/** | ||
* This type is used to force functions like `incr` and `decr` to only take | ||
* properties from the `DataModel` that are type "number". | ||
* | ||
* See: https://stackoverflow.com/a/49797062 | ||
*/ | ||
export declare type NumberPropertiesInType<T> = Pick<T, { | ||
[K in keyof T]: T[K] extends number ? K : never; | ||
}[keyof T]>; | ||
/** | ||
* A base dynamodb dao class that enforces types | ||
@@ -103,2 +112,4 @@ */ | ||
update(key: KeySchema, data: Partial<DataModel>, updateOptions?: UpdateOptions): Promise<DataModel>; | ||
incr(key: KeySchema, attr: keyof NumberPropertiesInType<DataModel>, incrBy?: number): Promise<DataModel>; | ||
decr(key: KeySchema, attr: keyof NumberPropertiesInType<DataModel>, decrBy?: number): Promise<DataModel>; | ||
/** | ||
@@ -105,0 +116,0 @@ * Executes a query to fetch a count |
36
index.js
@@ -171,2 +171,38 @@ "use strict"; | ||
} | ||
async incr(key, attr, incrBy = 1) { | ||
const { Attributes: attributes } = await this.documentClient | ||
.update({ | ||
TableName: this.tableName, | ||
Key: key, | ||
UpdateExpression: 'SET #incrAttr = if_not_exists(#incrAttr, :start) + :inc', | ||
ExpressionAttributeNames: { | ||
'#incrAttr': attr, | ||
}, | ||
ExpressionAttributeValues: { | ||
':inc': incrBy, | ||
':start': 0, | ||
}, | ||
ReturnValues: 'ALL_NEW', | ||
}) | ||
.promise(); | ||
return attributes; | ||
} | ||
async decr(key, attr, decrBy = 1) { | ||
const { Attributes: attributes } = await this.documentClient | ||
.update({ | ||
TableName: this.tableName, | ||
Key: key, | ||
UpdateExpression: 'SET #decrAttr = if_not_exists(#decrAttr, :start) - :dec', | ||
ExpressionAttributeNames: { | ||
'#decrAttr': attr, | ||
}, | ||
ExpressionAttributeValues: { | ||
':dec': decrBy, | ||
':start': 0, | ||
}, | ||
ReturnValues: 'ALL_NEW', | ||
}) | ||
.promise(); | ||
return attributes; | ||
} | ||
/** | ||
@@ -173,0 +209,0 @@ * Executes a query to fetch a count |
{ | ||
"name": "@jupiterone/dynamodb-dao", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "DynamoDB Data Access Object (DAO) helper library", | ||
@@ -35,2 +35,3 @@ "main": "index.js", | ||
"@types/jest": "^25.2.1", | ||
"@types/lodash.partition": "^4.6.6", | ||
"@types/node": "^13.11.1", | ||
@@ -48,2 +49,3 @@ "@types/uuid": "^8.0.0", | ||
"lint-staged": "^10.1.3", | ||
"lodash.partition": "^4.6.0", | ||
"prettier": "^2.0.4", | ||
@@ -50,0 +52,0 @@ "ts-jest": "^25.3.1", |
@@ -32,2 +32,3 @@ # dynamodb-dao | ||
name: string; | ||
total?: number; | ||
} | ||
@@ -102,2 +103,40 @@ | ||
**Incrementing/Decrementing** | ||
NOTE: This should only be used where overcounting and undercounting can be | ||
tolerated. See | ||
[the DynamoDB atomic counter documentation](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithItems.html#WorkingWithItems.AtomicCounters) | ||
for more information. | ||
If a property does not already exist, the initial value is assigned `0` and | ||
incremented/decremented from `0`. | ||
```ts | ||
// `total` will have the value `5` | ||
const { total } = await myDocumentDao.incr( | ||
// The key | ||
{ | ||
id: 'abc', | ||
accountId: 'def', | ||
}, | ||
// The `number` property to increment | ||
'total', | ||
// The number to increment by. Defaults to 1. | ||
5, | ||
); | ||
// `total` will have the value `-5` | ||
const { total } = await myDocumentDao.decr( | ||
// The key | ||
{ | ||
id: '123', | ||
accountId: 'def', | ||
}, | ||
// The `number` property to increment | ||
'total', | ||
// The number to decrement by. Defaults to 1. | ||
5, | ||
); | ||
``` | ||
## Developing | ||
@@ -104,0 +143,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25561
540
161
20