@mastra/memory 0.0.1
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/README.md +3 -0
- package/dist/cloudflare/index.d.ts +18 -0
- package/dist/cloudflare/kv.d.ts +53 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +8 -0
- package/dist/memory.cjs.development.js +2369 -0
- package/dist/memory.cjs.development.js.map +1 -0
- package/dist/memory.cjs.production.min.js +2 -0
- package/dist/memory.cjs.production.min.js.map +1 -0
- package/dist/memory.esm.js +2361 -0
- package/dist/memory.esm.js.map +1 -0
- package/dist/postgres/index.d.ts +16 -0
- package/dist/redis/index.d.ts +22 -0
- package/dist/redis/providers.d.ts +26 -0
- package/dist/redis/types.d.ts +17 -0
- package/docker-compose.yaml +18 -0
- package/jest.config.ts +19 -0
- package/package.json +49 -0
- package/src/cloudflare/index.test.ts +230 -0
- package/src/cloudflare/index.ts +169 -0
- package/src/cloudflare/kv.ts +139 -0
- package/src/index.ts +3 -0
- package/src/postgres/index.test.ts +60 -0
- package/src/postgres/index.ts +256 -0
- package/src/redis/index.test.ts +245 -0
- package/src/redis/index.ts +189 -0
- package/src/redis/providers.ts +191 -0
- package/src/redis/types.ts +18 -0
- package/tsconfig.json +11 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { Redis as UpstashRedis } from '@upstash/redis';
|
|
2
|
+
import { createClient } from 'redis';
|
|
3
|
+
|
|
4
|
+
import { RedisClient, RedisPipeline } from './types';
|
|
5
|
+
|
|
6
|
+
// Helper functions for date handling
|
|
7
|
+
function serializeData(data: any): any {
|
|
8
|
+
if (data === null || data === undefined) return data;
|
|
9
|
+
|
|
10
|
+
if (data instanceof Date) {
|
|
11
|
+
return { __type: 'Date', value: data.toISOString() };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (Array.isArray(data)) {
|
|
15
|
+
return data.map(item => serializeData(item));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (typeof data === 'object') {
|
|
19
|
+
return Object.keys(data).reduce((acc, key) => {
|
|
20
|
+
acc[key] = serializeData(data[key]);
|
|
21
|
+
return acc;
|
|
22
|
+
}, {} as any);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return data;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function deserializeData(data: any): any {
|
|
29
|
+
if (data === null || data === undefined) return data;
|
|
30
|
+
|
|
31
|
+
if (typeof data === 'object' && data.__type === 'Date') {
|
|
32
|
+
return new Date(data.value);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (Array.isArray(data)) {
|
|
36
|
+
return data.map(item => deserializeData(item));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (typeof data === 'object') {
|
|
40
|
+
return Object.keys(data).reduce((acc, key) => {
|
|
41
|
+
acc[key] = deserializeData(data[key]);
|
|
42
|
+
return acc;
|
|
43
|
+
}, {} as any);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return data;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Provider for Upstash/Vercel KV
|
|
50
|
+
export class UpstashProvider implements RedisClient {
|
|
51
|
+
private client: UpstashRedis;
|
|
52
|
+
|
|
53
|
+
constructor(url: string, token: string) {
|
|
54
|
+
this.client = new UpstashRedis({
|
|
55
|
+
url,
|
|
56
|
+
token,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
async get(key: string) {
|
|
60
|
+
const data = await this.client.get(key);
|
|
61
|
+
return deserializeData(data);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async set(key: string, value: any) {
|
|
65
|
+
return this.client.set(key, serializeData(value));
|
|
66
|
+
}
|
|
67
|
+
async del(key: string) {
|
|
68
|
+
return this.client.del(key);
|
|
69
|
+
}
|
|
70
|
+
async sadd(key: string, value: string) {
|
|
71
|
+
return this.client.sadd(key, value);
|
|
72
|
+
}
|
|
73
|
+
async srem(key: string, value: string) {
|
|
74
|
+
return this.client.srem(key, value);
|
|
75
|
+
}
|
|
76
|
+
async smembers(key: string) {
|
|
77
|
+
return this.client.smembers(key);
|
|
78
|
+
}
|
|
79
|
+
async flushall() {
|
|
80
|
+
return this.client.flushall();
|
|
81
|
+
}
|
|
82
|
+
pipeline() {
|
|
83
|
+
const multi = this.client.multi();
|
|
84
|
+
const pipeline: RedisPipeline = {
|
|
85
|
+
get: (key: string) => {
|
|
86
|
+
multi.get(key);
|
|
87
|
+
return pipeline;
|
|
88
|
+
},
|
|
89
|
+
set: (key: string, value: any) => {
|
|
90
|
+
multi.set(key, JSON.stringify(serializeData(value)));
|
|
91
|
+
return pipeline;
|
|
92
|
+
},
|
|
93
|
+
del: (key: string) => {
|
|
94
|
+
multi.del(key);
|
|
95
|
+
return pipeline;
|
|
96
|
+
},
|
|
97
|
+
srem: (key: string, value: string) => {
|
|
98
|
+
multi.srem(key, value);
|
|
99
|
+
return pipeline;
|
|
100
|
+
},
|
|
101
|
+
exec: async () => {
|
|
102
|
+
const results = await multi.exec();
|
|
103
|
+
return results.map(result => {
|
|
104
|
+
try {
|
|
105
|
+
// For get operations that return string data
|
|
106
|
+
if (typeof result === 'string') {
|
|
107
|
+
return deserializeData(JSON.parse(result));
|
|
108
|
+
}
|
|
109
|
+
// For array results (like from lists/sets)
|
|
110
|
+
if (Array.isArray(result)) {
|
|
111
|
+
return result.map(item => {
|
|
112
|
+
try {
|
|
113
|
+
return typeof item === 'string' ? deserializeData(JSON.parse(item)) : item;
|
|
114
|
+
} catch {
|
|
115
|
+
return item;
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
return result;
|
|
120
|
+
} catch {
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
return pipeline;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export class LocalRedisProvider implements RedisClient {
|
|
131
|
+
private client: any;
|
|
132
|
+
|
|
133
|
+
constructor() {
|
|
134
|
+
this.client = createClient({ url: 'redis://localhost:6379' });
|
|
135
|
+
this.client.connect();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async get(key: string) {
|
|
139
|
+
const data = await this.client.get(key);
|
|
140
|
+
return data ? deserializeData(JSON.parse(data)) : null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async set(key: string, value: any) {
|
|
144
|
+
return this.client.set(key, JSON.stringify(serializeData(value)));
|
|
145
|
+
}
|
|
146
|
+
async del(key: string) {
|
|
147
|
+
return this.client.del(key);
|
|
148
|
+
}
|
|
149
|
+
async sadd(key: string, value: string) {
|
|
150
|
+
return this.client.sAdd(key, value);
|
|
151
|
+
}
|
|
152
|
+
async srem(key: string, value: string) {
|
|
153
|
+
return this.client.sRem(key, value);
|
|
154
|
+
}
|
|
155
|
+
async smembers(key: string) {
|
|
156
|
+
return this.client.sMembers(key);
|
|
157
|
+
}
|
|
158
|
+
async flushall() {
|
|
159
|
+
return this.client.flushAll();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
pipeline() {
|
|
163
|
+
const multi = this.client.multi();
|
|
164
|
+
const pipeline: RedisPipeline = {
|
|
165
|
+
get: (key: string) => {
|
|
166
|
+
multi.get(key);
|
|
167
|
+
return pipeline;
|
|
168
|
+
},
|
|
169
|
+
set: (key: string, value: any) => {
|
|
170
|
+
multi.set(key, JSON.stringify(value));
|
|
171
|
+
return pipeline;
|
|
172
|
+
},
|
|
173
|
+
del: (key: string) => {
|
|
174
|
+
multi.del(key);
|
|
175
|
+
return pipeline;
|
|
176
|
+
},
|
|
177
|
+
srem: (key: string, value: string) => {
|
|
178
|
+
multi.sRem(key, value);
|
|
179
|
+
return pipeline;
|
|
180
|
+
},
|
|
181
|
+
exec: async () => {
|
|
182
|
+
return multi.exec();
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
return pipeline;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async quit() {
|
|
189
|
+
await this.client.quit();
|
|
190
|
+
}
|
|
191
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface RedisClient {
|
|
2
|
+
get(key: string): Promise<any>;
|
|
3
|
+
set(key: string, value: any): Promise<any>;
|
|
4
|
+
del(key: string): Promise<any>;
|
|
5
|
+
sadd(key: string, ...values: string[]): Promise<any>;
|
|
6
|
+
srem(key: string, ...values: string[]): Promise<any>;
|
|
7
|
+
smembers(key: string): Promise<string[]>;
|
|
8
|
+
flushall(): Promise<any>;
|
|
9
|
+
pipeline(): RedisPipeline;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface RedisPipeline {
|
|
13
|
+
get(key: string): RedisPipeline;
|
|
14
|
+
set(key: string, value: any): RedisPipeline;
|
|
15
|
+
del(key: string): RedisPipeline;
|
|
16
|
+
srem(key: string, value: string): RedisPipeline;
|
|
17
|
+
exec(): Promise<any>;
|
|
18
|
+
}
|
package/tsconfig.json
ADDED