Skip to content

Commit 14aaea2

Browse files
Copilothotlong
andcommitted
Address code review feedback - improve MongoDB compatibility, transaction isolation, and process cleanup
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 4912ce5 commit 14aaea2

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

packages/drivers/memory/src/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,12 @@ export class MemoryDriver implements Driver {
575575
async beginTransaction(): Promise<any> {
576576
const txId = `tx_${Date.now()}_${Math.random().toString(36).substring(7)}`;
577577

578-
// Create a snapshot of the current store
579-
const snapshot = new Map(this.store);
578+
// Create a deep snapshot of the current store to ensure complete transaction isolation
579+
// This prevents issues with nested objects and arrays being modified during the transaction
580+
const snapshot = new Map<string, any>();
581+
for (const [key, value] of this.store.entries()) {
582+
snapshot.set(key, JSON.parse(JSON.stringify(value)));
583+
}
580584

581585
const transaction: MemoryTransaction = {
582586
id: txId,
@@ -676,9 +680,11 @@ export class MemoryDriver implements Driver {
676680
}
677681

678682
// Set up auto-save timer
683+
// Use unref() to allow Node.js process to exit gracefully when idle
679684
this.persistenceTimer = setInterval(() => {
680685
this.saveToDisk();
681686
}, autoSaveInterval);
687+
this.persistenceTimer.unref();
682688
}
683689

684690
/**

packages/drivers/mongo/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,10 @@ export class MongoDriver implements Driver {
545545
const result = await collection.findOneAndUpdate(filter, update, mongoOptions);
546546

547547
// Map MongoDB document to API format (convert _id to id)
548-
// MongoDB's findOneAndUpdate returns { value: document } in newer versions
549-
const doc = result?.value || result;
548+
// MongoDB driver v5+ returns { value: document, ok: number }
549+
// Older versions (v4) return the document directly
550+
// We handle both for backward compatibility
551+
const doc = result?.value !== undefined ? result.value : result;
550552
return doc ? this.mapFromMongo(doc) : null;
551553
}
552554

0 commit comments

Comments
 (0)