@@ -16,45 +16,61 @@ export type MongoDBSaverParams = {
16
16
dbName ?: string ;
17
17
checkpointCollectionName ?: string ;
18
18
checkpointWritesCollectionName ?: string ;
19
- /**
20
- * Change 1 Made ttl optional
21
- */
22
- enableTTL ?:boolean
23
-
19
+ ttl ?: { expireAfterSeconds : number } ;
24
20
} ;
25
21
26
22
/**
27
23
* A LangGraph checkpoint saver backed by a MongoDB database.
28
24
*/
29
25
export class MongoDBSaver extends BaseCheckpointSaver {
30
26
protected client : MongoClient ;
31
-
27
+
32
28
protected db : MongoDatabase ;
33
29
30
+ protected ttl : { expireAfterSeconds : number } | undefined ;
31
+
32
+ protected isSetup : boolean ;
33
+
34
34
checkpointCollectionName = "checkpoints" ;
35
35
36
36
checkpointWritesCollectionName = "checkpoint_writes" ;
37
- /** Change 2:
38
- * Conditionally Added _createdATForTTL if ttl is enabled
39
- */
40
- protected enableTTL :boolean
41
-
42
37
38
+ async setup ( ) : Promise < void > {
39
+ await this . db . createIndex (
40
+ this . checkpointCollectionName ,
41
+ { _createdAtForTTL : 1 } ,
42
+ { expireAfterSeconds : 60 * 60 }
43
+ ) ;
44
+ this . isSetup = true ;
45
+ }
46
+
47
+ protected assertSetup ( ) {
48
+ // Skip setup check if TTL is not enabled
49
+ if ( this . ttl == null ) return ;
50
+
51
+ if ( ! this . isSetup ) {
52
+ throw new Error (
53
+ "MongoDBSaver is not initialized. Please call `MongoDBSaver.setup()` first before using the checkpointer."
54
+ ) ;
55
+ }
56
+ }
43
57
44
58
constructor (
45
59
{
46
60
client,
47
61
dbName,
48
62
checkpointCollectionName,
49
63
checkpointWritesCollectionName,
50
- enableTTL ,
64
+ ttl ,
51
65
} : MongoDBSaverParams ,
52
66
serde ?: SerializerProtocol
53
67
) {
54
68
super ( serde ) ;
55
69
this . client = client ;
56
- this . enableTTL = enableTTL ?? false ;
70
+ this . ttl = ttl ;
57
71
this . db = this . client . db ( dbName ) ;
72
+ this . isSetup = false ;
73
+
58
74
this . checkpointCollectionName =
59
75
checkpointCollectionName ?? this . checkpointCollectionName ;
60
76
this . checkpointWritesCollectionName =
@@ -68,6 +84,8 @@ export class MongoDBSaver extends BaseCheckpointSaver {
68
84
* for the given thread ID is retrieved.
69
85
*/
70
86
async getTuple ( config : RunnableConfig ) : Promise < CheckpointTuple | undefined > {
87
+ this . assertSetup ( ) ;
88
+
71
89
const {
72
90
thread_id,
73
91
checkpoint_ns = "" ,
@@ -97,7 +115,6 @@ export class MongoDBSaver extends BaseCheckpointSaver {
97
115
thread_id,
98
116
checkpoint_ns,
99
117
checkpoint_id : doc . checkpoint_id ,
100
-
101
118
} ;
102
119
const checkpoint = ( await this . serde . loadsTyped (
103
120
doc . type ,
@@ -149,6 +166,8 @@ export class MongoDBSaver extends BaseCheckpointSaver {
149
166
config : RunnableConfig ,
150
167
options ?: CheckpointListOptions
151
168
) : AsyncGenerator < CheckpointTuple > {
169
+ this . assertSetup ( ) ;
170
+
152
171
const { limit, before, filter } = options ?? { } ;
153
172
const query : Record < string , unknown > = { } ;
154
173
@@ -224,6 +243,8 @@ export class MongoDBSaver extends BaseCheckpointSaver {
224
243
checkpoint : Checkpoint ,
225
244
metadata : CheckpointMetadata
226
245
) : Promise < RunnableConfig > {
246
+ this . assertSetup ( ) ;
247
+
227
248
const thread_id = config . configurable ?. thread_id ;
228
249
const checkpoint_ns = config . configurable ?. checkpoint_ns ?? "" ;
229
250
const checkpoint_id = checkpoint . id ;
@@ -243,20 +264,13 @@ export class MongoDBSaver extends BaseCheckpointSaver {
243
264
if ( checkpointType !== metadataType ) {
244
265
throw new Error ( "Mismatched checkpoint and metadata types." ) ;
245
266
}
246
-
247
267
const doc = {
248
268
parent_checkpoint_id : config . configurable ?. checkpoint_id ,
249
269
type : checkpointType ,
250
270
checkpoint : serializedCheckpoint ,
251
271
metadata : serializedMetadata ,
252
- /** Change 3:
253
- * Conditionally Added _createdATForTTL if ttl is enabled
254
- */
255
- ...( this . enableTTL ? { _createdAtForTTL : new Date ( ) } : { } ) ,
272
+ ...( this . ttl ? { _createdAtForTTL : new Date ( ) } : { } ) ,
256
273
} ;
257
-
258
-
259
-
260
274
const upsertQuery = {
261
275
thread_id,
262
276
checkpoint_ns,
@@ -283,6 +297,8 @@ export class MongoDBSaver extends BaseCheckpointSaver {
283
297
writes : PendingWrite [ ] ,
284
298
taskId : string
285
299
) : Promise < void > {
300
+ this . assertSetup ( ) ;
301
+
286
302
const thread_id = config . configurable ?. thread_id ;
287
303
const checkpoint_ns = config . configurable ?. checkpoint_ns ;
288
304
const checkpoint_id = config . configurable ?. checkpoint_id ;
0 commit comments