@@ -10,20 +10,18 @@ const escapeRegex = (value) => {
1010} ;
1111
1212class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataSourceConnector {
13- db : MongoClient
1413
15- constructor ( { url } : { url : string } ) {
16- super ( ) ;
17- this . db = new MongoClient ( url ) ;
14+ async setupClient ( url ) : Promise < void > {
15+ this . client = new MongoClient ( url ) ;
1816 ( async ( ) => {
1917 try {
20- await this . db . connect ( ) ;
21- this . db . on ( 'error' , ( err ) => {
18+ await this . client . connect ( ) ;
19+ this . client . on ( 'error' , ( err ) => {
2220 console . log ( 'Mongo error: ' , err . message )
23- } ) ;
21+ } ) ;
2422 console . log ( 'Connected to Mongo' ) ;
2523 } catch ( e ) {
26- console . error ( 'ERROR: Failed to connect to Mongo' , e ) ;
24+ console . error ( ` Failed to connect to Mongo: ${ e } ` ) ;
2725 }
2826 } ) ( ) ;
2927 }
@@ -133,7 +131,7 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
133131 // const columns = resource.dataSourceColumns.filter(c=> !c.virtual).map((col) => col.name).join(', ');
134132 const tableName = resource . table ;
135133
136- const collection = this . db . db ( ) . collection ( tableName ) ;
134+ const collection = this . client . db ( ) . collection ( tableName ) ;
137135 const query = await this . genQuery ( { filters } ) ;
138136
139137 const sortArray : any [ ] = sort . map ( ( s ) => {
@@ -154,7 +152,7 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
154152 filters : { field : string , operator : AdminForthFilterOperators , value : any } [ ]
155153 } ) : Promise < number > {
156154
157- const collection = this . db . db ( ) . collection ( resource . table ) ;
155+ const collection = this . client . db ( ) . collection ( resource . table ) ;
158156 const query = { } ;
159157 for ( const filter of filters ) {
160158 query [ filter . field ] = this . OperatorsMap [ filter . operator ] ( filter . value ) ;
@@ -164,7 +162,7 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
164162
165163 async getMinMaxForColumnsWithOriginalTypes ( { resource, columns } ) {
166164 const tableName = resource . table ;
167- const collection = this . db . db ( ) . collection ( tableName ) ;
165+ const collection = this . client . db ( ) . collection ( tableName ) ;
168166 const result = { } ;
169167 for ( const column of columns ) {
170168 result [ column ] = await collection
@@ -178,7 +176,7 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
178176
179177 async createRecordOriginalValues ( { resource, record } ) {
180178 const tableName = resource . table ;
181- const collection = this . db . db ( ) . collection ( tableName ) ;
179+ const collection = this . client . db ( ) . collection ( tableName ) ;
182180 const columns = Object . keys ( record ) ;
183181 const newRecord = { } ;
184182 for ( const colName of columns ) {
@@ -188,19 +186,19 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
188186 }
189187
190188 async updateRecordOriginalValues ( { resource, recordId, newValues } ) {
191- const collection = this . db . db ( ) . collection ( resource . table ) ;
189+ const collection = this . client . db ( ) . collection ( resource . table ) ;
192190 await collection . updateOne ( { [ this . getPrimaryKey ( resource ) ] : recordId } , { $set : newValues } ) ;
193191 }
194192
195193 async deleteRecord ( { resource, recordId } ) : Promise < boolean > {
196194 const primaryKey = this . getPrimaryKey ( resource ) ;
197- const collection = this . db . db ( ) . collection ( resource . table ) ;
195+ const collection = this . client . db ( ) . collection ( resource . table ) ;
198196 const res = await collection . deleteOne ( { [ primaryKey ] : recordId } ) ;
199197 return res . deletedCount > 0 ;
200198 }
201199
202200 async close ( ) {
203- await this . db . close ( )
201+ await this . client . close ( )
204202 }
205203}
206204
0 commit comments