clear-all-tables.ts 961 B

12345678910111213141516171819202122232425262728
  1. import { ConnectionOptions, createConnection } from 'typeorm';
  2. import { isTestEnvironment } from '../e2e/test-utils';
  3. // tslint:disable:no-console
  4. // tslint:disable:no-floating-promises
  5. /**
  6. * A Class used for generating mock data directly into the database via TypeORM.
  7. */
  8. export async function clearAllTables(connectionOptions: ConnectionOptions, logging = true) {
  9. (connectionOptions as any).entities = [__dirname + '/../src/**/*.entity.ts'];
  10. const name = isTestEnvironment() ? undefined : 'clearAllTables';
  11. const connection = await createConnection({ ...connectionOptions, name });
  12. if (logging) {
  13. console.log('Clearing all tables...');
  14. }
  15. try {
  16. await connection.synchronize(true);
  17. } catch (err) {
  18. console.error('Error occurred when attempting to clear tables!');
  19. console.error(err);
  20. } finally {
  21. await connection.close();
  22. }
  23. if (logging) {
  24. console.log('Done!');
  25. }
  26. }