-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoggingMongooseModel.ts
29 lines (23 loc) · 1.1 KB
/
LoggingMongooseModel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import mongoose = require('mongoose');
import I = require('./Interfaces');
import logger = require('./logger');
class LoggingModel<T extends mongoose.Document> {
private Model: mongoose.Model<T>;
private readonly standardLibraryProxy: I.StandardLibraryProxy;
constructor(model: mongoose.Model<T>, standardLibraryProxy: I.StandardLibraryProxy) {
this.Model = model;
this.standardLibraryProxy = standardLibraryProxy;
}
public findOne(conditions?: Object, callback?: (err: any, res: T) => void): mongoose.DocumentQuery<T, T> {
const startTime: number = this.standardLibraryProxy.getCurrentDate().getTime();
return this.Model.findOne(conditions, (err: any, res: T) => {
callback(err, res);
const durationMs: number = this.standardLibraryProxy.getCurrentDate().getTime() - startTime;
logger.info("MongoCall", { queryType: "findOne", conditions: JSON.stringify(conditions), durationMs: durationMs });
});
}
public create(sourceObject:any): T {
return new this.Model(sourceObject);
}
}
export = LoggingModel;