@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 CHANGED
@@ -3,4 +3,4 @@ MYSQL_HOST = "rm-bp1a906h4019pldm6io.mysql.rds.aliyuncs.com"
3
3
  MYSQL_USER = "kms"
4
4
  MYSQL_PASS = "bmPWuq_vJgupdL9"
5
5
  MYSQL_PORT = 3306
6
- MYSQL_DB = "factory-cloud-dev"
6
+ MYSQL_DB = "business_5b5ecd9941d6cc1bbc065fc6"
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
- alias: string;
35
- on: string;
36
- type: 'left' | 'right' | 'inner';
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(table: string, alias: string, on: string, type: 'left' | 'right' | 'inner'): this;
90
+ join(opt: JoinOption): this;
92
91
  }
93
92
 
94
93
  export declare class QueryOperator extends Query {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axiosleo/orm-mysql",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "MySQL ORM tool",
5
5
  "keywords": [
6
6
  "mysql",
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
- // const mm = require('mm');
5
- // // const expect = require('chai').expect;
6
- // const mysql = require('mysql2');
6
+
7
+ const mysql = require('mysql2');
7
8
  const {
8
- // QueryHandler,
9
- Query
9
+ QueryHandler,
10
+ // Query
10
11
  } = require('../src/operator');
11
12
 
12
- // mm(mysql, 'createConnection', (options) => {
13
- // return {
14
-
15
- // };
16
- // });
17
-
18
- // const conn = mysql.createConnection({});
19
- // const hanlder = new QueryHandler(conn);
20
- // // const query = hanlder.table('meta_items', 'u');
21
-
22
- // const query = hanlder.table('meta_items');
23
- // const subQuery = new Query('select');
24
- // subQuery.table('meta_items').attr('item_code').groupBy('item_code').having('COUNT(*)', '>', 1);
25
- // const res = query.where('item_code', subQuery, 'IN').buildSql('select');
26
- // expect(res.sql).to.be.equal('SELECT * FROM `users` AS `u` WHERE `name` IN (SELECT * FROM `users`)');
27
-
28
- // debug.log(res);
29
-
30
- const q = new Query();
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 = options.sql;
9
- this.values = options.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
- switch (j.type) {
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
- return ` LEFT JOIN \`${j.table}\` AS \`${j.alias}\` ON ${j.on}`;
83
- case 'inner':
84
- return ` INNER JOIN \`${j.table}\` AS \`${j.alias}\` ON ${j.on}`;
88
+ sql = ' LEFT JOIN ';
89
+ break;
85
90
  case 'right':
86
- return ` RIGHT JOIN \`${j.table}\` AS \`${j.alias}\` ON ${j.on}`;
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
- join(table, alias, on, type) {
134
- this.options.joins.push({ table, alias, on, type });
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
  }