@@ -11,7 +11,7 @@ type DriverInterface = Data.DriverInterface;
1111 */
1212
1313import { Driver } from '@objectql/types' ;
14- import { MongoClient , Db , Filter , ObjectId , FindOptions , FindOneAndUpdateOptions , ChangeStream , ChangeStreamDocument } from 'mongodb' ;
14+ import { MongoClient , Db , Filter , ObjectId , FindOptions , FindOneAndUpdateOptions , UpdateFilter , ChangeStream , ChangeStreamDocument } from 'mongodb' ;
1515
1616/**
1717 * Change stream event handler callback
@@ -435,12 +435,24 @@ export class MongoDriver implements Driver {
435435 // But we should not allow updating the _id field itself
436436 const { id : _ignoredId , created_at : _ignoredCreatedAt , ...updateData } = data ; // intentionally ignore id and created_at to prevent updating them
437437
438- // Add updated_at timestamp
439- updateData . updated_at = new Date ( ) . toISOString ( ) ;
440-
441438 // Handle atomic operators if present
442439 const isAtomic = Object . keys ( updateData ) . some ( k => k . startsWith ( '$' ) ) ;
443- const update = isAtomic ? updateData : { $set : updateData } ;
440+
441+ // Build the update object with updated_at timestamp
442+ let update : UpdateFilter < any > ;
443+ if ( isAtomic ) {
444+ // When using atomic operators, add updated_at to $set
445+ // The spread is safe because id and created_at were already removed via destructuring
446+ update = { ...updateData } as any ;
447+ if ( ! update . $set ) {
448+ update . $set = { } ;
449+ }
450+ update . $set . updated_at = new Date ( ) . toISOString ( ) ;
451+ } else {
452+ // For regular updates, add updated_at to the data
453+ updateData . updated_at = new Date ( ) . toISOString ( ) ;
454+ update = { $set : updateData } ;
455+ }
444456
445457 // Use findOneAndUpdate to return the updated document
446458 const mongoOptions : FindOneAndUpdateOptions = { returnDocument : 'after' } ;
0 commit comments