-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
55 lines (46 loc) · 1.34 KB
/
server.ts
File metadata and controls
55 lines (46 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { Client } from '../../deps.ts';
export class Server {
private client: Client | null = null;
constructor(connectionString?: string) {
if (connectionString) {
this.client = new Client(connectionString);
}
}
public async connect(connectionString?: string) {
const maxRetries = 3;
let currentAttempt = 0;
while (currentAttempt < maxRetries) {
try {
const connection = Deno.env.get('POSTGRES') || connectionString;
if (!connection) {
throw new Error(
'Connection string not found \n Please set the POSTGRES environment variable or pass the connection string as an argument',
);
}
console.log('connecting', connection);
this.client = new Client(connection);
await this.client.connect();
console.log('connected');
return this.client;
} catch (error) {
currentAttempt++;
console.log(
`Attempt ${currentAttempt} failed: ${error.message}`,
);
if (currentAttempt >= maxRetries) {
throw new Error(
`Could not connect to the server after ${maxRetries} attempts: ${error.message}`,
);
}
// Wait for 1 second before the next attempt
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
}
public async close() {
if (this.client) {
await this.client.end();
}
}
// Additional methods for query execution can be added here
}