npm package: @samuraitruong/connect-dynamodb
Description: DynamoDB session store for Connect
Author: Mike Carson
Latest version: 2.2.0

# Connect DynamoDB

[![CI](https://github.com/samuraitruong/connect-dynamodb/actions/workflows/release.yml/badge.svg)](https://github.com/samuraitruong/connect-dynamodb/actions/workflows/release.yml)

This project was forked from https://github.com/ca98am79/connect-dynamodb and upgrade the code to moderm syntax + devtools. Going forward, I wil maintain and keep library up to date

connect-dynamodb is a DynamoDB session store backed by the [aws-sdk](https://github.com/aws/aws-sdk-js)

** This version is modified to allow read database session value and merge with new change to avoid race condition overriten session data **

## Installation

```sh
    npm install @samuraitruong/connect-dynamodb
    or
    yarn add @samuraitruong/connect-dynamodb
```

## Options

Rational defaults are set but can be overridden in the options object. Credentials and configuration are automatically loaded from [environment variables](http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-environment.html) or [shared credentials](http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-shared.html) but may optionally be passed through a JSON file or object. The client attribute is necessary for use with [DynamoDB Local](http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html) but can be left out if using DynamoDB with your AWS account. To use [DynamoDB TTL](http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html), enable it on the table and select the `expires` field.

- One of the following if not using environment variables or shared credentials:
  - `AWSConfigPath` Optional path to a [file containing your AWS credentials and configuration](http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-configuring.html#Credentials_from_Disk)
  - `AWSConfigJSON` Optional [JSON object containing your AWS credentials and configuration](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html)
- `client` Optional AWS DynamoDB object from `new AWS.DynamoDB()`
- `AWSRegion` Optional AWS region (defaults to 'us-east-1', ignored if using `AWSConfigPath` or `AWSConfigJSON`)
- `table` Optional DynamoDB server session table name (defaults to "sessions")
- `hashKey` Optional hash key (defaults to "id")
- `prefix` Optional key prefix (defaults to "sess")
- `reapInterval` Legacy session expiration cleanup in milliseconds. Should instead enable [DynamoDB TTL](http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html) and select the `expires` field.
- `ttl`: the values in second that will set for `ttlFieldName` field default = 365 days
- `ttlFieldName`: set the column name for ttl, default = ttl
- **BREAKING CHANGE** from v1.0.11 to v2.0.0 for reaping sessions with changes to the format of the expires field timestamp.

## Usage

````js
    const options = {
        // Optional DynamoDB table name, defaults to 'sessions'
        table: 'myapp-sessions',

        // Optional path to AWS credentials and configuration file
        // AWSConfigPath: './path/to/credentials.json',

        // Optional JSON object of AWS credentials and configuration
        AWSConfigJSON: {
            accessKeyId: <YOUR_ACCESS_KEY_ID>,
            secretAccessKey: <YOUR_SECRET_ACCESS_KEY>,
            region: 'us-east-1'
        },

        // Optional client for alternate endpoint, such as DynamoDB Local
        client: new AWS.DynamoDB({ endpoint: new AWS.Endpoint('http://localhost:8000')}),

        // Optional ProvisionedThroughput params, defaults to 5
        readCapacityUnits: 25,
        writeCapacityUnits: 25
        // TTL default set to 1 year, below to set ttl to 30 days
        ttl: 30 * 3600 * 24
    };
    ```

### with connect  [connect](https://github.com/senchalabs/connect)
```js
    var connect = require('connect');
    var DynamoDBStore = require('connect-dynamodb')(connect);
    connect()
        .use(connect.cookieParser())
        .use(connect.session({ store: new DynamoDBStore(options), secret: 'keyboard cat'}));
````

### With [express 3](http://expressjs.com/en/3x/api.html)

```js
    var DynamoDBStore = require('connect-dynamodb')(express);
    var app = express(
        express.cookieParser(),
        express.session({ store: new DynamoDBStore(options), secret: 'keyboard cat'});
    );
```

### With [express 4](http://expressjs.com/)

```js
var app = express();
var session = require('express-session');
var DynamoDBStore = require('connect-dynamodb')({ session: session });
app.use(session({ store: new DynamoDBStore(options), secret: 'keyboard cat' }));

OR;

var app = express();
var session = require('express-session');
var DynamoDBStore = require('connect-dynamodb')(session);
app.use(session({ store: new DynamoDBStore(options), secret: 'keyboard cat' }));
```

## Testing

If you want to run the tests locally and have the AWS environment variables setup you can run the command:

```
npm test
```

You can also run it locally by running the following two scripts in separate terminals:

```
docker run -it --rm -d \
  --name=dynamodb-test \
  -p 127.0.0.1:8000:8000 \
  amazon/dynamodb-local
```

```
export AWS_CONFIG_JSON='{"endpoint": "http://127.0.0.1:8000", "region": "us-east-1", "accessKeyId": "accesskey", "secretAccessKey": "secretaccesskey"}'
npm test
```

## IAM Permissions

Connect DynamoDB requires the following IAM permissions for DynamoDB:

- CreateTable
- PutItem
- DeleteItem
- GetItem
- Scan
- UpdateItem

Sample IAM policy (with least privilege):

_(Replace **\<AWS ACCOUNT ID\>**, **\<TABLE NAME\>** and **\<SOURCE IP AND BITMASK\>**)._

```
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "dynamodb:CreateTable",
                "dynamodb:PutItem",
                "dynamodb:DeleteItem",
                "dynamodb:GetItem",
                "dynamodb:Scan",
                "dynamodb:UpdateItem"
            ],
            "Resource": "arn:aws:dynamodb:*:<AWS ACCOUNT ID>:table/<TABLE NAME>"
        }
    ]
}
```

## License

connect-dynamodb is licensed under the [MIT license.](https://github.com/ca98am79/connect-dynamodb/blob/master/LICENSE.txt)

## Credits

All credeit to creator and maintainer of original repo (https://github.com/ca98am79/connect-dynamodb)[https://github.com/ca98am79/connect-dynamodb]