multi-db-orm 1.0.9 → 1.2.1

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:
@@ -1,4 +1,3 @@
1
- const { model } = require("mongoose");
2
1
  const { MultiDbORM } = require("./multidb");
3
2
 
4
3
  function removeUndefined(obj) {
@@ -91,7 +90,10 @@ class FireStoreDB extends MultiDbORM {
91
90
  var snapshot = await this._get(modelname, filter, options)
92
91
  if (snapshot == undefined)
93
92
  return []
93
+ this.metrics.get(modelname, filter, options)
94
+ let that = this;
94
95
  snapshot.forEach(doc => {
96
+ that.metrics.getOne(modelname, filter, options);
95
97
  result.push(doc.data())
96
98
  });
97
99
  if (this.loglevel > 2)
@@ -104,7 +106,7 @@ class FireStoreDB extends MultiDbORM {
104
106
  async getOne(modelname, filter, id, options) {
105
107
  var idx = id || filter.id
106
108
  if (idx) {
107
-
109
+ this.metrics.getOne(modelname, filter, options);
108
110
  const modelref = this.getdb().collection(modelname).doc(idx);
109
111
  const doc = await modelref.get();
110
112
  if (this.loglevel > 2)
@@ -127,12 +129,15 @@ class FireStoreDB extends MultiDbORM {
127
129
 
128
130
  async create(modelname, sampleObject) {
129
131
  this.sync.create(modelname, sampleObject)
132
+ this.metrics.create(modelname, sampleObject);
133
+
130
134
  if (this.loglevel > 3)
131
135
  console.log('CREATE : Not required in DB Type', this.dbType)
132
136
  }
133
137
 
134
138
  async insert(modelname, object, id) {
135
139
  this.sync.insert(modelname, object, id)
140
+ this.metrics.insert(modelname, object, id)
136
141
 
137
142
  var db = this.getdb();
138
143
  var idx = id || object.id || Date.now()
@@ -162,11 +167,15 @@ class FireStoreDB extends MultiDbORM {
162
167
  try {
163
168
  removeUndefined(object)
164
169
  if (idx) {
170
+ this.metrics.update(modelname, filter, object, id)
165
171
  await this.getdb().collection(modelname).doc(idx).update(object);
166
172
 
167
173
  } else {
168
174
  var snaps = await this._get(modelname, filter)
175
+ let that = this;
169
176
  snaps.forEach(async function (element) {
177
+ that.metrics.getOne(modelname, filter, id)
178
+ that.metrics.update(modelname, filter, object, id)
170
179
  await element.ref.update(object)
171
180
  });
172
181
  }
@@ -189,12 +198,15 @@ class FireStoreDB extends MultiDbORM {
189
198
  var idx = id || filter.id
190
199
 
191
200
  if (idx) {
192
-
201
+ this.metrics.delete(modelname, filter, id)
193
202
  await this.getdb().collection(modelname).doc(idx).delete();
194
203
 
195
204
  } else {
196
205
  var snaps = await this._get(modelname, filter)
206
+ let that = this;
197
207
  snaps.forEach(async function (element) {
208
+ that.metrics.getOne(modelname, filter, id)
209
+ that.metrics.delete(modelname, filter, id)
198
210
  await element.ref.delete();
199
211
  });
200
212
  }
@@ -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.9",
3
+ "version": "1.2.1",
4
4
  "description": "CRUD , Backup , Restore and Migration library for multiple databases",
5
5
  "main": "index.js",
6
6
  "dependencies": {
package/sync.js CHANGED
@@ -1,5 +1,9 @@
1
- const bkp = require('./backup')
2
- const rst = require('./restore')
1
+ try {
2
+ var bkp = require('./backup')
3
+ var rst = require('./restore')
4
+ } catch (e) {
5
+ console.log('Warning : Multi region sync not enabled.' )
6
+ }
3
7
 
4
8
  class Sync {
5
9
 
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();