Skip to content

Commit 591f098

Browse files
committed
Cleanup TTL interface
1 parent a31f027 commit 591f098

File tree

1 file changed

+38
-22
lines changed

1 file changed

+38
-22
lines changed

libs/checkpoint-mongodb/src/index.ts

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,61 @@ export type MongoDBSaverParams = {
1616
dbName?: string;
1717
checkpointCollectionName?: string;
1818
checkpointWritesCollectionName?: string;
19-
/**
20-
* Change 1 Made ttl optional
21-
*/
22-
enableTTL?:boolean
23-
19+
ttl?: { expireAfterSeconds: number };
2420
};
2521

2622
/**
2723
* A LangGraph checkpoint saver backed by a MongoDB database.
2824
*/
2925
export class MongoDBSaver extends BaseCheckpointSaver {
3026
protected client: MongoClient;
31-
27+
3228
protected db: MongoDatabase;
3329

30+
protected ttl: { expireAfterSeconds: number } | undefined;
31+
32+
protected isSetup: boolean;
33+
3434
checkpointCollectionName = "checkpoints";
3535

3636
checkpointWritesCollectionName = "checkpoint_writes";
37-
/** Change 2:
38-
* Conditionally Added _createdATForTTL if ttl is enabled
39-
*/
40-
protected enableTTL:boolean
41-
4237

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+
}
4357

4458
constructor(
4559
{
4660
client,
4761
dbName,
4862
checkpointCollectionName,
4963
checkpointWritesCollectionName,
50-
enableTTL,
64+
ttl,
5165
}: MongoDBSaverParams,
5266
serde?: SerializerProtocol
5367
) {
5468
super(serde);
5569
this.client = client;
56-
this.enableTTL = enableTTL ?? false;
70+
this.ttl = ttl;
5771
this.db = this.client.db(dbName);
72+
this.isSetup = false;
73+
5874
this.checkpointCollectionName =
5975
checkpointCollectionName ?? this.checkpointCollectionName;
6076
this.checkpointWritesCollectionName =
@@ -68,6 +84,8 @@ export class MongoDBSaver extends BaseCheckpointSaver {
6884
* for the given thread ID is retrieved.
6985
*/
7086
async getTuple(config: RunnableConfig): Promise<CheckpointTuple | undefined> {
87+
this.assertSetup();
88+
7189
const {
7290
thread_id,
7391
checkpoint_ns = "",
@@ -97,7 +115,6 @@ export class MongoDBSaver extends BaseCheckpointSaver {
97115
thread_id,
98116
checkpoint_ns,
99117
checkpoint_id: doc.checkpoint_id,
100-
101118
};
102119
const checkpoint = (await this.serde.loadsTyped(
103120
doc.type,
@@ -149,6 +166,8 @@ export class MongoDBSaver extends BaseCheckpointSaver {
149166
config: RunnableConfig,
150167
options?: CheckpointListOptions
151168
): AsyncGenerator<CheckpointTuple> {
169+
this.assertSetup();
170+
152171
const { limit, before, filter } = options ?? {};
153172
const query: Record<string, unknown> = {};
154173

@@ -224,6 +243,8 @@ export class MongoDBSaver extends BaseCheckpointSaver {
224243
checkpoint: Checkpoint,
225244
metadata: CheckpointMetadata
226245
): Promise<RunnableConfig> {
246+
this.assertSetup();
247+
227248
const thread_id = config.configurable?.thread_id;
228249
const checkpoint_ns = config.configurable?.checkpoint_ns ?? "";
229250
const checkpoint_id = checkpoint.id;
@@ -243,20 +264,13 @@ export class MongoDBSaver extends BaseCheckpointSaver {
243264
if (checkpointType !== metadataType) {
244265
throw new Error("Mismatched checkpoint and metadata types.");
245266
}
246-
247267
const doc = {
248268
parent_checkpoint_id: config.configurable?.checkpoint_id,
249269
type: checkpointType,
250270
checkpoint: serializedCheckpoint,
251271
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() } : {}),
256273
};
257-
258-
259-
260274
const upsertQuery = {
261275
thread_id,
262276
checkpoint_ns,
@@ -283,6 +297,8 @@ export class MongoDBSaver extends BaseCheckpointSaver {
283297
writes: PendingWrite[],
284298
taskId: string
285299
): Promise<void> {
300+
this.assertSetup();
301+
286302
const thread_id = config.configurable?.thread_id;
287303
const checkpoint_ns = config.configurable?.checkpoint_ns;
288304
const checkpoint_id = config.configurable?.checkpoint_id;

0 commit comments

Comments
 (0)