async connect() {
    if (this.isConnected()) {
      return Promise.resolve();
    }

    const createCollectionsAndIndex = (db) => {
      const recordingCollection = db.collection(this.RECORDING_COLLECTION);
      recordingCollection.createIndex({ dateStart: -1 });
      recordingCollection.createIndex({ id: 1 }, { unique: true });

      const trackerCollection = db.collection(this.TRACKER_COLLECTION);
      trackerCollection.createIndex({ recordingId: 1 });

      const counterCollection = db.collection(this.COUNTER_COLLECTION);
      counterCollection.createIndex({ recordingId: 1 });
      counterCollection.createIndex({ frameId: 1 });
    };

    const isConnectionString = this.config.url !== undefined
      && (typeof this.config.url === 'string' || this.config.url instanceof String);
    const isClientObject = this.config.client !== undefined && typeof this.config.client === 'object';

    if (isConnectionString) {
      return new Promise((resolve, reject) => {
        this.connectionString = this.config.url;
        const mongoConnectParams = { useNewUrlParser: true, useUnifiedTopology: true };
        MongoClient.connect(this.connectionString, mongoConnectParams, (err, client) => {
          if (err) {
            reject(err);
          } else {
            this.client = client;
            const db = client.db(this.DATABASE_NAME);
            this.db = db;

            createCollectionsAndIndex(db);

            resolve(db);
          }
        });
      });
    }
    if (isClientObject) {
      this.client = this.config.client;
      if (!this.isConnected()) {
        return new Promise((resolve, reject) => {
          this.client.connect((err, client) => {
            if (err) {
              reject(err);
            } else {
              this.client = client;
              const db = client.db(this.DATABASE_NAME);
              this.db = db;

              createCollectionsAndIndex(db);

              resolve(db);
            }
          });
        });
      }
      this.db = this.client.db(this.DATABASE_NAME);
      createCollectionsAndIndex(this.db);
      return Promise.resolve(this.db);
    }
    return Promise.reject(new Error());
  }
The connection string used or null if a Db object was used for the connection or the
connection has not been established yet.