Create a DynamoDB Table-based express-session store.
⛔️ NOT SUGGESTED ⛔️: createTableOptions
is not recommended for production use.
Note: This does not await creation of a table if createTableOptions
is passed (which should only
be used in quick and dirty tests). Use DynamoDBStore.create()
instead to await
creation of the table in testing scenarios.
Private
Optional
_createPrivate
_ddbPrivate
_dynamoDBClientPrivate
_hashPrivate
_prefixPrivate
_tablePrivate
_touchPrivate
_useStatic
Readonly
captureStatic
captureSets or gets the default captureRejection value for all emitters.
Static
defaultStatic
Readonly
errorThis symbol shall be used to install a listener for only monitoring 'error'
events. Listeners installed using this symbol are called before the regular
'error'
listeners are called.
Installing a listener using this symbol does not change the behavior once an
'error'
event is emitted, therefore the process will still crash if no
regular 'error'
listener is installed.
Hash key name of the existing DynamoDB table or name of the hash key
to create if the table does not exist and the createTableOptions
do not provide a hash key name.
Prefix to add to the sid
in the hashKey
written to the DynamoDB table.
Name of the DynamoDB table to use (and optionally create)
Only update the session TTL on touch
events if touchAfter
seconds has passed
since the last time the session TTL was updated.
Set to 0
to always update the session TTL. - This is not suggested.
Writes on DynamoDB cost 5x as much as reads for sessions < 1 KB.
Writes on DynamoDB cost 20x as much as reads for sessions >= 3 KB and < 4 KB
Managing settings on DynamoDB provisioned capacity tables
UpdateItem
— Modifies a single item in the table. DynamoDB considers the size of the item as
it appears before and after the update. The provisioned throughput consumed reflects the
larger of these item sizes. Even if you update just a subset of the item's attributes,
UpdateItem
will still consume the full amount of provisioned throughput (the larger of the
"before" and "after" item sizes).
Use Strongly Consistent Reads for session reads
Strongly Consistent Reads should rarely be needed for a session store unless the values in the session are updated frequently and they must absolutely be the most recent version (which is very unliley as the most recent write could fail, in which case the session would not be the most recent version...).
Reasons not to use Strongly Consistent Reads:
Rest
...args: any[]Optional
allOptional
clearPrivate
createCreate the table if it does not exist Enable TTL field on the table if configured
⛔️ NOT SUGGESTED ⛔️: This is not recommended for production use.
For production the table should be created with IaaC (infrastructure as code) such as AWS CDK, SAM, CloudFormation, Terraform, etc.
Destroy the session in DynamoDB
Session ID
Optional
callback: ((err?) => void)Callback to return an error if the session was not destroyed
Error
void
Callback to return an error if the session was not destroyed
Optional
err: unknownError
void
Session ID
Callback to return the session data
Error
Session data
void
Callback to return the session data
Error
Optional
session: null | SessionDataSession data
void
Private
getTTLSecondsOptional
lengthPrivate
newRest
...args: any[]Rest
...args: any[]Rest
...args: any[]Rest
...args: any[]Rest
...args: any[]Optional
event: string | symbolRest
...args: any[]Session ID
Session data
The expires
field is set by the session middleware and is used
by DynamoDB to automatically expire the session.
Optional
callback: ((err?) => void)Callback to return an error if the session was not saved
Error
void
Callback to return an error if the session was not saved
Optional
err: unknownError
void
Reset the TTL on the DynamoDB record to 100% of the original TTL
Session ID
Session data
Optional
callback: ((err?) => void)Callback to return an error if the session TTL was not updated
Callback to return an error if the session TTL was not updated
Optional
err: unknownThis is called by the session middleware on every single get
request.
Static
createCreate the store and optionally await creation of the table.
Note: Store-created tables is not advised for production use.
DynamoDBStore options
Static
listenerStatic
onStatic
onceGenerated using TypeDoc
DynamoDBStore is an express-session store that uses DynamoDB as the backing store.
Remarks
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
.Example of Pricing
Disclaimer: perform your own pricing calculation, monitor your costs while launching, and setup cost alerts to avoid unexpected charges.
Saved AWS Pricing Calculation
Assumptions:
Pricing: