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

Commit 1ae59fa

Browse files
committed
🎉 NEW: add 'order', 'limit' and 'having'
1 parent fabc80d commit 1ae59fa

File tree

9 files changed

+149
-15
lines changed

9 files changed

+149
-15
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"semi": ["error", "always"],
1717
"no-multi-spaces": ["error"],
1818
"eqeqeq": ["warn", "always"],
19-
"no-unused-vars": ["error"],
19+
"no-unused-vars": "off",
2020
"no-duplicate-case": ["error"],
2121
"no-extra-semi": ["error"],
2222
"no-unreachable": ["error"],

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
dist
3+
.env

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
1-
# node-mysql
1+
# mysql.ts
22

33
A simple node.js MySQL wrapper made with TypeScript.
44

55
**This package is still in development!**
6+
7+
## Installation
8+
9+
### npm
10+
11+
```bash
12+
npm i mysql.ts
13+
```
14+
15+
### Yarn
16+
17+
```bash
18+
yarn add mysql.ts
19+
```
20+
21+
## Usage
22+
23+
```ts
24+
import { createConnection } from "mysql.ts";
25+
26+
async function init() {
27+
const connection = await createConnection({
28+
/* options */
29+
});
30+
31+
// query something
32+
const results = await connection.query.select(["id", "name"]).from("books").exec();
33+
34+
console.log(results);
35+
}
36+
```

docs/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Documentation
2+
3+
## Installation
4+
5+
### npm
6+
7+
```bash
8+
npm i mysql.ts
9+
```
10+
11+
### Yarn
12+
13+
```bash
14+
yarn add mysql.ts
15+
```
16+
17+
## Usage
18+
19+
```ts
20+
import { createConnection } from "mysql.ts";
21+
22+
async function init() {
23+
const connection = await createConnection({
24+
/* options */
25+
});
26+
27+
// query something
28+
const results = await connection.query.select(["id", "name"]).from("books").exec();
29+
30+
console.log(results);
31+
}
32+
```

docs/connection.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Connection

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@casper124578/node-mysql",
2+
"name": "mysql.ts",
33
"version": "0.0.0-alpha",
44
"description": "A simple mysql wrapper for node.js",
55
"main": "dist/index.js",
@@ -10,7 +10,8 @@
1010
"keywords": [
1111
"mysql",
1212
"mysql-wrapper",
13-
"easy-mysql"
13+
"easy-mysql",
14+
"mysql typescript"
1415
],
1516
"author": {
1617
"name": "Dev-CasperTheGhost",

src/Connection.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ export class Connection {
1313
this.config = config;
1414

1515
if (typeof config !== "string") {
16-
this.reconnect = config.reconnect ?? false;
16+
this.reconnect = config.reconnect ?? true;
1717
} else {
18-
this.reconnect = false;
18+
this.reconnect = true;
1919
}
2020

2121
// @ts-expect-error ignore
@@ -37,6 +37,38 @@ export class Connection {
3737
});
3838
}
3939

40+
destroy() {
41+
this.connection.destroy();
42+
}
43+
44+
end() {
45+
return new Promise((resolve, reject) => {
46+
return this.connection.end((err) => {
47+
if (err) {
48+
return reject(err);
49+
}
50+
51+
resolve(true);
52+
});
53+
});
54+
}
55+
56+
format(sql: string, values: unknown[]) {
57+
return this.connection.format(sql, values);
58+
}
59+
60+
pause() {
61+
this.connection.pause();
62+
}
63+
64+
resume() {
65+
this.connection.resume();
66+
}
67+
68+
on(eventName: string, listener: (...args: any[]) => void) {
69+
this.connection.on(eventName, listener);
70+
}
71+
4072
async connect(): Promise<mysql.Connection> {
4173
const connection = mysql.createConnection(this.config);
4274

src/QueryBuilder.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,23 @@ export class QueryBuilder {
1111
this.values = [];
1212
}
1313

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+
}
1631

1732
return this;
1833
}
@@ -83,6 +98,24 @@ export class QueryBuilder {
8398
return this;
8499
}
85100

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+
86119
raw(query: string, values: unknown[]) {
87120
this.query = query;
88121
this.values = values;
@@ -94,9 +127,6 @@ export class QueryBuilder {
94127
* Execute the query
95128
*/
96129
async exec<T>(): Promise<T[]> {
97-
console.log(this.values);
98-
console.log(this.query);
99-
100130
return new Promise((resolve, reject) => {
101131
this.connection.query(this.query, this.values, (err, results) => {
102132
if (err) {

tests/index.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1-
import { createConnection } from "../dist/index";
1+
import { createConnection } from "../src/index";
22

33
async function test() {
44
const conn = await createConnection({
55
host: "192.168.0.140",
66
user: "root",
77
password: "",
88
database: "snaily-cad",
9+
reconnect: true,
910
});
1011

11-
// const sql = conn.query.select("id").from("citizens").where("id", "b33a9b77-3313-43df-a2c2-1e6e20858b81");
12+
const sql = conn.query.select("*").from("citizens").order("full_name", "ASC");
1213

13-
const sql = conn.query.delete("citizens").where("id", "b33a9b77-3313-43df-a2c2-1e6e20858b81");
14-
const result = await sql.exec<{ id: string }>().catch(console.error);
14+
// const sql = conn.query.delete("citizens").where("id", "b33a9b77-3313-43df-a2c2-1e6e20858b81");
15+
const result = await sql.exec().catch(console.error);
16+
17+
console.log(conn.query.values);
18+
console.log(conn.query.query);
1519

1620
console.log(result);
21+
22+
await conn.end();
1723
}
1824

1925
test();

0 commit comments

Comments
 (0)