multi-db-orm 2.1.4 → 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.
@@ -1,230 +1,230 @@
1
- const { MultiDbORM } = require("./multidb");
2
-
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
- }
14
- }
15
- } catch (e) {
16
- console.log('Error in sanitizing object before insert/update ! ', e.message)
17
- }
18
- }
19
-
20
- 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'
46
- }
47
-
48
- async run(query) {
49
- if (this.loglevel > 3)
50
- console.log('RUN : Not Supported in DB Type', this.dbType)
51
- }
52
-
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;
77
- }
78
-
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
- }
93
-
94
- return snapshot;
95
- }
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
-
113
- }
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
- }
137
- }
138
-
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)
145
- }
146
-
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
- }
166
-
167
- }
168
-
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
- }
201
-
202
- }
203
-
204
- async delete(modelname, filter, id) {
205
- this.sync.delete(modelname, filter, id)
206
-
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
-
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
- }
223
- }
224
- }
225
-
226
-
227
-
228
- module.exports = {
229
- FireStoreDB
230
- }
1
+ const { MultiDbORM } = require("./multidb");
2
+
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
+ }
14
+ }
15
+ } catch (e) {
16
+ console.log('Error in sanitizing object before insert/update ! ', e.message)
17
+ }
18
+ }
19
+
20
+ 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'
46
+ }
47
+
48
+ async run(query) {
49
+ if (this.loglevel > 3)
50
+ console.log('RUN : Not Supported in DB Type', this.dbType)
51
+ }
52
+
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;
77
+ }
78
+
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
+ }
93
+
94
+ return snapshot;
95
+ }
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
+
113
+ }
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
+ }
137
+ }
138
+
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)
145
+ }
146
+
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
+ }
166
+
167
+ }
168
+
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
+ }
201
+
202
+ }
203
+
204
+ async delete(modelname, filter, id) {
205
+ this.sync.delete(modelname, filter, id)
206
+
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
+
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
+ }
223
+ }
224
+ }
225
+
226
+
227
+
228
+ module.exports = {
229
+ FireStoreDB
230
+ }
@@ -1,5 +1,5 @@
1
- export { MultiDbORM } from './engines/multidb';
2
- export { SQLiteDB } from './engines/sqlitedb';
3
- export { MongoDB } from './engines/mongodb';
4
- export { FireStoreDB } from './engines/firestoredb';
5
- export { OracleDB } from './engines/oracledb';
1
+ export { MultiDbORM } from './engines/multidb';
2
+ export { SQLiteDB } from './engines/sqlitedb';
3
+ export { MongoDB } from './engines/mongodb';
4
+ export { FireStoreDB } from './engines/firestoredb';
5
+ export { OracleDB } from './engines/oracledb';
@@ -1,9 +1,9 @@
1
- export class Metrics {
2
- constructor(loglevel: number);
3
- get(modelname: string, filter: any, options: any): void;
4
- getOne(modelname: string, filter: any): void;
5
- create(modelname: string, sampleObject: any): void;
6
- insert(modelname: string, object: any): void;
7
- update(modelname: string, filter: any, object: any): void;
8
- delete(modelname: string, filter: any): void;
1
+ export class Metrics {
2
+ constructor(loglevel: number);
3
+ get(modelname: string, filter: any, options: any): void;
4
+ getOne(modelname: string, filter: any): void;
5
+ create(modelname: string, sampleObject: any): void;
6
+ insert(modelname: string, object: any): void;
7
+ update(modelname: string, filter: any, object: any): void;
8
+ delete(modelname: string, filter: any): void;
9
9
  }
@@ -1,78 +1,78 @@
1
- class Metrics {
2
-
3
- loglevel = 1
4
- dbstats;
5
-
6
- constructor(loglevel) {
7
- this.loglevel = loglevel;
8
- this.dbstats = {
9
- reads: 0,
10
- batchReads: 0,
11
- inserts: 0,
12
- updates: 0,
13
- deletes: 0
14
- }
15
- }
16
-
17
- printStatus() {
18
- console.log(dbstats)
19
- }
20
-
21
- reset() {
22
- this.dbstats = {
23
- reads: 0,
24
- batchReads: 0,
25
- inserts: 0,
26
- updates: 0,
27
- deletes: 0
28
- }
29
- }
30
-
31
- getStatus() {
32
- this.dbstats.readsTotal = this.dbstats.reads + this.dbstats.batchReads;
33
- return this.dbstats;
34
- }
35
-
36
- async get(modelname, filter) {
37
- this.dbstats.batchReads++;
38
- if (this.loglevel > 4)
39
- this.printStatus()
40
- }
41
-
42
- async getOne(modelname, filter) {
43
- this.dbstats.reads++;
44
- if (this.loglevel > 4)
45
- this.printStatus()
46
- }
47
-
48
- async create(modelname, object) {
49
- this.dbstats.inserts++;
50
- if (this.loglevel > 4)
51
- this.printStatus()
52
- }
53
-
54
- async insert(modelname, object) {
55
- this.dbstats.inserts++;
56
- if (this.loglevel > 4)
57
- printStatus()
58
- }
59
-
60
- async update(modelname, filter, object) {
61
- this.dbstats.updates++;
62
- if (this.loglevel > 4)
63
- this.printStatus()
64
- }
65
-
66
- async delete(modelname, filter) {
67
- this.dbstats.deletes++;
68
- if (this.loglevel > 4)
69
- this.printStatus()
70
- }
71
-
72
- }
73
-
74
-
75
-
76
- module.exports = {
77
- Metrics
1
+ class Metrics {
2
+
3
+ loglevel = 1
4
+ dbstats;
5
+
6
+ constructor(loglevel) {
7
+ this.loglevel = loglevel;
8
+ this.dbstats = {
9
+ reads: 0,
10
+ batchReads: 0,
11
+ inserts: 0,
12
+ updates: 0,
13
+ deletes: 0
14
+ }
15
+ }
16
+
17
+ printStatus() {
18
+ console.log(dbstats)
19
+ }
20
+
21
+ reset() {
22
+ this.dbstats = {
23
+ reads: 0,
24
+ batchReads: 0,
25
+ inserts: 0,
26
+ updates: 0,
27
+ deletes: 0
28
+ }
29
+ }
30
+
31
+ getStatus() {
32
+ this.dbstats.readsTotal = this.dbstats.reads + this.dbstats.batchReads;
33
+ return this.dbstats;
34
+ }
35
+
36
+ async get(modelname, filter) {
37
+ this.dbstats.batchReads++;
38
+ if (this.loglevel > 4)
39
+ this.printStatus()
40
+ }
41
+
42
+ async getOne(modelname, filter) {
43
+ this.dbstats.reads++;
44
+ if (this.loglevel > 4)
45
+ this.printStatus()
46
+ }
47
+
48
+ async create(modelname, object) {
49
+ this.dbstats.inserts++;
50
+ if (this.loglevel > 4)
51
+ this.printStatus()
52
+ }
53
+
54
+ async insert(modelname, object) {
55
+ this.dbstats.inserts++;
56
+ if (this.loglevel > 4)
57
+ printStatus()
58
+ }
59
+
60
+ async update(modelname, filter, object) {
61
+ this.dbstats.updates++;
62
+ if (this.loglevel > 4)
63
+ this.printStatus()
64
+ }
65
+
66
+ async delete(modelname, filter) {
67
+ this.dbstats.deletes++;
68
+ if (this.loglevel > 4)
69
+ this.printStatus()
70
+ }
71
+
72
+ }
73
+
74
+
75
+
76
+ module.exports = {
77
+ Metrics
78
78
  }
@@ -1,18 +1,18 @@
1
- import MongoClient from 'mongodb/lib/mongo_client';
2
- import { MultiDbORM, MultiDbORMOptions } from './MultiDbORM';
3
-
4
- export declare class MongoDB extends MultiDbORM {
5
- mongodbname: string;
6
- dbc: typeof import('mongodb');
7
- db: typeof import('mongodb');
8
- client: MongoClient;
9
- url: string;
10
-
11
- constructor(secureUrl: string, mongodbname: string);
12
-
13
- async _close(): Promise<void>;
14
-
15
- async _connect(): Promise<void>;
16
-
17
- async run(query: string): Promise<any>;
18
- }
1
+ import MongoClient from 'mongodb/lib/mongo_client';
2
+ import { MultiDbORM, MultiDbORMOptions } from './MultiDbORM';
3
+
4
+ export declare class MongoDB extends MultiDbORM {
5
+ mongodbname: string;
6
+ dbc: typeof import('mongodb');
7
+ db: typeof import('mongodb');
8
+ client: MongoClient;
9
+ url: string;
10
+
11
+ constructor(secureUrl: string, mongodbname: string);
12
+
13
+ async _close(): Promise<void>;
14
+
15
+ async _connect(): Promise<void>;
16
+
17
+ async run(query: string): Promise<any>;
18
+ }