Advertisement
Wolf2012

data-source

Feb 23rd, 2024
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import config from 'building-blocks/config/config';
  2. import { DataSource, DataSourceOptions } from 'typeorm';
  3. import { createDatabase } from 'typeorm-extension';
  4.  
  5. // use this file for running migration
  6. export const postgresOptions: DataSourceOptions = {
  7.   type: 'postgres',
  8.   host: config.postgres.host,
  9.   port: config.postgres.port,
  10.   username: config.postgres.username,
  11.   password: config.postgres.password,
  12.   database: config.postgres.database,
  13.   synchronize: config.postgres.synchronize,
  14.   entities: [__dirname + config.postgres.entities],
  15.   migrations: [__dirname + config.postgres.migrations],
  16.   logging: config.postgres.logging
  17. };
  18.  
  19. async function initializeDatabase() {
  20.   try {
  21.     await createDatabase({ options: postgresOptions, ifNotExist: true });
  22.  
  23.     const dataSource = new DataSource(postgresOptions);
  24.     await dataSource.initialize();
  25.     console.log('Database has been initialized!');
  26.   } catch (error) {
  27.     console.error('Error during Database initialization', error);
  28.   }
  29. }
  30.  
  31. export default initializeDatabase;
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement