Skip to content

Commit 325e6f1

Browse files
committed
refactor: improve database interaction by using PostgresAdapter and SQLKITException
- Updated test setup to utilize PostgresAdapter for executing SQL commands. - Introduced SQLKITException for better error handling in SQL execution. - Refactored query builders to ensure executor is provided and handle errors appropriately. - Cleaned up imports and removed redundant code in query builders.
1 parent ab4f22e commit 325e6f1

File tree

14 files changed

+161
-70
lines changed

14 files changed

+161
-70
lines changed

src/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/.idea/src.iml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/.idea/workspace.xml

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dialects/postgres.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
import { QueryResult, SqlExecutor } from "../types/common";
1+
import { Pool } from "pg";
2+
import { QueryResult, SqlExecutor } from "../types";
3+
import { SQLKITException } from "../exceptions";
24

35
export class PostgresAdapter implements SqlExecutor {
4-
constructor(private client: any) {}
6+
constructor(private pgPool: Pool) {}
57

68
async executeSQL<T>(sql: string, values: any[]): Promise<QueryResult> {
7-
try {
8-
const result = await this.client.query(sql, values);
9-
this.client.release();
10-
return { rows: result.rows as T[] };
11-
} catch (error) {
12-
throw new Error(`PostgreSQL error: ${(error as Error).message}`);
13-
} finally {
14-
// Ensure the client is released back to the pool
15-
if (this.client.release) {
16-
this.client.release();
17-
}
18-
}
9+
return new Promise((resolve, reject) => {
10+
this.pgPool.query<T>(sql, values, (err, result) => {
11+
if (err) {
12+
reject(new SQLKITException(err.message));
13+
} else {
14+
resolve(result);
15+
}
16+
});
17+
});
1918
}
2019
}

src/exceptions/SQLKIT_Exception.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export class SQLKITException extends Error {
2+
constructor(message: string) {
3+
super(message);
4+
this.name = "SQLKITException";
5+
}
6+
}

src/exceptions/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./SQLKIT_Exception";

src/query-builder/base.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
import { QueryResult, SqlExecutor } from "../types";
2+
import { SQLKITException } from "../exceptions";
23

34
export abstract class BaseQueryBuilder<T> {
45
protected constructor(
56
protected readonly tableName: string,
6-
protected readonly executor: SqlExecutor,
7+
protected readonly executor?: SqlExecutor<T>,
78
) {}
89

910
protected abstract build(): { sql: string; values: any[] };
1011

1112
async commit(): Promise<QueryResult> {
1213
const { sql, values } = this.build();
14+
15+
if (!this.executor) {
16+
throw new SQLKITException(`
17+
To commit a query, you must provide an executor.
18+
Please provide an executor when creating the query builder.
19+
`);
20+
}
21+
1322
return this.executor.executeSQL(sql, values);
1423
}
1524
}

src/query-builder/delete.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { SqlExecutor } from "../types";
21
import { WhereCondition } from "../types";
32
import { buildWhereClause } from "../utils";
43
import { BaseQueryBuilder } from "./base";
@@ -7,10 +6,6 @@ export class DeleteQueryBuilder<T> extends BaseQueryBuilder<T> {
76
private whereCondition?: WhereCondition<T>;
87
private returningColumns: Array<keyof T | "*"> = ["*"] as any;
98

10-
constructor(tableName: string, executor: SqlExecutor) {
11-
super(tableName, executor);
12-
}
13-
149
where(condition: WhereCondition<T>): this {
1510
this.whereCondition = condition;
1611
return this;

src/query-builder/insert.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import { SqlExecutor } from "../types";
21
import { toSnakeCase } from "../utils";
32
import { BaseQueryBuilder } from "./base";
43

54
export class InsertQueryBuilder<T> extends BaseQueryBuilder<T> {
65
private data: Partial<T> | Partial<T>[] = {};
76
private returningColumns: Array<keyof T> = ["*"] as any;
87

9-
constructor(tableName: string, executor: SqlExecutor) {
10-
super(tableName, executor);
11-
}
12-
138
values(data: Partial<T> | Partial<T>[]): this {
149
this.data = data;
1510
return this;

0 commit comments

Comments
 (0)