Skip to content

Commit

Permalink
refactor: function to create data source
Browse files Browse the repository at this point in the history
  • Loading branch information
aniebietafia committed Aug 31, 2024
1 parent 54fd711 commit c769eb4
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions brints-estate-api/src/database/data-source.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import { DataSource } from 'typeorm';

import { User } from 'src/users/entities/user.entity';
import { UserAuth } from 'src/users/entities/userAuth.entity';
import { DataSource } from 'typeorm';

const AppDataSource = new DataSource({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'postgres',
password: 'postgres',
database: 'brints-estate-backend',
synchronize: true,
entities: [User, UserAuth],
// entities: [__dirname + '/**/*.entity{.ts,.js}'],
});
const isDevelopment = process.env.NODE_ENV === 'development';

interface DataSourceConfig {
dbHost: string;
dbPort: number;
dbUser: string;
dbPassword: string;
dbName: string;
}

export function createDataSource(config: DataSourceConfig) {
return new DataSource({
type: 'postgres',
host: config.dbHost,
port: config.dbPort,
username: config.dbUser,
password: config.dbPassword,
database: config.dbName,
synchronize: isDevelopment,
entities: [User, UserAuth],
});
}

export async function initializeDataSource() {
export async function initializeDataSource(AppDataSource: DataSource) {
try {
if (!AppDataSource.isInitialized) {
await AppDataSource.initialize();
Expand All @@ -25,5 +37,3 @@ export async function initializeDataSource() {
throw error;
}
}

export default AppDataSource;

0 comments on commit c769eb4

Please sign in to comment.