@axiosleo/orm-mysql 0.9.3 → 0.9.5
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/bin/orm-mysql.js +1 -1
- package/index.d.ts +5 -2
- package/package.json +1 -1
- package/src/builder.js +15 -14
package/bin/orm-mysql.js
CHANGED
package/index.d.ts
CHANGED
|
@@ -53,9 +53,12 @@ export type QueryOperatorBaseOptions = {
|
|
|
53
53
|
queryHandler?: QueryHandler;
|
|
54
54
|
};
|
|
55
55
|
|
|
56
|
+
export type AttrSubQuery = () => Query;
|
|
57
|
+
export type Attr = string | AttrSubQuery;
|
|
58
|
+
|
|
56
59
|
export type QueryOperatorOptions = QueryOperatorBaseOptions & {
|
|
57
60
|
conditions: WhereOptions[];
|
|
58
|
-
attrs?:
|
|
61
|
+
attrs?: Attr[] | null;
|
|
59
62
|
orders: OrderByOptions[];
|
|
60
63
|
pageLimit?: number;
|
|
61
64
|
pageOffset?: number;
|
|
@@ -92,7 +95,7 @@ export declare class Query {
|
|
|
92
95
|
|
|
93
96
|
andWhere(key: string | null, opt: OptType, value: ConditionValueType | WhereOptions[]): this;
|
|
94
97
|
|
|
95
|
-
attr(...attr:
|
|
98
|
+
attr(...attr: Attr[]): this;
|
|
96
99
|
|
|
97
100
|
orderBy(sortField: string, sortOrder: 'asc' | 'desc'): this;
|
|
98
101
|
|
package/package.json
CHANGED
package/src/builder.js
CHANGED
|
@@ -37,6 +37,7 @@ class Builder {
|
|
|
37
37
|
if (attr instanceof Function) {
|
|
38
38
|
const query = attr();
|
|
39
39
|
const builder = new Builder(query.options);
|
|
40
|
+
this.values = this.values.concat(builder.values);
|
|
40
41
|
let s = `(${builder.sql})`;
|
|
41
42
|
if (query.alias) {
|
|
42
43
|
return query.alias.indexOf(' ') > -1 ? s + ' ' + this._buildFieldKey(query.alias)
|
|
@@ -48,9 +49,9 @@ class Builder {
|
|
|
48
49
|
});
|
|
49
50
|
emit(tmp, `SELECT ${attrs.length ? attrs.map((a) => this._buildFieldKey(a)).join(',') : '*'} FROM ${this._buildTables(options.tables)}`);
|
|
50
51
|
emit(tmp, this._buildJoins(options.joins));
|
|
51
|
-
emit(tmp, this.
|
|
52
|
+
emit(tmp, this._buildCondition(options.conditions));
|
|
52
53
|
emit(tmp, this._buildOrders(options.orders));
|
|
53
|
-
emit(tmp, this.
|
|
54
|
+
emit(tmp, this._buildPagination(options.pageLimit, options.pageOffset));
|
|
54
55
|
if (options.having && options.having.length && !options.groupField.length) {
|
|
55
56
|
throw new Error('having is not allowed without "GROUP BY"');
|
|
56
57
|
}
|
|
@@ -76,7 +77,7 @@ class Builder {
|
|
|
76
77
|
if (!options.conditions.length) {
|
|
77
78
|
throw new Error('At least one condition is required for update operation');
|
|
78
79
|
}
|
|
79
|
-
emit(tmp, this.
|
|
80
|
+
emit(tmp, this._buildCondition(options.conditions));
|
|
80
81
|
sql = tmp.join(' ');
|
|
81
82
|
break;
|
|
82
83
|
}
|
|
@@ -85,14 +86,14 @@ class Builder {
|
|
|
85
86
|
if (!options.conditions.length) {
|
|
86
87
|
throw new Error('At least one where condition is required for delete operation');
|
|
87
88
|
}
|
|
88
|
-
emit(tmp, this.
|
|
89
|
+
emit(tmp, this._buildCondition(options.conditions));
|
|
89
90
|
sql = tmp.join(' ');
|
|
90
91
|
break;
|
|
91
92
|
}
|
|
92
93
|
case 'count': {
|
|
93
94
|
emit(tmp, `SELECT COUNT(*) AS count FROM ${this._buildTables(options.tables)}`);
|
|
94
95
|
emit(tmp, this._buildJoins(options.joins));
|
|
95
|
-
emit(tmp, this.
|
|
96
|
+
emit(tmp, this._buildCondition(options.conditions));
|
|
96
97
|
if (options.having && options.having.length && !options.groupField.length) {
|
|
97
98
|
throw new Error('having is not allowed without "GROUP BY"');
|
|
98
99
|
}
|
|
@@ -122,7 +123,7 @@ class Builder {
|
|
|
122
123
|
if (!having || !having.length) {
|
|
123
124
|
return '';
|
|
124
125
|
}
|
|
125
|
-
return this.
|
|
126
|
+
return this._buildCondition(having, 'HAVING ');
|
|
126
127
|
}
|
|
127
128
|
|
|
128
129
|
_buildJoins(joins = []) {
|
|
@@ -130,7 +131,7 @@ class Builder {
|
|
|
130
131
|
let { table, alias, self_column, foreign_column, join_type } = j;
|
|
131
132
|
if (table instanceof Query) {
|
|
132
133
|
if (!alias) {
|
|
133
|
-
throw new Error('Alias is required for
|
|
134
|
+
throw new Error('Alias is required for subQuery');
|
|
134
135
|
}
|
|
135
136
|
const builder = new Builder(table.options);
|
|
136
137
|
this.values = this.values.concat(builder.values);
|
|
@@ -186,7 +187,7 @@ class Builder {
|
|
|
186
187
|
}).join(' , ');
|
|
187
188
|
}
|
|
188
189
|
|
|
189
|
-
|
|
190
|
+
_buildPagination(limit, offset) {
|
|
190
191
|
let sql = '';
|
|
191
192
|
if (limit) {
|
|
192
193
|
sql += ` LIMIT ${limit}`;
|
|
@@ -227,7 +228,7 @@ class Builder {
|
|
|
227
228
|
return null;
|
|
228
229
|
}
|
|
229
230
|
|
|
230
|
-
|
|
231
|
+
_buildConditionIn(condition, isNot = false) {
|
|
231
232
|
if (Array.isArray(condition.value) && !condition.value.length) {
|
|
232
233
|
throw new Error('Value must not be empty for "IN" condition');
|
|
233
234
|
} else if (!Array.isArray(condition.value) && !(condition.value instanceof Query)) {
|
|
@@ -244,7 +245,7 @@ class Builder {
|
|
|
244
245
|
return res ? `${this._buildFieldKey(condition.key)} ${opt} (${res})` : `${this._buildFieldKey(condition.key)} ${opt} (?)`;
|
|
245
246
|
}
|
|
246
247
|
|
|
247
|
-
|
|
248
|
+
_buildCondition(conditions, prefix) {
|
|
248
249
|
if (!conditions || !conditions.length) {
|
|
249
250
|
return '';
|
|
250
251
|
}
|
|
@@ -265,7 +266,7 @@ class Builder {
|
|
|
265
266
|
}
|
|
266
267
|
if (c.key && c.key.indexOf('->') !== -1) {
|
|
267
268
|
const keys = c.key.split('->');
|
|
268
|
-
return this.
|
|
269
|
+
return this._buildCondition([
|
|
269
270
|
{
|
|
270
271
|
key: `JSON_EXTRACT(${this._buildFieldKey(keys[0])}, '${keys[1]}')`,
|
|
271
272
|
opt: c.opt,
|
|
@@ -275,11 +276,11 @@ class Builder {
|
|
|
275
276
|
}
|
|
276
277
|
const opt = c.opt.toLowerCase();
|
|
277
278
|
if (opt === 'in') {
|
|
278
|
-
return this.
|
|
279
|
+
return this._buildConditionIn(c);
|
|
279
280
|
} else if (opt === 'not in') {
|
|
280
|
-
return this.
|
|
281
|
+
return this._buildConditionIn(c, true);
|
|
281
282
|
} else if (opt === 'group' && Array.isArray(c.value)) {
|
|
282
|
-
return `(${this.
|
|
283
|
+
return `(${this._buildCondition(c.value, '')})`;
|
|
283
284
|
}
|
|
284
285
|
let res = this._buildConditionValues(c.value);
|
|
285
286
|
if (!is.empty(res)) {
|