Skip to content

Commit 98d6aab

Browse files
committed
feat: Add database connection pool with automatic database creation and initialization
- Implemented `Client` class to manage database connections and initialization. - Integrated `ConnectionPool` class to handle database connection pooling using `pg` library. - Added error handling for failed database connections using custom `Catch` decorator. - Added functionality to automatically create the database if it doesn't exist. - Implemented `initialize` method in `Client` to connect, initialize, and seed database tables. - Encapsulated database connection logic in `ConnectionPool` with methods for connecting, creating a database, and retrieving a client. This feature provides an organized approach to managing database connections and ensures that the database is properly initialized and seeded on first use.
1 parent 92f2ae2 commit 98d6aab

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/database/Client.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ConnectionPool } from './ConnectionPool';
2+
import { TablesService } from './service/tables/Tables';
3+
export class Client {
4+
private _connectionPool: ConnectionPool;
5+
constructor() {
6+
this._connectionPool = new ConnectionPool();
7+
}
8+
async initialize() {
9+
await this._connectionPool.connect();
10+
const tablesService = new TablesService(this._connectionPool);
11+
console.log('connect database');
12+
await tablesService.initialTables();
13+
console.log('Initial Tables');
14+
await tablesService.seedTables();
15+
console.log('Initial Seed Tables');
16+
}
17+
getConnectionPool(): ConnectionPool {
18+
return this._connectionPool;
19+
}
20+
}

src/database/ConnectionPool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class ConnectionPool {
5353
return url || `postgresql://${user}:${password}@${host}:${port}/${databaseName}`;
5454
}
5555
async getClient(): Promise<PoolClient> {
56-
return this._pool.connect();
56+
return await this._pool.connect();
5757
}
5858

5959
async close(): Promise<void> {

0 commit comments

Comments
 (0)