async updateFromDiscord(discordCollection, guild) {
    const currentRows = await this.getCurrentDiscordRows(guild === undefined ? undefined : guild.id);

    const foundIndices = [];

    discordCollection.each(async entity => {
      const rowFromDiscord = this.getRowClass().createFromDiscordEntity(entity, guild);

      let foundIndex = -1;
      for (const [i, row] of currentRows.entries()) {
        if (row.equalsByKey(rowFromDiscord)) {
          foundIndex = i;
          foundIndices.push(i);
          break;
        }
      }

      if (foundIndex >= 0) {
        const updateQuery = currentRows[foundIndex].getKey();
        const valuesToUpdate = currentRows[foundIndex].getValuesToUpdate(rowFromDiscord);

        if (!OhUtils.isEmpty(valuesToUpdate)) {
          const newValues = { $set: valuesToUpdate };

          await this.dbManager.dbo.collection(this.getTableName()).updateOne(updateQuery, newValues);
          this.dbManager.context.log.i(
            'Table: ' +
              this.getTableName() +
              '; 1 entity updated, id: ' +
              util.inspect(updateQuery, { showHidden: false, depth: 1 }) +
              '; values: ' +
              util.inspect(newValues, { showHidden: false, depth: 1 })
          );

          if (this.hooks.onUpdateDuringUpdate !== undefined) {
            await this.hooks.onUpdateDuringUpdate(this.dbManager, updateQuery);
          }
        }
      } else {
        await this.dbManager.dbo.collection(this.getTableName()).insertOne(rowFromDiscord);

        if (this.hooks.onInsertDuringUpdate !== undefined) {
          this.hooks.onInsertDuringUpdate(this.dbManager, rowFromDiscord);
        }

        this.dbManager.context.log.i(
          'Table: ' +
            this.getTableName() +
            '; 1 entity inserted: ' +
            util.inspect(rowFromDiscord, { showHidden: true, depth: 1 })
        );
      }
    });

    const toDelete = currentRows.filter((value, index) => {
      return !foundIndices.includes(index);
    });

    if (toDelete.length > 0) {
      const toDeleteArray = [];
      for (const element of toDelete) {
        const keyObject = element.getKey();
        const entityArray = [];
        for (const keyField of Object.keys(keyObject)) {
          const keyFieldObject = {};
          keyFieldObject[keyField] = keyObject[keyField];
          entityArray.push(keyFieldObject);
        }

        toDeleteArray.push({ $and: entityArray });

        this.dbManager.context.log.i(
          'Table: ' +
            this.getTableName() +
            '; 1 entity to be deleted, id: ' +
            util.inspect(element, { showHidden: false, depth: 1 })
        );
      }

      await this.dbManager.dbo.collection(this.getTableName()).deleteMany({ $or: toDeleteArray });

      for (const element of toDelete) {
        if (this.hooks.onDeleteDuringUpdate !== undefined) {
          await this.hooks.onDeleteDuringUpdate(this.dbManager, element);
        }
      }
    }
  }
/**
   * Inserts, updates and deletes rows in the DB based on the actual information about the Discord guild.
   * @param  {Collection}  discordCollection the collection of Discord objects (roles, members etc.)
   * @param  {Guild}       guild             the Discord guild object
   * @return {Promise}                       nothing
   */
Inserts, updates and deletes rows in the DB based on the actual information about the Discord guild.
@param  {Collection}  discordCollection the collection of Discord objects (roles, members etc.)
@param  {Guild}       guild             the Discord guild object
@return {Promise}                       nothing
Inserts updates and deletes rows in the DB based on the actual information about the Discord guild . @param { Collection } discordCollection the collection of Discord objects ( roles members etc . ) @param { Guild } guild the Discord guild object @return { Promise } nothing
async updateFromDiscord(discordCollection, guild) {
    const currentRows = await this.getCurrentDiscordRows(guild === undefined ? undefined : guild.id);
    const foundIndices = [];
    discordCollection.each(async entity => {
      const rowFromDiscord = this.getRowClass().createFromDiscordEntity(entity, guild);
      let foundIndex = -1;
      for (const [i, row] of currentRows.entries()) {
        if (row.equalsByKey(rowFromDiscord)) {
          foundIndex = i;
          foundIndices.push(i);
          break;
        }
      }
      if (foundIndex >= 0) {
        const updateQuery = currentRows[foundIndex].getKey();
        const valuesToUpdate = currentRows[foundIndex].getValuesToUpdate(rowFromDiscord);
        if (!OhUtils.isEmpty(valuesToUpdate)) {
          const newValues = { $set: valuesToUpdate };
          await this.dbManager.dbo.collection(this.getTableName()).updateOne(updateQuery, newValues);
          this.dbManager.context.log.i(
            'Table: ' +
              this.getTableName() +
              '; 1 entity updated, id: ' +
              util.inspect(updateQuery, { showHidden: false, depth: 1 }) +
              '; values: ' +
              util.inspect(newValues, { showHidden: false, depth: 1 })
          );
          if (this.hooks.onUpdateDuringUpdate !== undefined) {
            await this.hooks.onUpdateDuringUpdate(this.dbManager, updateQuery);
          }
        }
      } else {
        await this.dbManager.dbo.collection(this.getTableName()).insertOne(rowFromDiscord);
        if (this.hooks.onInsertDuringUpdate !== undefined) {
          this.hooks.onInsertDuringUpdate(this.dbManager, rowFromDiscord);
        }
        this.dbManager.context.log.i(
          'Table: ' +
            this.getTableName() +
            '; 1 entity inserted: ' +
            util.inspect(rowFromDiscord, { showHidden: true, depth: 1 })
        );
      }
    });
    const toDelete = currentRows.filter((value, index) => {
      return !foundIndices.includes(index);
    });
    if (toDelete.length > 0) {
      const toDeleteArray = [];
      for (const element of toDelete) {
        const keyObject = element.getKey();
        const entityArray = [];
        for (const keyField of Object.keys(keyObject)) {
          const keyFieldObject = {};
          keyFieldObject[keyField] = keyObject[keyField];
          entityArray.push(keyFieldObject);
        }
        toDeleteArray.push({ $and: entityArray });
        this.dbManager.context.log.i(
          'Table: ' +
            this.getTableName() +
            '; 1 entity to be deleted, id: ' +
            util.inspect(element, { showHidden: false, depth: 1 })
        );
      }
      await this.dbManager.dbo.collection(this.getTableName()).deleteMany({ $or: toDeleteArray });
      for (const element of toDelete) {
        if (this.hooks.onDeleteDuringUpdate !== undefined) {
          await this.hooks.onDeleteDuringUpdate(this.dbManager, element);
        }
      }
    }
  }
async updateFromDiscord ( discordCollection , guild ) { const currentRows = await this . getCurrentDiscordRows ( guild === undefined ? undefined : guild . id ) ; const foundIndices = [ ] ; discordCollection . each ( async entity => { const rowFromDiscord = this . getRowClass ( ) . createFromDiscordEntity ( entity , guild ) ; let foundIndex = - 1 ; for ( const [ i , row ] of currentRows . entries ( ) ) { if ( row . equalsByKey ( rowFromDiscord ) ) { foundIndex = i ; foundIndices . push ( i ) ; break ; } } if ( foundIndex >= 0 ) { const updateQuery = currentRows [ foundIndex ] . getKey ( ) ; const valuesToUpdate = currentRows [ foundIndex ] . getValuesToUpdate ( rowFromDiscord ) ; if ( ! OhUtils . isEmpty ( valuesToUpdate ) ) { const newValues = { $set : valuesToUpdate } ; await this . dbManager . dbo . collection ( this . getTableName ( ) ) . updateOne ( updateQuery , newValues ) ; this . dbManager . context . log . i ( 'Table: ' + this . getTableName ( ) + '; 1 entity updated, id: ' + util . inspect ( updateQuery , { showHidden : false , depth : 1 } ) + '; values: ' + util . inspect ( newValues , { showHidden : false , depth : 1 } ) ) ; if ( this . hooks . onUpdateDuringUpdate !== undefined ) { await this . hooks . onUpdateDuringUpdate ( this . dbManager , updateQuery ) ; } } } else { await this . dbManager . dbo . collection ( this . getTableName ( ) ) . insertOne ( rowFromDiscord ) ; if ( this . hooks . onInsertDuringUpdate !== undefined ) { this . hooks . onInsertDuringUpdate ( this . dbManager , rowFromDiscord ) ; } this . dbManager . context . log . i ( 'Table: ' + this . getTableName ( ) + '; 1 entity inserted: ' + util . inspect ( rowFromDiscord , { showHidden : true , depth : 1 } ) ) ; } } ) ; const toDelete = currentRows . filter ( ( value , index ) => { return ! foundIndices . includes ( index ) ; } ) ; if ( toDelete . length > 0 ) { const toDeleteArray = [ ] ; for ( const element of toDelete ) { const keyObject = element . getKey ( ) ; const entityArray = [ ] ; for ( const keyField of Object . keys ( keyObject ) ) { const keyFieldObject = { } ; keyFieldObject [ keyField ] = keyObject [ keyField ] ; entityArray . push ( keyFieldObject ) ; } toDeleteArray . push ( { $and : entityArray } ) ; this . dbManager . context . log . i ( 'Table: ' + this . getTableName ( ) + '; 1 entity to be deleted, id: ' + util . inspect ( element , { showHidden : false , depth : 1 } ) ) ; } await this . dbManager . dbo . collection ( this . getTableName ( ) ) . deleteMany ( { $or : toDeleteArray } ) ; for ( const element of toDelete ) { if ( this . hooks . onDeleteDuringUpdate !== undefined ) { await this . hooks . onDeleteDuringUpdate ( this . dbManager , element ) ; } } } }
Inserts, updates and deletes rows in the DB based on the actual information about the Discord guild.
Inserts updates and deletes rows in the DB based on the actual information about the Discord guild .