@@ -11,7 +11,7 @@ type DriverInterface = Data.DriverInterface;
1111 */
1212
1313import { Driver } from '@objectql/types' ;
14- import { MongoClient , Db , Filter , ObjectId , FindOptions , ChangeStream , ChangeStreamDocument } from 'mongodb' ;
14+ import { MongoClient , Db , Filter , ObjectId , FindOptions , FindOneAndUpdateOptions , ChangeStream , ChangeStreamDocument } from 'mongodb' ;
1515
1616/**
1717 * Change stream event handler callback
@@ -410,9 +410,20 @@ export class MongoDriver implements Driver {
410410 mongoDoc . _id = new ObjectId ( ) . toHexString ( ) ;
411411 }
412412
413- // Pass session for transactional operations
414- const mongoOptions = options ?. session ? { session : options . session } : { } ;
415- const result = await collection . insertOne ( mongoDoc , mongoOptions ) ;
413+ // Add timestamps if not already present
414+ const now = new Date ( ) . toISOString ( ) ;
415+ if ( ! mongoDoc . created_at ) {
416+ mongoDoc . created_at = now ;
417+ }
418+ if ( ! mongoDoc . updated_at ) {
419+ mongoDoc . updated_at = now ;
420+ }
421+
422+ // Pass session for transactional operations only if it exists
423+ const result = options ?. session
424+ ? await collection . insertOne ( mongoDoc , { session : options . session } )
425+ : await collection . insertOne ( mongoDoc ) ;
426+
416427 // Return API format document (convert _id to id)
417428 return this . mapFromMongo ( { ...mongoDoc , _id : result . insertedId } ) ;
418429 }
@@ -422,23 +433,37 @@ export class MongoDriver implements Driver {
422433
423434 // Map API document (id) to MongoDB document (_id) for update data
424435 // But we should not allow updating the _id field itself
425- const { id : _ignoredId , ...updateData } = data ; // intentionally ignore id to prevent updating primary key
436+ const { id : _ignoredId , created_at : _ignoredCreatedAt , ...updateData } = data ; // intentionally ignore id and created_at to prevent updating them
437+
438+ // Add updated_at timestamp
439+ updateData . updated_at = new Date ( ) . toISOString ( ) ;
426440
427441 // Handle atomic operators if present
428442 const isAtomic = Object . keys ( updateData ) . some ( k => k . startsWith ( '$' ) ) ;
429443 const update = isAtomic ? updateData : { $set : updateData } ;
430444
431- // Pass session for transactional operations
432- const mongoOptions = options ?. session ? { session : options . session } : { } ;
433- const result = await collection . updateOne ( { _id : this . normalizeId ( id ) } , update , mongoOptions ) ;
434- return result . modifiedCount ; // or return updated document?
445+ // Use findOneAndUpdate to return the updated document
446+ const mongoOptions : FindOneAndUpdateOptions = { returnDocument : 'after' } ;
447+ if ( options ?. session ) {
448+ mongoOptions . session = options . session ;
449+ }
450+
451+ const result = await collection . findOneAndUpdate (
452+ { _id : this . normalizeId ( id ) } ,
453+ update ,
454+ mongoOptions
455+ ) ;
456+
457+ // Return API format document (convert _id to id)
458+ return this . mapFromMongo ( result ) ;
435459 }
436460
437461 async delete ( objectName : string , id : string | number , options ?: any ) {
438462 const collection = await this . getCollection ( objectName ) ;
439- // Pass session for transactional operations
440- const mongoOptions = options ?. session ? { session : options . session } : { } ;
441- const result = await collection . deleteOne ( { _id : this . normalizeId ( id ) } , mongoOptions ) ;
463+ // Pass session for transactional operations only if it exists
464+ const result = options ?. session
465+ ? await collection . deleteOne ( { _id : this . normalizeId ( id ) } , { session : options . session } )
466+ : await collection . deleteOne ( { _id : this . normalizeId ( id ) } ) ;
442467 return result . deletedCount ;
443468 }
444469
0 commit comments