@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/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@mastra/dynamodb",
3
+ "version": "0.0.2-alpha.0",
4
+ "description": "DynamoDB storage adapter for Mastra",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "src"
11
+ ],
12
+ "dependencies": {
13
+ "@aws-sdk/client-dynamodb": "^3.0.0",
14
+ "@aws-sdk/lib-dynamodb": "^3.0.0",
15
+ "electrodb": "^3.4.1"
16
+ },
17
+ "peerDependencies": {
18
+ "@mastra/core": "^0.9.4"
19
+ },
20
+ "devDependencies": {
21
+ "@microsoft/api-extractor": "^7.52.1",
22
+ "@types/node": "^20.17.27",
23
+ "@vitest/coverage-v8": "3.0.9",
24
+ "@vitest/ui": "3.0.9",
25
+ "axios": "^1.8.4",
26
+ "eslint": "^9.23.0",
27
+ "tsup": "^8.4.0",
28
+ "typescript": "^5.8.2",
29
+ "vitest": "^3.0.9",
30
+ "@mastra/core": "0.10.0-alpha.1",
31
+ "@internal/lint": "0.0.5"
32
+ },
33
+ "scripts": {
34
+ "build": "tsup src/index.ts --format esm,cjs --clean --treeshake=smallest --splitting",
35
+ "dev": "tsup --watch",
36
+ "clean": "rm -rf dist",
37
+ "lint": "eslint .",
38
+ "pretest": "docker compose up -d",
39
+ "test": "vitest run",
40
+ "posttest": "docker compose down -v",
41
+ "pretest:watch": "docker compose up -d",
42
+ "test:watch": "vitest watch",
43
+ "posttest:watch": "docker compose down -v",
44
+ "typecheck": "tsc --noEmit"
45
+ }
46
+ }
@@ -0,0 +1,102 @@
1
+ import { Entity } from 'electrodb';
2
+ import { baseAttributes } from './utils';
3
+
4
+ export const evalEntity = new Entity({
5
+ model: {
6
+ entity: 'eval',
7
+ version: '1',
8
+ service: 'mastra',
9
+ },
10
+ attributes: {
11
+ entity: {
12
+ type: 'string',
13
+ required: true,
14
+ },
15
+ ...baseAttributes,
16
+ input: {
17
+ type: 'string',
18
+ required: true,
19
+ },
20
+ output: {
21
+ type: 'string',
22
+ required: true,
23
+ },
24
+ result: {
25
+ type: 'string', // JSON stringified
26
+ required: true,
27
+ // Stringify object on set
28
+ set: (value?: any) => {
29
+ if (value && typeof value !== 'string') {
30
+ return JSON.stringify(value);
31
+ }
32
+ return value;
33
+ },
34
+ // Parse JSON string to object on get
35
+ get: (value?: string) => {
36
+ if (value) {
37
+ return JSON.parse(value);
38
+ }
39
+ return value;
40
+ },
41
+ },
42
+ agent_name: {
43
+ type: 'string',
44
+ required: true,
45
+ },
46
+ metric_name: {
47
+ type: 'string',
48
+ required: true,
49
+ },
50
+ instructions: {
51
+ type: 'string',
52
+ required: true,
53
+ },
54
+ test_info: {
55
+ type: 'string', // JSON stringified
56
+ required: false,
57
+ // Stringify object on set
58
+ set: (value?: any) => {
59
+ if (value && typeof value !== 'string') {
60
+ return JSON.stringify(value);
61
+ }
62
+ return value;
63
+ },
64
+ // Parse JSON string to object on get
65
+ get: (value?: string) => {
66
+ return value;
67
+ },
68
+ },
69
+ global_run_id: {
70
+ type: 'string',
71
+ required: true,
72
+ },
73
+ run_id: {
74
+ type: 'string',
75
+ required: true,
76
+ },
77
+ created_at: {
78
+ type: 'string',
79
+ required: true,
80
+ // Initialize with current timestamp if not provided
81
+ default: () => new Date().toISOString(),
82
+ // Convert Date to ISO string on set
83
+ set: (value?: Date | string) => {
84
+ if (value instanceof Date) {
85
+ return value.toISOString();
86
+ }
87
+ return value || new Date().toISOString();
88
+ },
89
+ },
90
+ },
91
+ indexes: {
92
+ primary: {
93
+ pk: { field: 'pk', composite: ['entity', 'run_id'] },
94
+ sk: { field: 'sk', composite: [] },
95
+ },
96
+ byAgent: {
97
+ index: 'gsi1',
98
+ pk: { field: 'gsi1pk', composite: ['entity', 'agent_name'] },
99
+ sk: { field: 'gsi1sk', composite: ['created_at'] },
100
+ },
101
+ },
102
+ });
@@ -0,0 +1,23 @@
1
+ import type { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb';
2
+ import { Service } from 'electrodb';
3
+ import { evalEntity } from './eval';
4
+ import { messageEntity } from './message';
5
+ import { threadEntity } from './thread';
6
+ import { traceEntity } from './trace';
7
+ import { workflowSnapshotEntity } from './workflow-snapshot';
8
+
9
+ export function getElectroDbService(client: DynamoDBDocumentClient, tableName: string) {
10
+ return new Service(
11
+ {
12
+ thread: threadEntity,
13
+ message: messageEntity,
14
+ eval: evalEntity,
15
+ trace: traceEntity,
16
+ workflowSnapshot: workflowSnapshotEntity,
17
+ },
18
+ {
19
+ client,
20
+ table: tableName,
21
+ },
22
+ );
23
+ }
@@ -0,0 +1,143 @@
1
+ import { Entity } from 'electrodb';
2
+ import { baseAttributes } from './utils';
3
+
4
+ export const messageEntity = new Entity({
5
+ model: {
6
+ entity: 'message',
7
+ version: '1',
8
+ service: 'mastra',
9
+ },
10
+ attributes: {
11
+ entity: {
12
+ type: 'string',
13
+ required: true,
14
+ },
15
+ ...baseAttributes,
16
+ id: {
17
+ type: 'string',
18
+ required: true,
19
+ },
20
+ threadId: {
21
+ type: 'string',
22
+ required: true,
23
+ },
24
+ content: {
25
+ type: 'string',
26
+ required: true,
27
+ // Stringify content object on set if it's not already a string
28
+ set: (value?: string | void) => {
29
+ if (value && typeof value !== 'string') {
30
+ return JSON.stringify(value);
31
+ }
32
+ return value;
33
+ },
34
+ // Parse JSON string to object on get ONLY if it looks like JSON
35
+ get: (value?: string) => {
36
+ if (value && typeof value === 'string') {
37
+ try {
38
+ // Attempt to parse only if it might be JSON (e.g., starts with { or [)
39
+ if (value.startsWith('{') || value.startsWith('[')) {
40
+ return JSON.parse(value);
41
+ }
42
+ } catch {
43
+ // Ignore parse error, return original string
44
+ return value;
45
+ }
46
+ }
47
+ return value;
48
+ },
49
+ },
50
+ role: {
51
+ type: 'string',
52
+ required: true,
53
+ },
54
+ type: {
55
+ type: 'string',
56
+ default: 'text',
57
+ },
58
+ resourceId: {
59
+ type: 'string',
60
+ required: false,
61
+ },
62
+ toolCallIds: {
63
+ type: 'string',
64
+ required: false,
65
+ set: (value?: string[] | string) => {
66
+ if (Array.isArray(value)) {
67
+ return JSON.stringify(value);
68
+ }
69
+ return value;
70
+ },
71
+ // Parse JSON string to array on get
72
+ get: (value?: string) => {
73
+ if (value && typeof value === 'string') {
74
+ try {
75
+ return JSON.parse(value);
76
+ } catch {
77
+ // Return raw value on error, consistent with 'content' field
78
+ return value;
79
+ }
80
+ }
81
+ // If value was not a string, or if it was an empty string, return it as is.
82
+ return value;
83
+ },
84
+ },
85
+ toolCallArgs: {
86
+ type: 'string',
87
+ required: false,
88
+ set: (value?: Record<string, unknown>[] | string) => {
89
+ if (value && typeof value !== 'string') {
90
+ return JSON.stringify(value);
91
+ }
92
+ return value;
93
+ },
94
+ // Parse JSON string to object on get
95
+ get: (value?: string) => {
96
+ if (value && typeof value === 'string') {
97
+ try {
98
+ return JSON.parse(value);
99
+ } catch {
100
+ // Return raw value on error, consistent with 'content' field
101
+ return value;
102
+ }
103
+ }
104
+ // If value was not a string, or if it was an empty string, return it as is.
105
+ return value;
106
+ },
107
+ },
108
+ toolNames: {
109
+ type: 'string',
110
+ required: false,
111
+ set: (value?: string[] | string) => {
112
+ if (Array.isArray(value)) {
113
+ return JSON.stringify(value);
114
+ }
115
+ return value;
116
+ },
117
+ // Parse JSON string to array on get
118
+ get: (value?: string) => {
119
+ if (value && typeof value === 'string') {
120
+ try {
121
+ return JSON.parse(value);
122
+ } catch {
123
+ // Return raw value on error, consistent with 'content' field
124
+ return value;
125
+ }
126
+ }
127
+ // If value was not a string, or if it was an empty string, return it as is.
128
+ return value;
129
+ },
130
+ },
131
+ },
132
+ indexes: {
133
+ primary: {
134
+ pk: { field: 'pk', composite: ['entity', 'id'] },
135
+ sk: { field: 'sk', composite: ['entity'] },
136
+ },
137
+ byThread: {
138
+ index: 'gsi1',
139
+ pk: { field: 'gsi1pk', composite: ['entity', 'threadId'] },
140
+ sk: { field: 'gsi1sk', composite: ['createdAt'] },
141
+ },
142
+ },
143
+ });
@@ -0,0 +1,66 @@
1
+ import { Entity } from 'electrodb';
2
+ import { baseAttributes } from './utils';
3
+
4
+ export const threadEntity = new Entity({
5
+ model: {
6
+ entity: 'thread',
7
+ version: '1',
8
+ service: 'mastra',
9
+ },
10
+ attributes: {
11
+ entity: {
12
+ type: 'string',
13
+ required: true,
14
+ },
15
+ ...baseAttributes,
16
+ id: {
17
+ type: 'string',
18
+ required: true,
19
+ },
20
+ resourceId: {
21
+ type: 'string',
22
+ required: true,
23
+ },
24
+ title: {
25
+ type: 'string',
26
+ required: true,
27
+ },
28
+ metadata: {
29
+ type: 'string',
30
+ required: false,
31
+ // Stringify metadata object on set if it's not already a string
32
+ set: (value?: Record<string, unknown> | string) => {
33
+ if (value && typeof value !== 'string') {
34
+ return JSON.stringify(value);
35
+ }
36
+ return value;
37
+ },
38
+ // Parse JSON string to object on get
39
+ get: (value?: string) => {
40
+ if (value && typeof value === 'string') {
41
+ try {
42
+ // Attempt to parse only if it might be JSON (e.g., starts with { or [)
43
+ if (value.startsWith('{') || value.startsWith('[')) {
44
+ return JSON.parse(value);
45
+ }
46
+ } catch {
47
+ // Ignore parse error, return original string
48
+ return value;
49
+ }
50
+ }
51
+ return value;
52
+ },
53
+ },
54
+ },
55
+ indexes: {
56
+ primary: {
57
+ pk: { field: 'pk', composite: ['entity', 'id'] },
58
+ sk: { field: 'sk', composite: ['id'] },
59
+ },
60
+ byResource: {
61
+ index: 'gsi1',
62
+ pk: { field: 'gsi1pk', composite: ['entity', 'resourceId'] },
63
+ sk: { field: 'gsi1sk', composite: ['createdAt'] },
64
+ },
65
+ },
66
+ });
@@ -0,0 +1,129 @@
1
+ import { Entity } from 'electrodb';
2
+ import { baseAttributes } from './utils';
3
+
4
+ export const traceEntity = new Entity({
5
+ model: {
6
+ entity: 'trace',
7
+ version: '1',
8
+ service: 'mastra',
9
+ },
10
+ attributes: {
11
+ entity: {
12
+ type: 'string',
13
+ required: true,
14
+ },
15
+ ...baseAttributes,
16
+ id: {
17
+ type: 'string',
18
+ required: true,
19
+ },
20
+ parentSpanId: {
21
+ type: 'string',
22
+ required: false,
23
+ },
24
+ name: {
25
+ type: 'string',
26
+ required: true,
27
+ },
28
+ traceId: {
29
+ type: 'string',
30
+ required: true,
31
+ },
32
+ scope: {
33
+ type: 'string',
34
+ required: true,
35
+ },
36
+ kind: {
37
+ type: 'number',
38
+ required: true,
39
+ },
40
+ attributes: {
41
+ type: 'string', // JSON stringified
42
+ required: false,
43
+ // Stringify object on set
44
+ set: (value?: any) => {
45
+ if (value && typeof value !== 'string') {
46
+ return JSON.stringify(value);
47
+ }
48
+ return value;
49
+ },
50
+ // Parse JSON string to object on get
51
+ get: (value?: string) => {
52
+ return value ? JSON.parse(value) : value;
53
+ },
54
+ },
55
+ status: {
56
+ type: 'string', // JSON stringified
57
+ required: false,
58
+ // Stringify object on set
59
+ set: (value?: any) => {
60
+ if (value && typeof value !== 'string') {
61
+ return JSON.stringify(value);
62
+ }
63
+ return value;
64
+ },
65
+ // Parse JSON string to object on get
66
+ get: (value?: string) => {
67
+ return value;
68
+ },
69
+ },
70
+ events: {
71
+ type: 'string', // JSON stringified
72
+ required: false,
73
+ // Stringify object on set
74
+ set: (value?: any) => {
75
+ if (value && typeof value !== 'string') {
76
+ return JSON.stringify(value);
77
+ }
78
+ return value;
79
+ },
80
+ // Parse JSON string to object on get
81
+ get: (value?: string) => {
82
+ return value;
83
+ },
84
+ },
85
+ links: {
86
+ type: 'string', // JSON stringified
87
+ required: false,
88
+ // Stringify object on set
89
+ set: (value?: any) => {
90
+ if (value && typeof value !== 'string') {
91
+ return JSON.stringify(value);
92
+ }
93
+ return value;
94
+ },
95
+ // Parse JSON string to object on get
96
+ get: (value?: string) => {
97
+ return value;
98
+ },
99
+ },
100
+ other: {
101
+ type: 'string',
102
+ required: false,
103
+ },
104
+ startTime: {
105
+ type: 'number',
106
+ required: true,
107
+ },
108
+ endTime: {
109
+ type: 'number',
110
+ required: true,
111
+ },
112
+ },
113
+ indexes: {
114
+ primary: {
115
+ pk: { field: 'pk', composite: ['entity', 'id'] },
116
+ sk: { field: 'sk', composite: [] },
117
+ },
118
+ byName: {
119
+ index: 'gsi1',
120
+ pk: { field: 'gsi1pk', composite: ['entity', 'name'] },
121
+ sk: { field: 'gsi1sk', composite: ['startTime'] },
122
+ },
123
+ byScope: {
124
+ index: 'gsi2',
125
+ pk: { field: 'gsi2pk', composite: ['entity', 'scope'] },
126
+ sk: { field: 'gsi2sk', composite: ['startTime'] },
127
+ },
128
+ },
129
+ });
@@ -0,0 +1,51 @@
1
+ export const baseAttributes = {
2
+ createdAt: {
3
+ type: 'string',
4
+ required: true,
5
+ readOnly: true,
6
+ // Convert Date to ISO string on set
7
+ set: (value?: Date | string) => {
8
+ if (value instanceof Date) {
9
+ return value.toISOString();
10
+ }
11
+ return value || new Date().toISOString();
12
+ },
13
+ // Initialize with current timestamp if not provided
14
+ default: () => new Date().toISOString(),
15
+ },
16
+ updatedAt: {
17
+ type: 'string',
18
+ required: true,
19
+ // Convert Date to ISO string on set
20
+ set: (value?: Date | string) => {
21
+ if (value instanceof Date) {
22
+ return value.toISOString();
23
+ }
24
+ return value || new Date().toISOString();
25
+ },
26
+ // Always use current timestamp when creating/updating
27
+ default: () => new Date().toISOString(),
28
+ },
29
+ metadata: {
30
+ type: 'string', // JSON stringified
31
+ // Stringify objects on set
32
+ set: (value?: Record<string, unknown> | string) => {
33
+ if (value && typeof value !== 'string') {
34
+ return JSON.stringify(value);
35
+ }
36
+ return value;
37
+ },
38
+ // Parse JSON string to object on get
39
+ get: (value?: string) => {
40
+ if (value) {
41
+ try {
42
+ return JSON.parse(value);
43
+ } catch {
44
+ // If parsing fails, return the original string
45
+ return value;
46
+ }
47
+ }
48
+ return value;
49
+ },
50
+ },
51
+ } as const;
@@ -0,0 +1,56 @@
1
+ import { Entity } from 'electrodb';
2
+ import { baseAttributes } from './utils';
3
+
4
+ export const workflowSnapshotEntity = new Entity({
5
+ model: {
6
+ entity: 'workflow_snapshot',
7
+ version: '1',
8
+ service: 'mastra',
9
+ },
10
+ attributes: {
11
+ entity: {
12
+ type: 'string',
13
+ required: true,
14
+ },
15
+ ...baseAttributes,
16
+ workflow_name: {
17
+ type: 'string',
18
+ required: true,
19
+ },
20
+ run_id: {
21
+ type: 'string',
22
+ required: true,
23
+ },
24
+ snapshot: {
25
+ type: 'string', // JSON stringified
26
+ required: true,
27
+ // Stringify snapshot object on set
28
+ set: (value?: any) => {
29
+ if (value && typeof value !== 'string') {
30
+ return JSON.stringify(value);
31
+ }
32
+ return value;
33
+ },
34
+ // Parse JSON string to object on get
35
+ get: (value?: string) => {
36
+ return value ? JSON.parse(value) : value;
37
+ },
38
+ },
39
+ resourceId: {
40
+ type: 'string',
41
+ required: false,
42
+ },
43
+ },
44
+ indexes: {
45
+ primary: {
46
+ pk: { field: 'pk', composite: ['entity', 'workflow_name'] },
47
+ sk: { field: 'sk', composite: ['run_id'] },
48
+ },
49
+ // GSI to allow querying by run_id efficiently without knowing the workflow_name
50
+ gsi2: {
51
+ index: 'gsi2',
52
+ pk: { field: 'gsi2pk', composite: ['entity', 'run_id'] },
53
+ sk: { field: 'gsi2sk', composite: ['workflow_name'] },
54
+ },
55
+ },
56
+ });
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './storage';
@@ -0,0 +1,16 @@
1
+ version: '3.8'
2
+
3
+ services:
4
+ dynamodb-local:
5
+ image: amazon/dynamodb-local:latest
6
+ # Use host network mode for simpler connection from tests,
7
+ # or define ports if running in bridge mode.
8
+ # network_mode: host
9
+ ports:
10
+ - '8000:8000' # Map container port 8000 to host port 8000
11
+ command: ['-jar', 'DynamoDBLocal.jar', '-sharedDb']
12
+ volumes:
13
+ - dynamodb_data:/home/dynamodblocal/data
14
+
15
+ volumes:
16
+ dynamodb_data: {}