Skip to content

Commit 8d6cce9

Browse files
committed
feat(mongodb-checkpoint): add optional TTL support via _createdAtForTTL field
1 parent 86adfb7 commit 8d6cce9

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

libs/checkpoint-mongodb/src/index.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,44 @@ export type MongoDBSaverParams = {
1616
dbName?: string;
1717
checkpointCollectionName?: string;
1818
checkpointWritesCollectionName?: string;
19+
/**
20+
* Change 1 Made ttl optional
21+
*/
22+
enableTTL?:boolean
23+
1924
};
2025

2126
/**
2227
* A LangGraph checkpoint saver backed by a MongoDB database.
2328
*/
2429
export class MongoDBSaver extends BaseCheckpointSaver {
2530
protected client: MongoClient;
26-
31+
2732
protected db: MongoDatabase;
2833

2934
checkpointCollectionName = "checkpoints";
3035

3136
checkpointWritesCollectionName = "checkpoint_writes";
37+
/** Change 2:
38+
* Conditionally Added _createdATForTTL if ttl is enabled
39+
*/
40+
protected enableTTL:boolean
41+
42+
3243

3344
constructor(
3445
{
3546
client,
3647
dbName,
3748
checkpointCollectionName,
3849
checkpointWritesCollectionName,
50+
enableTTL,
3951
}: MongoDBSaverParams,
4052
serde?: SerializerProtocol
4153
) {
4254
super(serde);
4355
this.client = client;
56+
this.enableTTL = enableTTL ?? false;
4457
this.db = this.client.db(dbName);
4558
this.checkpointCollectionName =
4659
checkpointCollectionName ?? this.checkpointCollectionName;
@@ -84,6 +97,7 @@ export class MongoDBSaver extends BaseCheckpointSaver {
8497
thread_id,
8598
checkpoint_ns,
8699
checkpoint_id: doc.checkpoint_id,
100+
87101
};
88102
const checkpoint = (await this.serde.loadsTyped(
89103
doc.type,
@@ -224,12 +238,20 @@ export class MongoDBSaver extends BaseCheckpointSaver {
224238
if (checkpointType !== metadataType) {
225239
throw new Error("Mismatched checkpoint and metadata types.");
226240
}
241+
227242
const doc = {
228243
parent_checkpoint_id: config.configurable?.checkpoint_id,
229244
type: checkpointType,
230245
checkpoint: serializedCheckpoint,
231246
metadata: serializedMetadata,
247+
/** Change 3:
248+
* Conditionally Added _createdATForTTL if ttl is enabled
249+
*/
250+
...(this.enableTTL ? { _createdAtForTTL: new Date() } : {}),
232251
};
252+
253+
254+
233255
const upsertQuery = {
234256
thread_id,
235257
checkpoint_ns,

0 commit comments

Comments
 (0)