text: import { groupBy } from 'lodash';
  import settings from '../../../../settings';
  
  export const truncateTables = async (knex, Promise, tables) => {
    if (settings.db.dbType === 'sqlite' || process.env.NODE_ENV === 'test') {
      return Promise.all(tables.map(table => knex(table).truncate()));
    } else if (settings.db.dbType === 'mysql') {
      return knex.transaction(async function(trx) {
        await knex.raw('SET FOREIGN_KEY_CHECKS=0').transacting(trx);
        await Promise.all(tables.map(table => knex.raw(`TRUNCATE TABLE ${table}`).transacting(trx)));
        await trx.commit;
        await knex.raw('SET FOREIGN_KEY_CHECKS=1').transacting(trx);
      });
    } else if (settings.db.dbType === 'pg') {
      return Promise.all(tables.map(table => knex.raw(`TRUNCATE "${table}" RESTART IDENTITY CASCADE`)));
    }
  };
  
  export const orderedFor = (rows, collection, field, singleObject) => {
    // return the rows ordered for the collection
    const inGroupsOfField = groupBy(rows, field);
    return collection.map(element => {
      const elementArray = inGroupsOfField[element];
      if (elementArray) {
        return singleObject ? elementArray[0] : elementArray;
      }
      return singleObject ? {} : [];
    });
  };
  