forked from barklan/sql-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgresDatabase.ts
More file actions
43 lines (38 loc) · 861 Bytes
/
Copy pathpostgresDatabase.ts
File metadata and controls
43 lines (38 loc) · 861 Bytes
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
import { Pool } from "pg";
import IDatabase, { sqlError } from "./interface";
export default class PostgresDatabase implements IDatabase {
private pool: Pool;
constructor(
host: string,
user: string,
password: string,
port?: number,
database?: string
) {
this.pool = new Pool({
host,
user,
password,
port,
database,
});
}
public async lintQuery(query: string): Promise<sqlError | null> {
return new Promise<sqlError | null>((resolve) => {
this.pool.query(`EXPLAIN ${query}`, (err) => {
if (err) {
resolve({
code: err.name,
sqlMessage: err.message,
});
return;
}
// resolve with null if there is no error.
resolve(null);
});
});
}
public end(): void {
this.pool.end();
}
}