@axiosleo/orm-mysql 0.9.12 → 0.9.14
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 +7 -0
- package/bin/orm-mysql.js +1 -1
- package/index.d.ts +22 -2
- package/package.json +1 -1
- package/src/builder.js +24 -2
- package/src/operator.js +6 -0
- package/src/query.js +1 -0
package/README.md
CHANGED
|
@@ -104,6 +104,13 @@ async function insertExample() {
|
|
|
104
104
|
name: "Joe",
|
|
105
105
|
age: 18,
|
|
106
106
|
});
|
|
107
|
+
|
|
108
|
+
// The insert operation will be changed to the update operation if the uuid already exists
|
|
109
|
+
row = await query.keys('uuid').insert({
|
|
110
|
+
uuid: 'uuid-string',
|
|
111
|
+
name: "Joe",
|
|
112
|
+
age: 18,
|
|
113
|
+
})
|
|
107
114
|
}
|
|
108
115
|
|
|
109
116
|
async function updateExample() {
|
package/bin/orm-mysql.js
CHANGED
package/index.d.ts
CHANGED
|
@@ -22,8 +22,8 @@ export type Clients = {
|
|
|
22
22
|
export type ConditionValueType = null | string | number | boolean | Date | Array<string | number | boolean | Date> | Query;
|
|
23
23
|
|
|
24
24
|
export type OptType = '=' | '!=' | '>' | '<' | '>=' | '<=' |
|
|
25
|
-
'LIKE' | 'NOT LIKE' | 'IN' | 'NOT IN' | 'BETWEEN' | 'NOT BETWEEN' | 'IS' | 'IS NOT' | 'REGEXP' | 'NOT REGEXP' | 'AND' | 'OR' | 'GROUP' |
|
|
26
|
-
'like' | 'not like' | 'in' | 'not in' | 'between' | 'not between' | 'is' | 'is not' | 'regexp' | 'not regexp' | 'and' | 'or' | 'group';
|
|
25
|
+
'LIKE' | 'NOT LIKE' | 'IN' | 'NOT IN' | 'BETWEEN' | 'NOT BETWEEN' | 'IS' | 'IS NOT' | 'REGEXP' | 'NOT REGEXP' | 'AND' | 'OR' | 'GROUP' | 'CONTAIN' | 'NOT contain' |
|
|
26
|
+
'like' | 'not like' | 'in' | 'not in' | 'between' | 'not between' | 'is' | 'is not' | 'regexp' | 'not regexp' | 'and' | 'or' | 'group' | 'contain' | 'not contain';
|
|
27
27
|
|
|
28
28
|
export interface WhereOptions {
|
|
29
29
|
key: string | null;
|
|
@@ -81,6 +81,7 @@ export type QueryOperatorOptions = QueryOperatorBaseOptions & {
|
|
|
81
81
|
having: WhereOptions[];
|
|
82
82
|
suffix?: string | null;
|
|
83
83
|
transaction: boolean;
|
|
84
|
+
explain?: boolean;
|
|
84
85
|
}
|
|
85
86
|
|
|
86
87
|
export declare class Query {
|
|
@@ -133,6 +134,23 @@ export declare class Query {
|
|
|
133
134
|
|
|
134
135
|
export type QueryResult = any | undefined | RowDataPacket[] | RowDataPacket | MySQLQueryResult;
|
|
135
136
|
|
|
137
|
+
export type ExplainResult = {
|
|
138
|
+
select_type: 'PRIMARY' | 'DERIVED';
|
|
139
|
+
table: string;
|
|
140
|
+
partitions: string | null;
|
|
141
|
+
type: string | 'ALL' | 'eq_ref';
|
|
142
|
+
possible_keys: null | 'PRIMARY';
|
|
143
|
+
key: null | string;
|
|
144
|
+
key_len: null | number;
|
|
145
|
+
ref: null | string;
|
|
146
|
+
rows: number;
|
|
147
|
+
filtered: number;
|
|
148
|
+
Extra: null | string | 'Using filesort';
|
|
149
|
+
|
|
150
|
+
id?: number;
|
|
151
|
+
[property: string]: any;
|
|
152
|
+
};
|
|
153
|
+
|
|
136
154
|
export declare class QueryOperator extends Query {
|
|
137
155
|
conn: Connection | Pool;
|
|
138
156
|
options: QueryOperatorOptions
|
|
@@ -143,6 +161,8 @@ export declare class QueryOperator extends Query {
|
|
|
143
161
|
|
|
144
162
|
exec(): Promise<QueryResult>;
|
|
145
163
|
|
|
164
|
+
explain(operator: OperatorType): Promise<ExplainResult[]>;
|
|
165
|
+
|
|
146
166
|
select<T>(...attrs: string[]): Promise<T[]>;
|
|
147
167
|
|
|
148
168
|
find<T>(): Promise<T>;
|
package/package.json
CHANGED
package/src/builder.js
CHANGED
|
@@ -120,6 +120,10 @@ class Builder {
|
|
|
120
120
|
throw new Error('Invalid operator: ' + options.operator);
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
+
if (options.explain) {
|
|
124
|
+
sql = 'EXPLAIN ' + sql;
|
|
125
|
+
}
|
|
126
|
+
|
|
123
127
|
this.sql = sql;
|
|
124
128
|
}
|
|
125
129
|
|
|
@@ -147,8 +151,10 @@ class Builder {
|
|
|
147
151
|
const builder = new Builder(table.options);
|
|
148
152
|
this.values = this.values.concat(builder.values);
|
|
149
153
|
table = `(${builder.sql})`;
|
|
150
|
-
|
|
151
|
-
|
|
154
|
+
if (alias) {
|
|
155
|
+
table = `${table} AS \`${alias}\``;
|
|
156
|
+
}
|
|
157
|
+
} else if (alias) {
|
|
152
158
|
table = `\`${table}\` AS \`${alias}\``;
|
|
153
159
|
} else {
|
|
154
160
|
table = `\`${table}\``;
|
|
@@ -273,6 +279,18 @@ class Builder {
|
|
|
273
279
|
return res ? `${this._buildFieldKey(condition.key)} ${opt} (${res})` : `${this._buildFieldKey(condition.key)} ${opt} (?)`;
|
|
274
280
|
}
|
|
275
281
|
|
|
282
|
+
_buildConditionContain(condition, isNot = false) {
|
|
283
|
+
if (condition.key.indexOf('$') !== -1) {
|
|
284
|
+
let res = this._buildConditionValues(condition.value);
|
|
285
|
+
let sql = res ? `JSON_CONTAINS(${this._buildFieldKey(condition.key)}, JSON_ARRAY(${res}))` :
|
|
286
|
+
`JSON_CONTAINS(${this._buildFieldKey(condition.key)}, JSON_ARRAY(?))`;
|
|
287
|
+
return isNot ? `${sql}=0` : sql;
|
|
288
|
+
}
|
|
289
|
+
let res = this._buildConditionValues(condition.value);
|
|
290
|
+
const opt = isNot ? 'NOT LIKE' : 'LIKE';
|
|
291
|
+
return res ? `${this._buildFieldKey(condition.key)} ${opt} CONCAT('%', ?, '%')` : `${this._buildFieldKey(condition.key)} ${opt} CONCAT('%', ?, '%')`;
|
|
292
|
+
}
|
|
293
|
+
|
|
276
294
|
_buildCondition(conditions, prefix) {
|
|
277
295
|
if (!conditions || !conditions.length) {
|
|
278
296
|
return '';
|
|
@@ -309,6 +327,10 @@ class Builder {
|
|
|
309
327
|
return this._buildConditionIn(c, true);
|
|
310
328
|
} else if (opt === 'group' && Array.isArray(c.value)) {
|
|
311
329
|
return `(${this._buildCondition(c.value, '')})`;
|
|
330
|
+
} else if (opt === 'contain') {
|
|
331
|
+
return this._buildConditionContain(c);
|
|
332
|
+
} else if (opt === 'not contain') {
|
|
333
|
+
return this._buildConditionContain(c, true);
|
|
312
334
|
}
|
|
313
335
|
let res = this._buildConditionValues(c.value);
|
|
314
336
|
if (!is.empty(res)) {
|
package/src/operator.js
CHANGED
|
@@ -32,6 +32,12 @@ class QueryOperator extends Query {
|
|
|
32
32
|
return new Builder(this.options);
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
async explain(operator) {
|
|
36
|
+
this.options.operator = operator;
|
|
37
|
+
this.options.explain = true;
|
|
38
|
+
return await this.exec();
|
|
39
|
+
}
|
|
40
|
+
|
|
35
41
|
async exec() {
|
|
36
42
|
if (!this.options.operator) {
|
|
37
43
|
throw new Error('Invalid operator: ' + this.options.operator);
|