Skip to content
This repository was archived by the owner on Jan 23, 2022. It is now read-only.

Commit 85ec734

Browse files
committed
🎉 NEW: 0.0.17
1 parent 66e206d commit 85ec734

File tree

6 files changed

+38
-19
lines changed

6 files changed

+38
-19
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 0.0.17
4+
5+
- Add `returnEmptyArrayForNoResults` to `ConnectionConfig`
6+
- Minor code improvements
7+
38
## 0.0.16
49

510
- add `QueryBuilder#resetQuery` method

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@casper124578/mysql.ts",
3-
"version": "0.0.16",
3+
"version": "0.0.17",
44
"description": "A simple mysql wrapper for node.js",
55
"main": "dist/index.js",
66
"private": false,

src/Connection.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@ export class Connection<Tables> {
77
public config: ConnectionConfig | string;
88
public connection!: mysql.Connection;
99
private reconnect: boolean;
10-
private debug: boolean;
1110

1211
constructor(config: ConnectionConfig | string = {}) {
1312
let _connection: mysql.Connection;
1413
this.config = config;
1514

1615
if (typeof config !== "string") {
1716
this.reconnect = config.reconnect ?? true;
18-
this.debug = config.debugExec ?? false;
1917
} else {
20-
this.debug = false;
2118
this.reconnect = true;
2219
}
2320

@@ -78,7 +75,7 @@ export class Connection<Tables> {
7875
}
7976

8077
query<T = any>(): QueryBuilder<Tables, T> {
81-
return new QueryBuilder<Tables, T>(this.connection, this.debug);
78+
return new QueryBuilder<Tables, T>(this.connection, this.config);
8279
}
8380

8481
resume(): void {

src/QueryBuilder.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import mysql from "mysql";
2-
import { QueryValue } from "./types";
2+
import { ConnectionConfig, QueryValue } from "./types";
33

44
export class QueryBuilder<Tables, T = any> {
55
private connection: mysql.Connection;
6-
private debug!: boolean;
6+
private config!: ConnectionConfig;
77
query: string;
88
values: unknown[];
99

10-
constructor(connection: mysql.Connection, debug?: boolean) {
10+
constructor(connection: mysql.Connection, config?: string | ConnectionConfig) {
1111
this.connection = connection;
1212

13-
if (debug) {
14-
this.debug = debug;
13+
if (config && typeof config === "object") {
14+
this.config = config;
1515
}
1616

1717
this.query = "";
@@ -58,7 +58,7 @@ export class QueryBuilder<Tables, T = any> {
5858
});
5959

6060
this.query += `INSERT INTO ${tableName} (${this.createKeys(data)}) VALUES (${this.createValues(data)}) `;
61-
this.values = [...this.values, ...values];
61+
this.values.push(...values);
6262

6363
return this;
6464
}
@@ -78,7 +78,7 @@ export class QueryBuilder<Tables, T = any> {
7878
});
7979

8080
this.query += `UPDATE ${tableName} SET ${keys} `;
81-
this.values = [...this.values, ...values];
81+
this.values.push(...values);
8282

8383
return this;
8484
}
@@ -272,8 +272,8 @@ export class QueryBuilder<Tables, T = any> {
272272
/**
273273
* Execute the query
274274
*/
275-
async exec(options?: Omit<mysql.QueryOptions, "sql" | "values">): Promise<T[]> {
276-
if (this.debug === true) {
275+
async exec(options?: Omit<mysql.QueryOptions, "sql" | "values">): Promise<T[] | undefined> {
276+
if (this.config.debugExec === true) {
277277
console.info(`[mysql.ts]: Query: ${this.query}`);
278278
console.info("[mysql.ts]: Values: ", this.values);
279279
}
@@ -286,14 +286,19 @@ export class QueryBuilder<Tables, T = any> {
286286
const opts = options ? { ...options, sql: this.query, values: this.values } : this.query;
287287

288288
this.connection.query(opts, this.values, (err, results) => {
289-
this.query = "";
290-
this.values = [];
289+
this.resetQuery();
291290

292291
if (err) {
293292
return reject(err);
294293
}
295294

296-
return resolve(results);
295+
if (this.config.returnEmptyArrayForNoResults) {
296+
return resolve(results);
297+
} else {
298+
if (results.length <= 0) {
299+
return resolve(undefined);
300+
}
301+
}
297302
});
298303
});
299304
}

src/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ export interface ConnectionConfig extends MySQLConnectionConfig {
77
* This will show the full query & values when a query is executed with the `QueryBuilder#exec` method
88
*/
99
debugExec?: boolean;
10+
11+
/**
12+
* When no results are found for a query:
13+
*
14+
* `false` = return `undefined`
15+
*
16+
* `true` = returns an empty array
17+
*
18+
* @default false
19+
*/
20+
returnEmptyArrayForNoResults?: boolean;
1021
}
1122

1223
export interface StatisticsPacket {

tests/index.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ async function test() {
1717
database: "snaily-cad",
1818
reconnect: true,
1919
debugExec: true,
20+
returnEmptyArrayForNoResults: false,
2021
});
2122

22-
const x = conn.query().select("*").from("citizens").where("x", 3);
23+
const x = await conn.query().select("*").from("citizens").where("id", "qsd").exec();
2324

24-
console.log(x);
25+
console.log(x?.[0].id);
2526

2627
// const d = await conn.query().drop("books", "database").exec();
2728

0 commit comments

Comments
 (0)