multi-db-orm 3.0.12 → 3.1.0

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,227 +1,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
- 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
- }
133
- }
134
- }
135
-
136
- async create(modelname, sampleObject) {
137
- this.sync.create(modelname, sampleObject);
138
- this.metrics.create(modelname, sampleObject);
139
-
140
- if (this.loglevel > 3)
141
- console.log("CREATE : Not required in DB Type", this.dbType);
142
- }
143
-
144
- async insert(modelname, object, id) {
145
- replaceUndefinedWithNull(object);
146
- this.sync.insert(modelname, object, id);
147
- this.metrics.insert(modelname, object, id);
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
- }
165
- }
166
- }
167
-
168
- async update(modelname, filter, object, id) {
169
- this.sync.update(modelname, filter, object, id);
170
-
171
- var idx = id || filter.id || object.id;
172
-
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
- });
221
- }
222
- }
223
- }
224
-
225
- module.exports = {
226
- FireStoreDB,
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
+ 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
+ }
133
+ }
134
+ }
135
+
136
+ async create(modelname, sampleObject) {
137
+ this.sync.create(modelname, sampleObject);
138
+ this.metrics.create(modelname, sampleObject);
139
+
140
+ if (this.loglevel > 3)
141
+ console.log("CREATE : Not required in DB Type", this.dbType);
142
+ }
143
+
144
+ async insert(modelname, object, id) {
145
+ replaceUndefinedWithNull(object);
146
+ this.sync.insert(modelname, object, id);
147
+ this.metrics.insert(modelname, object, id);
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
+ }
165
+ }
166
+ }
167
+
168
+ async update(modelname, filter, object, id) {
169
+ this.sync.update(modelname, filter, object, id);
170
+
171
+ var idx = id || filter.id || object.id;
172
+
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
+ });
221
+ }
222
+ }
223
+ }
224
+
225
+ module.exports = {
226
+ FireStoreDB,
227
+ };
@@ -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
+ }