@@ -11,8 +11,23 @@ export class QueryBuilder {
11
11
this . values = [ ] ;
12
12
}
13
13
14
- select ( selector : string ) {
15
- this . query += `SELECT ${ selector } ` ;
14
+ /**
15
+ * Select 1 or more items from a table
16
+ * @param {string|string[] } selector
17
+ * @example
18
+ *
19
+ * // 1 item
20
+ * <Connection>.query.select("id").from("books");
21
+ *
22
+ * // multiple items
23
+ * <Connection>.query.select(["id", "name"]).from("books");
24
+ */
25
+ select ( selector : string | string [ ] ) {
26
+ if ( typeof selector === "string" ) {
27
+ this . query += `SELECT ${ selector } ` ;
28
+ } else {
29
+ this . query += `SELECT ${ selector . join ( ", " ) } ` ;
30
+ }
16
31
17
32
return this ;
18
33
}
@@ -83,6 +98,24 @@ export class QueryBuilder {
83
98
return this ;
84
99
}
85
100
101
+ order ( selector : string , type : "ASC" | "DESC" ) {
102
+ this . query += `ORDER BY ${ selector } ${ type . toUpperCase ( ) } ` ;
103
+
104
+ return this ;
105
+ }
106
+
107
+ limit ( n : number | string ) {
108
+ this . query += `LIMIT ${ n } ` ;
109
+
110
+ return this ;
111
+ }
112
+
113
+ having ( condition : string ) {
114
+ this . query += `HAVING ${ condition } ` ;
115
+
116
+ return this ;
117
+ }
118
+
86
119
raw ( query : string , values : unknown [ ] ) {
87
120
this . query = query ;
88
121
this . values = values ;
@@ -94,9 +127,6 @@ export class QueryBuilder {
94
127
* Execute the query
95
128
*/
96
129
async exec < T > ( ) : Promise < T [ ] > {
97
- console . log ( this . values ) ;
98
- console . log ( this . query ) ;
99
-
100
130
return new Promise ( ( resolve , reject ) => {
101
131
this . connection . query ( this . query , this . values , ( err , results ) => {
102
132
if ( err ) {
0 commit comments