@mastra/dynamodb 0.0.2-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +46 -0
- package/README.md +144 -0
- package/dist/index.cjs +1273 -0
- package/dist/index.js +1271 -0
- package/package.json +46 -0
- package/src/entities/eval.ts +102 -0
- package/src/entities/index.ts +23 -0
- package/src/entities/message.ts +143 -0
- package/src/entities/thread.ts +66 -0
- package/src/entities/trace.ts +129 -0
- package/src/entities/utils.ts +51 -0
- package/src/entities/workflow-snapshot.ts +56 -0
- package/src/index.ts +1 -0
- package/src/storage/docker-compose.yml +16 -0
- package/src/storage/index.test.ts +1026 -0
- package/src/storage/index.ts +1036 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Elastic License 2.0 (ELv2)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Mastra AI, Inc.
|
|
4
|
+
|
|
5
|
+
**Acceptance**
|
|
6
|
+
By using the software, you agree to all of the terms and conditions below.
|
|
7
|
+
|
|
8
|
+
**Copyright License**
|
|
9
|
+
The licensor grants you a non-exclusive, royalty-free, worldwide, non-sublicensable, non-transferable license to use, copy, distribute, make available, and prepare derivative works of the software, in each case subject to the limitations and conditions below
|
|
10
|
+
|
|
11
|
+
**Limitations**
|
|
12
|
+
You may not provide the software to third parties as a hosted or managed service, where the service provides users with access to any substantial set of the features or functionality of the software.
|
|
13
|
+
|
|
14
|
+
You may not move, change, disable, or circumvent the license key functionality in the software, and you may not remove or obscure any functionality in the software that is protected by the license key.
|
|
15
|
+
|
|
16
|
+
You may not alter, remove, or obscure any licensing, copyright, or other notices of the licensor in the software. Any use of the licensor’s trademarks is subject to applicable law.
|
|
17
|
+
|
|
18
|
+
**Patents**
|
|
19
|
+
The licensor grants you a license, under any patent claims the licensor can license, or becomes able to license, to make, have made, use, sell, offer for sale, import and have imported the software, in each case subject to the limitations and conditions in this license. This license does not cover any patent claims that you cause to be infringed by modifications or additions to the software. If you or your company make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
|
|
20
|
+
|
|
21
|
+
**Notices**
|
|
22
|
+
You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms.
|
|
23
|
+
|
|
24
|
+
If you modify the software, you must include in any modified copies of the software prominent notices stating that you have modified the software.
|
|
25
|
+
|
|
26
|
+
**No Other Rights**
|
|
27
|
+
These terms do not imply any licenses other than those expressly granted in these terms.
|
|
28
|
+
|
|
29
|
+
**Termination**
|
|
30
|
+
If you use the software in violation of these terms, such use is not licensed, and your licenses will automatically terminate. If the licensor provides you with a notice of your violation, and you cease all violation of this license no later than 30 days after you receive that notice, your licenses will be reinstated retroactively. However, if you violate these terms after such reinstatement, any additional violation of these terms will cause your licenses to terminate automatically and permanently.
|
|
31
|
+
|
|
32
|
+
**No Liability**
|
|
33
|
+
As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.
|
|
34
|
+
|
|
35
|
+
**Definitions**
|
|
36
|
+
The _licensor_ is the entity offering these terms, and the _software_ is the software the licensor makes available under these terms, including any portion of it.
|
|
37
|
+
|
|
38
|
+
_you_ refers to the individual or entity agreeing to these terms.
|
|
39
|
+
|
|
40
|
+
_your company_ is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. _control_ means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
|
|
41
|
+
|
|
42
|
+
_your licenses_ are all the licenses granted to you for the software under these terms.
|
|
43
|
+
|
|
44
|
+
_use_ means anything you do with the software requiring one of your licenses.
|
|
45
|
+
|
|
46
|
+
_trademark_ means trademarks, service marks, and similar rights.
|
package/README.md
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# @mastra/dynamodb
|
|
2
|
+
|
|
3
|
+
A DynamoDB storage implementation for Mastra using a single-table design pattern with ElectroDB.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Efficient single-table design for all Mastra storage needs
|
|
8
|
+
- Based on ElectroDB for type-safe DynamoDB access
|
|
9
|
+
- Support for AWS credentials, regions, and endpoints
|
|
10
|
+
- Compatible with AWS DynamoDB Local for development
|
|
11
|
+
- Thread, Message, Trace, Eval, and Workflow operations
|
|
12
|
+
- Optimized for serverless environments
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @mastra/dynamodb
|
|
18
|
+
# or
|
|
19
|
+
pnpm add @mastra/dynamodb
|
|
20
|
+
# or
|
|
21
|
+
yarn add @mastra/dynamodb
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Prerequisites
|
|
25
|
+
|
|
26
|
+
Before using this package, you need to create a DynamoDB table with the required structure. See [TABLE_SETUP.md](./TABLE_SETUP.md) for detailed instructions on how to set up the table using CloudFormation or AWS CDK.
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
### Basic Usage
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { Memory } from '@mastra/memory';
|
|
34
|
+
import { DynamoDBStore } from '@mastra/dynamodb';
|
|
35
|
+
import { PineconeVector } from '@mastra/pinecone';
|
|
36
|
+
|
|
37
|
+
// Initialize the DynamoDB storage
|
|
38
|
+
const storage = new DynamoDBStore({
|
|
39
|
+
name: 'dynamodb',
|
|
40
|
+
config: {
|
|
41
|
+
region: 'us-east-1',
|
|
42
|
+
tableName: 'mastra-single-table', // Name of your DynamoDB table
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Initialize vector store (if using semantic recall)
|
|
47
|
+
const vector = new PineconeVector({
|
|
48
|
+
apiKey: process.env.PINECONE_API_KEY,
|
|
49
|
+
environment: process.env.PINECONE_ENVIRONMENT,
|
|
50
|
+
index: process.env.PINECONE_INDEX,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Memory combines storage (like DynamoDBStore) with an optional vector store for recall
|
|
54
|
+
// Create memory with DynamoDB storage
|
|
55
|
+
const memory = new Memory({
|
|
56
|
+
storage,
|
|
57
|
+
vector,
|
|
58
|
+
options: {
|
|
59
|
+
lastMessages: 10,
|
|
60
|
+
semanticRecall: true,
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Local Development
|
|
66
|
+
|
|
67
|
+
For local development, you can use DynamoDB Local:
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
const storage = new DynamoDBStore({
|
|
71
|
+
name: 'dynamodb',
|
|
72
|
+
config: {
|
|
73
|
+
region: 'us-east-1',
|
|
74
|
+
tableName: 'mastra-single-table',
|
|
75
|
+
endpoint: 'http://localhost:8000', // Local DynamoDB endpoint
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### AWS IAM Permissions
|
|
81
|
+
|
|
82
|
+
The IAM role or user used by this package needs the following permissions:
|
|
83
|
+
|
|
84
|
+
```json
|
|
85
|
+
{
|
|
86
|
+
"Version": "2012-10-17",
|
|
87
|
+
"Statement": [
|
|
88
|
+
{
|
|
89
|
+
"Effect": "Allow",
|
|
90
|
+
"Action": [
|
|
91
|
+
"dynamodb:DescribeTable",
|
|
92
|
+
"dynamodb:GetItem",
|
|
93
|
+
"dynamodb:PutItem",
|
|
94
|
+
"dynamodb:UpdateItem",
|
|
95
|
+
"dynamodb:DeleteItem",
|
|
96
|
+
"dynamodb:Query",
|
|
97
|
+
"dynamodb:Scan",
|
|
98
|
+
"dynamodb:BatchGetItem",
|
|
99
|
+
"dynamodb:BatchWriteItem"
|
|
100
|
+
],
|
|
101
|
+
"Resource": [
|
|
102
|
+
"arn:aws:dynamodb:*:*:table/${YOUR_TABLE_NAME}",
|
|
103
|
+
"arn:aws:dynamodb:*:*:table/${YOUR_TABLE_NAME}/index/*"
|
|
104
|
+
]
|
|
105
|
+
}
|
|
106
|
+
]
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Configuration Options
|
|
111
|
+
|
|
112
|
+
The `DynamoDBStore` constructor accepts the following configuration options:
|
|
113
|
+
|
|
114
|
+
| Option | Type | Required | Description |
|
|
115
|
+
| -------------------- | ------ | -------- | ---------------------------------------------------------- |
|
|
116
|
+
| `name` | string | Yes | A name for the storage instance |
|
|
117
|
+
| `config.region` | string | No | AWS region (default: 'us-east-1') |
|
|
118
|
+
| `config.tableName` | string | Yes | The name of your DynamoDB table |
|
|
119
|
+
| `config.endpoint` | string | No | Custom endpoint for DynamoDB (e.g., for local development) |
|
|
120
|
+
| `config.credentials` | object | No | AWS credentials (accessKeyId, secretAccessKey) |
|
|
121
|
+
|
|
122
|
+
## Architectural Approach
|
|
123
|
+
|
|
124
|
+
This storage adapter utilizes a **single-table design pattern** leveraging [ElectroDB](https://electrodb.dev/), which is a common and recommended approach for DynamoDB. This differs architecturally from relational database adapters (like `@mastra/pg` or `@mastra/libsql`) that typically use multiple tables, each dedicated to a specific entity (threads, messages, etc.).
|
|
125
|
+
|
|
126
|
+
Key aspects of this approach:
|
|
127
|
+
|
|
128
|
+
- **DynamoDB Native:** The single-table design is optimized for DynamoDB's key-value and query capabilities, often leading to better performance and scalability compared to mimicking relational models.
|
|
129
|
+
- **External Table Management:** Unlike some adapters that might offer helper functions to create tables via code, this adapter **expects the DynamoDB table and its associated Global Secondary Indexes (GSIs) to be provisioned externally** before use. Please refer to `TABLE_SETUP.md` for detailed instructions using tools like AWS CloudFormation or CDK. The adapter focuses solely on interacting with the pre-existing table structure.
|
|
130
|
+
- **Consistency via Interface:** While the underlying storage model differs, this adapter adheres to the same `MastraStorage` interface as other adapters, ensuring it can be used interchangeably within the Mastra `Memory` component.
|
|
131
|
+
|
|
132
|
+
## Advantage of Single-Table Design
|
|
133
|
+
|
|
134
|
+
This implementation uses a single-table design pattern with ElectroDB, which offers several advantages within the context of DynamoDB:
|
|
135
|
+
|
|
136
|
+
1. **Lower cost**: One table means fewer read/write capacity units to provision
|
|
137
|
+
2. **Better performance**: Related data is stored together for faster access
|
|
138
|
+
3. **Simplified administration**: Only one table to monitor and back up
|
|
139
|
+
4. **Reduced complexity**: Consistent access patterns across entities
|
|
140
|
+
5. **Transaction support**: Atomic operations across different entity types
|
|
141
|
+
|
|
142
|
+
## License
|
|
143
|
+
|
|
144
|
+
MIT
|