@@ -4,6 +4,7 @@ import {MongoClient} from "mongodb";
44export class DbClient {
55
66 static async connect ( ) : Promise < any > {
7+
78 return new Promise ( ( resolve , reject ) => {
89 MongoClient . connect ( Config . database . path , ( err : Error , client : any ) => {
910 if ( err ) return reject ( err ) ;
@@ -24,100 +25,69 @@ export class DbClient {
2425 }
2526
2627 static async insertMany ( collection : string , data : any [ ] ) : Promise < any [ ] > {
27- return this . connect ( )
28- . then ( ( client : any ) => {
29- return client . db ( Config . database . db )
30- . collection ( collection )
31- . insertMany ( data )
32- . then ( ( ) => {
33- client . close ( ) ;
34- return data ;
35- } ) ;
36- } ) ;
28+ const client : any = await this . connect ( ) ;
29+ await client . db ( Config . database . db ) ;
30+ client . close ( ) ;
31+ return data ;
3732 }
3833
3934 static async insertOne ( collection : string , data : any ) : Promise < any > {
40- return this . connect ( )
41- . then ( ( client : any ) => {
42- return client . db ( Config . database . db )
43- . collection ( collection )
44- . insertOne ( data )
45- . then ( ( ) => {
46- client . close ( ) ;
47- return data ;
48- } ) ;
49- } ) ;
35+ const client : any = await this . connect ( ) ;
36+ await client . db ( Config . database . db )
37+ . collection ( collection )
38+ . insertOne ( data ) ;
39+ client . close ( ) ;
40+ return data ;
5041 }
5142
5243 static async insertOneIfNotExist ( collection : string , filter : any , data : any ) : Promise < any > {
53- return this . connect ( )
54- . then ( ( client : any ) => {
55- return client . db ( Config . database . db )
56- . collection ( collection )
57- . findOne ( filter )
58- . then ( ( result : any ) => {
59- if ( result ) throw new Error ( 'data already exist' ) ;
60- return this . insertOne ( collection , data )
61- . then ( ( ) => {
62- return data ;
63- } ) ;
64- } ) ;
65- } ) ;
44+ const client : any = await this . connect ( ) ;
45+ const result : any = await client . db ( Config . database . db )
46+ . collection ( collection )
47+ . findOne ( filter ) ;
48+ if ( result ) throw new Error ( 'data already exist' ) ;
49+ await this . insertOne ( collection , data ) ;
50+ return data ;
6651 }
6752
6853 static async find ( collection : string ) : Promise < any [ ] > {
69- return this . connect ( )
70- . then ( ( client : any ) => {
71- return client . db ( Config . database . db )
72- . collection ( collection )
73- . find ( )
74- . toArray ( ) ;
75- } ) ;
54+ const client : any = await this . connect ( ) ;
55+ return await client . db ( Config . database . db )
56+ . collection ( collection )
57+ . find ( )
58+ . toArray ( ) ;
7659 }
7760
7861 static async findOne ( collection : string , filter : any ) : Promise < any > {
79- return this . connect ( )
80- . then ( ( client : any ) => {
81- return client . db ( Config . database . db )
82- . collection ( collection )
83- . findOne ( filter )
84- . then ( ( result : any ) => {
85- client . close ( ) ;
86- return result ;
87- } ) ;
88- } ) ;
62+ const client : any = await this . connect ( ) ;
63+ const result : any = await client . db ( Config . database . db )
64+ . collection ( collection )
65+ . findOne ( filter ) ;
66+ client . close ( ) ;
67+ return result ;
8968 }
9069
9170 static async findOneAndUpdate ( collection : string , filter : any , update : any ) : Promise < any > {
92- return this . connect ( )
93- . then ( ( client : any ) => {
94- return client . db ( Config . database . db )
95- . collection ( collection )
96- . findOneAndUpdate ( filter , { $set : update } )
97- . then ( ( ) => {
98- client . close ( ) ;
99- return update ;
100- } ) ;
101- } ) ;
71+ const client : any = await this . connect ( ) ;
72+ const result : any = await client . db ( Config . database . db )
73+ . collection ( collection )
74+ . findOneAndUpdate ( filter , { $set : update } ) ;
75+ client . close ( ) ;
76+ return result ;
10277 }
10378
10479 static async findOneAndUpdateOrInsert ( collection : string , filter : any , update : any ) : Promise < any > {
105- return this . connect ( )
106- . then ( ( ) => {
107- return this . findOne ( collection , filter )
108- . then ( async ( result : any ) => {
109- if ( result ) return this . findOneAndUpdate ( collection , filter , update ) ;
110- else return this . insertOne ( collection , update ) ;
111- } ) ;
112- } ) ;
80+ const result : any = await this . findOne ( collection , filter ) ;
81+ if ( result ) return this . findOneAndUpdate ( collection , filter , update ) ;
82+ else return this . insertOne ( collection , update ) ;
11383 }
11484
11585 static async findOneAndDelete ( collection : string , filter : any ) : Promise < any > {
116- return this . connect ( )
117- . then ( ( client : any ) => {
118- return client . db ( Config . database . db )
119- . collection ( collection )
120- . findOneAndDelete ( filter ) ;
121- } ) ;
86+ const client : any = await this . connect ( ) ;
87+ const result : any = await client . db ( Config . database . db )
88+ . collection ( collection )
89+ . findOneAndDelete ( filter ) ;
90+ client . close ( ) ;
91+ return result ;
12292 }
12393}
0 commit comments