Skip to content

Commit beafd6c

Browse files
authored
Doc updates (#13)
* Doc updates * Fixes
1 parent 52861ee commit beafd6c

File tree

2 files changed

+102
-12
lines changed

2 files changed

+102
-12
lines changed

README.md

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,92 @@
66

77
DynamoDB is an excellent choice for session stores because it is a fully managed service that is highly available, durable, and can scale automatically (to nearly unlimited levels) to meet demand. DynamoDB reads will typically return in 1-3 ms if capacity is set correctly and the caller is located in the same region as the `Table`.
88

9+
# Getting Started
10+
11+
## Installation
12+
13+
The package is available on npm as [@pwrdrvr/dynamodb-session-store](https://www.npmjs.com/package/@pwrdrvr/dynamodb-session-store)
14+
15+
`npm i @pwrdrvr/dynamodb-session-store`
16+
17+
## Importing
18+
19+
```typescript
20+
import { DynamoDBStore } from '@pwrdrvr/dynamodb-session-store';
21+
```
22+
23+
## Necessary IAM Policy
24+
25+
The following IAM permissions are required for the DynamoDB Table:
26+
27+
```json
28+
{
29+
"Version": "2012-10-17",
30+
"Statement": [
31+
{
32+
"Sid": "AllowDynamoDBAccess",
33+
"Effect": "Allow",
34+
"Action": [
35+
"dynamodb:DescribeTable",
36+
"dynamodb:GetItem",
37+
"dynamodb:UpdateItem",
38+
"dynamodb:PutItem",
39+
"dynamodb:DeleteItem"
40+
],
41+
"Resource": "arn:aws:dynamodb:us-east-1:123456789012:table/dynamodb-session-store-test"
42+
}
43+
]
44+
}
45+
```
46+
47+
## Example Usage
48+
49+
[See the examples directory for more examples](./examples)
50+
51+
```typescript
52+
import express from 'express';
53+
import session from 'express-session';
54+
import { DynamoDBStore } from '@pwrdrvr/dynamodb-session-store';
55+
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
56+
57+
const app = express();
58+
const dynamoDBClient = new DynamoDBClient({});
59+
60+
app.use(
61+
session({
62+
store: new DynamoDBStore({
63+
tableName: 'some-table',
64+
dynamoDBClient,
65+
touchAfter: 60 * 60, // 60 minutes in seconds
66+
}),
67+
secret: 'yeah-change-this',
68+
cookie: {
69+
maxAge: 14 * 24 * 60 * 60 * 1000, // 14 days in milliseconds
70+
},
71+
resave: false,
72+
saveUninitialized: false,
73+
}),
74+
);
75+
76+
app.get('/login', (req, res) => {
77+
console.log(`Session ID: ${req.session?.id}`);
78+
req.session.user = 'test';
79+
res.send('Logged in');
80+
});
81+
82+
app.get('/*', (req, res) => {
83+
res.status(200).send('Hello world');
84+
});
85+
86+
app.listen(Number.parseInt(PORT, 10), () => {
87+
console.log(`Example app listening on port ${port}`);
88+
});
89+
```
90+
91+
## API Documentation
92+
93+
After installing the package review the [API Documentation](https://pwrdrvr.github.io/dynamodb-session-store/classes/DynamoDBStore.html) for detailed on each configuration option.
94+
995
# Features
1096

1197
- Configurability of `Strongly Consistent Reads`
@@ -23,8 +109,8 @@ DynamoDB is an excellent choice for session stores because it is a fully managed
23109

24110
# Configuration Tips
25111

26-
- Use a Table per-region if you are deployed in multiple regions
27-
- Use a Table per-environment if you are deployed in multiple environments (e.g. dev/qa/prod)
112+
- Use a Table per-region if the app is deployed in multiple regions
113+
- Use a Table per-environment if the app is deployed in multiple environments (e.g. dev/qa/prod)
28114
- Use a Table unique to the session store - do not try to overload other data into this Table as the scaling and expiration needs will not overlap well
29115
- For applications attached to a VPC (including Lambda's attached to a VPC), use a VPC Endpoint for DynamoDB to avoid the cost, latency, and additional reliability exposure of crossing NAT Gateway to reach DynamoDB
30116
- Use Provisioned Capacity with auto-scaling to avoid throttling and to achieve the lowest cost - On Demand seems nice but it is costly

src/dynamodb-store.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ export interface DynamoDBStoreOptions {
8080
/**
8181
* Create the DynamoDB table if it does not exist with OnDemand capacity
8282
*
83-
* NOT SUGGESTED: this can create the table in many accounts and regions
83+
* @remarks
84+
*
85+
* ⛔️ NOT SUGGESTED ⛔️: this can create the table in many accounts and regions
8486
* for any developer running the app locally with AWS credentials that
85-
* have permission to create tables. This is also a bad idea
87+
* have permission to create tables. This is also a bad idea
8688
* because the least expensive option for relatively stable loads
8789
* is to use ProvisionedCapacity with Application Auto Scaling
8890
* configured to adjust the Read and Write capacity.
@@ -95,6 +97,9 @@ export interface DynamoDBStoreOptions {
9597
readonly createTableOptions?: Partial<CreateTableCommandInput>;
9698

9799
/**
100+
* Use Strongly Consistent Reads for session reads
101+
*
102+
* @remarks
98103
* Strongly Consistent Reads should rarely be needed for a session store unless
99104
* the values in the session are updated frequently and they must absolutely
100105
* be the most recent version (which is very unliley as the most recent
@@ -217,7 +222,7 @@ export class DynamoDBStore extends session.Store {
217222
* Enable TTL field on the table if configured
218223
*
219224
* @remarks
220-
* This is not recommended for production use.
225+
* ⛔️ NOT SUGGESTED ⛔️: This is not recommended for production use.
221226
*
222227
* For production the table should be created with IaaC (infrastructure as code)
223228
* such as AWS CDK, SAM, CloudFormation, Terraform, etc.
@@ -308,15 +313,14 @@ export class DynamoDBStore extends session.Store {
308313
}
309314

310315
/**
311-
* Create a DynamoDB Table-based express-session store.
312-
*
313-
* Note: This does not await creation of a table (which should only
314-
* be used in quick and dirty tests).
315-
*
316-
* @param options DynamoDBStore options
316+
* Create a DynamoDB Table-based [express-session](https://www.npmjs.com/package/express-session) store.
317317
*
318318
* @remarks
319-
* `createTableOptions` is not recommended for production use.
319+
* ⛔️ NOT SUGGESTED ⛔️: `createTableOptions` is not recommended for production use.
320+
*
321+
* Note: This does not await creation of a table if `createTableOptions` is passed (which should only
322+
* be used in quick and dirty tests). Use `DynamoDBStore.create()` instead to await
323+
* creation of the table in testing scenarios.
320324
*/
321325
constructor(options: DynamoDBStoreOptions) {
322326
super();

0 commit comments

Comments
 (0)