multi-db-orm 3.0.9 → 3.0.11

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/README.md CHANGED
@@ -258,3 +258,17 @@ Working on enhancing the tool with below features in progress. Feel free to cont
258
258
  - [x] Range Operations like `>=` `<=`
259
259
  - [ ] Aggregations
260
260
  - [ ] InsertMany
261
+
262
+ ## Notes
263
+
264
+ ### Firebase
265
+
266
+ Please create the indexes as well:
267
+
268
+ https://console.firebase.google.com/u/0/project/<<PROJECT>>/firestore/databases/-default-/indexes?create_composite=ClBwcm9qZWN0cy9zZW1pYml0bWVkaWEvZGF0YWJhc2VzLyhkZWZhdWx0KS9jb2xsZWN0aW9uR3JvdXBzL3BzX3BpcGVsYW5lL2luZGV4ZXMvXxABGgoKBmFjdGl2ZRACGhQKEHVwZGF0ZWRUaW1lc3RhbXAQAhoMCghfX25hbWVfXxAC
269
+
270
+ https://console.firebase.google.com/u/0/project/<<PROJECT>>/firestore/databases/-default-/indexes?create_composite=ClVwcm9qZWN0cy9zZW1pYml0bWVkaWEvZGF0YWJhc2VzLyhkZWZhdWx0KS9jb2xsZWN0aW9uR3JvdXBzL3BzX3BpcGVsYW5lX3Rhc2svaW5kZXhlcy9fEAEaEAoMcGlwZWxhbmVOYW1lEAEaCAoEc3RlcBABGgwKCF9fbmFtZV9fEAE
271
+
272
+ https://console.firebase.google.com/u/0/project/<<PROJECT>>/firestore/databases/-default-/indexes?create_composite=ClVwcm9qZWN0cy9zZW1pYml0bWVkaWEvZGF0YWJhc2VzLyhkZWZhdWx0KS9jb2xsZWN0aW9uR3JvdXBzL3BzX3BpcGVsYW5lX2V4ZWMvaW5kZXhlcy9fEAEaCAoEbmFtZRABGg0KCXN0YXJ0VGltZRACGgwKCF9fbmFtZV9fEAI
273
+
274
+ https://console.firebase.google.com/u/0/project/<<PROJECT>>/firestore/databases/-default-/indexes?create_composite=Clpwcm9qZWN0cy9zZW1pYml0bWVkaWEvZGF0YWJhc2VzLyhkZWZhdWx0KS9jb2xsZWN0aW9uR3JvdXBzL3BzX3BpcGVsYW5lX3Rhc2tfZXhlYy9pbmRleGVzL18QARoQCgxwaXBlbGFuZUV4SWQQARoNCglzdGFydFRpbWUQARoMCghfX25hbWVfXxAB
@@ -1,230 +1,227 @@
1
1
  const { MultiDbORM } = require("./multidb");
2
2
 
3
3
  function replaceUndefinedWithNull(obj) {
4
- try {
5
-
6
- for (let key in obj) {
7
- if (obj.hasOwnProperty(key)) {
8
- if (typeof obj[key] === 'object' && obj[key] !== null) {
9
- replaceUndefinedWithNull(obj[key]);
10
- } else if (typeof obj[key] === 'undefined') {
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
- } catch (e) {
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
- admin
23
- serviceAccount
24
- constructor(serviceAccount, appname) {
25
- super()
26
-
27
- var admin = require('firebase-admin');
28
- this.serviceAccount = serviceAccount;
29
-
30
- if (appname) {
31
- admin = admin.initializeApp({
32
- credential: admin.credential.cert(this.serviceAccount),
33
- databaseURL: `https://${this.serviceAccount.project_id}.firebaseio.com`
34
- }, appname);
35
- } else
36
- admin = admin.initializeApp({
37
- credential: admin.credential.cert(this.serviceAccount),
38
- databaseURL: `https://${this.serviceAccount.project_id}.firebaseio.com`
39
- });
40
-
41
- const db = admin.firestore();
42
- this.db = db
43
- this.admin = admin
44
- console.log("Firestore Initialized");
45
- this.dbType = 'firestore'
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
- async run(query) {
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
- attachOptions(modelref, options) {
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
- async _get(modelname, filter, options) {
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
- return snapshot;
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
- async get(modelname, filter, options) {
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
- async getOne(modelname, filter, id, options) {
116
- var idx = id || filter.id
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
- async create(modelname, sampleObject) {
140
- this.sync.create(modelname, sampleObject)
141
- this.metrics.create(modelname, sampleObject);
142
-
143
- if (this.loglevel > 3)
144
- console.log('CREATE : Not required in DB Type', this.dbType)
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
- async insert(modelname, object, id) {
148
- replaceUndefinedWithNull(object)
149
- this.sync.insert(modelname, object, id)
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
- async update(modelname, filter, object, id) {
172
- this.sync.update(modelname, filter, object, id)
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
- async delete(modelname, filter, id) {
205
- this.sync.delete(modelname, filter, id)
206
-
168
+ async update(modelname, filter, object, id) {
169
+ this.sync.update(modelname, filter, object, id);
207
170
 
208
- var idx = id || filter.id
171
+ var idx = id || filter.id || object.id;
209
172
 
210
- if (idx) {
211
- this.metrics.delete(modelname, filter, id)
212
- await this.getdb().collection(modelname).doc(idx).delete();
213
-
214
- } else {
215
- var snaps = await this._get(modelname, filter)
216
- let that = this;
217
- snaps.forEach(async function (element) {
218
- that.metrics.getOne(modelname, filter, id)
219
- that.metrics.delete(modelname, filter, id)
220
- await element.ref.delete();
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
- FireStoreDB
230
- }
226
+ FireStoreDB,
227
+ };
@@ -19,6 +19,7 @@ class MySQLDB extends MultiDbORM {
19
19
  const mysql = require("mysql");
20
20
  this.mysql = mysql;
21
21
  this.pool = mysql.createPool({
22
+ ...credentials,
22
23
  connectionLimit: credentials.connectionLimit || 10,
23
24
  host: credentials.host,
24
25
  port: credentials.port,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "multi-db-orm",
3
- "version": "3.0.9",
3
+ "version": "3.0.11",
4
4
  "description": "CRUD , Backup , Restore and Migration library for multiple databases",
5
5
  "main": "index.js",
6
6
  "dependencies": {