@@ -16,31 +16,44 @@ 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
24
} ;
20
25
21
26
/**
22
27
* A LangGraph checkpoint saver backed by a MongoDB database.
23
28
*/
24
29
export class MongoDBSaver extends BaseCheckpointSaver {
25
30
protected client : MongoClient ;
26
-
31
+
27
32
protected db : MongoDatabase ;
28
33
29
34
checkpointCollectionName = "checkpoints" ;
30
35
31
36
checkpointWritesCollectionName = "checkpoint_writes" ;
37
+ /** Change 2:
38
+ * Conditionally Added _createdATForTTL if ttl is enabled
39
+ */
40
+ protected enableTTL :boolean
41
+
42
+
32
43
33
44
constructor (
34
45
{
35
46
client,
36
47
dbName,
37
48
checkpointCollectionName,
38
49
checkpointWritesCollectionName,
50
+ enableTTL,
39
51
} : MongoDBSaverParams ,
40
52
serde ?: SerializerProtocol
41
53
) {
42
54
super ( serde ) ;
43
55
this . client = client ;
56
+ this . enableTTL = enableTTL ?? false ;
44
57
this . db = this . client . db ( dbName ) ;
45
58
this . checkpointCollectionName =
46
59
checkpointCollectionName ?? this . checkpointCollectionName ;
@@ -84,6 +97,7 @@ export class MongoDBSaver extends BaseCheckpointSaver {
84
97
thread_id,
85
98
checkpoint_ns,
86
99
checkpoint_id : doc . checkpoint_id ,
100
+
87
101
} ;
88
102
const checkpoint = ( await this . serde . loadsTyped (
89
103
doc . type ,
@@ -224,12 +238,20 @@ export class MongoDBSaver extends BaseCheckpointSaver {
224
238
if ( checkpointType !== metadataType ) {
225
239
throw new Error ( "Mismatched checkpoint and metadata types." ) ;
226
240
}
241
+
227
242
const doc = {
228
243
parent_checkpoint_id : config . configurable ?. checkpoint_id ,
229
244
type : checkpointType ,
230
245
checkpoint : serializedCheckpoint ,
231
246
metadata : serializedMetadata ,
247
+ /** Change 3:
248
+ * Conditionally Added _createdATForTTL if ttl is enabled
249
+ */
250
+ ...( this . enableTTL ? { _createdAtForTTL : new Date ( ) } : { } ) ,
232
251
} ;
252
+
253
+
254
+
233
255
const upsertQuery = {
234
256
thread_id,
235
257
checkpoint_ns,
0 commit comments