Skip to content
This repository has been archived by the owner on Jul 17, 2022. It is now read-only.

Commit

Permalink
feat(dynamodb): configurable dynamodb keys (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdottv authored Feb 2, 2022
1 parent a3ce841 commit c9555aa
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 68 deletions.
19 changes: 17 additions & 2 deletions packages/dynamodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

This is the AWS DynamoDB Adapter for next-auth. This package can only be used in conjunction with the primary next-auth package. It is not a standalone package.

You need a table with a partition key `pk` and a sort key `sk`. Your table also needs a global secondary index named `GSI1` with `GSI1PK` as partition key and `GSI1SK` as sorting key. You can set whatever you want as the table name and the billing method.
By default, the adapter expects a table with a partition key `pk` and a sort key `sk`, as well as a global secondary index named `GSI1` with `GSI1PK` as partition key and `GSI1SK` as sorting key. You can set whatever you want as the table name and the billing method.

If you want sessions and verification tokens to get automatically removed from your table you need to [activate TTL](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html) on your table with the TTL attribute name set to `expires`

Expand Down Expand Up @@ -88,12 +88,27 @@ The table respects the single table design pattern. This has many advantages:
- Querying relations is faster than with multi-table schemas (for eg. retreiving all sessions for a user).
- Only one table needs to be replicated, if you want to go multi-region.

Here is a schema of the table :
Here is the default schema of the table:

<p align="center">
<img src="https://i.imgur.com/hGZtWDq.png" alt="">
</p>

## Customize table structure

You can configure your custom table structure by passing the `options` key to the adapter constructor:

```
const adapter = DynamoDBAdapter(client, {
tableName: "custom-table-name",
partitionKey: "custom-pk",
sortKey: "custom-sk",
indexName: "custom-index-name",
indexPartitionKey: "custom-index-pk",
indexSortKey: "custom-index-sk",
})
```

## Contributing

We're open to all community contributions! If you'd like to contribute in any way, please read our [Contributing Guide](https://github.com/nextauthjs/adapters/blob/main/CONTRIBUTING.md).
Expand Down
30 changes: 30 additions & 0 deletions packages/dynamodb/jest-dynamodb-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,36 @@ module.exports = {
],
ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1 },
},
{
TableName: `next-auth-custom`,
KeySchema: [
{ AttributeName: "PK", KeyType: "HASH" },
{ AttributeName: "SK", KeyType: "RANGE" },
],
AttributeDefinitions: [
{ AttributeName: "PK", AttributeType: "S" },
{ AttributeName: "SK", AttributeType: "S" },
{ AttributeName: "gsi1pk", AttributeType: "S" },
{ AttributeName: "gsi1sk", AttributeType: "S" },
],
GlobalSecondaryIndexes: [
{
IndexName: "gsi1",
KeySchema: [
{ AttributeName: "gsi1pk", KeyType: "HASH" },
{ AttributeName: "gsi1sk", KeyType: "RANGE" },
],
Projection: {
ProjectionType: "ALL",
},
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1,
},
},
],
ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1 },
},
// etc
],
port: 8000,
Expand Down
Loading

0 comments on commit c9555aa

Please sign in to comment.