@@ -10,7 +10,7 @@ type DriverInterface = z.infer<typeof Data.DriverInterface>;
1010 * LICENSE file in the root directory of this source tree.
1111 */
1212
13- import { Driver } from '@objectql/types' ;
13+ import { Driver , ObjectQLError } from '@objectql/types' ;
1414import { MongoClient , Db , Filter , ObjectId , FindOptions , FindOneAndUpdateOptions , UpdateFilter , ChangeStream , ChangeStreamDocument } from 'mongodb' ;
1515
1616/**
@@ -126,7 +126,7 @@ export class MongoDriver implements Driver {
126126
127127 private async getCollection ( objectName : string ) {
128128 await this . connected ;
129- if ( ! this . db ) throw new Error ( " Database not initialized" ) ;
129+ if ( ! this . db ) throw new ObjectQLError ( { code : 'DRIVER_CONNECTION_FAILED' , message : ' Database not initialized' } ) ;
130130 return this . db . collection < any > ( objectName ) ;
131131 }
132132
@@ -651,7 +651,7 @@ export class MongoDriver implements Driver {
651651 */
652652 async commitTransaction ( transaction : any ) : Promise < void > {
653653 if ( ! transaction || typeof transaction . commitTransaction !== 'function' ) {
654- throw new Error ( ' Invalid transaction object. Must be a MongoDB ClientSession.') ;
654+ throw new ObjectQLError ( { code : 'DRIVER_TRANSACTION_FAILED' , message : ' Invalid transaction object. Must be a MongoDB ClientSession.' } ) ;
655655 }
656656
657657 try {
@@ -677,7 +677,7 @@ export class MongoDriver implements Driver {
677677 */
678678 async rollbackTransaction ( transaction : any ) : Promise < void > {
679679 if ( ! transaction || typeof transaction . abortTransaction !== 'function' ) {
680- throw new Error ( ' Invalid transaction object. Must be a MongoDB ClientSession.') ;
680+ throw new ObjectQLError ( { code : 'DRIVER_TRANSACTION_FAILED' , message : ' Invalid transaction object. Must be a MongoDB ClientSession.' } ) ;
681681 }
682682
683683 try {
@@ -824,7 +824,7 @@ export class MongoDriver implements Driver {
824824 switch ( command . type ) {
825825 case 'create' :
826826 if ( ! command . data ) {
827- throw new Error ( ' Create command requires data') ;
827+ throw new ObjectQLError ( { code : 'DRIVER_QUERY_FAILED' , message : ' Create command requires data' } ) ;
828828 }
829829 const created = await this . create ( command . object , command . data , cmdOptions ) ;
830830 return {
@@ -835,7 +835,7 @@ export class MongoDriver implements Driver {
835835
836836 case 'update' :
837837 if ( ! command . id || ! command . data ) {
838- throw new Error ( ' Update command requires id and data') ;
838+ throw new ObjectQLError ( { code : 'DRIVER_QUERY_FAILED' , message : ' Update command requires id and data' } ) ;
839839 }
840840 const updated = await this . update ( command . object , command . id , command . data , cmdOptions ) ;
841841 return {
@@ -846,7 +846,7 @@ export class MongoDriver implements Driver {
846846
847847 case 'delete' :
848848 if ( ! command . id ) {
849- throw new Error ( ' Delete command requires id') ;
849+ throw new ObjectQLError ( { code : 'DRIVER_QUERY_FAILED' , message : ' Delete command requires id' } ) ;
850850 }
851851 const deleteCount = await this . delete ( command . object , command . id , cmdOptions ) ;
852852 return {
@@ -856,7 +856,7 @@ export class MongoDriver implements Driver {
856856
857857 case 'bulkCreate' :
858858 if ( ! command . records || ! Array . isArray ( command . records ) ) {
859- throw new Error ( ' BulkCreate command requires records array') ;
859+ throw new ObjectQLError ( { code : 'DRIVER_QUERY_FAILED' , message : ' BulkCreate command requires records array' } ) ;
860860 }
861861 const bulkCreated = await this . createMany ( command . object , command . records , cmdOptions ) ;
862862 return {
@@ -867,7 +867,7 @@ export class MongoDriver implements Driver {
867867
868868 case 'bulkUpdate' :
869869 if ( ! command . updates || ! Array . isArray ( command . updates ) ) {
870- throw new Error ( ' BulkUpdate command requires updates array') ;
870+ throw new ObjectQLError ( { code : 'DRIVER_QUERY_FAILED' , message : ' BulkUpdate command requires updates array' } ) ;
871871 }
872872 let updateCount = 0 ;
873873 const updateResults = [ ] ;
@@ -884,7 +884,7 @@ export class MongoDriver implements Driver {
884884
885885 case 'bulkDelete' :
886886 if ( ! command . ids || ! Array . isArray ( command . ids ) ) {
887- throw new Error ( ' BulkDelete command requires ids array') ;
887+ throw new ObjectQLError ( { code : 'DRIVER_QUERY_FAILED' , message : ' BulkDelete command requires ids array' } ) ;
888888 }
889889 let deleted = 0 ;
890890 for ( const id of command . ids ) {
@@ -898,7 +898,7 @@ export class MongoDriver implements Driver {
898898
899899 default :
900900 const validTypes = [ 'create' , 'update' , 'delete' , 'bulkCreate' , 'bulkUpdate' , 'bulkDelete' ] ;
901- throw new Error ( `Unknown command type: ${ ( command as any ) . type } . Valid types are: ${ validTypes . join ( ', ' ) } ` ) ;
901+ throw new ObjectQLError ( { code : 'DRIVER_UNSUPPORTED_OPERATION' , message : `Unknown command type: ${ ( command as any ) . type } . Valid types are: ${ validTypes . join ( ', ' ) } ` } ) ;
902902 }
903903 } catch ( error : any ) {
904904 return {
@@ -934,11 +934,12 @@ export class MongoDriver implements Driver {
934934 // MongoDB driver doesn't support raw command execution in the traditional SQL sense
935935 // Use executeCommand() instead for mutations (create/update/delete)
936936 // Example: await driver.executeCommand({ type: 'create', object: 'users', data: {...} })
937- throw new Error (
938- 'MongoDB driver does not support raw command execution. ' +
939- 'Use executeCommand() for mutations or aggregate() for complex queries. ' +
940- 'Example: driver.executeCommand({ type: "create", object: "users", data: {...} })'
941- ) ;
937+ throw new ObjectQLError ( {
938+ code : 'DRIVER_UNSUPPORTED_OPERATION' ,
939+ message : 'MongoDB driver does not support raw command execution. ' +
940+ 'Use executeCommand() for mutations or aggregate() for complex queries. ' +
941+ 'Example: driver.executeCommand({ type: "create", object: "users", data: {...} })'
942+ } ) ;
942943 }
943944}
944945
0 commit comments