multi-db-orm 1.0.7 → 1.2.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.
@@ -0,0 +1,21 @@
1
+ # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2
+ # For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
3
+
4
+ name: NPM Publish
5
+
6
+ on:
7
+ release:
8
+ types: [created]
9
+
10
+ jobs:
11
+ publish:
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - uses: actions/checkout@v1
15
+ - uses: actions/setup-node@v1
16
+ with:
17
+ node-version: 14
18
+ - run: npm install
19
+ - uses: JS-DevTools/npm-publish@v1
20
+ with:
21
+ token: ${{ secrets.NPM_TOKEN }}
package/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # multi-db-orm
2
2
  ORM for multiple SQL and NoSQL databases like firestore , MongoDB , SQlite with Sync , Backup and Restore support .
3
3
 
4
+ [![NPM Publish](https://github.com/shiveshnavin/multi-db-orm/actions/workflows/npm-publish.yml/badge.svg)](https://github.com/shiveshnavin/multi-db-orm/actions/workflows/npm-publish.yml)
5
+
4
6
  ## 1. Object Relational Mapping
5
7
 
6
8
  Supported databases:
@@ -189,6 +191,6 @@ Working on enhancing the tool with below features in progress. Feel free to cont
189
191
 
190
192
  - [ ] Add Backup support for other databases
191
193
  - [ ] Add Restore support for other databases
192
- - [ ] Range Operations like `>=` `<=`
194
+ - [X] Range Operations like `>=` `<=`
193
195
  - [ ] Aggregations
194
196
  - [ ] InsertMany
@@ -1,6 +1,20 @@
1
1
  const { model } = require("mongoose");
2
2
  const { MultiDbORM } = require("./multidb");
3
3
 
4
+ function removeUndefined(obj) {
5
+ try {
6
+
7
+ Object.keys(obj).forEach(function (key) {
8
+ if (typeof obj[key] === 'undefined') {
9
+ delete obj[key];
10
+ }
11
+ });
12
+ return obj
13
+ } catch (e) {
14
+ console.log('Error in sanitizing update of object ! ', e.message)
15
+ }
16
+ }
17
+
4
18
  class FireStoreDB extends MultiDbORM {
5
19
 
6
20
  admin
@@ -77,7 +91,10 @@ class FireStoreDB extends MultiDbORM {
77
91
  var snapshot = await this._get(modelname, filter, options)
78
92
  if (snapshot == undefined)
79
93
  return []
94
+ this.metrics.get(modelname, filter, options)
95
+ let that = this;
80
96
  snapshot.forEach(doc => {
97
+ that.metrics.getOne(modelname, filter, options);
81
98
  result.push(doc.data())
82
99
  });
83
100
  if (this.loglevel > 2)
@@ -90,7 +107,7 @@ class FireStoreDB extends MultiDbORM {
90
107
  async getOne(modelname, filter, id, options) {
91
108
  var idx = id || filter.id
92
109
  if (idx) {
93
-
110
+ this.metrics.getOne(modelname, filter, options);
94
111
  const modelref = this.getdb().collection(modelname).doc(idx);
95
112
  const doc = await modelref.get();
96
113
  if (this.loglevel > 2)
@@ -113,17 +130,21 @@ class FireStoreDB extends MultiDbORM {
113
130
 
114
131
  async create(modelname, sampleObject) {
115
132
  this.sync.create(modelname, sampleObject)
133
+ this.metrics.create(modelname, sampleObject);
134
+
116
135
  if (this.loglevel > 3)
117
136
  console.log('CREATE : Not required in DB Type', this.dbType)
118
137
  }
119
138
 
120
139
  async insert(modelname, object, id) {
121
140
  this.sync.insert(modelname, object, id)
141
+ this.metrics.insert(modelname, object, id)
122
142
 
123
143
  var db = this.getdb();
124
144
  var idx = id || object.id || Date.now()
125
145
  const docref = db.collection(modelname).doc("" + idx);
126
146
  try {
147
+ removeUndefined(object)
127
148
  return await docref.set(object);
128
149
  } catch (e) {
129
150
 
@@ -145,20 +166,24 @@ class FireStoreDB extends MultiDbORM {
145
166
 
146
167
 
147
168
  try {
169
+ removeUndefined(object)
148
170
  if (idx) {
149
-
171
+ this.metrics.update(modelname, filter, object, id)
150
172
  await this.getdb().collection(modelname).doc(idx).update(object);
151
173
 
152
174
  } else {
153
175
  var snaps = await this._get(modelname, filter)
176
+ let that = this;
154
177
  snaps.forEach(async function (element) {
178
+ that.metrics.getOne(modelname, filter, id)
179
+ that.metrics.update(modelname, filter, object, id)
155
180
  await element.ref.update(object)
156
181
  });
157
182
  }
158
183
  } catch (e) {
159
184
 
160
185
  if (e.message.indexOf("Firestore doesn't support JavaScript objects with custom prototypes") > -1) {
161
- return await update(modelname, filter, JSON.parse(JSON.stringify(object)), id);
186
+ return await this.update(modelname, filter, JSON.parse(JSON.stringify(object)), id);
162
187
  }
163
188
  else {
164
189
  throw e;
@@ -174,12 +199,15 @@ class FireStoreDB extends MultiDbORM {
174
199
  var idx = id || filter.id
175
200
 
176
201
  if (idx) {
177
-
202
+ this.metrics.delete(modelname, filter, id)
178
203
  await this.getdb().collection(modelname).doc(idx).delete();
179
204
 
180
205
  } else {
181
206
  var snaps = await this._get(modelname, filter)
207
+ let that = this;
182
208
  snaps.forEach(async function (element) {
209
+ that.metrics.getOne(modelname, filter, id)
210
+ that.metrics.delete(modelname, filter, id)
183
211
  await element.ref.delete();
184
212
  });
185
213
  }
@@ -0,0 +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
78
+ }
@@ -53,6 +53,7 @@ class MongoDB extends MultiDbORM {
53
53
  }
54
54
  }
55
55
  async get(modelname, filter, options) {
56
+ this.metrics.get(modelname, filter, options);
56
57
  if (options && options.apply && options.apply.ineq) {
57
58
  filter[`${options.apply.field}`] = {};
58
59
  filter[`${options.apply.field}`][`${this._inq2mongop(options.apply.ineq.op)}`] = options.apply.ineq.value
@@ -98,12 +99,14 @@ class MongoDB extends MultiDbORM {
98
99
  }
99
100
 
100
101
  async getOne(modelname, filter, options) {
102
+ this.metrics.getOne(modelname, filter, options);
101
103
  var snapshot = await this.getdb().collection(modelname).findOne(filter)
102
104
  return snapshot;
103
105
  }
104
106
 
105
107
  async create(modelname, sampleObject) {
106
108
  this.sync.create(modelname, sampleObject)
109
+ this.metrics.create(modelname, sampleObject);
107
110
 
108
111
  if (this.loglevel > 3)
109
112
  console.log('CREATE : Not required in DB Type', this.dbType)
@@ -111,6 +114,7 @@ class MongoDB extends MultiDbORM {
111
114
 
112
115
  async insert(modelname, object) {
113
116
  this.sync.insert(modelname, object)
117
+ this.metrics.insert(modelname, object)
114
118
 
115
119
  const collref = this.getdb().collection(modelname)
116
120
  try {
@@ -125,12 +129,14 @@ class MongoDB extends MultiDbORM {
125
129
 
126
130
  async update(modelname, filter, object) {
127
131
  this.sync.update(modelname, filter, object)
132
+ this.metrics.update(modelname, filter, object)
128
133
  var resp = await this.getdb().collection(modelname).updateMany(filter, { $set: object })
129
134
  return resp;
130
135
  }
131
136
 
132
137
  async delete(modelname, filter) {
133
138
  this.sync.delete(modelname, filter)
139
+ this.metrics.delete(modelname, filter)
134
140
  var resp = await this.getdb().collection(modelname).deleteMany(filter)
135
141
  return resp;
136
142
  }
@@ -1,4 +1,5 @@
1
1
  const {Sync} = require('../sync')
2
+ const { Metrics } = require('./metrics')
2
3
  class MultiDbORM {
3
4
 
4
5
  db
@@ -7,10 +8,12 @@ class MultiDbORM {
7
8
  lastQLatency
8
9
  loglevel = 0
9
10
  sync
11
+ metrics
10
12
 
11
13
  constructor(db) {
12
14
  this.db = db
13
15
  this.sync = new Sync()
16
+ this.metrics = new Metrics(this.loglevel)
14
17
  }
15
18
 
16
19
  setdb(db) {
@@ -45,6 +45,7 @@ class SQLiteDB extends MultiDbORM {
45
45
  }
46
46
 
47
47
  async get(modelname, filter, options) {
48
+ this.metrics.get(modelname, filter, options)
48
49
  var where = ''
49
50
  for (var key in filter) {
50
51
  where = where + `${key} = '${filter[key]}' AND `
@@ -62,9 +63,9 @@ class SQLiteDB extends MultiDbORM {
62
63
  }
63
64
  else if (options.sort) {
64
65
  sort = `ORDER BY`
65
- for(let i=0;i<options.sort.length;i++){
66
+ for (let i = 0; i < options.sort.length; i++) {
66
67
  sort = sort + ` ${options.sort[i].field} ${options.sort[i].order}`;
67
- if(i < options.sort.length - 1){
68
+ if (i < options.sort.length - 1) {
68
69
  sort = sort + ' , ';
69
70
  }
70
71
 
@@ -76,6 +77,7 @@ class SQLiteDB extends MultiDbORM {
76
77
  }
77
78
 
78
79
  async getOne(modelname, filter) {
80
+ this.metrics.getOne(modelname, filter)
79
81
  var where = ''
80
82
  for (var key in filter) {
81
83
  where = where + `${key} = '${filter[key]}' AND `
@@ -88,6 +90,7 @@ class SQLiteDB extends MultiDbORM {
88
90
 
89
91
  async create(modelname, sampleObject) {
90
92
  this.sync.create(modelname, sampleObject)
93
+ this.metrics.create(modelname, sampleObject)
91
94
 
92
95
  var cols = ''
93
96
  for (var key in sampleObject) {
@@ -106,6 +109,7 @@ class SQLiteDB extends MultiDbORM {
106
109
 
107
110
  async insert(modelname, object) {
108
111
  this.sync.insert(modelname, object)
112
+ this.metrics.insert(modelname, object)
109
113
  var cols = ''
110
114
  var vals = ''
111
115
  for (var key in object) {
@@ -131,6 +135,7 @@ class SQLiteDB extends MultiDbORM {
131
135
 
132
136
  async update(modelname, filter, object) {
133
137
  this.sync.update(modelname, filter, object)
138
+ this.metrics.update(modelname, filter, object)
134
139
 
135
140
  var where = ''
136
141
  var vals = ''
@@ -149,6 +154,7 @@ class SQLiteDB extends MultiDbORM {
149
154
 
150
155
  async delete(modelname, filter) {
151
156
  this.sync.delete(modelname, filter)
157
+ this.metrics.delete(modelname, filter)
152
158
 
153
159
  var where = ''
154
160
  for (var key in filter) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "multi-db-orm",
3
- "version": "1.0.7",
3
+ "version": "1.2.0",
4
4
  "description": "CRUD , Backup , Restore and Migration library for multiple databases",
5
5
  "main": "index.js",
6
6
  "dependencies": {
package/test/models.js CHANGED
@@ -1,10 +1,10 @@
1
1
  class Game {
2
2
 
3
- id
4
- timeStamp
5
- userid
6
- amount
7
- type
3
+ id;
4
+ timeStamp;
5
+ userid;
6
+ amount;
7
+ type;
8
8
 
9
9
  constructor(id,timeStamp,userid,amount,type){
10
10
  this.id=id
package/test/test.js CHANGED
@@ -11,6 +11,7 @@ function checkTestsCompleted() {
11
11
 
12
12
  async function testSqlite() {
13
13
  var sqlitedb = new SQLiteDB();
14
+ console.log(sqlitedb.metrics.getStatus())
14
15
  var gm = new Game('IndVSPak', Date.now(), 'Dhoni', 67.33, 'paid')
15
16
  sqlitedb.loglevel = 1
16
17
  var res = await sqlitedb.create('games', gm);
@@ -49,6 +50,7 @@ async function testSqlite() {
49
50
  })
50
51
 
51
52
  console.log('SQLite DB Tests Successfull')
53
+ console.log(sqlitedb.metrics.getStatus())
52
54
  checkTestsCompleted();
53
55
  }
54
56
 
@@ -63,6 +65,8 @@ async function testFireStore() {
63
65
  return
64
66
  }
65
67
  var firebasedb = new FireStoreDB(require('../creds.json'));
68
+ console.log(firebasedb.metrics.getStatus())
69
+
66
70
  var gm = new Game('IndVSPak', Date.now(), 'Dhoni', 67.33, 'free')
67
71
  firebasedb.loglevel = 1
68
72
  var res = await firebasedb.create('games', gm);
@@ -104,6 +108,8 @@ async function testFireStore() {
104
108
  })
105
109
 
106
110
  console.log('Firestore DB Tests Successfull')
111
+ console.log(firebasedb.metrics.getStatus())
112
+
107
113
  checkTestsCompleted();
108
114
 
109
115
  }
@@ -120,6 +126,8 @@ async function testMongo() {
120
126
  }
121
127
  var crd = require('../creds.json')
122
128
  var mongodb = new MongoDB(crd.mongourl, crd.mongodbname);
129
+ console.log(mongodb.metrics.getStatus())
130
+
123
131
  if (mongodb.db == undefined) {
124
132
  await mongodb._connect();
125
133
  }
@@ -163,6 +171,8 @@ async function testMongo() {
163
171
  })
164
172
  await mongodb.delete('games', { type: 'free' })
165
173
  console.log('Mongo DB Tests Successfull')
174
+ console.log(mongodb.metrics.getStatus())
175
+
166
176
 
167
177
  } finally {
168
178
  mongodb._close();