@motiadev/adapter-redis-state 0.8.2-beta.140-709523
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/CHANGELOG.md +17 -0
- package/LICENSE +21 -0
- package/README.md +142 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/redis-state-adapter.d.ts +28 -0
- package/dist/redis-state-adapter.d.ts.map +1 -0
- package/dist/redis-state-adapter.js +238 -0
- package/dist/types.d.ts +14 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/package.json +23 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.1.0] - 2025-10-22
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
- Initial release of Redis state adapter for Motia
|
|
7
|
+
- Support for distributed state management across multiple instances
|
|
8
|
+
- TTL support for automatic state cleanup
|
|
9
|
+
- Atomic operations for data consistency
|
|
10
|
+
- Efficient SCAN operations for large datasets
|
|
11
|
+
- Type preservation for state values
|
|
12
|
+
- Support for filtering and querying state items
|
|
13
|
+
- Automatic reconnection handling
|
|
14
|
+
- Configurable key prefixes for namespacing
|
|
15
|
+
- Full TypeScript support with type definitions
|
|
16
|
+
- Comprehensive documentation and examples
|
|
17
|
+
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Motia
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# @motiadev/adapter-redis-state
|
|
2
|
+
|
|
3
|
+
Redis state adapter for Motia framework, enabling distributed state management across multiple instances.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @motiadev/adapter-redis-state
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
Configure the Redis state adapter in your `motia.config.ts`:
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { config } from '@motiadev/core'
|
|
17
|
+
import { RedisStateAdapter } from '@motiadev/adapter-redis-state'
|
|
18
|
+
|
|
19
|
+
export default config({
|
|
20
|
+
adapters: {
|
|
21
|
+
state: new RedisStateAdapter({
|
|
22
|
+
host: process.env.REDIS_HOST || 'localhost',
|
|
23
|
+
port: parseInt(process.env.REDIS_PORT || '6379'),
|
|
24
|
+
password: process.env.REDIS_PASSWORD,
|
|
25
|
+
keyPrefix: 'motia:state:',
|
|
26
|
+
ttl: 3600,
|
|
27
|
+
}),
|
|
28
|
+
},
|
|
29
|
+
})
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Configuration Options
|
|
33
|
+
|
|
34
|
+
### RedisStateAdapterConfig
|
|
35
|
+
|
|
36
|
+
| Option | Type | Default | Description |
|
|
37
|
+
|--------|------|---------|-------------|
|
|
38
|
+
| `host` | `string` | `'localhost'` | Redis server host |
|
|
39
|
+
| `port` | `number` | `6379` | Redis server port |
|
|
40
|
+
| `password` | `string` | `undefined` | Redis authentication password |
|
|
41
|
+
| `username` | `string` | `undefined` | Redis authentication username |
|
|
42
|
+
| `database` | `number` | `0` | Redis database number |
|
|
43
|
+
| `keyPrefix` | `string` | `'motia:state:'` | Prefix for all state keys |
|
|
44
|
+
| `ttl` | `number` | `undefined` | Time-to-live in seconds for state entries |
|
|
45
|
+
| `socket.reconnectStrategy` | `function` | Auto-retry | Custom reconnection strategy |
|
|
46
|
+
| `socket.connectTimeout` | `number` | `10000` | Connection timeout in milliseconds |
|
|
47
|
+
|
|
48
|
+
## Features
|
|
49
|
+
|
|
50
|
+
- **Distributed State**: Share state across multiple Motia instances
|
|
51
|
+
- **Automatic TTL**: Optional automatic cleanup of expired state
|
|
52
|
+
- **Connection Pooling**: Efficient Redis connection management
|
|
53
|
+
- **Automatic Reconnection**: Handles connection failures gracefully
|
|
54
|
+
- **Atomic Operations**: Ensures data consistency
|
|
55
|
+
- **Efficient Scanning**: Uses SCAN for large datasets to avoid blocking
|
|
56
|
+
- **Type Preservation**: Maintains type information for state values
|
|
57
|
+
|
|
58
|
+
## Key Namespacing
|
|
59
|
+
|
|
60
|
+
The adapter uses the following key pattern:
|
|
61
|
+
```
|
|
62
|
+
{keyPrefix}{traceId}:{key}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
For example:
|
|
66
|
+
```
|
|
67
|
+
motia:state:trace-123:userData
|
|
68
|
+
motia:state:trace-456:orderDetails
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Example
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
import { RedisStateAdapter } from '@motiadev/adapter-redis-state'
|
|
75
|
+
|
|
76
|
+
const adapter = new RedisStateAdapter({
|
|
77
|
+
host: 'redis.example.com',
|
|
78
|
+
port: 6379,
|
|
79
|
+
password: 'your-secure-password',
|
|
80
|
+
keyPrefix: 'myapp:motia:state:',
|
|
81
|
+
ttl: 7200,
|
|
82
|
+
socket: {
|
|
83
|
+
connectTimeout: 5000,
|
|
84
|
+
reconnectStrategy: (retries) => {
|
|
85
|
+
if (retries > 20) {
|
|
86
|
+
return new Error('Too many retries')
|
|
87
|
+
}
|
|
88
|
+
return Math.min(retries * 50, 2000)
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
})
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Environment Variables
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
REDIS_HOST=localhost
|
|
98
|
+
REDIS_PORT=6379
|
|
99
|
+
REDIS_PASSWORD=your-password
|
|
100
|
+
REDIS_DATABASE=0
|
|
101
|
+
STATE_KEY_PREFIX=motia:state:
|
|
102
|
+
STATE_TTL=3600
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Performance Considerations
|
|
106
|
+
|
|
107
|
+
- **TTL Settings**: Set appropriate TTL values to prevent memory bloat
|
|
108
|
+
- **Key Prefix**: Use descriptive prefixes to organize keys
|
|
109
|
+
- **Connection Pooling**: Redis client handles connection pooling automatically
|
|
110
|
+
- **Batch Operations**: The adapter uses batch operations where possible
|
|
111
|
+
- **Scan Operations**: Uses SCAN instead of KEYS to avoid blocking
|
|
112
|
+
|
|
113
|
+
## Troubleshooting
|
|
114
|
+
|
|
115
|
+
### Connection Issues
|
|
116
|
+
|
|
117
|
+
If you experience connection problems:
|
|
118
|
+
1. Verify Redis is running and accessible
|
|
119
|
+
2. Check your host, port, and credentials
|
|
120
|
+
3. Ensure firewall rules allow connections on the Redis port
|
|
121
|
+
4. Review Redis logs for errors
|
|
122
|
+
|
|
123
|
+
### Memory Issues
|
|
124
|
+
|
|
125
|
+
If Redis runs out of memory:
|
|
126
|
+
1. Configure appropriate TTL values
|
|
127
|
+
2. Implement state cleanup strategies
|
|
128
|
+
3. Monitor Redis memory usage
|
|
129
|
+
4. Consider using Redis eviction policies
|
|
130
|
+
|
|
131
|
+
### Performance Issues
|
|
132
|
+
|
|
133
|
+
If you experience slow operations:
|
|
134
|
+
1. Monitor Redis server performance
|
|
135
|
+
2. Check network latency between app and Redis
|
|
136
|
+
3. Review key patterns and optimize queries
|
|
137
|
+
4. Consider Redis clustering for large datasets
|
|
138
|
+
|
|
139
|
+
## License
|
|
140
|
+
|
|
141
|
+
MIT
|
|
142
|
+
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAA;AACzD,YAAY,EAAE,uBAAuB,EAAE,MAAM,SAAS,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RedisStateAdapter = void 0;
|
|
4
|
+
var redis_state_adapter_1 = require("./redis-state-adapter");
|
|
5
|
+
Object.defineProperty(exports, "RedisStateAdapter", { enumerable: true, get: function () { return redis_state_adapter_1.RedisStateAdapter; } });
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { StateAdapter, StateItem, StateItemsInput } from '@motiadev/core';
|
|
2
|
+
import type { RedisStateAdapterConfig } from './types';
|
|
3
|
+
export declare class RedisStateAdapter implements StateAdapter {
|
|
4
|
+
private client;
|
|
5
|
+
private keyPrefix;
|
|
6
|
+
private ttl?;
|
|
7
|
+
private connected;
|
|
8
|
+
constructor(config: RedisStateAdapterConfig);
|
|
9
|
+
private connect;
|
|
10
|
+
private ensureConnected;
|
|
11
|
+
private makeKey;
|
|
12
|
+
private makeTracePrefix;
|
|
13
|
+
private extractKey;
|
|
14
|
+
private determineType;
|
|
15
|
+
get<T>(traceId: string, key: string): Promise<T | null>;
|
|
16
|
+
set<T>(traceId: string, key: string, value: T): Promise<T>;
|
|
17
|
+
delete<T>(traceId: string, key: string): Promise<T | null>;
|
|
18
|
+
getGroup<T>(traceId: string): Promise<T[]>;
|
|
19
|
+
clear(traceId: string): Promise<void>;
|
|
20
|
+
cleanup(): Promise<void>;
|
|
21
|
+
keys(traceId: string): Promise<string[]>;
|
|
22
|
+
traceIds(): Promise<string[]>;
|
|
23
|
+
items(input: StateItemsInput): Promise<StateItem[]>;
|
|
24
|
+
private scanKeys;
|
|
25
|
+
private applyFilters;
|
|
26
|
+
private matchesFilter;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=redis-state-adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redis-state-adapter.d.ts","sourceRoot":"","sources":["../src/redis-state-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAe,SAAS,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAE3F,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,SAAS,CAAA;AAEtD,qBAAa,iBAAkB,YAAW,YAAY;IACpD,OAAO,CAAC,MAAM,CAAiB;IAC/B,OAAO,CAAC,SAAS,CAAQ;IACzB,OAAO,CAAC,GAAG,CAAC,CAAQ;IACpB,OAAO,CAAC,SAAS,CAAQ;gBAEb,MAAM,EAAE,uBAAuB;YA2C7B,OAAO;YAWP,eAAe;IAM7B,OAAO,CAAC,OAAO;IAIf,OAAO,CAAC,eAAe;IAIvB,OAAO,CAAC,UAAU;IAKlB,OAAO,CAAC,aAAa;IAQf,GAAG,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IAOvD,GAAG,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAc1D,MAAM,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IAQ1D,QAAQ,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC;IAW1C,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAUrC,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAMxB,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAOxC,QAAQ,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAiB7B,KAAK,CAAC,KAAK,EAAE,eAAe,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC;YAkD3C,QAAQ;IAgBtB,OAAO,CAAC,YAAY;IAMpB,OAAO,CAAC,aAAa;CAmCtB"}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RedisStateAdapter = void 0;
|
|
4
|
+
const redis_1 = require("redis");
|
|
5
|
+
class RedisStateAdapter {
|
|
6
|
+
constructor(config) {
|
|
7
|
+
this.connected = false;
|
|
8
|
+
this.keyPrefix = config.keyPrefix || 'motia:state:';
|
|
9
|
+
this.ttl = config.ttl;
|
|
10
|
+
this.client = (0, redis_1.createClient)({
|
|
11
|
+
socket: {
|
|
12
|
+
host: config.host || 'localhost',
|
|
13
|
+
port: config.port || 6379,
|
|
14
|
+
reconnectStrategy: config.socket?.reconnectStrategy ||
|
|
15
|
+
((retries) => {
|
|
16
|
+
if (retries > 10) {
|
|
17
|
+
return new Error('Redis connection retry limit exceeded');
|
|
18
|
+
}
|
|
19
|
+
return Math.min(retries * 100, 3000);
|
|
20
|
+
}),
|
|
21
|
+
connectTimeout: config.socket?.connectTimeout || 10000,
|
|
22
|
+
},
|
|
23
|
+
password: config.password,
|
|
24
|
+
username: config.username,
|
|
25
|
+
database: config.database || 0,
|
|
26
|
+
});
|
|
27
|
+
this.client.on('error', (err) => {
|
|
28
|
+
console.error('[Redis State] Client error:', err);
|
|
29
|
+
});
|
|
30
|
+
this.client.on('connect', () => {
|
|
31
|
+
this.connected = true;
|
|
32
|
+
});
|
|
33
|
+
this.client.on('disconnect', () => {
|
|
34
|
+
console.warn('[Redis State] Disconnected');
|
|
35
|
+
this.connected = false;
|
|
36
|
+
});
|
|
37
|
+
this.client.on('reconnecting', () => {
|
|
38
|
+
console.log('[Redis State] Reconnecting...');
|
|
39
|
+
});
|
|
40
|
+
this.connect();
|
|
41
|
+
}
|
|
42
|
+
async connect() {
|
|
43
|
+
if (!this.connected && !this.client.isOpen) {
|
|
44
|
+
try {
|
|
45
|
+
await this.client.connect();
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
console.error('[Redis State] Failed to connect:', error);
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async ensureConnected() {
|
|
54
|
+
if (!this.client.isOpen) {
|
|
55
|
+
await this.connect();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
makeKey(traceId, key) {
|
|
59
|
+
return `${this.keyPrefix}${traceId}:${key}`;
|
|
60
|
+
}
|
|
61
|
+
makeTracePrefix(traceId) {
|
|
62
|
+
return `${this.keyPrefix}${traceId}:`;
|
|
63
|
+
}
|
|
64
|
+
extractKey(fullKey, traceId) {
|
|
65
|
+
const prefix = this.makeTracePrefix(traceId);
|
|
66
|
+
return fullKey.slice(prefix.length);
|
|
67
|
+
}
|
|
68
|
+
determineType(value) {
|
|
69
|
+
if (value === null)
|
|
70
|
+
return 'null';
|
|
71
|
+
if (Array.isArray(value))
|
|
72
|
+
return 'array';
|
|
73
|
+
return (['string', 'number', 'boolean', 'object'].find((type) => typeof value === type) || 'object');
|
|
74
|
+
}
|
|
75
|
+
async get(traceId, key) {
|
|
76
|
+
await this.ensureConnected();
|
|
77
|
+
const fullKey = this.makeKey(traceId, key);
|
|
78
|
+
const value = await this.client.get(fullKey);
|
|
79
|
+
return value ? JSON.parse(value) : null;
|
|
80
|
+
}
|
|
81
|
+
async set(traceId, key, value) {
|
|
82
|
+
await this.ensureConnected();
|
|
83
|
+
const fullKey = this.makeKey(traceId, key);
|
|
84
|
+
const serialized = JSON.stringify(value);
|
|
85
|
+
if (this.ttl) {
|
|
86
|
+
await this.client.setEx(fullKey, this.ttl, serialized);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
await this.client.set(fullKey, serialized);
|
|
90
|
+
}
|
|
91
|
+
return value;
|
|
92
|
+
}
|
|
93
|
+
async delete(traceId, key) {
|
|
94
|
+
await this.ensureConnected();
|
|
95
|
+
const fullKey = this.makeKey(traceId, key);
|
|
96
|
+
const value = await this.get(traceId, key);
|
|
97
|
+
await this.client.del(fullKey);
|
|
98
|
+
return value;
|
|
99
|
+
}
|
|
100
|
+
async getGroup(traceId) {
|
|
101
|
+
await this.ensureConnected();
|
|
102
|
+
const pattern = `${this.makeTracePrefix(traceId)}*`;
|
|
103
|
+
const keys = await this.scanKeys(pattern);
|
|
104
|
+
if (keys.length === 0)
|
|
105
|
+
return [];
|
|
106
|
+
const values = await this.client.mGet(keys);
|
|
107
|
+
return values.filter((v) => v !== null).map((v) => JSON.parse(v));
|
|
108
|
+
}
|
|
109
|
+
async clear(traceId) {
|
|
110
|
+
await this.ensureConnected();
|
|
111
|
+
const pattern = `${this.makeTracePrefix(traceId)}*`;
|
|
112
|
+
const keys = await this.scanKeys(pattern);
|
|
113
|
+
if (keys.length > 0) {
|
|
114
|
+
await this.client.del(keys);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
async cleanup() {
|
|
118
|
+
if (this.client.isOpen) {
|
|
119
|
+
await this.client.quit();
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
async keys(traceId) {
|
|
123
|
+
await this.ensureConnected();
|
|
124
|
+
const pattern = `${this.makeTracePrefix(traceId)}*`;
|
|
125
|
+
const keys = await this.scanKeys(pattern);
|
|
126
|
+
return keys.map((key) => this.extractKey(key, traceId));
|
|
127
|
+
}
|
|
128
|
+
async traceIds() {
|
|
129
|
+
await this.ensureConnected();
|
|
130
|
+
const pattern = `${this.keyPrefix}*`;
|
|
131
|
+
const keys = await this.scanKeys(pattern);
|
|
132
|
+
const traceIdSet = new Set();
|
|
133
|
+
for (const key of keys) {
|
|
134
|
+
const withoutPrefix = key.slice(this.keyPrefix.length);
|
|
135
|
+
const traceId = withoutPrefix.split(':')[0];
|
|
136
|
+
if (traceId) {
|
|
137
|
+
traceIdSet.add(traceId);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return Array.from(traceIdSet);
|
|
141
|
+
}
|
|
142
|
+
async items(input) {
|
|
143
|
+
await this.ensureConnected();
|
|
144
|
+
const items = [];
|
|
145
|
+
if (input.groupId) {
|
|
146
|
+
const pattern = `${this.makeTracePrefix(input.groupId)}*`;
|
|
147
|
+
const keys = await this.scanKeys(pattern);
|
|
148
|
+
for (const fullKey of keys) {
|
|
149
|
+
const value = await this.client.get(fullKey);
|
|
150
|
+
if (value !== null) {
|
|
151
|
+
const key = this.extractKey(fullKey, input.groupId);
|
|
152
|
+
const parsedValue = JSON.parse(value);
|
|
153
|
+
items.push({
|
|
154
|
+
groupId: input.groupId,
|
|
155
|
+
key,
|
|
156
|
+
type: this.determineType(parsedValue),
|
|
157
|
+
value: parsedValue,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
const traceIds = await this.traceIds();
|
|
164
|
+
for (const traceId of traceIds) {
|
|
165
|
+
const pattern = `${this.makeTracePrefix(traceId)}*`;
|
|
166
|
+
const keys = await this.scanKeys(pattern);
|
|
167
|
+
for (const fullKey of keys) {
|
|
168
|
+
const value = await this.client.get(fullKey);
|
|
169
|
+
if (value !== null) {
|
|
170
|
+
const key = this.extractKey(fullKey, traceId);
|
|
171
|
+
const parsedValue = JSON.parse(value);
|
|
172
|
+
items.push({
|
|
173
|
+
groupId: traceId,
|
|
174
|
+
key,
|
|
175
|
+
type: this.determineType(parsedValue),
|
|
176
|
+
value: parsedValue,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (input.filter && input.filter.length > 0) {
|
|
183
|
+
return this.applyFilters(items, input.filter);
|
|
184
|
+
}
|
|
185
|
+
return items;
|
|
186
|
+
}
|
|
187
|
+
async scanKeys(pattern) {
|
|
188
|
+
const keys = [];
|
|
189
|
+
let cursor = 0;
|
|
190
|
+
do {
|
|
191
|
+
const result = await this.client.scan(cursor, {
|
|
192
|
+
MATCH: pattern,
|
|
193
|
+
COUNT: 100,
|
|
194
|
+
});
|
|
195
|
+
cursor = result.cursor;
|
|
196
|
+
keys.push(...result.keys);
|
|
197
|
+
} while (cursor !== 0);
|
|
198
|
+
return keys;
|
|
199
|
+
}
|
|
200
|
+
applyFilters(items, filters) {
|
|
201
|
+
return items.filter((item) => {
|
|
202
|
+
return filters.every((filter) => this.matchesFilter(item, filter));
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
matchesFilter(item, filter) {
|
|
206
|
+
const value = typeof item.value === 'object' && item.value !== null ? item.value[filter.valueKey] : item.value;
|
|
207
|
+
const filterValue = filter.value;
|
|
208
|
+
switch (filter.operation) {
|
|
209
|
+
case 'eq':
|
|
210
|
+
return value === filterValue;
|
|
211
|
+
case 'neq':
|
|
212
|
+
return value !== filterValue;
|
|
213
|
+
case 'gt':
|
|
214
|
+
return value > filterValue;
|
|
215
|
+
case 'gte':
|
|
216
|
+
return value >= filterValue;
|
|
217
|
+
case 'lt':
|
|
218
|
+
return value < filterValue;
|
|
219
|
+
case 'lte':
|
|
220
|
+
return value <= filterValue;
|
|
221
|
+
case 'contains':
|
|
222
|
+
return typeof value === 'string' && value.includes(filterValue);
|
|
223
|
+
case 'notContains':
|
|
224
|
+
return typeof value === 'string' && !value.includes(filterValue);
|
|
225
|
+
case 'startsWith':
|
|
226
|
+
return typeof value === 'string' && value.startsWith(filterValue);
|
|
227
|
+
case 'endsWith':
|
|
228
|
+
return typeof value === 'string' && value.endsWith(filterValue);
|
|
229
|
+
case 'isNotNull':
|
|
230
|
+
return value !== null && value !== undefined;
|
|
231
|
+
case 'isNull':
|
|
232
|
+
return value === null || value === undefined;
|
|
233
|
+
default:
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
exports.RedisStateAdapter = RedisStateAdapter;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface RedisStateAdapterConfig {
|
|
2
|
+
host?: string;
|
|
3
|
+
port?: number;
|
|
4
|
+
password?: string;
|
|
5
|
+
username?: string;
|
|
6
|
+
database?: number;
|
|
7
|
+
keyPrefix?: string;
|
|
8
|
+
ttl?: number;
|
|
9
|
+
socket?: {
|
|
10
|
+
reconnectStrategy?: (retries: number) => number | Error;
|
|
11
|
+
connectTimeout?: number;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,uBAAuB;IACtC,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,MAAM,CAAC,EAAE;QACP,iBAAiB,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM,GAAG,KAAK,CAAA;QACvD,cAAc,CAAC,EAAE,MAAM,CAAA;KACxB,CAAA;CACF"}
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@motiadev/adapter-redis-state",
|
|
3
|
+
"description": "Redis state adapter for Motia framework, enabling distributed state management across multiple instances.",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"version": "0.8.2-beta.140-709523",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"redis": "^4.7.0",
|
|
9
|
+
"@motiadev/core": "0.8.2-beta.140-709523"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@types/node": "^22.10.2",
|
|
13
|
+
"typescript": "^5.7.2"
|
|
14
|
+
},
|
|
15
|
+
"peerDependencies": {
|
|
16
|
+
"@motiadev/core": "^0.8.0"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "rm -rf dist && tsc",
|
|
20
|
+
"lint": "biome check .",
|
|
21
|
+
"watch": "tsc --watch"
|
|
22
|
+
}
|
|
23
|
+
}
|