clear-all-tables.ts 999 B

12345678910111213141516171819202122232425262728
  1. import { VendureConfig } from '@vendure/core';
  2. import { preBootstrapConfig } from '@vendure/core/dist/bootstrap';
  3. import { createConnection } from 'typeorm';
  4. /* eslint-disable no-console */
  5. /* eslint-disable @typescript-eslint/no-floating-promises */
  6. /**
  7. * Clears all tables in the database specified by the connectionOptions
  8. */
  9. export async function clearAllTables(config: VendureConfig, logging = true) {
  10. if (logging) {
  11. console.log('Clearing all tables...');
  12. }
  13. config = await preBootstrapConfig(config);
  14. const entityIdStrategy = config.entityIdStrategy ?? config.entityOptions?.entityIdStrategy;
  15. const connection = await createConnection({ ...config.dbConnectionOptions });
  16. try {
  17. await connection.synchronize(true);
  18. } catch (err: any) {
  19. console.error('Error occurred when attempting to clear tables!');
  20. console.log(err);
  21. } finally {
  22. await connection.close();
  23. }
  24. if (logging) {
  25. console.log('Done!');
  26. }
  27. }