@@ -10,20 +10,18 @@ const escapeRegex = (value) => {
10
10
} ;
11
11
12
12
class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataSourceConnector {
13
- db : MongoClient
14
13
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 ) ;
18
16
( async ( ) => {
19
17
try {
20
- await this . db . connect ( ) ;
21
- this . db . on ( 'error' , ( err ) => {
18
+ await this . client . connect ( ) ;
19
+ this . client . on ( 'error' , ( err ) => {
22
20
console . log ( 'Mongo error: ' , err . message )
23
- } ) ;
21
+ } ) ;
24
22
console . log ( 'Connected to Mongo' ) ;
25
23
} catch ( e ) {
26
- console . error ( 'ERROR: Failed to connect to Mongo' , e ) ;
24
+ console . error ( ` Failed to connect to Mongo: ${ e } ` ) ;
27
25
}
28
26
} ) ( ) ;
29
27
}
@@ -133,7 +131,7 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
133
131
// const columns = resource.dataSourceColumns.filter(c=> !c.virtual).map((col) => col.name).join(', ');
134
132
const tableName = resource . table ;
135
133
136
- const collection = this . db . db ( ) . collection ( tableName ) ;
134
+ const collection = this . client . db ( ) . collection ( tableName ) ;
137
135
const query = await this . genQuery ( { filters } ) ;
138
136
139
137
const sortArray : any [ ] = sort . map ( ( s ) => {
@@ -154,7 +152,7 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
154
152
filters : { field : string , operator : AdminForthFilterOperators , value : any } [ ]
155
153
} ) : Promise < number > {
156
154
157
- const collection = this . db . db ( ) . collection ( resource . table ) ;
155
+ const collection = this . client . db ( ) . collection ( resource . table ) ;
158
156
const query = { } ;
159
157
for ( const filter of filters ) {
160
158
query [ filter . field ] = this . OperatorsMap [ filter . operator ] ( filter . value ) ;
@@ -164,7 +162,7 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
164
162
165
163
async getMinMaxForColumnsWithOriginalTypes ( { resource, columns } ) {
166
164
const tableName = resource . table ;
167
- const collection = this . db . db ( ) . collection ( tableName ) ;
165
+ const collection = this . client . db ( ) . collection ( tableName ) ;
168
166
const result = { } ;
169
167
for ( const column of columns ) {
170
168
result [ column ] = await collection
@@ -178,7 +176,7 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
178
176
179
177
async createRecordOriginalValues ( { resource, record } ) {
180
178
const tableName = resource . table ;
181
- const collection = this . db . db ( ) . collection ( tableName ) ;
179
+ const collection = this . client . db ( ) . collection ( tableName ) ;
182
180
const columns = Object . keys ( record ) ;
183
181
const newRecord = { } ;
184
182
for ( const colName of columns ) {
@@ -188,19 +186,19 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
188
186
}
189
187
190
188
async updateRecordOriginalValues ( { resource, recordId, newValues } ) {
191
- const collection = this . db . db ( ) . collection ( resource . table ) ;
189
+ const collection = this . client . db ( ) . collection ( resource . table ) ;
192
190
await collection . updateOne ( { [ this . getPrimaryKey ( resource ) ] : recordId } , { $set : newValues } ) ;
193
191
}
194
192
195
193
async deleteRecord ( { resource, recordId } ) : Promise < boolean > {
196
194
const primaryKey = this . getPrimaryKey ( resource ) ;
197
- const collection = this . db . db ( ) . collection ( resource . table ) ;
195
+ const collection = this . client . db ( ) . collection ( resource . table ) ;
198
196
const res = await collection . deleteOne ( { [ primaryKey ] : recordId } ) ;
199
197
return res . deletedCount > 0 ;
200
198
}
201
199
202
200
async close ( ) {
203
- await this . db . close ( )
201
+ await this . client . close ( )
204
202
}
205
203
}
206
204
0 commit comments