This repository has been archived by the owner on May 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
statistics.ts
61 lines (57 loc) · 2.05 KB
/
statistics.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* This file is part of the @egodigital/egoose distribution.
* Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/)
*
* @egodigital/egoose is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, version 3.
*
* @egodigital/egoose is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { MongoDatabase } from '../mongo';
import { StatisticProviderBase } from '../statistics';
import * as mongoose from 'mongoose';
/**
* A basic statistic provider that loads data from Mongo database.
*/
export abstract class MongoDatabaseStatisticProviderBase<
TDatabase extends MongoDatabase = MongoDatabase
> extends StatisticProviderBase {
/**
* Initializes a new instance of that class.
*
* @param {TDatabase} database The underlying database connection.
*/
public constructor(
public readonly database: TDatabase,
) {
super();
}
/** @inheritdoc */
public async close(): Promise<void> {
await this.database
.disconnect();
}
/**
* Counts all documents of a collection.
*
* @param {Function} modelSelector The function that returns the model to use.
* @param {any} [filter] The optional filter.
*
* @return {Promise<number>} The promise that contains the number of documents.
*/
protected async countDocuments<TDocument extends mongoose.Document>(
modelSelector: (db: TDatabase) => mongoose.Model<TDocument>, filter?: any,
): Promise<number> {
return await modelSelector(this.database)
.find(filter)
.countDocuments()
.exec();
}
}