Skip to content

Commit 998dc24

Browse files
committedMay 18, 2018
add config options
1 parent dd40d0d commit 998dc24

12 files changed

+1999
-1983
lines changed
 

‎.eslintrc.json

+17-35
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,18 @@
11
{
2-
"env": {
3-
"es6": true,
4-
"node": true
5-
},
6-
"extends": "eslint:recommended",
7-
"parserOptions": {
8-
"sourceType": "module"
9-
},
10-
"rules": {
11-
"indent": [
12-
"error",
13-
4
14-
],
15-
"linebreak-style": [
16-
"error",
17-
"unix"
18-
],
19-
"quotes": [
20-
"error",
21-
"single"
22-
],
23-
"semi": [
24-
"error",
25-
"always"
26-
]
27-
},
28-
"globals": {
29-
"describe" : false,
30-
"it" : false,
31-
"before" : false,
32-
"beforeEach" : false,
33-
"after" : false,
34-
"afterEach" : false
35-
}
36-
}
2+
"env": {
3+
"es6": true,
4+
"node": true
5+
},
6+
"extends": "eslint:recommended",
7+
"parserOptions": {
8+
"sourceType": "module"
9+
},
10+
"globals": {
11+
"describe": false,
12+
"it": false,
13+
"before": false,
14+
"beforeEach": false,
15+
"after": false,
16+
"afterEach": false
17+
}
18+
}

‎CHANGELOG.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
21
## Changelog
32

43
### v2.0.0
54

6-
You are now able to pass configuration options to the library. Notably, you are able to pass options to the [AWS.DynamoDB.DocumentClient](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#constructor-property) constructor, for example if you want to run against a [Dynamodb Local](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html) instance while developing. Allowing options to be passed means that the wrapper now has to be configured before use. Previously you could just use the wrapper directly:
5+
You are now able to pass configuration options to the library. Notably, you are able to pass options to the [AWS.DynamoDB.DocumentClient](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#constructor-property) constructor, for example if you want to run against a [Dynamodb Local](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html) instance while developing. Allowing options to be passed means that the wrapper has to be configured before use. Previously you could just use the wrapper directly:
76

87
```js
9-
const clientWrapper = require('dynamodb-doc-client-wrapper');
10-
// clientWrapper is good to go
8+
// old way:
9+
const clientWrapper = require("dynamodb-doc-client-wrapper");
10+
// clientWrapper was then good to go
1111
```
1212

1313
Now it is a factory function and so needs to be invoked before use. This means that you will almost certainly want to add a file that creates an instance of the wrapper, configured how you see fit, and that then exports the configured instance. You would then require this file wherever in your service you need to access Dynamodb:
1414

1515
```js
16-
const clientWrapper = require('dynamodb-doc-client-wrapper');
17-
exports = clientWrapper() // <-- can now pass config options here
18-
```
16+
// new way:
17+
const clientWrapper = require("dynamodb-doc-client-wrapper");
18+
module.exports = clientWrapper(); // <-- can now pass config options here
19+
```

‎README.md

+45-36
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ methods, returning the results as Promises.
1818
```
1919
$ npm install --save dynamodb-doc-client-wrapper
2020
```
21+
2122
or
23+
2224
```
2325
$ yarn add dynamodb-doc-client-wrapper
2426
```
2527

26-
You also need to have the `aws-sdk` package available as a peer dependency. When running AWS Lambda functions on AWS, that package is already installed; you can install it as a dev dependency so it is available locally when testing.
28+
You also need to have the `aws-sdk` package available as a peer dependency. When running AWS Lambda functions on AWS, that package is already installed; you can install `aws-sdk` as a dev dependency so it is available locally when testing.
2729

2830
## Usage
2931

@@ -32,8 +34,8 @@ You also need to have the `aws-sdk` package available as a peer dependency. When
3234
You should create one client wrapper instance in a file that you then require where needed elsewhere in your service. If you do not need to configure the wrapper, you can create this basic file...
3335

3436
```js
35-
const clientWrapper = require('dynamodb-doc-client-wrapper');
36-
exports = clientWrapper()
37+
const clientWrapper = require("dynamodb-doc-client-wrapper");
38+
module.exports = clientWrapper();
3739
```
3840

3941
... and then require it as needed:
@@ -42,40 +44,47 @@ exports = clientWrapper()
4244
const dynamodbClient = require('./path/to/the/file/above')
4345

4446
// later in a function in this file...
45-
const response = yield dynamodbClient.query({
47+
const response = await dynamodbClient.query({
4648
TableName: 'MyTable',
4749
KeyConditionExpression: 'tagType = :tagType',
4850
ExpressionAttributeValues: { ':tagType': 'audience' },
4951
ProjectionExpression: 'id, label'
5052
});
5153
```
5254

53-
If you need to configure the wrapper, you can create a file similar to this one:
55+
If you need to configure the wrapper, you can instead create a file similar to this one:
5456

5557
```js
56-
const clientWrapper = require('dynamodb-doc-client-wrapper');
57-
const config = {
58-
dynamodb: {
59-
region: 'localhost',
60-
endpoint: 'http://localhost:8000'
61-
}
62-
}
63-
exports = clientWrapper(config)
58+
const clientWrapper = require("dynamodb-doc-client-wrapper");
59+
60+
const config = process.env.IS_OFFLINE
61+
? {
62+
connection: {
63+
region: "localhost",
64+
endpoint: "http://localhost:8000"
65+
}
66+
}
67+
: null;
68+
69+
module.exports = clientWrapper(config);
6470
```
6571

66-
The options that you can pass in the config object are:
72+
Note: in the above example, `process.env.IS_OFFLINE` gets set by the [serverless-offline](https://github.com/dherault/serverless-offline) plugin.
73+
74+
All config options are optional. The allowed options are:
6775

68-
| Option | Description | Default Value |
69-
| ------------- | ------------- | ------------- |
70-
| dynamodb | An [AWS.DynamoDB.DocumentClient](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#constructor-property) config object | `null` |
71-
| notFoundMsg | The message of the error thrown when a `get` or `batchGet` request returns a 404 response. | '[404] Entity Not Found' |
76+
| Name | Description | Default Value |
77+
| -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------ |
78+
| connection | An [AWS.DynamoDB.DocumentClient](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#constructor-property) config object. Ignored if the `documentClient` option exists. | n/a |
79+
| documentClient | Your own preconfigured [AWS.DynamoDB.DocumentClient](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#constructor-property) instance. | n/a |
80+
| notFoundMsg | The message of the error thrown when a `get` or `batchGet` request returns a 404 response. | '[404] Entity Not Found' |
7281

7382
### API
7483

7584
#### Query
7685

7786
```js
78-
const response = yield clientWrapper.query({
87+
const response = await clientWrapper.query({
7988
TableName: 'MyTable',
8089
KeyConditionExpression: 'tagType = :tagType',
8190
ExpressionAttributeValues: { ':tagType': 'audience' },
@@ -92,7 +101,7 @@ on total response size in DynamoDB.
92101
#### QueryBasic
93102

94103
```js
95-
const response = yield clientWrapper.queryBasic({
104+
const response = await clientWrapper.queryBasic({
96105
TableName: 'MyTable',
97106
KeyConditionExpression: 'tagType = :tagType',
98107
ExpressionAttributeValues: { ':tagType': 'audience' },
@@ -110,7 +119,7 @@ you will manage getting all the results yourself.
110119
#### Scan
111120

112121
```js
113-
const response = yield clientWrapper.scan({
122+
const response = await clientWrapper.scan({
114123
TableName: 'MyTable',
115124
ProjectionExpression: 'id, label'
116125
});
@@ -125,7 +134,7 @@ on total response size in DynamoDB.
125134
#### ScanBasic
126135

127136
```js
128-
const response = yield clientWrapper.scanBasic({
137+
const response = await clientWrapper.scanBasic({
129138
TableName: 'MyTable',
130139
ProjectionExpression: 'id, label'
131140
});
@@ -141,7 +150,7 @@ you will manage getting all the results yourself.
141150
#### BatchGet
142151

143152
```js
144-
const response = yield clientWrapper.batchGet({
153+
const response = await clientWrapper.batchGet({
145154
RequestItems: {
146155
'Table1': {
147156
Keys: [{ id: 1 }, { id: 2 }]
@@ -172,13 +181,13 @@ All items will be retrieved, even if the number of items to be retrieved
172181
exceeds the DynamoDB limit of 100 items, or if the limit
173182
on total response size in DynamoDB was exceeded.
174183

175-
An exception is thrown if any requested db item was not found. The
184+
An exception is thrown if any requested db item was not found. The
176185
exception message by default is '[404] Entity Not Found'.
177186

178187
#### BatchGetBasic
179188

180189
```js
181-
const response = yield clientWrapper.batchGetBasic({
190+
const response = await clientWrapper.batchGetBasic({
182191
RequestItems: {
183192
'Table1': {
184193
Keys: [{ id: 1 }, { id: 2 }]
@@ -192,26 +201,26 @@ const response = yield clientWrapper.batchGetBasic({
192201
This is a simple pass-through wrapper around the
193202
`AWS.DynamoDB.DocumentClient.batchGet` method, for when
194203
you want access to the entire response object and
195-
you will manage getting all the results yourself.
204+
you will manage getting all the results yourself.
196205

197206
#### Get
198207

199208
```js
200-
const response = yield clientWrapper.get({
209+
const response = await clientWrapper.get({
201210
TableName: 'MyTable',
202211
Index: { id: 1 }
203212
});
204213

205214
// response is the item, e.g. { id: 1, name: 'a' }
206215
```
207216

208-
An exception is thrown if the requested db item was not found. The
217+
An exception is thrown if the requested db item was not found. The
209218
exception message by default is '[404] Entity Not Found'.
210219

211220
#### TryGet
212221

213222
```js
214-
const response = yield clientWrapper.tryGet({
223+
const response = await clientWrapper.tryGet({
215224
TableName: 'MyTable',
216225
Index: { id: 1 }
217226
});
@@ -225,7 +234,7 @@ If the requested db item was not found then `null` is returned.
225234
#### GetBasic
226235

227236
```js
228-
const response = yield clientWrapper.getBasic({
237+
const response = await clientWrapper.getBasic({
229238
TableName: 'MyTable',
230239
Index: { id: 1 }
231240
});
@@ -240,7 +249,7 @@ you want access to the entire response object.
240249
#### Put
241250

242251
```js
243-
yield clientWrapper.put({
252+
await clientWrapper.put({
244253
TableName: 'MyTable',
245254
Item: { id: 1, name: 'a' }
246255
});
@@ -252,7 +261,7 @@ This is a simple pass-through wrapper around the
252261
#### BatchWrite
253262

254263
```js
255-
yield clientWrapper.batchWrite({
264+
await clientWrapper.batchWrite({
256265
RequestItems: {
257266
'Table1': [
258267
{ DeleteRequest: { Key: { id: 1 } } }
@@ -267,10 +276,10 @@ but it takes care of batching up the writes so that
267276
a single request does not exceed the DynamoDB limits,
268277
and it resubmits unprocessed writes.
269278

270-
#### BatchWriteBasic
279+
#### BatchWriteBasic
271280

272281
```js
273-
yield clientWrapper.batchWriteBasic({
282+
await clientWrapper.batchWriteBasic({
274283
RequestItems: {
275284
'Table1': [
276285
{ DeleteRequest: { Key: { id: 1 } } }
@@ -285,7 +294,7 @@ This is a simple pass-through wrapper around the
285294
#### Update
286295

287296
```js
288-
yield clientWrapper.update({
297+
await clientWrapper.update({
289298
TableName: 'Table',
290299
Key: { HashKey : 'hashkey' },
291300
UpdateExpression: 'set #a = :x + :y',
@@ -298,7 +307,7 @@ This is a simple pass-through wrapper around the
298307
#### Delete
299308

300309
```js
301-
yield clientWrapper.delete({
310+
await clientWrapper.delete({
302311
TableName: 'MyTable',
303312
Index: { id: 1 }
304313
});

0 commit comments

Comments
 (0)
Please sign in to comment.