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

Commit cb8d685

Browse files
committed
🎉 NEW: 0.0.1
1 parent 1ae59fa commit cb8d685

File tree

9 files changed

+134
-19
lines changed

9 files changed

+134
-19
lines changed

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"no-useless-catch": ["warn"],
2626
"prettier/prettier": ["error"],
2727
"@typescript-eslint/no-explicit-any": "off",
28-
"@typescript-eslint/explicit-module-boundary-types": "off"
28+
"@typescript-eslint/explicit-module-boundary-types": "off",
29+
"prefer-rest-params": "off"
2930
}
3031
}

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Changelog
2+
3+
## 0.0.1
4+
5+
- Initial release
6+
- Add documentation
7+
8+
## 0.0.0-alpha
9+
10+
- Initial version

docs/Query.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Query
2+
3+
## Table of contents
4+
5+
- [Select](#select)
6+
- [Insert](#insert)
7+
- [Update](#update)
8+
- [Delete](#delete)
9+
- [Raw](#raw)
10+
- [All methods](#all-methods)
11+
12+
## Select
13+
14+
```ts
15+
const connection = await createConnection({
16+
/* ... */
17+
});
18+
19+
const books = await connection.query.select("*").from("books").where("name", "some-book-name").exec();
20+
```
21+
22+
## Insert
23+
24+
```ts
25+
const connection = await createConnection({
26+
/* ... */
27+
});
28+
29+
const data = {
30+
id: 1,
31+
name: "book-name",
32+
};
33+
34+
const result = await connection.query.insert("books", data).exec();
35+
```
36+
37+
## Update
38+
39+
```ts
40+
const connection = await createConnection({
41+
/* ... */
42+
});
43+
44+
const data = {
45+
id: 1,
46+
name: "book-name",
47+
};
48+
49+
const result = await connection.query.update("books", data).where("id", data.id).exec();
50+
```
51+
52+
## Delete
53+
54+
```ts
55+
const connection = await createConnection({
56+
/* ... */
57+
});
58+
59+
const result = await connection.query.delete("books").where("name", "book-name").exec();
60+
```
61+
62+
## Raw
63+
64+
```ts
65+
const connection = await createConnection({
66+
/* ... */
67+
});
68+
69+
// Full raw query
70+
const result = await connection.query.raw("SELECT * FROM `books`").exec();
71+
72+
const result = await connection.query
73+
.raw("INSERT INTO `books` (`id`, `name`) VALUES (?, ?)", ["2", "book-name"])
74+
.exec();
75+
76+
// With chaining
77+
const result = await connection.query.raw("SELECT * FROM `books`").where("name", "cool-book-name").exec();
78+
```
79+
80+
## All methods
81+
82+
- `select`
83+
- `from`
84+
- `insert`
85+
- `update`
86+
- `delete`
87+
- `where`
88+
- `order`
89+
- `limit`
90+
- `having`
91+
- `raw`
92+
- `exec`

docs/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ async function init() {
3030
console.log(results);
3131
}
3232
```
33+
34+
## More
35+
36+
- [Query](./Query.md)

docs/connection.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

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

src/QueryBuilder.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export class QueryBuilder {
4545
*/
4646
insert<T = Record<string, unknown>>(tableName: string, data: T) {
4747
const values = Object.keys(data).map((key) => {
48-
// eslint-disable-next-line
4948
return `${(data as any)[key]}`;
5049
});
5150

@@ -80,10 +79,10 @@ export class QueryBuilder {
8079
* @param selector The table name
8180
* @example
8281
* // delete an item with 'where'
83-
* <Connection>.query.delete("books").where("name", "cool-book-name");
82+
* <Connection>.query.delete("books").where("name", "cool-book-name").exec();
8483
*
8584
* // delete all items from table
86-
* <Connection>.query.delete("books");
85+
* <Connection>.query.delete("books").exec();
8786
*/
8887
delete(selector: string) {
8988
this.query = `DELETE FROM ${selector} `;
@@ -116,9 +115,21 @@ export class QueryBuilder {
116115
return this;
117116
}
118117

119-
raw(query: string, values: unknown[]) {
120-
this.query = query;
121-
this.values = values;
118+
/**
119+
* Create a raw query
120+
* @param query The raw query
121+
* @param values Values that are needed for insert, update, ..
122+
* @example
123+
*
124+
* // Full raw query
125+
* <Connection>.query.raw("SELECT * FROM `books`").exec();
126+
*
127+
* // With chaining
128+
* <Connection>.query.raw("SELECT * FROM `books`").where("name", "cool-book-name").exec();
129+
*/
130+
raw(query: string, values?: unknown[]) {
131+
this.query = `${query} `;
132+
this.values = values ?? [];
122133

123134
return this;
124135
}

tests/index.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ async function test() {
99
reconnect: true,
1010
});
1111

12-
const sql = conn.query.select("*").from("citizens").order("full_name", "ASC");
12+
// const sql = conn.query.select("*").from("citizens").order("full_name", "ASC");
13+
const sql = conn.query.raw("SELECT * FROM `citizens`").where("id", "a22e0c79-5ecd-4012-85bb-a214196e2d72");
1314

1415
// const sql = conn.query.delete("citizens").where("id", "b33a9b77-3313-43df-a2c2-1e6e20858b81");
1516
const result = await sql.exec().catch(console.error);
1617

17-
console.log(conn.query.values);
18-
console.log(conn.query.query);
19-
20-
console.log(result);
18+
result;
2119

2220
await conn.end();
2321
}

0 commit comments

Comments
 (0)