@axiosleo/orm-mysql 0.1.0-alpha.1 → 0.1.0-alpha.4
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/README.md +85 -2
- package/index.d.ts +3 -1
- package/package.json +1 -1
- package/src/builder.js +0 -4
- package/src/query.js +8 -0
package/README.md
CHANGED
|
@@ -1,2 +1,85 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
1
|
+
# @axiosleo/orm-mysql
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @axiosleo/orm-mysql
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```javascript
|
|
12
|
+
const { createClient, QueryHandler } = require('@axiosleo/orm-mysql');
|
|
13
|
+
|
|
14
|
+
const conn = createClient({
|
|
15
|
+
host: process.env.MYSQL_HOST,
|
|
16
|
+
port: process.env.MYSQL_PORT,
|
|
17
|
+
user: process.env.MYSQL_USER,
|
|
18
|
+
password: process.env.MYSQL_PASS,
|
|
19
|
+
database: process.env.MYSQL_DB,
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const hanlder = new QueryHandler(conn);
|
|
23
|
+
|
|
24
|
+
async function selectExample(){
|
|
25
|
+
const query = handler.table('users'); // init QueryOperator by table name
|
|
26
|
+
|
|
27
|
+
query.attr('id', 'name', 'age'); // set attributes
|
|
28
|
+
query.where('name','Joe'); // set where condition
|
|
29
|
+
query.orWhere('age', '>', 18); // set or where condition
|
|
30
|
+
query.andWhere('age', '<', 30); // set and where condition
|
|
31
|
+
query.orderBy('age', 'desc'); // set order by
|
|
32
|
+
query.limit(10); // set limit
|
|
33
|
+
query.offset(0); // set offset
|
|
34
|
+
|
|
35
|
+
let rows = await query.select(); // select
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function findExample(){
|
|
39
|
+
const query = handler.table('users'); // init QueryOperator by table name
|
|
40
|
+
|
|
41
|
+
query.attr('id', 'name', 'age'); // set attributes
|
|
42
|
+
query.where('name','Joe'); // set where condition
|
|
43
|
+
query.orWhere('age', '>', 18); // set or where condition
|
|
44
|
+
query.andWhere('age', '<', 30); // set and where condition
|
|
45
|
+
query.orderBy('age', 'desc'); // set order by
|
|
46
|
+
// query.limit(10); // not supported set limit
|
|
47
|
+
// query.offset(10); // not supported set offset
|
|
48
|
+
|
|
49
|
+
let row = await query.find(); // find single row
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function insertExample(){
|
|
53
|
+
const query = handler.table('users');
|
|
54
|
+
|
|
55
|
+
// insert
|
|
56
|
+
let row = await query.insert({
|
|
57
|
+
name: 'Joe',
|
|
58
|
+
age: 18,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function updateExample(){
|
|
63
|
+
const query = handler.table('users');
|
|
64
|
+
|
|
65
|
+
// update
|
|
66
|
+
let row = await query.where('name','Joe').update({
|
|
67
|
+
name: 'Joe',
|
|
68
|
+
age: 18,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function deleteExample(){
|
|
73
|
+
const query = handler.table('users');
|
|
74
|
+
|
|
75
|
+
// delete with conditions
|
|
76
|
+
let result = await query.where('name','Joe').delete();
|
|
77
|
+
|
|
78
|
+
// delete by id
|
|
79
|
+
result = await query.delete(1);
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
This project is open-sourced software licensed under the [MIT](LICENSE).
|
package/index.d.ts
CHANGED
|
@@ -103,6 +103,8 @@ export declare class QueryOperator {
|
|
|
103
103
|
insert(data?: any): Promise<OkPacket>;
|
|
104
104
|
|
|
105
105
|
count(): Promise<number>;
|
|
106
|
+
|
|
107
|
+
delete(id?: number): Promise<OkPacket>;
|
|
106
108
|
}
|
|
107
109
|
|
|
108
110
|
export declare class QueryHandler {
|
|
@@ -117,6 +119,6 @@ export declare class QueryHandler {
|
|
|
117
119
|
upsert(tableName: string, data: any, condition: Record<string, ConditionValueType>): Promise<OkPacket>;
|
|
118
120
|
}
|
|
119
121
|
|
|
120
|
-
export function createClient(options: ConnectionOptions): Connection;
|
|
122
|
+
export function createClient(options: ConnectionOptions, name?: string | null | undefined): Connection;
|
|
121
123
|
|
|
122
124
|
export function getClient(name: string): Connection;
|
package/package.json
CHANGED
package/src/builder.js
CHANGED
|
@@ -78,10 +78,6 @@ const _buildValues = (obj) => {
|
|
|
78
78
|
const values = [];
|
|
79
79
|
Object.keys(obj).forEach((key) => {
|
|
80
80
|
fields.push(`${key}`);
|
|
81
|
-
if (obj[key] === null) {
|
|
82
|
-
values.push('NULL');
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
81
|
if (obj[key] instanceof Date) {
|
|
86
82
|
values.push(obj[key]);
|
|
87
83
|
} else if (Array.isArray(obj[key]) || is.object(obj[key])) {
|
package/src/query.js
CHANGED
|
@@ -193,6 +193,14 @@ class QueryOperator {
|
|
|
193
193
|
this.options.operator = 'count';
|
|
194
194
|
return await this.exec();
|
|
195
195
|
}
|
|
196
|
+
|
|
197
|
+
async delete(id) {
|
|
198
|
+
if (id) {
|
|
199
|
+
this.where('id', id);
|
|
200
|
+
}
|
|
201
|
+
this.options.operator = 'delete';
|
|
202
|
+
return await this.exec();
|
|
203
|
+
}
|
|
196
204
|
}
|
|
197
205
|
|
|
198
206
|
class QueryHandler {
|