deno.land / x / bld@v0.0.1 / query.ts

نووسراو ببینە
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { objectToKeyValueArrays, toSQLKeyValuePair, toSQLValueType, valuePlaceholders,} from "./helpers.ts";
type WhereCondition = "=" | ">" | "<" | ">=" | "<=" | "!=" | "LIKE" | "IN";
export class Query<T extends Record<string, unknown>> { private _table = ""; private _query = ""; private _placeholderCount = 1;
public select(...fields: string[]) { const keys = fields.length > 0 ? fields.join(", ") : "*";
this._query = `SELECT ${keys} FROM ${this._table}`; return this; }
public insert(data: T) { const [keys, values] = objectToKeyValueArrays(data);
const keysSQL = `(${keys.join(", ")})`;
this._query = `INSERT INTO ${this._table} ${keysSQL} VALUES ${ valuePlaceholders(values, this._placeholderCount++) }`; return this; }
public update(data: T) { const [keys, values] = objectToKeyValueArrays(data);
let result = ""; for (let i = 0; i < keys.length; i++) { result += ` ${toSQLKeyValuePair(keys[i], values[i])}`; if (i != keys.length - 1) { result += ","; } }
this._query += ` UPDATE ${this._table} SET ${result.trim()}`; return this; }
public delete() { this._query = `DELETE FROM ${this._table}`; return this; }
// TODO: This is the most basic form of where, need to improve. // Should I use value or placeholder? public where( key: string, condition: WhereCondition, value: string | number | boolean, ) { const valueSQL = toSQLValueType(value); this._query += ` WHERE ${key} ${condition} ${valueSQL}`; return this; }
public returning(...fields: string[]) { const keys = fields.length > 0 ? fields.join(", ") : "*";
this._query += ` RETURNING ${keys}`; return this; }
public table(name: string) { this._table = name; return this; }
public build() { const res = this._query.trim() + ";"; this._query = ""; return res; }}
bld
DocumentationDocumentation
GitHub Repositorywralith/bld
GitHub Stars
0

Version Info

Tagged at
2 months ago

External Dependencies

No external dependencies 🎉