You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
2.0 KiB
70 lines
2.0 KiB
4 years ago
|
import { Collection } from '../collection';
|
||
|
import { DbConnector } from './index';
|
||
|
import { dbCollection } from '../../data_layer';
|
||
|
import * as config from '../../../config';
|
||
|
|
||
|
import * as mongodb from 'mongodb';
|
||
|
|
||
|
|
||
|
export class MongodbConnector implements DbConnector {
|
||
|
private static _instance: MongodbConnector;
|
||
|
|
||
|
private _db = new Map<Collection, dbCollection>();
|
||
|
|
||
|
private static COLLECTION_TO_NAME_MAPPING = new Map<Collection, string>([
|
||
|
[Collection.ANALYTIC_UNITS, 'analytic_units'],
|
||
|
[Collection.ANALYTIC_UNIT_CACHES, 'analytic_unit_caches'],
|
||
|
[Collection.SEGMENTS, 'segments'],
|
||
|
[Collection.THRESHOLD, 'threshold'],
|
||
|
[Collection.DETECTION_SPANS, 'detection_spans'],
|
||
|
[Collection.DB_META, 'db_meta']
|
||
|
]);
|
||
|
|
||
|
private _client: mongodb.MongoClient;
|
||
|
|
||
|
private constructor() { }
|
||
|
|
||
|
async init(): Promise<void> {
|
||
|
const dbConfig = config.HASTIC_DB_CONFIG;
|
||
|
const uri = `mongodb://${dbConfig.user}:${dbConfig.password}@${dbConfig.url}`;
|
||
|
const auth = {
|
||
|
user: dbConfig.user,
|
||
|
password: dbConfig.password
|
||
|
};
|
||
|
this._client = new mongodb.MongoClient(uri, {
|
||
|
useNewUrlParser: true,
|
||
|
auth,
|
||
|
autoReconnect: true,
|
||
|
useUnifiedTopology: true,
|
||
|
// TODO: it should be configurable
|
||
|
authMechanism: 'SCRAM-SHA-1',
|
||
|
authSource: dbConfig.dbName
|
||
|
});
|
||
|
|
||
|
try {
|
||
|
const client: mongodb.MongoClient = await this._client.connect();
|
||
|
const hasticDb: mongodb.Db = client.db(dbConfig.dbName);
|
||
|
MongodbConnector.COLLECTION_TO_NAME_MAPPING.forEach(
|
||
|
(name: string, collection: Collection) => {
|
||
|
this._db.set(collection, hasticDb.collection(name));
|
||
|
}
|
||
|
);
|
||
|
} catch(err) {
|
||
|
console.log(`got error while connecting to MongoDB: ${err}`);
|
||
|
// TODO: throw a better error, e.g.: ServiceInitializationError
|
||
|
throw err;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
get db(): Map<Collection, dbCollection> {
|
||
|
return this._db;
|
||
|
}
|
||
|
|
||
|
static get instance(): MongodbConnector {
|
||
|
if(this._instance === undefined) {
|
||
|
this._instance = new this();
|
||
|
}
|
||
|
return this._instance;
|
||
|
}
|
||
|
}
|