multi-db-orm 3.0.12 → 3.1.1
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/.vscode/launch.json +29 -29
- package/.vscode/settings.json +10 -10
- package/Dockerfile +17 -17
- package/backup.js +107 -107
- package/databases.js +19 -19
- package/engines/bigquerydb.d.ts +36 -36
- package/engines/bigquerydb.js +236 -236
- package/engines/firestoredb.d.ts +39 -39
- package/engines/firestoredb.js +242 -227
- package/engines/hanadb.d.ts +24 -24
- package/engines/hanadb.js +259 -244
- package/engines/index.d.ts +8 -8
- package/engines/metrics.d.ts +8 -8
- package/engines/metrics.js +157 -77
- package/engines/mongodb.d.ts +18 -18
- package/engines/mongodb.js +155 -148
- package/engines/multidb.d.ts +41 -41
- package/engines/multidb.js +72 -67
- package/engines/mysqldb.d.ts +25 -25
- package/engines/mysqldb.js +25 -12
- package/engines/oracledb.d.ts +24 -24
- package/engines/oracledb.js +266 -250
- package/engines/sqlitedb.d.ts +11 -11
- package/engines/sqlitedb.js +179 -166
- package/index.js +23 -23
- package/migrate.sh +11 -11
- package/package.json +3 -3
- 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 +431 -434
package/engines/firestoredb.js
CHANGED
|
@@ -1,227 +1,242 @@
|
|
|
1
|
-
const { MultiDbORM } = require("./multidb");
|
|
2
|
-
|
|
3
|
-
function replaceUndefinedWithNull(obj) {
|
|
4
|
-
try {
|
|
5
|
-
for (let key in obj) {
|
|
6
|
-
if (obj.hasOwnProperty(key)) {
|
|
7
|
-
if (typeof obj[key] === "object" && obj[key] !== null) {
|
|
8
|
-
replaceUndefinedWithNull(obj[key]);
|
|
9
|
-
} else if (typeof obj[key] === "undefined") {
|
|
10
|
-
obj[key] = null;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
} catch (e) {
|
|
15
|
-
console.log(
|
|
16
|
-
"Error in sanitizing object before insert/update ! ",
|
|
17
|
-
e.message
|
|
18
|
-
);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
class FireStoreDB extends MultiDbORM {
|
|
23
|
-
admin;
|
|
24
|
-
serviceAccount;
|
|
25
|
-
constructor(serviceAccount, appname) {
|
|
26
|
-
super();
|
|
27
|
-
|
|
28
|
-
var admin = require("firebase-admin");
|
|
29
|
-
this.serviceAccount = serviceAccount;
|
|
30
|
-
|
|
31
|
-
if (appname) {
|
|
32
|
-
admin = admin.initializeApp(
|
|
33
|
-
{
|
|
34
|
-
credential: admin.credential.cert(this.serviceAccount),
|
|
35
|
-
databaseURL: `https://${this.serviceAccount.project_id}.firebaseio.com`,
|
|
36
|
-
},
|
|
37
|
-
appname
|
|
38
|
-
);
|
|
39
|
-
} else
|
|
40
|
-
admin = admin.initializeApp({
|
|
41
|
-
credential: admin.credential.cert(this.serviceAccount),
|
|
42
|
-
databaseURL: `https://${this.serviceAccount.project_id}.firebaseio.com`,
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
const db = admin.firestore();
|
|
46
|
-
this.db = db;
|
|
47
|
-
this.admin = admin;
|
|
48
|
-
console.log("Firestore Initialized");
|
|
49
|
-
this.dbType = "firestore";
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async run(query) {
|
|
53
|
-
if (this.loglevel > 3)
|
|
54
|
-
console.log("RUN : Not Supported in DB Type", this.dbType);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
attachOptions(modelref, options) {
|
|
58
|
-
if (options.apply) {
|
|
59
|
-
if (options.apply.ineq) {
|
|
60
|
-
modelref = modelref.where(
|
|
61
|
-
options.apply.field,
|
|
62
|
-
options.apply.ineq.op,
|
|
63
|
-
options.apply.ineq.value
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
if (options.apply.sort) {
|
|
67
|
-
modelref = modelref.orderBy(options.apply.field, options.apply.sort);
|
|
68
|
-
}
|
|
69
|
-
} else if (options.sort) {
|
|
70
|
-
options.sort.forEach((srt) => {
|
|
71
|
-
modelref = modelref.orderBy(srt.field, srt.order);
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
if (options.limit) {
|
|
75
|
-
modelref = modelref.limit(options.limit);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (options.offset) {
|
|
79
|
-
modelref = modelref.offset(options.offset);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return modelref;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
async _get(modelname, filter, options) {
|
|
86
|
-
const modelref = this.getdb().collection(modelname);
|
|
87
|
-
var where = modelref;
|
|
88
|
-
for (var key in filter) {
|
|
89
|
-
where = where.where(key, "==", filter[key]);
|
|
90
|
-
}
|
|
91
|
-
if (options) {
|
|
92
|
-
where = this.attachOptions(where, options);
|
|
93
|
-
}
|
|
94
|
-
const snapshot = await where.get();
|
|
95
|
-
if (snapshot.empty) {
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return snapshot;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
async get(modelname, filter, options) {
|
|
103
|
-
var result = [];
|
|
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
|
-
this.
|
|
148
|
-
|
|
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
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
)
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
id
|
|
198
|
-
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
}
|
|
1
|
+
const { MultiDbORM } = require("./multidb");
|
|
2
|
+
|
|
3
|
+
function replaceUndefinedWithNull(obj) {
|
|
4
|
+
try {
|
|
5
|
+
for (let key in obj) {
|
|
6
|
+
if (obj.hasOwnProperty(key)) {
|
|
7
|
+
if (typeof obj[key] === "object" && obj[key] !== null) {
|
|
8
|
+
replaceUndefinedWithNull(obj[key]);
|
|
9
|
+
} else if (typeof obj[key] === "undefined") {
|
|
10
|
+
obj[key] = null;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
} catch (e) {
|
|
15
|
+
console.log(
|
|
16
|
+
"Error in sanitizing object before insert/update ! ",
|
|
17
|
+
e.message
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
class FireStoreDB extends MultiDbORM {
|
|
23
|
+
admin;
|
|
24
|
+
serviceAccount;
|
|
25
|
+
constructor(serviceAccount, appname) {
|
|
26
|
+
super();
|
|
27
|
+
|
|
28
|
+
var admin = require("firebase-admin");
|
|
29
|
+
this.serviceAccount = serviceAccount;
|
|
30
|
+
|
|
31
|
+
if (appname) {
|
|
32
|
+
admin = admin.initializeApp(
|
|
33
|
+
{
|
|
34
|
+
credential: admin.credential.cert(this.serviceAccount),
|
|
35
|
+
databaseURL: `https://${this.serviceAccount.project_id}.firebaseio.com`,
|
|
36
|
+
},
|
|
37
|
+
appname
|
|
38
|
+
);
|
|
39
|
+
} else
|
|
40
|
+
admin = admin.initializeApp({
|
|
41
|
+
credential: admin.credential.cert(this.serviceAccount),
|
|
42
|
+
databaseURL: `https://${this.serviceAccount.project_id}.firebaseio.com`,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const db = admin.firestore();
|
|
46
|
+
this.db = db;
|
|
47
|
+
this.admin = admin;
|
|
48
|
+
console.log("Firestore Initialized");
|
|
49
|
+
this.dbType = "firestore";
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async run(query) {
|
|
53
|
+
if (this.loglevel > 3)
|
|
54
|
+
console.log("RUN : Not Supported in DB Type", this.dbType);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
attachOptions(modelref, options) {
|
|
58
|
+
if (options.apply) {
|
|
59
|
+
if (options.apply.ineq) {
|
|
60
|
+
modelref = modelref.where(
|
|
61
|
+
options.apply.field,
|
|
62
|
+
options.apply.ineq.op,
|
|
63
|
+
options.apply.ineq.value
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
if (options.apply.sort) {
|
|
67
|
+
modelref = modelref.orderBy(options.apply.field, options.apply.sort);
|
|
68
|
+
}
|
|
69
|
+
} else if (options.sort) {
|
|
70
|
+
options.sort.forEach((srt) => {
|
|
71
|
+
modelref = modelref.orderBy(srt.field, srt.order);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
if (options.limit) {
|
|
75
|
+
modelref = modelref.limit(options.limit);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (options.offset) {
|
|
79
|
+
modelref = modelref.offset(options.offset);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return modelref;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async _get(modelname, filter, options) {
|
|
86
|
+
const modelref = this.getdb().collection(modelname);
|
|
87
|
+
var where = modelref;
|
|
88
|
+
for (var key in filter) {
|
|
89
|
+
where = where.where(key, "==", filter[key]);
|
|
90
|
+
}
|
|
91
|
+
if (options) {
|
|
92
|
+
where = this.attachOptions(where, options);
|
|
93
|
+
}
|
|
94
|
+
const snapshot = await where.get();
|
|
95
|
+
if (snapshot.empty) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return snapshot;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async get(modelname, filter, options) {
|
|
103
|
+
var result = [];
|
|
104
|
+
const span = this.metrics.getSpan();
|
|
105
|
+
var snapshot = await this._get(modelname, filter, options);
|
|
106
|
+
if (snapshot == undefined) {
|
|
107
|
+
this.metrics.get(modelname, filter, options, span);
|
|
108
|
+
return [];
|
|
109
|
+
}
|
|
110
|
+
this.metrics.get(modelname, filter, options, span);
|
|
111
|
+
let that = this;
|
|
112
|
+
snapshot.forEach((doc) => {
|
|
113
|
+
const docSpan = this.metrics.getOneSpan();
|
|
114
|
+
result.push(doc.data());
|
|
115
|
+
that.metrics.getOne(modelname, filter, options, docSpan);
|
|
116
|
+
});
|
|
117
|
+
if (this.loglevel > 2) console.log("Retrieved ", result);
|
|
118
|
+
|
|
119
|
+
return result;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async getOne(modelname, filter, id, options) {
|
|
123
|
+
var idx = id || filter.id;
|
|
124
|
+
if (idx) {
|
|
125
|
+
const span = this.metrics.getOneSpan();
|
|
126
|
+
const modelref = this.getdb().collection(modelname).doc(idx);
|
|
127
|
+
const doc = await modelref.get();
|
|
128
|
+
this.metrics.getOne(modelname, filter, options, span);
|
|
129
|
+
if (this.loglevel > 2) console.log("Retrieved ", doc.data());
|
|
130
|
+
return doc.data();
|
|
131
|
+
} else {
|
|
132
|
+
var rows = await this.get(modelname, filter, options);
|
|
133
|
+
if (rows != undefined) {
|
|
134
|
+
if (this.loglevel > 2) console.log("Retrieved ", rows[0]);
|
|
135
|
+
return rows[0];
|
|
136
|
+
} else {
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async create(modelname, sampleObject) {
|
|
143
|
+
this.sync.create(modelname, sampleObject);
|
|
144
|
+
const span = this.metrics.createSpan();
|
|
145
|
+
this.metrics.create(modelname, sampleObject, span);
|
|
146
|
+
|
|
147
|
+
if (this.loglevel > 3)
|
|
148
|
+
console.log("CREATE : Not required in DB Type", this.dbType);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async insert(modelname, object, id) {
|
|
152
|
+
replaceUndefinedWithNull(object);
|
|
153
|
+
this.sync.insert(modelname, object, id);
|
|
154
|
+
const span = this.metrics.insertSpan();
|
|
155
|
+
|
|
156
|
+
var db = this.getdb();
|
|
157
|
+
var idx = id || object.id || Date.now();
|
|
158
|
+
const docref = db.collection(modelname).doc("" + idx);
|
|
159
|
+
try {
|
|
160
|
+
replaceUndefinedWithNull(object);
|
|
161
|
+
const res = await docref.set(object);
|
|
162
|
+
this.metrics.insert(modelname, object, id, span);
|
|
163
|
+
return res;
|
|
164
|
+
} catch (e) {
|
|
165
|
+
if (
|
|
166
|
+
e.message.indexOf(
|
|
167
|
+
"Firestore doesn't support JavaScript objects with custom prototypes"
|
|
168
|
+
) > -1
|
|
169
|
+
) {
|
|
170
|
+
const res = await docref.set(JSON.parse(JSON.stringify(object)));
|
|
171
|
+
this.metrics.insert(modelname, object, id, span);
|
|
172
|
+
return res;
|
|
173
|
+
} else {
|
|
174
|
+
throw e;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async update(modelname, filter, object, id) {
|
|
180
|
+
this.sync.update(modelname, filter, object, id);
|
|
181
|
+
|
|
182
|
+
var idx = id || filter.id || object.id;
|
|
183
|
+
|
|
184
|
+
try {
|
|
185
|
+
replaceUndefinedWithNull(object);
|
|
186
|
+
if (idx) {
|
|
187
|
+
const span = this.metrics.updateSpan();
|
|
188
|
+
await this.getdb().collection(modelname).doc(idx).update(object);
|
|
189
|
+
this.metrics.update(modelname, filter, object, id, span);
|
|
190
|
+
} else {
|
|
191
|
+
var snaps = (await this._get(modelname, filter)) || [];
|
|
192
|
+
const list = snaps.docs || snaps;
|
|
193
|
+
for (const element of list) {
|
|
194
|
+
const span = this.metrics.updateSpan();
|
|
195
|
+
await element.ref.update(object);
|
|
196
|
+
this.metrics.getOne(modelname, filter, id, span);
|
|
197
|
+
this.metrics.update(modelname, filter, object, id, span);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
} catch (e) {
|
|
201
|
+
if (
|
|
202
|
+
e.message.indexOf(
|
|
203
|
+
"Firestore doesn't support JavaScript objects with custom prototypes"
|
|
204
|
+
) > -1
|
|
205
|
+
) {
|
|
206
|
+
return await this.update(
|
|
207
|
+
modelname,
|
|
208
|
+
filter,
|
|
209
|
+
JSON.parse(JSON.stringify(object)),
|
|
210
|
+
id
|
|
211
|
+
);
|
|
212
|
+
} else {
|
|
213
|
+
throw e;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async delete(modelname, filter, id) {
|
|
219
|
+
this.sync.delete(modelname, filter, id);
|
|
220
|
+
|
|
221
|
+
var idx = id || filter.id;
|
|
222
|
+
|
|
223
|
+
if (idx) {
|
|
224
|
+
const span = this.metrics.deleteSpan();
|
|
225
|
+
await this.getdb().collection(modelname).doc(idx).delete();
|
|
226
|
+
this.metrics.delete(modelname, filter, id, span);
|
|
227
|
+
} else {
|
|
228
|
+
var snaps = (await this._get(modelname, filter)) || [];
|
|
229
|
+
const list = snaps.docs || snaps;
|
|
230
|
+
for (const element of list) {
|
|
231
|
+
const span = this.metrics.deleteSpan();
|
|
232
|
+
await element.ref.delete();
|
|
233
|
+
this.metrics.getOne(modelname, filter, id, span);
|
|
234
|
+
this.metrics.delete(modelname, filter, id, span);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
module.exports = {
|
|
241
|
+
FireStoreDB,
|
|
242
|
+
};
|
package/engines/hanadb.d.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { MultiDbORM } from './multidb';
|
|
2
|
-
|
|
3
|
-
export type HanaDBConfig = {
|
|
4
|
-
host: string;
|
|
5
|
-
port: string;
|
|
6
|
-
username: string;
|
|
7
|
-
password: string;
|
|
8
|
-
database?: string;
|
|
9
|
-
connectionLimit?: Number;
|
|
10
|
-
connectTimeout?: Number;
|
|
11
|
-
acquireTimeout?: Number;
|
|
12
|
-
timeout?: Number;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export class HanaDB extends MultiDbORM {
|
|
16
|
-
declare db: typeof import('@sap/hana-client')
|
|
17
|
-
connection: any;
|
|
18
|
-
|
|
19
|
-
constructor(credentials: HanaDBConfig);
|
|
20
|
-
|
|
21
|
-
run(query: string): Promise<any[]>;
|
|
22
|
-
|
|
23
|
-
closePool(): void;
|
|
24
|
-
}
|
|
1
|
+
import { MultiDbORM } from './multidb';
|
|
2
|
+
|
|
3
|
+
export type HanaDBConfig = {
|
|
4
|
+
host: string;
|
|
5
|
+
port: string;
|
|
6
|
+
username: string;
|
|
7
|
+
password: string;
|
|
8
|
+
database?: string;
|
|
9
|
+
connectionLimit?: Number;
|
|
10
|
+
connectTimeout?: Number;
|
|
11
|
+
acquireTimeout?: Number;
|
|
12
|
+
timeout?: Number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class HanaDB extends MultiDbORM {
|
|
16
|
+
declare db: typeof import('@sap/hana-client')
|
|
17
|
+
connection: any;
|
|
18
|
+
|
|
19
|
+
constructor(credentials: HanaDBConfig);
|
|
20
|
+
|
|
21
|
+
run(query: string): Promise<any[]>;
|
|
22
|
+
|
|
23
|
+
closePool(): void;
|
|
24
|
+
}
|