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.
Files changed (2) hide show
  1. package/engines/mysqldb.js +228 -229
  2. package/package.json +1 -1
@@ -1,252 +1,251 @@
1
- const { MultiDbORM } = require('./multidb');
1
+ const { MultiDbORM } = require("./multidb");
2
2
 
3
3
  class MySQLDB extends MultiDbORM {
4
-
5
- mysql;
6
- pool;
7
- dataMap = {
8
- "id": "VARCHAR(50) NOT NULL PRIMARY KEY",
9
- "string": "VARCHAR(4000)",
10
- "stringlarge": "TEXT",
11
- "stringsmall": "VARCHAR(255)",
12
- "number": "DOUBLE",
13
- "boolean": "BOOL",
14
- "array": "TEXT",
15
- "object": "JSON"
16
- };
17
-
18
- constructor(credentials) {
19
- super();
20
- const mysql = require('mysql');
21
- this.mysql = mysql;
22
- this.pool = mysql.createPool({
23
- connectionLimit: credentials.connectionLimit || 10,
24
- host: credentials.host,
25
- port: credentials.port,
26
- user: credentials.username,
27
- password: credentials.password,
28
- database: credentials.database,
29
- waitForConnections: true,
30
- queueLimit: 0,
31
- connectTimeout: credentials.connectTimeout || 10000,
32
- acquireTimeout: credentials.acquireTimeout || 10000,
33
- timeout: credentials.timeout || 60000
34
- });
35
- this.db = this.pool
36
- this.pool.on('connection', (connection) => {
37
- if (this.loglevel > 1)
38
- console.log('MySQLDB: New connection acquired');
39
- });
40
-
41
- this.pool.on('error', (err) => {
42
- if (this.loglevel > 0)
43
- console.error('MySQLDB: Pool error:', err);
44
- });
45
-
46
- this.dbType = 'mysql';
47
- this.reqMade = 0;
48
- }
49
-
50
- async run(query) {
51
- var that = this;
52
- this.reqMade++;
53
- return new Promise(function (resolve, reject) {
54
- that.pool.query(query, function (err, results) {
55
- if (err) {
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
- let limit = ''
94
- let offset = ''
95
- if(options?.limit){
96
- limit = `LIMIT ${options.limit}`
57
+ if (that.loglevel > 3) {
58
+ console.log("Query ", query, " -> ", results);
97
59
  }
98
- if(options?.offset){
99
- offset = `OFFSET ${options.offset}`
100
- }
101
- var query = `SELECT * FROM ${modelname} WHERE ${where} ${sort} ${limit} ${offset};`;
102
- return await this.run(query);
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
- async getOne(modelname, filter) {
106
- this.metrics.getOne(modelname, filter);
107
- var where = '';
108
- for (var key in filter) {
109
- where = where + `${key} = '${filter[key]}' AND `;
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
- where = where + " 1 ";
112
- var query = `SELECT * FROM ${modelname} WHERE ${where} LIMIT 1;`;
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
- cols = cols.substring(0, cols.length - 1);
140
- var query = `CREATE TABLE IF NOT EXISTS ${modelname} (${cols});`;
141
- try {
142
- return await this.run(query);
143
- } catch (err) {
144
- if (this.loglevel > 0)
145
- console.log(err);
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
- async insert(modelname, object) {
151
- this.sync.insert(modelname, object);
152
- this.metrics.insert(modelname, object);
153
- var cols = '';
154
- var vals = '';
155
- for (var key in object) {
156
- cols = cols + `${key},`;
157
- let val = object[key]
158
- if (typeof val == 'object')
159
- val = JSON.stringify(object[key])
160
- val = this.pool.escape(val)
161
- if (typeof val == "undefined")
162
- vals = vals + `Null,`;
163
- else if (typeof val == 'boolean')
164
- vals = vals + `${val},`;
165
- else
166
- vals = vals + `${val},`;
167
- }
168
- cols = cols.substring(0, cols.length - 1);
169
- vals = vals.substring(0, vals.length - 1);
170
-
171
- var query = `INSERT INTO ${modelname} (${cols}) VALUES(${vals});`;
172
-
173
- try {
174
- return await this.run(query);
175
- } catch (err) {
176
- if (err.code && err.code === 'ER_NO_SUCH_TABLE') {
177
- await this.create(modelname, object);
178
- return await this.run(query);
179
- } else {
180
- throw err;
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
- async update(modelname, filter, object) {
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
- var where = '';
190
- var vals = '';
191
- for (var key in filter) {
192
- where = where + `${key} = '${filter[key]}' AND `;
193
- }
194
- for (var key in object) {
195
- let val = object[key]
196
- if (typeof val == 'object' && val != undefined && val != null)
197
- val = JSON.stringify(object[key])
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
- async delete(modelname, filter) {
221
- this.sync.delete(modelname, filter);
222
- this.metrics.delete(modelname, filter);
183
+ async update(modelname, filter, object) {
184
+ this.sync.update(modelname, filter, object);
185
+ this.metrics.update(modelname, filter, object);
223
186
 
224
- var where = '';
225
- for (var key in filter) {
226
- where = where + `${key} = '${filter[key]}' AND `;
227
- }
228
- where = where + " 1 ";
229
- var query = `DELETE FROM ${modelname} WHERE ${where};`;
230
- return await this.run(query);
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
- 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)
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
- MySQLDB
250
+ MySQLDB,
252
251
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "multi-db-orm",
3
- "version": "2.1.25",
3
+ "version": "2.1.26",
4
4
  "description": "CRUD , Backup , Restore and Migration library for multiple databases",
5
5
  "main": "index.js",
6
6
  "dependencies": {