multi-db-orm 3.0.8 → 3.0.10
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/databases.js +2 -0
- package/engines/firestoredb.js +196 -199
- package/index.js +2 -0
- package/package.json +1 -1
package/databases.js
CHANGED
|
@@ -5,6 +5,7 @@ const { SQLiteDB } = require("./engines/sqlitedb");
|
|
|
5
5
|
const { OracleDB } = require("./engines/oracledb");
|
|
6
6
|
const { MySQLDB } = require("./engines/mysqldb");
|
|
7
7
|
const { HanaDB } = require("./engines/hanadb");
|
|
8
|
+
const { BigQueryDB } = require("./engines/bigquerydb");
|
|
8
9
|
|
|
9
10
|
module.exports = {
|
|
10
11
|
MultiDbORM,
|
|
@@ -14,4 +15,5 @@ module.exports = {
|
|
|
14
15
|
OracleDB,
|
|
15
16
|
MySQLDB,
|
|
16
17
|
HanaDB,
|
|
18
|
+
BigQueryDB,
|
|
17
19
|
};
|
package/engines/firestoredb.js
CHANGED
|
@@ -1,230 +1,227 @@
|
|
|
1
1
|
const { MultiDbORM } = require("./multidb");
|
|
2
2
|
|
|
3
3
|
function replaceUndefinedWithNull(obj) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
obj[key] = null;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
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;
|
|
14
11
|
}
|
|
15
|
-
|
|
16
|
-
console.log('Error in sanitizing object before insert/update ! ', e.message)
|
|
12
|
+
}
|
|
17
13
|
}
|
|
14
|
+
} catch (e) {
|
|
15
|
+
console.log(
|
|
16
|
+
"Error in sanitizing object before insert/update ! ",
|
|
17
|
+
e.message
|
|
18
|
+
);
|
|
19
|
+
}
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
class FireStoreDB extends MultiDbORM {
|
|
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
|
-
|
|
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
|
+
});
|
|
46
73
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
if (this.loglevel > 3)
|
|
50
|
-
console.log('RUN : Not Supported in DB Type', this.dbType)
|
|
74
|
+
if (options.limit) {
|
|
75
|
+
modelref = modelref.limit(options.limit);
|
|
51
76
|
}
|
|
52
77
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
if (options.apply) {
|
|
56
|
-
if (options.apply.ineq) {
|
|
57
|
-
modelref = modelref.where(options.apply.field, options.apply.ineq.op, options.apply.ineq.value)
|
|
58
|
-
}
|
|
59
|
-
if (options.apply.sort) {
|
|
60
|
-
modelref = modelref.orderBy(options.apply.field, options.apply.sort)
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
else if (options.sort) {
|
|
64
|
-
options.sort.forEach(srt => {
|
|
65
|
-
modelref = modelref.orderBy(srt.field, srt.order)
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
if (options.limit) {
|
|
69
|
-
modelref = modelref.limit(options.limit)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (options.offset) {
|
|
73
|
-
modelref = modelref.offset(options.offset)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return modelref;
|
|
78
|
+
if (options.offset) {
|
|
79
|
+
modelref = modelref.offset(options.offset);
|
|
77
80
|
}
|
|
78
81
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
const modelref = this.getdb().collection(modelname);
|
|
82
|
-
var where = modelref
|
|
83
|
-
for (var key in filter) {
|
|
84
|
-
where = where.where(key, '==', filter[key])
|
|
85
|
-
}
|
|
86
|
-
if (options) {
|
|
87
|
-
where = this.attachOptions(where, options);
|
|
88
|
-
}
|
|
89
|
-
const snapshot = await where.get();
|
|
90
|
-
if (snapshot.empty) {
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
82
|
+
return modelref;
|
|
83
|
+
}
|
|
93
84
|
|
|
94
|
-
|
|
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]);
|
|
95
90
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
var result = []
|
|
99
|
-
var snapshot = await this._get(modelname, filter, options)
|
|
100
|
-
if (snapshot == undefined)
|
|
101
|
-
return []
|
|
102
|
-
this.metrics.get(modelname, filter, options)
|
|
103
|
-
let that = this;
|
|
104
|
-
snapshot.forEach(doc => {
|
|
105
|
-
that.metrics.getOne(modelname, filter, options);
|
|
106
|
-
result.push(doc.data())
|
|
107
|
-
});
|
|
108
|
-
if (this.loglevel > 2)
|
|
109
|
-
console.log('Retrieved ', result)
|
|
110
|
-
|
|
111
|
-
return result;
|
|
112
|
-
|
|
91
|
+
if (options) {
|
|
92
|
+
where = this.attachOptions(where, options);
|
|
113
93
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (idx) {
|
|
118
|
-
this.metrics.getOne(modelname, filter, options);
|
|
119
|
-
const modelref = this.getdb().collection(modelname).doc(idx);
|
|
120
|
-
const doc = await modelref.get();
|
|
121
|
-
if (this.loglevel > 2)
|
|
122
|
-
console.log('Retrieved ', doc.data())
|
|
123
|
-
return doc.data();
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
else {
|
|
127
|
-
var rows = await this.get(modelname, filter, options)
|
|
128
|
-
if (rows != undefined) {
|
|
129
|
-
if (this.loglevel > 2)
|
|
130
|
-
console.log('Retrieved ', rows[0])
|
|
131
|
-
return rows[0]
|
|
132
|
-
}
|
|
133
|
-
else {
|
|
134
|
-
return undefined;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
94
|
+
const snapshot = await where.get();
|
|
95
|
+
if (snapshot.empty) {
|
|
96
|
+
return;
|
|
137
97
|
}
|
|
138
98
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
99
|
+
return snapshot;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async get(modelname, filter, options) {
|
|
103
|
+
var result = [];
|
|
104
|
+
var snapshot = await this._get(modelname, filter, options);
|
|
105
|
+
if (snapshot == undefined) return [];
|
|
106
|
+
this.metrics.get(modelname, filter, options);
|
|
107
|
+
let that = this;
|
|
108
|
+
snapshot.forEach((doc) => {
|
|
109
|
+
that.metrics.getOne(modelname, filter, options);
|
|
110
|
+
result.push(doc.data());
|
|
111
|
+
});
|
|
112
|
+
if (this.loglevel > 2) console.log("Retrieved ", result);
|
|
113
|
+
|
|
114
|
+
return result;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async getOne(modelname, filter, id, options) {
|
|
118
|
+
var idx = id || filter.id;
|
|
119
|
+
if (idx) {
|
|
120
|
+
this.metrics.getOne(modelname, filter, options);
|
|
121
|
+
const modelref = this.getdb().collection(modelname).doc(idx);
|
|
122
|
+
const doc = await modelref.get();
|
|
123
|
+
if (this.loglevel > 2) console.log("Retrieved ", doc.data());
|
|
124
|
+
return doc.data();
|
|
125
|
+
} else {
|
|
126
|
+
var rows = await this.get(modelname, filter, options);
|
|
127
|
+
if (rows != undefined) {
|
|
128
|
+
if (this.loglevel > 2) console.log("Retrieved ", rows[0]);
|
|
129
|
+
return rows[0];
|
|
130
|
+
} else {
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
145
133
|
}
|
|
134
|
+
}
|
|
146
135
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
this.metrics.insert(modelname, object, id)
|
|
151
|
-
|
|
152
|
-
var db = this.getdb();
|
|
153
|
-
var idx = id || object.id || Date.now()
|
|
154
|
-
const docref = db.collection(modelname).doc("" + idx);
|
|
155
|
-
try {
|
|
156
|
-
replaceUndefinedWithNull(object)
|
|
157
|
-
return await docref.set(object);
|
|
158
|
-
} catch (e) {
|
|
159
|
-
|
|
160
|
-
if (e.message.indexOf("Firestore doesn't support JavaScript objects with custom prototypes") > -1) {
|
|
161
|
-
return await docref.set(JSON.parse(JSON.stringify(object)));
|
|
162
|
-
}
|
|
163
|
-
else {
|
|
164
|
-
throw e;
|
|
165
|
-
}
|
|
136
|
+
async create(modelname, sampleObject) {
|
|
137
|
+
this.sync.create(modelname, sampleObject);
|
|
138
|
+
this.metrics.create(modelname, sampleObject);
|
|
166
139
|
|
|
167
|
-
|
|
140
|
+
if (this.loglevel > 3)
|
|
141
|
+
console.log("CREATE : Not required in DB Type", this.dbType);
|
|
142
|
+
}
|
|
168
143
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
var idx = id || filter.id || object.id
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
try {
|
|
178
|
-
replaceUndefinedWithNull(object)
|
|
179
|
-
if (idx) {
|
|
180
|
-
this.metrics.update(modelname, filter, object, id)
|
|
181
|
-
await this.getdb().collection(modelname).doc(idx).update(object);
|
|
182
|
-
|
|
183
|
-
} else {
|
|
184
|
-
var snaps = await this._get(modelname, filter)
|
|
185
|
-
let that = this;
|
|
186
|
-
snaps.forEach(async function (element) {
|
|
187
|
-
that.metrics.getOne(modelname, filter, id)
|
|
188
|
-
that.metrics.update(modelname, filter, object, id)
|
|
189
|
-
await element.ref.update(object)
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
} catch (e) {
|
|
193
|
-
|
|
194
|
-
if (e.message.indexOf("Firestore doesn't support JavaScript objects with custom prototypes") > -1) {
|
|
195
|
-
return await this.update(modelname, filter, JSON.parse(JSON.stringify(object)), id);
|
|
196
|
-
}
|
|
197
|
-
else {
|
|
198
|
-
throw e;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
144
|
+
async insert(modelname, object, id) {
|
|
145
|
+
replaceUndefinedWithNull(object);
|
|
146
|
+
this.sync.insert(modelname, object, id);
|
|
147
|
+
this.metrics.insert(modelname, object, id);
|
|
201
148
|
|
|
149
|
+
var db = this.getdb();
|
|
150
|
+
var idx = id || object.id || Date.now();
|
|
151
|
+
const docref = db.collection(modelname).doc("" + idx);
|
|
152
|
+
try {
|
|
153
|
+
replaceUndefinedWithNull(object);
|
|
154
|
+
return await docref.set(object);
|
|
155
|
+
} catch (e) {
|
|
156
|
+
if (
|
|
157
|
+
e.message.indexOf(
|
|
158
|
+
"Firestore doesn't support JavaScript objects with custom prototypes"
|
|
159
|
+
) > -1
|
|
160
|
+
) {
|
|
161
|
+
return await docref.set(JSON.parse(JSON.stringify(object)));
|
|
162
|
+
} else {
|
|
163
|
+
throw e;
|
|
164
|
+
}
|
|
202
165
|
}
|
|
166
|
+
}
|
|
203
167
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
168
|
+
async update(modelname, filter, object, id) {
|
|
169
|
+
this.sync.update(modelname, filter, object, id);
|
|
207
170
|
|
|
208
|
-
|
|
171
|
+
var idx = id || filter.id || object.id;
|
|
209
172
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
}
|
|
173
|
+
try {
|
|
174
|
+
replaceUndefinedWithNull(object);
|
|
175
|
+
if (idx) {
|
|
176
|
+
this.metrics.update(modelname, filter, object, id);
|
|
177
|
+
await this.getdb().collection(modelname).doc(idx).update(object);
|
|
178
|
+
} else {
|
|
179
|
+
var snaps = (await this._get(modelname, filter)) || [];
|
|
180
|
+
let that = this;
|
|
181
|
+
snaps.forEach(async function (element) {
|
|
182
|
+
that.metrics.getOne(modelname, filter, id);
|
|
183
|
+
that.metrics.update(modelname, filter, object, id);
|
|
184
|
+
await element.ref.update(object);
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
} catch (e) {
|
|
188
|
+
if (
|
|
189
|
+
e.message.indexOf(
|
|
190
|
+
"Firestore doesn't support JavaScript objects with custom prototypes"
|
|
191
|
+
) > -1
|
|
192
|
+
) {
|
|
193
|
+
return await this.update(
|
|
194
|
+
modelname,
|
|
195
|
+
filter,
|
|
196
|
+
JSON.parse(JSON.stringify(object)),
|
|
197
|
+
id
|
|
198
|
+
);
|
|
199
|
+
} else {
|
|
200
|
+
throw e;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async delete(modelname, filter, id) {
|
|
206
|
+
this.sync.delete(modelname, filter, id);
|
|
207
|
+
|
|
208
|
+
var idx = id || filter.id;
|
|
209
|
+
|
|
210
|
+
if (idx) {
|
|
211
|
+
this.metrics.delete(modelname, filter, id);
|
|
212
|
+
await this.getdb().collection(modelname).doc(idx).delete();
|
|
213
|
+
} else {
|
|
214
|
+
var snaps = (await this._get(modelname, filter)) || [];
|
|
215
|
+
let that = this;
|
|
216
|
+
snaps.forEach(async function (element) {
|
|
217
|
+
that.metrics.getOne(modelname, filter, id);
|
|
218
|
+
that.metrics.delete(modelname, filter, id);
|
|
219
|
+
await element.ref.delete();
|
|
220
|
+
});
|
|
223
221
|
}
|
|
222
|
+
}
|
|
224
223
|
}
|
|
225
224
|
|
|
226
|
-
|
|
227
|
-
|
|
228
225
|
module.exports = {
|
|
229
|
-
|
|
230
|
-
}
|
|
226
|
+
FireStoreDB,
|
|
227
|
+
};
|
package/index.js
CHANGED