multi-db-orm 2.1.6 → 2.1.7
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/.github/workflows/npm-publish.yml +24 -24
- package/.vscode/launch.json +29 -29
- package/.vscode/settings.json +10 -10
- package/Dockerfile +17 -17
- package/README.md +229 -229
- package/backup.js +107 -107
- package/databases.js +14 -14
- package/engines/firestoredb.d.ts +39 -39
- package/engines/firestoredb.js +230 -230
- package/engines/index.d.ts +5 -5
- package/engines/metrics.d.ts +8 -8
- package/engines/metrics.js +77 -77
- package/engines/mongodb.d.ts +18 -18
- package/engines/mongodb.js +148 -148
- package/engines/multidb.d.ts +29 -29
- package/engines/multidb.js +67 -67
- package/engines/mysqldb.d.ts +44 -42
- package/engines/mysqldb.js +227 -212
- package/engines/oracledb.d.ts +24 -24
- package/engines/oracledb.js +250 -250
- package/engines/sqlitedb.d.ts +11 -11
- package/engines/sqlitedb.js +172 -172
- package/index.d.ts +7 -7
- package/index.js +11 -11
- package/migrate.sh +11 -11
- package/package.json +49 -49
- package/postinstall.js +8 -8
- package/restore.js +102 -102
- package/sync.d.ts +5 -5
- package/sync.js +48 -48
- package/test/models.js +23 -23
- package/test/test.js +279 -279
package/engines/mysqldb.js
CHANGED
|
@@ -1,212 +1,227 @@
|
|
|
1
|
-
const mysql = require('mysql');
|
|
2
|
-
const { MultiDbORM } = require('./multidb');
|
|
3
|
-
|
|
4
|
-
class MySQLDB extends MultiDbORM {
|
|
5
|
-
|
|
6
|
-
mysql;
|
|
7
|
-
pool;
|
|
8
|
-
dataMap = {
|
|
9
|
-
"id": "VARCHAR(50) NOT NULL PRIMARY KEY",
|
|
10
|
-
"string": "VARCHAR(4000)",
|
|
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
|
-
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
var
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
if (
|
|
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
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
for (var key in
|
|
186
|
-
|
|
187
|
-
}
|
|
188
|
-
where = where + " 1 ";
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
1
|
+
const mysql = require('mysql');
|
|
2
|
+
const { MultiDbORM } = require('./multidb');
|
|
3
|
+
|
|
4
|
+
class MySQLDB extends MultiDbORM {
|
|
5
|
+
|
|
6
|
+
mysql;
|
|
7
|
+
pool;
|
|
8
|
+
dataMap = {
|
|
9
|
+
"id": "VARCHAR(50) NOT NULL PRIMARY KEY",
|
|
10
|
+
"string": "VARCHAR(4000)",
|
|
11
|
+
"stringlarge": "TEXT",
|
|
12
|
+
"stringsmall": "VARCHAR(255)",
|
|
13
|
+
"number": "DOUBLE",
|
|
14
|
+
"boolean": "BOOL",
|
|
15
|
+
"array": "TEXT",
|
|
16
|
+
"object": "JSON"
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
constructor(credentials) {
|
|
20
|
+
super();
|
|
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
|
+
|
|
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
|
+
}
|
|
92
|
+
}
|
|
93
|
+
var query = `SELECT * FROM ${modelname} WHERE ${where} ${sort} ;`;
|
|
94
|
+
return await this.run(query);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async getOne(modelname, filter) {
|
|
98
|
+
this.metrics.getOne(modelname, filter);
|
|
99
|
+
var where = '';
|
|
100
|
+
for (var key in filter) {
|
|
101
|
+
where = where + `${key} = '${filter[key]}' AND `;
|
|
102
|
+
}
|
|
103
|
+
where = where + " 1 ";
|
|
104
|
+
var query = `SELECT * FROM ${modelname} WHERE ${where} LIMIT 1;`;
|
|
105
|
+
var row = await this.run(query);
|
|
106
|
+
return row[0];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async create(modelname, sampleObject) {
|
|
110
|
+
this.sync.create(modelname, sampleObject);
|
|
111
|
+
this.metrics.create(modelname, sampleObject);
|
|
112
|
+
|
|
113
|
+
var cols = '';
|
|
114
|
+
for (var key in sampleObject) {
|
|
115
|
+
var type;
|
|
116
|
+
if (this.dataMap[sampleObject[key]]) {
|
|
117
|
+
type = this.dataMap[sampleObject[key]]
|
|
118
|
+
} else {
|
|
119
|
+
type = this.dataMap[typeof (sampleObject[key])] || 'TEXT';
|
|
120
|
+
if (typeof (sampleObject[key]) == 'string') {
|
|
121
|
+
if (sampleObject[key].length > 4000) {
|
|
122
|
+
type = this.dataMap['stringlarge']
|
|
123
|
+
}
|
|
124
|
+
if (sampleObject[key].length <= 255) {
|
|
125
|
+
type = this.dataMap['stringsmall']
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
cols = cols + `${key} ${type},`;
|
|
130
|
+
}
|
|
131
|
+
cols = cols.substring(0, cols.length - 1);
|
|
132
|
+
var query = `CREATE TABLE IF NOT EXISTS ${modelname} (${cols});`;
|
|
133
|
+
try {
|
|
134
|
+
return await this.run(query);
|
|
135
|
+
} catch (err) {
|
|
136
|
+
if (this.loglevel > 0)
|
|
137
|
+
console.log(err);
|
|
138
|
+
throw err
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async insert(modelname, object) {
|
|
143
|
+
this.sync.insert(modelname, object);
|
|
144
|
+
this.metrics.insert(modelname, object);
|
|
145
|
+
var cols = '';
|
|
146
|
+
var vals = '';
|
|
147
|
+
for (var key in object) {
|
|
148
|
+
cols = cols + `${key},`;
|
|
149
|
+
let val = object[key]
|
|
150
|
+
if (typeof val == 'object')
|
|
151
|
+
val = JSON.stringify(object[key])
|
|
152
|
+
if (typeof val == "undefined")
|
|
153
|
+
vals = vals + `Null,`;
|
|
154
|
+
else if (typeof val == 'boolean')
|
|
155
|
+
vals = vals + `${val},`;
|
|
156
|
+
else
|
|
157
|
+
vals = vals + `'${val}',`;
|
|
158
|
+
}
|
|
159
|
+
cols = cols.substring(0, cols.length - 1);
|
|
160
|
+
vals = vals.substring(0, vals.length - 1);
|
|
161
|
+
|
|
162
|
+
var query = `INSERT INTO ${modelname} (${cols}) VALUES(${vals});`;
|
|
163
|
+
|
|
164
|
+
try {
|
|
165
|
+
return await this.run(query);
|
|
166
|
+
} catch (err) {
|
|
167
|
+
if (err.code && err.code === 'ER_NO_SUCH_TABLE') {
|
|
168
|
+
await this.create(modelname, object);
|
|
169
|
+
return await this.run(query);
|
|
170
|
+
} else {
|
|
171
|
+
throw err;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async update(modelname, filter, object) {
|
|
177
|
+
this.sync.update(modelname, filter, object);
|
|
178
|
+
this.metrics.update(modelname, filter, object);
|
|
179
|
+
|
|
180
|
+
var where = '';
|
|
181
|
+
var vals = '';
|
|
182
|
+
for (var key in filter) {
|
|
183
|
+
where = where + `${key} = '${filter[key]}' AND `;
|
|
184
|
+
}
|
|
185
|
+
for (var key in object) {
|
|
186
|
+
vals = vals + ` ${key} = '${object[key]}',`;
|
|
187
|
+
}
|
|
188
|
+
where = where + " 1 ";
|
|
189
|
+
vals = vals.substring(0, vals.length - 1);
|
|
190
|
+
|
|
191
|
+
var query = `UPDATE ${modelname} SET ${vals} WHERE ${where};`;
|
|
192
|
+
return await this.run(query);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async delete(modelname, filter) {
|
|
196
|
+
this.sync.delete(modelname, filter);
|
|
197
|
+
this.metrics.delete(modelname, filter);
|
|
198
|
+
|
|
199
|
+
var where = '';
|
|
200
|
+
for (var key in filter) {
|
|
201
|
+
where = where + `${key} = '${filter[key]}' AND `;
|
|
202
|
+
}
|
|
203
|
+
where = where + " 1 ";
|
|
204
|
+
var query = `DELETE FROM ${modelname} WHERE ${where};`;
|
|
205
|
+
return await this.run(query);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
closePool() {
|
|
209
|
+
return new Promise((res, rej) => {
|
|
210
|
+
this.pool.end((err) => {
|
|
211
|
+
if (err) {
|
|
212
|
+
rej(err)
|
|
213
|
+
if (this.loglevel > 1)
|
|
214
|
+
console.error('Error closing connection pool:', err);
|
|
215
|
+
} else {
|
|
216
|
+
res()
|
|
217
|
+
if (this.loglevel > 1)
|
|
218
|
+
console.log('MySQLDB: Connection pool closed');
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
})
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
module.exports = {
|
|
226
|
+
MySQLDB
|
|
227
|
+
};
|
package/engines/oracledb.d.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { MultiDbORM, MultiDbORMOptions } from './MultiDbORM';
|
|
2
|
-
|
|
3
|
-
export interface OracleDBConfig {
|
|
4
|
-
username: string;
|
|
5
|
-
password: string;
|
|
6
|
-
net_service_name: string;
|
|
7
|
-
wallet_dir: string;
|
|
8
|
-
connection_pool_name?: string;
|
|
9
|
-
wallet_password?: string;
|
|
10
|
-
lib_dir?: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export declare class OracleDB extends MultiDbORM {
|
|
14
|
-
db: typeof import('oracledb')
|
|
15
|
-
connection_pool_name: string;
|
|
16
|
-
schema: string;
|
|
17
|
-
pool_creation: Promise<any>;
|
|
18
|
-
|
|
19
|
-
constructor(config: OracleDBConfig);
|
|
20
|
-
|
|
21
|
-
async connect(): Promise<void>;
|
|
22
|
-
|
|
23
|
-
async run(query: string): Promise<any>;
|
|
24
|
-
}
|
|
1
|
+
import { MultiDbORM, MultiDbORMOptions } from './MultiDbORM';
|
|
2
|
+
|
|
3
|
+
export interface OracleDBConfig {
|
|
4
|
+
username: string;
|
|
5
|
+
password: string;
|
|
6
|
+
net_service_name: string;
|
|
7
|
+
wallet_dir: string;
|
|
8
|
+
connection_pool_name?: string;
|
|
9
|
+
wallet_password?: string;
|
|
10
|
+
lib_dir?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export declare class OracleDB extends MultiDbORM {
|
|
14
|
+
db: typeof import('oracledb')
|
|
15
|
+
connection_pool_name: string;
|
|
16
|
+
schema: string;
|
|
17
|
+
pool_creation: Promise<any>;
|
|
18
|
+
|
|
19
|
+
constructor(config: OracleDBConfig);
|
|
20
|
+
|
|
21
|
+
async connect(): Promise<void>;
|
|
22
|
+
|
|
23
|
+
async run(query: string): Promise<any>;
|
|
24
|
+
}
|