Created
February 25, 2018 15:14
-
-
Save Deviad/665c5ba59b2bc60d0114b1ffe1da0baa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { inject, injectable } from 'inversify'; | |
import { MongoDBClient } from '../utils/mongodb/client'; | |
import { User } from '../models/user'; | |
import TYPES from '../constant/types'; | |
@injectable() | |
export class GenericService<T extends GenericEntity> { | |
private mongoClient: MongoDBClient; | |
constructor( | |
@inject(TYPES.MongoDBClient) mongoClient: MongoDBClient | |
) { | |
this.mongoClient = mongoClient; | |
} | |
public getUsers(): Promise<T[]> { | |
return new Promise<User[]>((resolve, reject) => { | |
this.mongoClient.find('user', {}, (error, data: T[]) => { | |
resolve(data); | |
}); | |
}); | |
} | |
public getUser(id: string): Promise<T> { | |
return new Promise<T>((resolve, reject) => { | |
this.mongoClient.findOneById('user', id, (error, data: T) => { | |
resolve(data); | |
}); | |
}); | |
} | |
public newUser(user: T): Promise<T> { | |
return new Promise<T>((resolve, reject) => { | |
this.mongoClient.insert('user', user, (error, data: T) => { //how do I retreive the Type T so that I can put its name instead of 'user'? | |
resolve(data); | |
}); | |
}); | |
} | |
public updateUser(id: string, user: T): Promise<T> { | |
return new Promise<T>((resolve, reject) => { | |
this.mongoClient.update('user', id, user, (error, data: T) => { | |
resolve(data); | |
}); | |
}); | |
} | |
public deleteUser(id: string): Promise<T> { | |
return new Promise<any>((resolve, reject) => { | |
this.mongoClient.remove('user', id, (error, data: any) => { | |
resolve(data); | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment