import { Db , ObjectID } from 'mongodb' ; import { injectable } from 'inversify' ; import { MongoDBConnection } from './connection' ; import { User } from '../../models/user' ; @ injectable ( ) export class MongoDBClient { public db : Db ; constructor ( ) { MongoDBConnection . getConnection ( ( connection ) => { this . db = connection ; } ) ; } public find ( collection , filter , result ) { this . db . collection ( collection ) . find ( filter ) . toArray ( ( error , find ) => { return result ( error , find ) ; } ) ; } public findOneById ( collection , objectId , result ) { this . db . collection ( collection ) . find ( { _id : new ObjectID ( objectId ) } ) . limit ( 1 ) . toArray ( ( error , find ) => { return result ( error , find [ 0 ] ) ; } ) ; } public insert ( collection , model , result ) { this . db . collection ( collection ) . insertOne ( model , ( error , insert ) => { return result ( error , insert . ops [ 0 ] ) ; } ) ; } public update ( collection , objectId , model , result ) { this . db . collection ( collection ) . updateOne ( { _id : new ObjectID ( objectId ) } , { $set : model } , ( error , update ) => result ( error , model ) ) ; } public remove ( collection , objectId , result ) { this . db . collection ( collection ) . deleteOne ( { _id : new ObjectID ( objectId ) } , ( error , remove ) => { return result ( error , remove ) ; } ) ; } }