-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.ts
More file actions
36 lines (30 loc) · 957 Bytes
/
delete.ts
File metadata and controls
36 lines (30 loc) · 957 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
import { WhereCondition } from "../types";
import { buildWhereClause } from "../utils";
import { BaseQueryBuilder } from "./base";
export class DeleteQueryBuilder<T> extends BaseQueryBuilder<T> {
private whereCondition?: WhereCondition<T>;
private returningColumns: Array<keyof T | "*"> = ["*"] as any;
where(condition: WhereCondition<T>): this {
this.whereCondition = condition;
return this;
}
returning(columns?: Array<keyof T>): this {
if (columns && columns.length > 0) {
this.returningColumns = columns;
}
return this;
}
build(): { sql: string; values: any[] } {
const { whereClause, values } = buildWhereClause(
this.whereCondition,
this.tableName
);
const returning = this.returningColumns.join(", ");
const sql = `
DELETE FROM "${this.tableName}"
${whereClause ? `WHERE ${whereClause}` : ""}
RETURNING ${returning};
`;
return { sql, values };
}
}