async insertOrUpdate(entity) {
    const keyColumns = this.getRowClass().getKeyColumns();
    const query = {};
    for (const column of keyColumns) {
      query[column] = entity[column];
    }

    // False positive, the unicorn thinks that "query" is a function here.
    /* eslint-disable unicorn/no-fn-reference-in-iterator */
    const rawRows = await this.dbManager.dbo.collection(this.getTableName()).find(query).toArray();
    /* eslint-enable unicorn/no-fn-reference-in-iterator */
    if (rawRows[0] === undefined) {
      const columns = this.getRowClass().getColumns();
      const insertQuery = {};
      for (const column of columns) {
        insertQuery[column] = entity[column];
      }

      try {
        await this.dbManager.dbo.collection(this.getTableName()).insertOne(insertQuery);
      } catch (e) {
        this.dbManager.context.log.w('insertOrUpdate insert error: ' + util.inspect(e, { showHidden: true, depth: 5 }));
        return false;
      }

      this.dbManager.context.log.i(
        'Table: ' +
          this.getTableName() +
          '; 1 entity inserted: ' +
          util.inspect(insertQuery, { showHidden: true, depth: 1 })
      );
    } else {
      const currentRow = this.getRowInstance(rawRows[0]);
      const updatedRow = this.getRowInstance(entity);

      const updateQuery = currentRow.getKey();
      const valuesToUpdate = currentRow.getValuesToUpdate(updatedRow);

      if (OhUtils.isEmpty(valuesToUpdate)) {
        this.dbManager.context.log.v(
          'Table: ' +
            this.getTableName() +
            '; 1 entity is up to date, id: ' +
            util.inspect(updateQuery, { showHidden: false, depth: 1 }).replace(/[\r\n]+/gm, '')
        );
      } else {
        const newValues = { $set: valuesToUpdate };

        try {
          await this.dbManager.dbo.collection(this.getTableName()).updateOne(updateQuery, newValues);
        } catch (e) {
          this.dbManager.context.log.w('insertOrUpdate update error: ' +
            util.inspect(e, { showHidden: true, depth: 5 }));
          return false;
        }

        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 })
        );
      }
    }

    return true;
  }
/**
   * Inserts or updates a DB row based on whether the key matches to one of the existing rows.
   * @param  {Object}             entity raw object which values need to be inserted
   * @return {Promise<boolean>}          true if successful, false otherwise
   */
Inserts or updates a DB row based on whether the key matches to one of the existing rows.
@param  {Object}             entity raw object which values need to be inserted
@return {Promise}          true if successful, false otherwise
Inserts or updates a DB row based on whether the key matches to one of the existing rows . @param { Object } entity raw object which values need to be inserted @return { Promise } true if successful false otherwise
async insertOrUpdate(entity) {
    const keyColumns = this.getRowClass().getKeyColumns();
    const query = {};
    for (const column of keyColumns) {
      query[column] = entity[column];
    }
    const rawRows = await this.dbManager.dbo.collection(this.getTableName()).find(query).toArray();
    if (rawRows[0] === undefined) {
      const columns = this.getRowClass().getColumns();
      const insertQuery = {};
      for (const column of columns) {
        insertQuery[column] = entity[column];
      }
      try {
        await this.dbManager.dbo.collection(this.getTableName()).insertOne(insertQuery);
      } catch (e) {
        this.dbManager.context.log.w('insertOrUpdate insert error: ' + util.inspect(e, { showHidden: true, depth: 5 }));
        return false;
      }
      this.dbManager.context.log.i(
        'Table: ' +
          this.getTableName() +
          '; 1 entity inserted: ' +
          util.inspect(insertQuery, { showHidden: true, depth: 1 })
      );
    } else {
      const currentRow = this.getRowInstance(rawRows[0]);
      const updatedRow = this.getRowInstance(entity);
      const updateQuery = currentRow.getKey();
      const valuesToUpdate = currentRow.getValuesToUpdate(updatedRow);
      if (OhUtils.isEmpty(valuesToUpdate)) {
        this.dbManager.context.log.v(
          'Table: ' +
            this.getTableName() +
            '; 1 entity is up to date, id: ' +
            util.inspect(updateQuery, { showHidden: false, depth: 1 }).replace(/[\r\n]+/gm, '')
        );
      } else {
        const newValues = { $set: valuesToUpdate };
        try {
          await this.dbManager.dbo.collection(this.getTableName()).updateOne(updateQuery, newValues);
        } catch (e) {
          this.dbManager.context.log.w('insertOrUpdate update error: ' +
            util.inspect(e, { showHidden: true, depth: 5 }));
          return false;
        }
        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 })
        );
      }
    }
    return true;
  }
async insertOrUpdate ( entity ) { const keyColumns = this . getRowClass ( ) . getKeyColumns ( ) ; const query = { } ; for ( const column of keyColumns ) { query [ column ] = entity [ column ] ; } const rawRows = await this . dbManager . dbo . collection ( this . getTableName ( ) ) . find ( query ) . toArray ( ) ; if ( rawRows [ 0 ] === undefined ) { const columns = this . getRowClass ( ) . getColumns ( ) ; const insertQuery = { } ; for ( const column of columns ) { insertQuery [ column ] = entity [ column ] ; } try { await this . dbManager . dbo . collection ( this . getTableName ( ) ) . insertOne ( insertQuery ) ; } catch ( e ) { this . dbManager . context . log . w ( 'insertOrUpdate insert error: ' + util . inspect ( e , { showHidden : true , depth : 5 } ) ) ; return false ; } this . dbManager . context . log . i ( 'Table: ' + this . getTableName ( ) + '; 1 entity inserted: ' + util . inspect ( insertQuery , { showHidden : true , depth : 1 } ) ) ; } else { const currentRow = this . getRowInstance ( rawRows [ 0 ] ) ; const updatedRow = this . getRowInstance ( entity ) ; const updateQuery = currentRow . getKey ( ) ; const valuesToUpdate = currentRow . getValuesToUpdate ( updatedRow ) ; if ( OhUtils . isEmpty ( valuesToUpdate ) ) { this . dbManager . context . log . v ( 'Table: ' + this . getTableName ( ) + '; 1 entity is up to date, id: ' + util . inspect ( updateQuery , { showHidden : false , depth : 1 } ) . replace ( / [\r\n]+ / gm , '' ) ) ; } else { const newValues = { $set : valuesToUpdate } ; try { await this . dbManager . dbo . collection ( this . getTableName ( ) ) . updateOne ( updateQuery , newValues ) ; } catch ( e ) { this . dbManager . context . log . w ( 'insertOrUpdate update error: ' + util . inspect ( e , { showHidden : true , depth : 5 } ) ) ; return false ; } 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 } ) ) ; } } return true ; }
Inserts or updates a DB row based on whether the key matches to one of the existing rows.
Inserts or updates a DB row based on whether the key matches to one of the existing rows .
// False positive, the unicorn thinks that "query" is a function here. /* eslint-disable unicorn/no-fn-reference-in-iterator */ /* eslint-enable unicorn/no-fn-reference-in-iterator */