@axiosleo/orm-mysql 0.2.2 → 0.3.0
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/.env +1 -1
- package/index.d.ts +5 -6
- package/package.json +1 -1
- package/runtimes/test.js +38 -25
- package/src/builder.js +19 -8
- package/src/query.js +31 -5
package/.env
CHANGED
package/index.d.ts
CHANGED
|
@@ -31,9 +31,10 @@ export type OperatorType = 'select' | 'find' | 'insert' | 'update' | 'delete' |
|
|
|
31
31
|
|
|
32
32
|
export interface JoinOption {
|
|
33
33
|
table: string;
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
table_alias?: string;
|
|
35
|
+
self_column: string;
|
|
36
|
+
foreign_column: string;
|
|
37
|
+
join_type?: 'left' | 'right' | 'inner';
|
|
37
38
|
}
|
|
38
39
|
|
|
39
40
|
export interface TableOption {
|
|
@@ -42,8 +43,6 @@ export interface TableOption {
|
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
export interface QueryOperatorOptions {
|
|
45
|
-
sql: string;
|
|
46
|
-
values: any[];
|
|
47
46
|
conditions: WhereOptions[];
|
|
48
47
|
attrs?: string[] | null;
|
|
49
48
|
orders: OrderByOptions[];
|
|
@@ -88,7 +87,7 @@ export declare class Query {
|
|
|
88
87
|
|
|
89
88
|
set(data: any): this;
|
|
90
89
|
|
|
91
|
-
join(
|
|
90
|
+
join(opt: JoinOption): this;
|
|
92
91
|
}
|
|
93
92
|
|
|
94
93
|
export declare class QueryOperator extends Query {
|
package/package.json
CHANGED
package/runtimes/test.js
CHANGED
|
@@ -1,32 +1,45 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const dotenv = require('dotenv');
|
|
4
|
+
dotenv.config();
|
|
3
5
|
const { debug } = require('@axiosleo/cli-tool');
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
// const mysql = require('mysql2');
|
|
6
|
+
|
|
7
|
+
const mysql = require('mysql2');
|
|
7
8
|
const {
|
|
8
|
-
|
|
9
|
-
Query
|
|
9
|
+
QueryHandler,
|
|
10
|
+
// Query
|
|
10
11
|
} = require('../src/operator');
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
13
|
+
const conn = mysql.createConnection({
|
|
14
|
+
host: process.env.MYSQL_HOST,
|
|
15
|
+
port: process.env.MYSQL_PORT,
|
|
16
|
+
user: process.env.MYSQL_USER,
|
|
17
|
+
password: process.env.MYSQL_PASS,
|
|
18
|
+
database: process.env.MYSQL_DB,
|
|
19
|
+
});
|
|
20
|
+
const hanlder = new QueryHandler(conn);
|
|
21
|
+
|
|
22
|
+
const test = async () => {
|
|
23
|
+
const query = hanlder.table('meta_items_relationship', 'mir')
|
|
24
|
+
.attr('mi_p.meta as p_meta', 'mi_c.meta as c_meta', 'mir.item_parent as item_parent_id', 'mir.item_child as item_child_id');
|
|
25
|
+
const res = query
|
|
26
|
+
.where('mi_p.id', 601)
|
|
27
|
+
.join({
|
|
28
|
+
table: 'meta_items',
|
|
29
|
+
table_alias: 'mi_p',
|
|
30
|
+
self_column: 'mir.item_parent',
|
|
31
|
+
foreign_column: 'mi_p.id',
|
|
32
|
+
// join_type: 'left'
|
|
33
|
+
})
|
|
34
|
+
.join({
|
|
35
|
+
table: 'meta_items',
|
|
36
|
+
table_alias: 'mi_c',
|
|
37
|
+
self_column: 'mir.item_child',
|
|
38
|
+
foreign_column: 'mi_c.id',
|
|
39
|
+
}).buildSql('select');
|
|
40
|
+
const result = await query.select();
|
|
41
|
+
debug.halt(res, result);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
test();
|
|
31
45
|
|
|
32
|
-
debug.log(typeof q, q instanceof Query);
|
package/src/builder.js
CHANGED
|
@@ -5,9 +5,8 @@ const is = require('@axiosleo/cli-tool/src/helper/is');
|
|
|
5
5
|
|
|
6
6
|
class Builder {
|
|
7
7
|
constructor(options) {
|
|
8
|
-
let sql =
|
|
9
|
-
this.values =
|
|
10
|
-
|
|
8
|
+
let sql = '';
|
|
9
|
+
this.values = [];
|
|
11
10
|
switch (options.operator) {
|
|
12
11
|
case 'find': {
|
|
13
12
|
options.pageLimit = 1;
|
|
@@ -77,14 +76,26 @@ class Builder {
|
|
|
77
76
|
|
|
78
77
|
_buildJoins(joins = []) {
|
|
79
78
|
return joins.map((j) => {
|
|
80
|
-
|
|
79
|
+
let { table, alias, self_column, foreign_column, join_type } = j;
|
|
80
|
+
if (alias) {
|
|
81
|
+
table = `\`${table}\` AS \`${alias}\``;
|
|
82
|
+
} else {
|
|
83
|
+
table = `\`${table}\``;
|
|
84
|
+
}
|
|
85
|
+
let sql = '';
|
|
86
|
+
switch (join_type) {
|
|
81
87
|
case 'left':
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return ` INNER JOIN \`${j.table}\` AS \`${j.alias}\` ON ${j.on}`;
|
|
88
|
+
sql = ' LEFT JOIN ';
|
|
89
|
+
break;
|
|
85
90
|
case 'right':
|
|
86
|
-
|
|
91
|
+
sql = ' RIGHT JOIN ';
|
|
92
|
+
break;
|
|
93
|
+
default:
|
|
94
|
+
sql = ' INNER JOIN ';
|
|
95
|
+
break;
|
|
87
96
|
}
|
|
97
|
+
sql += `${table} ON ${this._buildFieldWithTableName(self_column)} = ${this._buildFieldWithTableName(foreign_column)}`;
|
|
98
|
+
return sql;
|
|
88
99
|
}).join(' ');
|
|
89
100
|
}
|
|
90
101
|
|
package/src/query.js
CHANGED
|
@@ -3,15 +3,14 @@
|
|
|
3
3
|
class Query {
|
|
4
4
|
constructor(operator = 'select') {
|
|
5
5
|
this.options = {
|
|
6
|
-
sql: '',
|
|
7
|
-
values: [],
|
|
8
6
|
conditions: [],
|
|
9
7
|
orders: [],
|
|
10
8
|
tables: [],
|
|
11
9
|
operator,
|
|
12
10
|
data: null,
|
|
13
11
|
groupField: [],
|
|
14
|
-
having: []
|
|
12
|
+
having: [],
|
|
13
|
+
joins: []
|
|
15
14
|
};
|
|
16
15
|
}
|
|
17
16
|
|
|
@@ -30,7 +29,17 @@ class Query {
|
|
|
30
29
|
return this;
|
|
31
30
|
}
|
|
32
31
|
|
|
32
|
+
/**
|
|
33
|
+
*
|
|
34
|
+
* @param {string} key
|
|
35
|
+
* @param {any} value
|
|
36
|
+
* @param {*} opt
|
|
37
|
+
* @returns
|
|
38
|
+
*/
|
|
33
39
|
where(key, value, opt = '=') {
|
|
40
|
+
if (!key) {
|
|
41
|
+
throw new Error('key is required');
|
|
42
|
+
}
|
|
34
43
|
if (this.options.conditions.length) {
|
|
35
44
|
this.options.conditions.push({ key: null, opt: 'AND', value: null });
|
|
36
45
|
}
|
|
@@ -130,8 +139,25 @@ class Query {
|
|
|
130
139
|
return this;
|
|
131
140
|
}
|
|
132
141
|
|
|
133
|
-
|
|
134
|
-
|
|
142
|
+
/**
|
|
143
|
+
* @param {{table:string,table_alias?:string,self_column: string,foreign_column: string,join_type?: 'left' | 'right' | 'inner'}} opt
|
|
144
|
+
* @returns
|
|
145
|
+
*/
|
|
146
|
+
join(opt = {}) {
|
|
147
|
+
let { table, table_alias, self_column, foreign_column, join_type } = opt;
|
|
148
|
+
if (!table) {
|
|
149
|
+
throw new Error('table is required');
|
|
150
|
+
}
|
|
151
|
+
if (!self_column) {
|
|
152
|
+
throw new Error('self_column is required');
|
|
153
|
+
}
|
|
154
|
+
if (!foreign_column) {
|
|
155
|
+
throw new Error('foreign_column is required');
|
|
156
|
+
}
|
|
157
|
+
if (join_type && ['left', 'right', 'inner'].indexOf(join_type) === -1) {
|
|
158
|
+
throw new Error('Invalid join type : ' + join_type + '; only supported left, right, inner');
|
|
159
|
+
}
|
|
160
|
+
this.options.joins.push({ table, alias: table_alias, self_column, foreign_column, join_type });
|
|
135
161
|
return this;
|
|
136
162
|
}
|
|
137
163
|
}
|