clear-all-tables.ts 1.2 KB

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