@axiosleo/orm-mysql 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierignore +1 -0
- package/README.md +95 -46
- package/package.json +1 -1
- package/src/builder.js +3 -0
package/.prettierignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
README.md
|
package/README.md
CHANGED
|
@@ -8,75 +8,124 @@ npm install @axiosleo/orm-mysql
|
|
|
8
8
|
|
|
9
9
|
## Usage
|
|
10
10
|
|
|
11
|
+
### Create mysql client
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
const { createClient } = require("@axiosleo/orm-mysql");
|
|
15
|
+
|
|
16
|
+
const client = createClient({
|
|
17
|
+
host: process.env.MYSQL_HOST,
|
|
18
|
+
port: process.env.MYSQL_PORT,
|
|
19
|
+
user: process.env.MYSQL_USER,
|
|
20
|
+
password: process.env.MYSQL_PASS,
|
|
21
|
+
database: process.env.MYSQL_DB,
|
|
22
|
+
});
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Initialize database handler
|
|
26
|
+
|
|
27
|
+
```javascript
|
|
28
|
+
const { QueryHandler } = require("@axiosleo/orm-mysql");
|
|
29
|
+
|
|
30
|
+
const db = new QueryHandler(client);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Initialize query
|
|
34
|
+
|
|
35
|
+
```javascript
|
|
36
|
+
const query = db.table('<table-name>');
|
|
37
|
+
|
|
38
|
+
query.attr("id", "name", "age"); // set attributes
|
|
39
|
+
query.where("name", "Joe"); // set where condition
|
|
40
|
+
query.orWhere("age", ">", 18); // set or where condition
|
|
41
|
+
query.andWhere("age", "<", 30); // set and where condition
|
|
42
|
+
query.orderBy("age", "desc"); // set order by
|
|
43
|
+
query.limit(10); // set limit
|
|
44
|
+
query.offset(0); // set offset
|
|
45
|
+
|
|
46
|
+
let rows = await query.select(); // select
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Some Examples
|
|
50
|
+
|
|
11
51
|
```javascript
|
|
12
|
-
const { createClient, QueryHandler } = require(
|
|
52
|
+
const { createClient, QueryHandler, Query } = require("@axiosleo/orm-mysql");
|
|
13
53
|
|
|
14
54
|
const conn = createClient({
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
55
|
+
host: process.env.MYSQL_HOST,
|
|
56
|
+
port: process.env.MYSQL_PORT,
|
|
57
|
+
user: process.env.MYSQL_USER,
|
|
58
|
+
password: process.env.MYSQL_PASS,
|
|
59
|
+
database: process.env.MYSQL_DB,
|
|
20
60
|
});
|
|
21
61
|
|
|
22
62
|
const hanlder = new QueryHandler(conn);
|
|
23
63
|
|
|
24
|
-
async function selectExample(){
|
|
25
|
-
|
|
64
|
+
async function selectExample() {
|
|
65
|
+
const query = handler.table("users"); // init QueryOperator by table name
|
|
26
66
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
67
|
+
query.attr("id", "name", "age"); // set attributes
|
|
68
|
+
query.where("name", "Joe"); // set where condition
|
|
69
|
+
query.orWhere("age", ">", 18); // set or where condition
|
|
70
|
+
query.andWhere("age", "<", 30); // set and where condition
|
|
71
|
+
query.orderBy("age", "desc"); // set order by
|
|
72
|
+
query.limit(10); // set limit
|
|
73
|
+
query.offset(0); // set offset
|
|
34
74
|
|
|
35
|
-
|
|
75
|
+
let rows = await query.select(); // select
|
|
36
76
|
}
|
|
37
77
|
|
|
38
|
-
async function findExample(){
|
|
39
|
-
|
|
78
|
+
async function findExample() {
|
|
79
|
+
const query = handler.table("users"); // init QueryOperator by table name
|
|
40
80
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
81
|
+
query.attr("id", "name", "age"); // set attributes
|
|
82
|
+
query.where("name", "Joe"); // set where condition
|
|
83
|
+
query.orWhere("age", ">", 18); // set or where condition
|
|
84
|
+
query.andWhere("age", "<", 30); // set and where condition
|
|
85
|
+
query.orderBy("age", "desc"); // set order by
|
|
86
|
+
// query.limit(10); // not supported set limit
|
|
87
|
+
// query.offset(10); // not supported set offset
|
|
48
88
|
|
|
49
|
-
|
|
89
|
+
let row = await query.find(); // find single row
|
|
50
90
|
}
|
|
51
91
|
|
|
52
|
-
async function insertExample(){
|
|
53
|
-
|
|
92
|
+
async function insertExample() {
|
|
93
|
+
const query = handler.table("users");
|
|
54
94
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
95
|
+
// insert
|
|
96
|
+
let row = await query.insert({
|
|
97
|
+
name: "Joe",
|
|
98
|
+
age: 18,
|
|
99
|
+
});
|
|
60
100
|
}
|
|
61
101
|
|
|
62
|
-
async function updateExample(){
|
|
63
|
-
|
|
102
|
+
async function updateExample() {
|
|
103
|
+
const query = handler.table("users");
|
|
64
104
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
105
|
+
// update
|
|
106
|
+
let row = await query.where("name", "Joe").update({
|
|
107
|
+
name: "Joe",
|
|
108
|
+
age: 18,
|
|
109
|
+
});
|
|
70
110
|
}
|
|
71
111
|
|
|
72
|
-
async function deleteExample(){
|
|
73
|
-
|
|
112
|
+
async function deleteExample() {
|
|
113
|
+
const query = handler.table("users");
|
|
114
|
+
|
|
115
|
+
// delete with conditions
|
|
116
|
+
let result = await query.where("name", "Joe").delete();
|
|
117
|
+
|
|
118
|
+
// delete by id
|
|
119
|
+
result = await query.delete(1);
|
|
120
|
+
}
|
|
74
121
|
|
|
75
|
-
|
|
76
|
-
|
|
122
|
+
async function subqueryExample() {
|
|
123
|
+
const query = hanlder.table("users", "u");
|
|
124
|
+
const subQuery = new Query("select");
|
|
125
|
+
subQuery.table("users").having("COUNT(*)", ">", 1);
|
|
77
126
|
|
|
78
|
-
|
|
79
|
-
|
|
127
|
+
const sql = query.where("u.name", subQuery, "IN").buildSql("select").sql;
|
|
128
|
+
// SELECT * FROM `users` AS `u` WHERE `u`.`name` IN (SELECT * FROM `users` GROUP BY `u`.`name` HAVING COUNT(*) > ?)
|
|
80
129
|
}
|
|
81
130
|
```
|
|
82
131
|
|
package/package.json
CHANGED
package/src/builder.js
CHANGED
|
@@ -36,6 +36,9 @@ class Builder {
|
|
|
36
36
|
case 'update': {
|
|
37
37
|
const fields = this._buildValues(options.data);
|
|
38
38
|
sql = `UPDATE ${this._buildTables(options.tables)} SET ${fields.map(f => `${this._buildFieldKey(f)} = ?`).join(',')}`;
|
|
39
|
+
if (!options.conditions.length) {
|
|
40
|
+
throw new Error('At least one condition is required for update operation');
|
|
41
|
+
}
|
|
39
42
|
sql += this._buildContidion(options.conditions);
|
|
40
43
|
break;
|
|
41
44
|
}
|