clear-all-tables.ts 1.0 KB

123456789101112131415161718192021222324252627282930
  1. import { createConnection } from 'typeorm';
  2. import { isTestEnvironment } from '../e2e/utils/test-environment';
  3. import { registerCustomEntityFields } from '../src/entity/register-custom-entity-fields';
  4. // tslint:disable:no-console
  5. // tslint:disable:no-floating-promises
  6. /**
  7. * Clears all tables in the detabase sepcified by the connectionOptions
  8. */
  9. export async function clearAllTables(config: any, logging = true) {
  10. (config.dbConnectionOptions as any).entities = [__dirname + '/../src/**/*.entity.ts'];
  11. const name = isTestEnvironment() ? undefined : 'clearAllTables';
  12. registerCustomEntityFields(config);
  13. const connection = await createConnection({ ...config.dbConnectionOptions, name });
  14. if (logging) {
  15. console.log('Clearing all tables...');
  16. }
  17. try {
  18. await connection.synchronize(true);
  19. } catch (err) {
  20. console.error('Error occurred when attempting to clear tables!');
  21. console.error(err);
  22. } finally {
  23. await connection.close();
  24. }
  25. if (logging) {
  26. console.log('Done!');
  27. }
  28. }