multi-db-orm 2.1.25 → 2.1.26
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/engines/mysqldb.js +228 -229
- package/package.json +1 -1
package/engines/mysqldb.js
CHANGED
|
@@ -1,252 +1,251 @@
|
|
|
1
|
-
const { MultiDbORM } = require(
|
|
1
|
+
const { MultiDbORM } = require("./multidb");
|
|
2
2
|
|
|
3
3
|
class MySQLDB extends MultiDbORM {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
reject(err);
|
|
57
|
-
} else {
|
|
58
|
-
resolve(results);
|
|
59
|
-
}
|
|
60
|
-
if (that.loglevel > 3) {
|
|
61
|
-
console.log("Query ", query, ' -> ', results);
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
async get(modelname, filter, options) {
|
|
68
|
-
this.metrics.get(modelname, filter, options);
|
|
69
|
-
var where = '';
|
|
70
|
-
for (var key in filter) {
|
|
71
|
-
where = where + `${key} = '${filter[key]}' AND `;
|
|
72
|
-
}
|
|
73
|
-
where = where + " 1 ";
|
|
74
|
-
var sort = "";
|
|
75
|
-
if (options) {
|
|
76
|
-
if (options.apply) {
|
|
77
|
-
if (options.apply.ineq) {
|
|
78
|
-
where = where + ` AND '${options.apply.field}' ${options.apply.ineq.op} '${options.apply.ineq.value}'`;
|
|
79
|
-
}
|
|
80
|
-
if (options.apply.sort) {
|
|
81
|
-
sort = `ORDER BY ${options.apply.field} ${options.apply.sort}`;
|
|
82
|
-
}
|
|
83
|
-
} else if (options.sort) {
|
|
84
|
-
sort = `ORDER BY`;
|
|
85
|
-
for (let i = 0; i < options.sort.length; i++) {
|
|
86
|
-
sort = sort + ` ${options.sort[i].field} ${options.sort[i].order}`;
|
|
87
|
-
if (i < options.sort.length - 1) {
|
|
88
|
-
sort = sort + ' , ';
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
4
|
+
mysql;
|
|
5
|
+
pool;
|
|
6
|
+
dataMap = {
|
|
7
|
+
id: "VARCHAR(50) NOT NULL PRIMARY KEY",
|
|
8
|
+
string: "VARCHAR(4000)",
|
|
9
|
+
stringlarge: "TEXT",
|
|
10
|
+
stringsmall: "VARCHAR(255)",
|
|
11
|
+
number: "DOUBLE",
|
|
12
|
+
boolean: "BOOL",
|
|
13
|
+
array: "TEXT",
|
|
14
|
+
object: "JSON",
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
constructor(credentials) {
|
|
18
|
+
super();
|
|
19
|
+
const mysql = require("mysql");
|
|
20
|
+
this.mysql = mysql;
|
|
21
|
+
this.pool = mysql.createPool({
|
|
22
|
+
connectionLimit: credentials.connectionLimit || 10,
|
|
23
|
+
host: credentials.host,
|
|
24
|
+
port: credentials.port,
|
|
25
|
+
user: credentials.username,
|
|
26
|
+
password: credentials.password,
|
|
27
|
+
database: credentials.database,
|
|
28
|
+
waitForConnections: true,
|
|
29
|
+
queueLimit: 0,
|
|
30
|
+
connectTimeout: credentials.connectTimeout || 10000,
|
|
31
|
+
acquireTimeout: credentials.acquireTimeout || 10000,
|
|
32
|
+
timeout: credentials.timeout || 60000,
|
|
33
|
+
});
|
|
34
|
+
this.db = this.pool;
|
|
35
|
+
this.pool.on("connection", (connection) => {
|
|
36
|
+
if (this.loglevel > 1) console.log("MySQLDB: New connection acquired");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
this.pool.on("error", (err) => {
|
|
40
|
+
if (this.loglevel > 0) console.error("MySQLDB: Pool error:", err);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
this.dbType = "mysql";
|
|
44
|
+
this.reqMade = 0;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async run(query) {
|
|
48
|
+
var that = this;
|
|
49
|
+
this.reqMade++;
|
|
50
|
+
return new Promise(function (resolve, reject) {
|
|
51
|
+
that.pool.query(query, function (err, results) {
|
|
52
|
+
if (err) {
|
|
53
|
+
reject(err);
|
|
54
|
+
} else {
|
|
55
|
+
resolve(results);
|
|
92
56
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
if(options?.limit){
|
|
96
|
-
limit = `LIMIT ${options.limit}`
|
|
57
|
+
if (that.loglevel > 3) {
|
|
58
|
+
console.log("Query ", query, " -> ", results);
|
|
97
59
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async get(modelname, filter, options) {
|
|
65
|
+
this.metrics.get(modelname, filter, options);
|
|
66
|
+
var where = "";
|
|
67
|
+
for (var key in filter) {
|
|
68
|
+
where = where + `${key} = '${filter[key]}' AND `;
|
|
103
69
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
70
|
+
where = where + " 1 ";
|
|
71
|
+
var sort = "";
|
|
72
|
+
if (options) {
|
|
73
|
+
if (options.apply) {
|
|
74
|
+
if (options.apply.ineq) {
|
|
75
|
+
where =
|
|
76
|
+
where +
|
|
77
|
+
` AND '${options.apply.field}' ${options.apply.ineq.op} '${options.apply.ineq.value}'`;
|
|
110
78
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
var row = await this.run(query);
|
|
114
|
-
return row[0];
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
async create(modelname, sampleObject) {
|
|
118
|
-
this.sync.create(modelname, sampleObject);
|
|
119
|
-
this.metrics.create(modelname, sampleObject);
|
|
120
|
-
|
|
121
|
-
var cols = '';
|
|
122
|
-
for (var key in sampleObject) {
|
|
123
|
-
var type;
|
|
124
|
-
if (this.dataMap[sampleObject[key]]) {
|
|
125
|
-
type = this.dataMap[sampleObject[key]]
|
|
126
|
-
} else {
|
|
127
|
-
type = this.dataMap[typeof (sampleObject[key])] || 'TEXT';
|
|
128
|
-
if (typeof (sampleObject[key]) == 'string') {
|
|
129
|
-
if (sampleObject[key].length > 4000) {
|
|
130
|
-
type = this.dataMap['stringlarge']
|
|
131
|
-
}
|
|
132
|
-
if (sampleObject[key].length <= 255) {
|
|
133
|
-
type = this.dataMap['stringsmall']
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
cols = cols + `${key} ${type},`;
|
|
79
|
+
if (options.apply.sort) {
|
|
80
|
+
sort = `ORDER BY ${options.apply.field} ${options.apply.sort}`;
|
|
138
81
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
throw err
|
|
82
|
+
} else if (options.sort) {
|
|
83
|
+
sort = `ORDER BY`;
|
|
84
|
+
for (let i = 0; i < options.sort.length; i++) {
|
|
85
|
+
sort = sort + ` ${options.sort[i].field} ${options.sort[i].order}`;
|
|
86
|
+
if (i < options.sort.length - 1) {
|
|
87
|
+
sort = sort + " , ";
|
|
88
|
+
}
|
|
147
89
|
}
|
|
90
|
+
}
|
|
148
91
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
92
|
+
let limit = "";
|
|
93
|
+
let offset = "";
|
|
94
|
+
if (options?.limit) {
|
|
95
|
+
limit = `LIMIT ${options.limit}`;
|
|
96
|
+
}
|
|
97
|
+
if (options?.offset) {
|
|
98
|
+
offset = `OFFSET ${options.offset}`;
|
|
99
|
+
}
|
|
100
|
+
var query = `SELECT * FROM ${modelname} WHERE ${where} ${sort} ${limit} ${offset};`;
|
|
101
|
+
return await this.run(query);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async getOne(modelname, filter) {
|
|
105
|
+
this.metrics.getOne(modelname, filter);
|
|
106
|
+
var where = "";
|
|
107
|
+
for (var key in filter) {
|
|
108
|
+
where = where + `${key} = '${filter[key]}' AND `;
|
|
109
|
+
}
|
|
110
|
+
where = where + " 1 ";
|
|
111
|
+
var query = `SELECT * FROM ${modelname} WHERE ${where} LIMIT 1;`;
|
|
112
|
+
var row = await this.run(query);
|
|
113
|
+
return row[0];
|
|
114
|
+
}
|
|
115
|
+
async create(modelname, sampleObject) {
|
|
116
|
+
this.sync.create(modelname, sampleObject);
|
|
117
|
+
this.metrics.create(modelname, sampleObject);
|
|
118
|
+
|
|
119
|
+
var cols = "";
|
|
120
|
+
for (var key in sampleObject) {
|
|
121
|
+
var type;
|
|
122
|
+
if (this.dataMap[sampleObject[key]]) {
|
|
123
|
+
type = this.dataMap[sampleObject[key]];
|
|
124
|
+
} else {
|
|
125
|
+
type = this.dataMap[typeof sampleObject[key]] || "TEXT";
|
|
126
|
+
if (typeof sampleObject[key] == "string") {
|
|
127
|
+
if (sampleObject[key].length > 4000) {
|
|
128
|
+
type = this.dataMap["stringlarge"];
|
|
129
|
+
}
|
|
130
|
+
if (sampleObject[key].length <= 255) {
|
|
131
|
+
type = this.dataMap["stringsmall"];
|
|
132
|
+
}
|
|
182
133
|
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (key.toLowerCase().trim() === "id") {
|
|
137
|
+
cols = cols + `${key} ${type} PRIMARY KEY NOT NULL UNIQUE,`;
|
|
138
|
+
} else {
|
|
139
|
+
cols = cols + `${key} ${type},`;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
cols = cols.substring(0, cols.length - 1);
|
|
143
|
+
var query = `CREATE TABLE IF NOT EXISTS ${modelname} (${cols});`;
|
|
144
|
+
try {
|
|
145
|
+
return await this.run(query);
|
|
146
|
+
} catch (err) {
|
|
147
|
+
if (this.loglevel > 0) console.log(err);
|
|
148
|
+
throw err;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async insert(modelname, object) {
|
|
153
|
+
this.sync.insert(modelname, object);
|
|
154
|
+
this.metrics.insert(modelname, object);
|
|
155
|
+
var cols = "";
|
|
156
|
+
var vals = "";
|
|
157
|
+
for (var key in object) {
|
|
158
|
+
cols = cols + `${key},`;
|
|
159
|
+
let val = object[key];
|
|
160
|
+
if (typeof val == "object") val = JSON.stringify(object[key]);
|
|
161
|
+
val = this.pool.escape(val);
|
|
162
|
+
if (typeof val == "undefined") vals = vals + `Null,`;
|
|
163
|
+
else if (typeof val == "boolean") vals = vals + `${val},`;
|
|
164
|
+
else vals = vals + `${val},`;
|
|
183
165
|
}
|
|
166
|
+
cols = cols.substring(0, cols.length - 1);
|
|
167
|
+
vals = vals.substring(0, vals.length - 1);
|
|
184
168
|
|
|
185
|
-
|
|
186
|
-
this.sync.update(modelname, filter, object);
|
|
187
|
-
this.metrics.update(modelname, filter, object);
|
|
169
|
+
var query = `INSERT INTO ${modelname} (${cols}) VALUES(${vals});`;
|
|
188
170
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
val = this.pool.escape(val)
|
|
199
|
-
|
|
200
|
-
if ( val == "undefined" || val == undefined || val == 'null'|| val == null)
|
|
201
|
-
vals = vals + `${key} = Null,`;
|
|
202
|
-
else if (typeof val == 'boolean')
|
|
203
|
-
vals = vals + `${key} = ${val},`;
|
|
204
|
-
else
|
|
205
|
-
vals = vals + `${key} = ${val},`;
|
|
206
|
-
}
|
|
207
|
-
where = where + " 1 ";
|
|
208
|
-
vals = vals.substring(0, vals.length - 1);
|
|
209
|
-
|
|
210
|
-
var query = `UPDATE ${modelname} SET ${vals} WHERE ${where};`;
|
|
211
|
-
try{
|
|
212
|
-
return await this.run(query);
|
|
213
|
-
}catch(e){
|
|
214
|
-
if(this.loglevel > 4)
|
|
215
|
-
console.log('Error in update',e)
|
|
216
|
-
throw e;
|
|
217
|
-
}
|
|
171
|
+
try {
|
|
172
|
+
return await this.run(query);
|
|
173
|
+
} catch (err) {
|
|
174
|
+
if (err.code && err.code === "ER_NO_SUCH_TABLE") {
|
|
175
|
+
await this.create(modelname, object);
|
|
176
|
+
return await this.run(query);
|
|
177
|
+
} else {
|
|
178
|
+
throw err;
|
|
179
|
+
}
|
|
218
180
|
}
|
|
181
|
+
}
|
|
219
182
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
183
|
+
async update(modelname, filter, object) {
|
|
184
|
+
this.sync.update(modelname, filter, object);
|
|
185
|
+
this.metrics.update(modelname, filter, object);
|
|
223
186
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
187
|
+
var where = "";
|
|
188
|
+
var vals = "";
|
|
189
|
+
for (var key in filter) {
|
|
190
|
+
where = where + `${key} = '${filter[key]}' AND `;
|
|
191
|
+
}
|
|
192
|
+
for (var key in object) {
|
|
193
|
+
let val = object[key];
|
|
194
|
+
if (typeof val == "object" && val != undefined && val != null)
|
|
195
|
+
val = JSON.stringify(object[key]);
|
|
196
|
+
val = this.pool.escape(val);
|
|
197
|
+
|
|
198
|
+
if (
|
|
199
|
+
val == "undefined" ||
|
|
200
|
+
val == undefined ||
|
|
201
|
+
val == "null" ||
|
|
202
|
+
val == null
|
|
203
|
+
)
|
|
204
|
+
vals = vals + `${key} = Null,`;
|
|
205
|
+
else if (typeof val == "boolean") vals = vals + `${key} = ${val},`;
|
|
206
|
+
else vals = vals + `${key} = ${val},`;
|
|
231
207
|
}
|
|
208
|
+
where = where + " 1 ";
|
|
209
|
+
vals = vals.substring(0, vals.length - 1);
|
|
210
|
+
|
|
211
|
+
var query = `UPDATE ${modelname} SET ${vals} WHERE ${where};`;
|
|
212
|
+
try {
|
|
213
|
+
return await this.run(query);
|
|
214
|
+
} catch (e) {
|
|
215
|
+
if (this.loglevel > 4) console.log("Error in update", e);
|
|
216
|
+
throw e;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async delete(modelname, filter) {
|
|
221
|
+
this.sync.delete(modelname, filter);
|
|
222
|
+
this.metrics.delete(modelname, filter);
|
|
232
223
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
if (err) {
|
|
237
|
-
rej(err)
|
|
238
|
-
if (this.loglevel > 1)
|
|
239
|
-
console.error('Error closing connection pool:', err);
|
|
240
|
-
} else {
|
|
241
|
-
res()
|
|
242
|
-
if (this.loglevel > 1)
|
|
243
|
-
console.log('MySQLDB: Connection pool closed');
|
|
244
|
-
}
|
|
245
|
-
});
|
|
246
|
-
})
|
|
224
|
+
var where = "";
|
|
225
|
+
for (var key in filter) {
|
|
226
|
+
where = where + `${key} = '${filter[key]}' AND `;
|
|
247
227
|
}
|
|
228
|
+
where = where + " 1 ";
|
|
229
|
+
var query = `DELETE FROM ${modelname} WHERE ${where};`;
|
|
230
|
+
return await this.run(query);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
closePool() {
|
|
234
|
+
return new Promise((res, rej) => {
|
|
235
|
+
this.pool.end((err) => {
|
|
236
|
+
if (err) {
|
|
237
|
+
rej(err);
|
|
238
|
+
if (this.loglevel > 1)
|
|
239
|
+
console.error("Error closing connection pool:", err);
|
|
240
|
+
} else {
|
|
241
|
+
res();
|
|
242
|
+
if (this.loglevel > 1) console.log("MySQLDB: Connection pool closed");
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
}
|
|
248
247
|
}
|
|
249
248
|
|
|
250
249
|
module.exports = {
|
|
251
|
-
|
|
250
|
+
MySQLDB,
|
|
252
251
|
};
|