multi-db-orm 2.1.27 → 3.0.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.
- package/README.md +67 -37
- package/databases.js +11 -9
- package/engines/hanadb.d.ts +24 -0
- package/engines/hanadb.js +249 -0
- package/engines/index.d.ts +2 -1
- package/index.js +18 -9
- package/package.json +3 -2
- package/test/test.js +395 -241
package/README.md
CHANGED
|
@@ -1,34 +1,39 @@
|
|
|
1
1
|
# multi-db-orm
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
ORM for multiple SQL and NoSQL databases like firestore , MongoDB , SQlite with Sync , Backup and Restore support .
|
|
3
4
|
|
|
4
5
|
[](https://github.com/shiveshnavin/multi-db-orm/actions/workflows/npm-publish.yml)
|
|
5
6
|
|
|
6
7
|
## 1. Object Relational Mapping
|
|
7
8
|
|
|
8
9
|
Supported databases:
|
|
10
|
+
|
|
9
11
|
1. MongoDB
|
|
10
12
|
2. Google Firestore
|
|
11
13
|
3. SQlite3
|
|
12
14
|
4. Oracle
|
|
13
|
-
|
|
15
|
+
5. MySQL
|
|
14
16
|
|
|
15
17
|
### Install
|
|
18
|
+
|
|
16
19
|
The package is available on npm
|
|
17
|
-
`
|
|
18
|
-
npm install multi-db-orm
|
|
19
|
-
`
|
|
20
|
+
`npm install multi-db-orm`
|
|
20
21
|
|
|
21
22
|
### Initialize
|
|
23
|
+
|
|
22
24
|
Install the target optional database dependencies based on your usage
|
|
25
|
+
|
|
23
26
|
```
|
|
24
27
|
npm install --save mongodb
|
|
25
28
|
npm install --save firebase-admin
|
|
26
29
|
npm install --save sqlite3
|
|
27
30
|
npm install --save oracledb oracle-instantclient
|
|
28
31
|
npm install --save mysql
|
|
32
|
+
npm install --save @sap/hana-client
|
|
29
33
|
```
|
|
30
34
|
|
|
31
35
|
Configure the database
|
|
36
|
+
|
|
32
37
|
```
|
|
33
38
|
const { MultiDbORM, FireStoreDB, MongoDB, SQLiteDB, MySQLDB, Sync } = require("multi-db-orm");
|
|
34
39
|
|
|
@@ -64,29 +69,42 @@ var mysqldb = new MySQLDB({
|
|
|
64
69
|
"database": "test"
|
|
65
70
|
});
|
|
66
71
|
|
|
72
|
+
// HanaDB
|
|
73
|
+
var mysqldb = new HanaDB({
|
|
74
|
+
"host": "db.hana.com",
|
|
75
|
+
"port": "443",
|
|
76
|
+
"username": "test",
|
|
77
|
+
"password": "Password@123"
|
|
78
|
+
});
|
|
67
79
|
var db = firebasedb;
|
|
68
80
|
```
|
|
69
81
|
|
|
70
|
-
### Usage
|
|
82
|
+
### Usage
|
|
83
|
+
|
|
71
84
|
You can perform Create,Insert,Get,GetOne,Update,Delete queries . You only need to base your code on the interface MultiDbORM
|
|
72
85
|
|
|
73
86
|
<b>Note:</b>
|
|
74
|
-
|
|
75
|
-
|
|
87
|
+
|
|
88
|
+
1. Firestore DB requires a `documentPath` specified for each document in a collection . For FirestoreDB functions you can optionally specify this path as the last parameter in getOne,update,insert,delete functions or have a 'id' field in all your objects . If none are specified the Date.now() is used.
|
|
89
|
+
2. All the functions are `async` i.e. they return a promise so you can use `..then()` or `await`
|
|
76
90
|
|
|
77
91
|
#### Create
|
|
92
|
+
|
|
78
93
|
You can create a Table from a sample object in SQlite . NoSQL databases need not create a entity explicitly .
|
|
94
|
+
|
|
79
95
|
```
|
|
80
96
|
// db.create(modelname,sampleObject)
|
|
81
97
|
|
|
82
98
|
var db = new SQLiteDB("/path/to/mydatabase.db"); // if no path is passed , an in-memory db is used
|
|
83
|
-
db.create('game',aSampleGameObject);
|
|
84
|
-
// creates a game table in db .
|
|
99
|
+
db.create('game',aSampleGameObject);
|
|
100
|
+
// creates a game table in db .
|
|
85
101
|
// The fields and their data types are extracted from aSampleGameObject but aSampleGameObject is not saved in db
|
|
86
102
|
```
|
|
87
103
|
|
|
88
104
|
#### Insert
|
|
105
|
+
|
|
89
106
|
The same code will insert a object to the database entity based on the Implementation of MultiDbORM selected from Initialize Step above . Calling `db.insert()` returns a promise so can be used with async/await easily .
|
|
107
|
+
|
|
90
108
|
```
|
|
91
109
|
// db.insert(modelname,object)
|
|
92
110
|
|
|
@@ -102,22 +120,25 @@ db.insert('game', gm).then(response=>{
|
|
|
102
120
|
```
|
|
103
121
|
|
|
104
122
|
#### Get
|
|
123
|
+
|
|
105
124
|
The code will retrieve object(s) from the database .
|
|
125
|
+
|
|
106
126
|
```
|
|
107
127
|
// db.get(modelname,filter)
|
|
108
128
|
|
|
109
|
-
var games = await db.get('game', { amount: 19.00 , type: 'Hockey' });
|
|
129
|
+
var games = await db.get('game', { amount: 19.00 , type: 'Hockey' });
|
|
110
130
|
// returns an array of games having amount = 19.00 and type = Hockey
|
|
111
131
|
|
|
112
|
-
var oneGame = await db.getOne('game', { country: 'India' }); // returns single game having country = 19.00
|
|
132
|
+
var oneGame = await db.getOne('game', { country: 'India' }); // returns single game having country = 19.00
|
|
113
133
|
|
|
114
|
-
var oneGameFr = await db.getOne('game', { country: 'India' },"32g274hfn48vnf"));
|
|
134
|
+
var oneGameFr = await db.getOne('game', { country: 'India' },"32g274hfn48vnf"));
|
|
115
135
|
// Only for firestore if docPath is passed optionally , filter is ignored and the object is returned
|
|
116
136
|
```
|
|
117
137
|
|
|
118
|
-
|
|
119
138
|
#### Get with Range and sort
|
|
139
|
+
|
|
120
140
|
The code will retrieve object(s) from the database with range (supported range operations : > , < , >= , >= , != , = ) and sort (asc or desc) on single field , limit and offset.
|
|
141
|
+
|
|
121
142
|
```
|
|
122
143
|
var gamesFr = await mongodb.get('games', { amount: 400 }, {
|
|
123
144
|
apply: {
|
|
@@ -134,75 +155,83 @@ var gamesFr = await mongodb.get('games', { amount: 400 }, {
|
|
|
134
155
|
```
|
|
135
156
|
|
|
136
157
|
#### Get with Sort , Limit and Offset
|
|
158
|
+
|
|
137
159
|
The code will retrieve object(s) from the database with sort (asc or desc) , limit and offset.
|
|
160
|
+
|
|
138
161
|
```
|
|
139
|
-
var oneGameFr = await mongodb.get('game',
|
|
162
|
+
var oneGameFr = await mongodb.get('game',
|
|
140
163
|
{ country: 'India' },
|
|
141
164
|
{ sort: [{ field: 'timeStamp', order: 'asc' },
|
|
142
|
-
{ field: 'amount', order: 'asc' }],
|
|
143
|
-
limit: 5, offset: 1
|
|
165
|
+
{ field: 'amount', order: 'asc' }],
|
|
166
|
+
limit: 5, offset: 1
|
|
144
167
|
})
|
|
145
168
|
|
|
146
169
|
```
|
|
147
|
-
|
|
170
|
+
|
|
171
|
+
Note :
|
|
172
|
+
|
|
148
173
|
1. For firestore indexes have to be created before using sort . In case indexes are not there you will get an error in the console with a link where you can create the required index .
|
|
149
174
|
2. sort is not applicable when using apply and will be ignored
|
|
150
175
|
|
|
151
|
-
|
|
152
176
|
#### Update
|
|
177
|
+
|
|
153
178
|
The code will update objects in the database .
|
|
179
|
+
|
|
154
180
|
```
|
|
155
181
|
// db.update(modelname,filter,object)
|
|
156
182
|
|
|
157
183
|
|
|
158
|
-
var result = await db.update('game', { amount: 19.00 , type: 'Hockey' },{status : "cancelled",closingTime:Date.now()});
|
|
159
|
-
// updates all the games having amount=19.00 and type=Hockey to status=cancelled
|
|
184
|
+
var result = await db.update('game', { amount: 19.00 , type: 'Hockey' },{status : "cancelled",closingTime:Date.now()});
|
|
185
|
+
// updates all the games having amount=19.00 and type=Hockey to status=cancelled
|
|
160
186
|
// and closingTime as current time while other fields are not touched
|
|
161
187
|
|
|
162
|
-
var result_fire = await db.update('game', { amount: 19.00 , type: 'Hockey' },"32g274hfn48vnf");
|
|
188
|
+
var result_fire = await db.update('game', { amount: 19.00 , type: 'Hockey' },"32g274hfn48vnf");
|
|
163
189
|
/* Only for firestore with optional docPath , it will update collection("game").doc("32g274hfn48vnf") .
|
|
164
190
|
The filters amount and type are ignored when docPath is passed */
|
|
165
191
|
```
|
|
166
192
|
|
|
167
193
|
#### Delete
|
|
194
|
+
|
|
168
195
|
The code will delete objects in the database .
|
|
169
|
-
|
|
196
|
+
|
|
197
|
+
```
|
|
170
198
|
// db.delete(modelname,filter)
|
|
171
199
|
|
|
172
200
|
|
|
173
|
-
var result = await db.delete('game', { amount: 19.00 , type: 'Hockey' });
|
|
174
|
-
// deletes all the games having amount=19.00 and type=Hockey
|
|
201
|
+
var result = await db.delete('game', { amount: 19.00 , type: 'Hockey' });
|
|
202
|
+
// deletes all the games having amount=19.00 and type=Hockey
|
|
175
203
|
|
|
176
204
|
var result_fire = await db.delete('game', { amount: 19.00 , type: 'Hockey' },"32g274hfn48vnf");
|
|
177
205
|
/* Only for firestore with optional docPath , it will delete collection("game").doc("32g274hfn48vnf") .
|
|
178
206
|
The filters amount and type are ignored when docPath is passed */
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
207
|
+
```
|
|
183
208
|
|
|
184
209
|
## 2. Migration
|
|
185
210
|
|
|
186
211
|
#### Mongo DB
|
|
187
212
|
|
|
188
|
-
Pass your SOURCE and TARGET DB credentials
|
|
213
|
+
Pass your SOURCE and TARGET DB credentials
|
|
189
214
|
|
|
190
215
|
Using Docker
|
|
216
|
+
|
|
191
217
|
```
|
|
192
218
|
docker run shiveshnavin/multi-db-safe 'mongodb://username:paswd@dbhost:13873/sourceDBName' 'mongodb://username:paswd@dbhost:13873/targetDBName' 0
|
|
193
|
-
|
|
219
|
+
|
|
194
220
|
```
|
|
221
|
+
|
|
195
222
|
Using Shell
|
|
223
|
+
|
|
196
224
|
```
|
|
197
225
|
./migrate.sh 'mongodb://username:paswd@dbhost:13873/sourceDBName' 'mongodb://username:paswd@dbhost:13873/targetDBName' 0
|
|
198
|
-
|
|
226
|
+
|
|
199
227
|
```
|
|
200
|
-
|
|
228
|
+
|
|
201
229
|
Note : To run deduplication as well set 1 instead of 0 at last cmd line argument
|
|
202
230
|
|
|
203
|
-
## 3. Backup
|
|
231
|
+
## 3. Backup
|
|
204
232
|
|
|
205
233
|
#### Mongo DB
|
|
234
|
+
|
|
206
235
|
```
|
|
207
236
|
node backup.js 'mongodb://username:paswd@dbhost:13873/sourceDBName'
|
|
208
237
|
```
|
|
@@ -212,18 +241,19 @@ This will create a dump file in dumps/
|
|
|
212
241
|
## 4. Restore
|
|
213
242
|
|
|
214
243
|
#### Mongo DB
|
|
244
|
+
|
|
215
245
|
```
|
|
216
|
-
Without Deduplication : node restore.js dumpfile.json 'mongodb://username:paswd@dbhost:13873/targetDBName'
|
|
246
|
+
Without Deduplication : node restore.js dumpfile.json 'mongodb://username:paswd@dbhost:13873/targetDBName'
|
|
217
247
|
|
|
218
248
|
With Deduplication : node restore.js dumpfile.json 'mongodb://username:paswd@dbhost:13873/targetDBName' 1
|
|
219
249
|
```
|
|
220
250
|
|
|
221
|
-
|
|
222
251
|
## 5. Work in Progress
|
|
252
|
+
|
|
223
253
|
Working on enhancing the tool with below features in progress. Feel free to contribute and create a PR .
|
|
224
254
|
|
|
225
255
|
- [ ] Add Backup support for other databases
|
|
226
256
|
- [ ] Add Restore support for other databases
|
|
227
|
-
- [
|
|
257
|
+
- [x] Range Operations like `>=` `<=`
|
|
228
258
|
- [ ] Aggregations
|
|
229
259
|
- [ ] InsertMany
|
package/databases.js
CHANGED
|
@@ -2,14 +2,16 @@ const { MultiDbORM } = require("./engines/multidb");
|
|
|
2
2
|
const { FireStoreDB } = require("./engines/firestoredb");
|
|
3
3
|
const { MongoDB } = require("./engines/mongodb");
|
|
4
4
|
const { SQLiteDB } = require("./engines/sqlitedb");
|
|
5
|
-
const { OracleDB } = require(
|
|
6
|
-
const { MySQLDB } = require(
|
|
5
|
+
const { OracleDB } = require("./engines/oracledb");
|
|
6
|
+
const { MySQLDB } = require("./engines/mysqldb");
|
|
7
|
+
const { HanaDB } = require("./engines/hanadb");
|
|
7
8
|
|
|
8
9
|
module.exports = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
MultiDbORM,
|
|
11
|
+
FireStoreDB,
|
|
12
|
+
MongoDB,
|
|
13
|
+
SQLiteDB,
|
|
14
|
+
OracleDB,
|
|
15
|
+
MySQLDB,
|
|
16
|
+
HanaDB,
|
|
17
|
+
};
|
|
@@ -0,0 +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
|
+
}
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
const { MultiDbORM } = require("./multidb");
|
|
2
|
+
|
|
3
|
+
class HanaDB extends MultiDbORM {
|
|
4
|
+
db;
|
|
5
|
+
conn;
|
|
6
|
+
dataMap = {
|
|
7
|
+
id: "VARCHAR(50) NOT NULL PRIMARY KEY",
|
|
8
|
+
string: "NVARCHAR(4000)",
|
|
9
|
+
stringlarge: "NCLOB",
|
|
10
|
+
stringsmall: "NVARCHAR(255)",
|
|
11
|
+
number: "DOUBLE",
|
|
12
|
+
boolean: "BOOLEAN",
|
|
13
|
+
array: "NCLOB",
|
|
14
|
+
object: "NCLOB",
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
constructor(credentials) {
|
|
18
|
+
super();
|
|
19
|
+
const hana = require("@sap/hana-client");
|
|
20
|
+
this.db = hana;
|
|
21
|
+
this.conn_params = {
|
|
22
|
+
...credentials,
|
|
23
|
+
host: credentials.host,
|
|
24
|
+
port: credentials.port,
|
|
25
|
+
serverNode: `${credentials.host}:${credentials.port}`,
|
|
26
|
+
uid: credentials.username,
|
|
27
|
+
pwd: credentials.password,
|
|
28
|
+
databaseName: credentials.database,
|
|
29
|
+
communicationTimeout: credentials.connectionLimit || 60000,
|
|
30
|
+
connectTimeout: credentials.connectionLimit || 60000,
|
|
31
|
+
reconnect: credentials.reconnect ?? true,
|
|
32
|
+
poolOptions: {
|
|
33
|
+
maxPoolSize: credentials.connectionLimit || 10,
|
|
34
|
+
idleTimeout: credentials.timeout || 60000,
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
this.conn = hana.createConnection();
|
|
38
|
+
this.db = this.conn;
|
|
39
|
+
this.dbType = "hana";
|
|
40
|
+
this.reqMade = 0;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async run(query) {
|
|
44
|
+
const that = this;
|
|
45
|
+
this.reqMade++;
|
|
46
|
+
return new Promise((resolve, reject) => {
|
|
47
|
+
let onConnect = function (err) {
|
|
48
|
+
if (err) throw err;
|
|
49
|
+
that.conn.exec(query, (err, result) => {
|
|
50
|
+
if (err) {
|
|
51
|
+
reject(err);
|
|
52
|
+
} else {
|
|
53
|
+
resolve(result);
|
|
54
|
+
}
|
|
55
|
+
if (that.loglevel > 3) {
|
|
56
|
+
console.log("Query", query, "->", result);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
if (that.conn.state() == "disconnected") {
|
|
61
|
+
that.conn.connect(that.conn_params, onConnect);
|
|
62
|
+
} else {
|
|
63
|
+
onConnect();
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async get(modelname, filter, options) {
|
|
69
|
+
this.metrics.get(modelname, filter, options);
|
|
70
|
+
let where = "";
|
|
71
|
+
for (const key in filter) {
|
|
72
|
+
where += `${key} = '${filter[key]}' AND `;
|
|
73
|
+
}
|
|
74
|
+
where += "1 = 1";
|
|
75
|
+
let sort = "";
|
|
76
|
+
if (options) {
|
|
77
|
+
if (options.apply) {
|
|
78
|
+
if (options.apply.ineq) {
|
|
79
|
+
where += ` AND ${options.apply.field} ${options.apply.ineq.op} '${options.apply.ineq.value}'`;
|
|
80
|
+
}
|
|
81
|
+
if (options.apply.sort) {
|
|
82
|
+
sort = `ORDER BY ${options.apply.field} ${options.apply.sort}`;
|
|
83
|
+
}
|
|
84
|
+
} else if (options.sort) {
|
|
85
|
+
sort = "ORDER BY";
|
|
86
|
+
for (let i = 0; i < options.sort.length; i++) {
|
|
87
|
+
sort += ` ${options.sort[i].field} ${options.sort[i].order}`;
|
|
88
|
+
if (i < options.sort.length - 1) {
|
|
89
|
+
sort += ", ";
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const limit = options?.limit ? `LIMIT ${options.limit}` : "";
|
|
95
|
+
const offset = options?.offset ? `OFFSET ${options.offset}` : "";
|
|
96
|
+
const query = `SELECT * FROM ${modelname} WHERE ${where} ${sort} ${limit} ${offset};`;
|
|
97
|
+
return (await this.run(query)) || [];
|
|
98
|
+
}
|
|
99
|
+
escapeSQLValue(value) {
|
|
100
|
+
if (typeof value === "string") {
|
|
101
|
+
return `'${value.replace(/'/g, "''")}'`;
|
|
102
|
+
} else if (value === null || value === undefined) {
|
|
103
|
+
return "NULL";
|
|
104
|
+
}
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
107
|
+
async getOne(modelname, filter) {
|
|
108
|
+
this.metrics.getOne(modelname, filter);
|
|
109
|
+
let where = "";
|
|
110
|
+
for (const key in filter) {
|
|
111
|
+
where += `${key} = '${filter[key]}' AND `;
|
|
112
|
+
}
|
|
113
|
+
where += "1 = 1";
|
|
114
|
+
const query = `SELECT * FROM ${modelname} WHERE ${where} LIMIT 1;`;
|
|
115
|
+
const row = await this.run(query);
|
|
116
|
+
return row[0];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async create(modelname, sampleObject) {
|
|
120
|
+
this.sync.create(modelname, sampleObject);
|
|
121
|
+
this.metrics.create(modelname, sampleObject);
|
|
122
|
+
|
|
123
|
+
let cols = "";
|
|
124
|
+
for (const key in sampleObject) {
|
|
125
|
+
let type;
|
|
126
|
+
if (this.dataMap[sampleObject[key]]) {
|
|
127
|
+
type = this.dataMap[sampleObject[key]];
|
|
128
|
+
} else {
|
|
129
|
+
type = this.dataMap[typeof sampleObject[key]] || "NCLOB";
|
|
130
|
+
if (typeof sampleObject[key] === "string") {
|
|
131
|
+
if (sampleObject[key].length > 4000) {
|
|
132
|
+
type = this.dataMap["stringlarge"];
|
|
133
|
+
}
|
|
134
|
+
if (sampleObject[key].length <= 255) {
|
|
135
|
+
type = this.dataMap["stringsmall"];
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (key.toLowerCase().trim() === "id") {
|
|
141
|
+
cols += `${key} ${type} PRIMARY KEY NOT NULL ,`;
|
|
142
|
+
} else {
|
|
143
|
+
cols += `${key} ${type},`;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
cols = cols.substring(0, cols.length - 1);
|
|
147
|
+
const query = `CREATE COLUMN TABLE ${modelname} (${cols});`;
|
|
148
|
+
try {
|
|
149
|
+
return await this.run(query);
|
|
150
|
+
} catch (err) {
|
|
151
|
+
if (this.loglevel > 0) console.log(err);
|
|
152
|
+
throw err;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async insert(modelname, object) {
|
|
157
|
+
this.sync.insert(modelname, object);
|
|
158
|
+
this.metrics.insert(modelname, object);
|
|
159
|
+
let cols = "";
|
|
160
|
+
let vals = "";
|
|
161
|
+
for (const key in object) {
|
|
162
|
+
cols = cols + `${key},`;
|
|
163
|
+
let val = object[key];
|
|
164
|
+
if (typeof val == "object") val = JSON.stringify(object[key]);
|
|
165
|
+
val = this.escapeSQLValue(val);
|
|
166
|
+
if (typeof val == "undefined") vals = vals + `Null,`;
|
|
167
|
+
else if (typeof val == "boolean") vals = vals + `${val},`;
|
|
168
|
+
else if (typeof val == "number") vals = vals + `${val},`;
|
|
169
|
+
else vals = vals + `${val},`;
|
|
170
|
+
}
|
|
171
|
+
cols = cols.slice(0, -1);
|
|
172
|
+
vals = vals.slice(0, -1);
|
|
173
|
+
|
|
174
|
+
const query = `INSERT INTO ${modelname} (${cols}) VALUES (${vals});`;
|
|
175
|
+
|
|
176
|
+
try {
|
|
177
|
+
return await this.run(query);
|
|
178
|
+
} catch (err) {
|
|
179
|
+
if (err.code && err.code === "259") {
|
|
180
|
+
// Table does not exist
|
|
181
|
+
await this.create(modelname, object);
|
|
182
|
+
return await this.run(query);
|
|
183
|
+
} else {
|
|
184
|
+
throw err;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
async update(modelname, filter, object) {
|
|
190
|
+
this.sync.update(modelname, filter, object);
|
|
191
|
+
this.metrics.update(modelname, filter, object);
|
|
192
|
+
|
|
193
|
+
let where = "";
|
|
194
|
+
let vals = "";
|
|
195
|
+
for (const key in filter) {
|
|
196
|
+
where += `${key} = '${filter[key]}' AND `;
|
|
197
|
+
}
|
|
198
|
+
for (const key in object) {
|
|
199
|
+
let val = object[key];
|
|
200
|
+
if (typeof val === "object" && val != null)
|
|
201
|
+
val = JSON.stringify(object[key]);
|
|
202
|
+
val = this.escapeSQLValue(val);
|
|
203
|
+
|
|
204
|
+
vals += `${key} = ${val},`;
|
|
205
|
+
}
|
|
206
|
+
where += "1 = 1";
|
|
207
|
+
vals = vals.slice(0, -1);
|
|
208
|
+
|
|
209
|
+
const query = `UPDATE ${modelname} SET ${vals} WHERE ${where};`;
|
|
210
|
+
try {
|
|
211
|
+
return await this.run(query);
|
|
212
|
+
} catch (e) {
|
|
213
|
+
if (this.loglevel > 4) console.log("Error in update", e);
|
|
214
|
+
throw e;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async delete(modelname, filter) {
|
|
219
|
+
this.sync.delete(modelname, filter);
|
|
220
|
+
this.metrics.delete(modelname, filter);
|
|
221
|
+
|
|
222
|
+
let where = "";
|
|
223
|
+
for (const key in filter) {
|
|
224
|
+
where += `${key} = '${filter[key]}' AND `;
|
|
225
|
+
}
|
|
226
|
+
where += "1 = 1";
|
|
227
|
+
const query = `DELETE FROM ${modelname} WHERE ${where};`;
|
|
228
|
+
return await this.run(query);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
closePool() {
|
|
232
|
+
return new Promise((resolve, reject) => {
|
|
233
|
+
this.conn.disconnect((err) => {
|
|
234
|
+
if (err) {
|
|
235
|
+
reject(err);
|
|
236
|
+
if (this.loglevel > 1)
|
|
237
|
+
console.error("Error closing connection pool:", err);
|
|
238
|
+
} else {
|
|
239
|
+
resolve();
|
|
240
|
+
if (this.loglevel > 1) console.log("HanaDB: Connection pool closed");
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
module.exports = {
|
|
248
|
+
HanaDB,
|
|
249
|
+
};
|
package/engines/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -1,12 +1,21 @@
|
|
|
1
|
-
const {
|
|
1
|
+
const {
|
|
2
|
+
MultiDBSafe,
|
|
3
|
+
FireStoreDB,
|
|
4
|
+
MongoDB,
|
|
5
|
+
SQLiteDB,
|
|
6
|
+
OracleDB,
|
|
7
|
+
MySQLDB,
|
|
8
|
+
HanaDB,
|
|
9
|
+
} = require("./databases");
|
|
2
10
|
const { Sync } = require("./sync");
|
|
3
11
|
|
|
4
12
|
module.exports = {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
MultiDBSafe,
|
|
14
|
+
FireStoreDB,
|
|
15
|
+
MongoDB,
|
|
16
|
+
SQLiteDB,
|
|
17
|
+
OracleDB,
|
|
18
|
+
MySQLDB,
|
|
19
|
+
HanaDB,
|
|
20
|
+
Sync,
|
|
21
|
+
};
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "multi-db-orm",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"description": "CRUD , Backup , Restore and Migration library for multiple databases",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"fs": "0.0.1-security"
|
|
8
8
|
},
|
|
9
9
|
"devDependencies": {
|
|
10
|
+
"@sap/hana-client": "^2.23.24",
|
|
10
11
|
"firebase-admin": "^9.3.0",
|
|
11
12
|
"mongodb": "^3.6.3",
|
|
12
13
|
"mysql": "^2.18.1",
|
|
@@ -46,4 +47,4 @@
|
|
|
46
47
|
"url": "https://github.com/shiveshnavin/multi-db-orm/issues"
|
|
47
48
|
},
|
|
48
49
|
"homepage": "https://github.com/shiveshnavin/multi-db-orm#readme"
|
|
49
|
-
}
|
|
50
|
+
}
|
package/test/test.js
CHANGED
|
@@ -1,280 +1,434 @@
|
|
|
1
1
|
const { Game } = require("./models");
|
|
2
|
-
const {
|
|
3
|
-
|
|
2
|
+
const {
|
|
3
|
+
MultiDBSafe,
|
|
4
|
+
FireStoreDB,
|
|
5
|
+
MongoDB,
|
|
6
|
+
SQLiteDB,
|
|
7
|
+
Sync,
|
|
8
|
+
OracleDB,
|
|
9
|
+
} = require("../index");
|
|
10
|
+
const path = require("path");
|
|
4
11
|
const { MySQLDB } = require("../engines/mysqldb");
|
|
12
|
+
const { HanaDB } = require("../engines/hanadb");
|
|
5
13
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var testCount = 0
|
|
14
|
+
var testCount = 0;
|
|
9
15
|
function checkTestsCompleted() {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
16
|
+
if (--testCount <= 0) {
|
|
17
|
+
process.exit(0);
|
|
18
|
+
}
|
|
13
19
|
}
|
|
14
20
|
|
|
15
21
|
async function testSqlite() {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
22
|
+
var sqlitedb = new SQLiteDB();
|
|
23
|
+
console.log(sqlitedb.metrics.getStatus());
|
|
24
|
+
var gm = new Game("IndVSPak", Date.now(), "Dhoni", 67.33, "paid");
|
|
25
|
+
sqlitedb.loglevel = 1;
|
|
26
|
+
var res = await sqlitedb.create("games", gm);
|
|
27
|
+
res = await sqlitedb.insert("games", gm);
|
|
28
|
+
res = await sqlitedb.insert("games", gm);
|
|
29
|
+
gm.amount = 1;
|
|
30
|
+
res = await sqlitedb.insert("games", gm);
|
|
31
|
+
res = await sqlitedb.get("games", { amount: 1 });
|
|
32
|
+
res = await sqlitedb.getOne("games", { amount: 1 });
|
|
33
|
+
res = await sqlitedb.update("games", { amount: 1 }, { userid: "xxxx" });
|
|
34
|
+
res = await sqlitedb.getOne("games", { userid: "xxxx" });
|
|
35
|
+
|
|
36
|
+
res = await sqlitedb.insert(
|
|
37
|
+
"games",
|
|
38
|
+
new Game("IndVSPak1", Date.now(), "Dhoni", 100, "free")
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
res = await sqlitedb.insert(
|
|
42
|
+
"games",
|
|
43
|
+
new Game("IndVSPak2", Date.now(), "Dhoni", 200, "free")
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
res = await sqlitedb.insert(
|
|
47
|
+
"games",
|
|
48
|
+
new Game("IndVSPak3", Date.now(), "Dhoni", 300, "paid")
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
res = await sqlitedb.insert(
|
|
52
|
+
"games",
|
|
53
|
+
new Game("IndVSPak4", Date.now(), "Dhoni", 400, "paid")
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
res = await sqlitedb.get("games", undefined, {
|
|
57
|
+
sort: [
|
|
58
|
+
{ field: "timeStamp", order: "asc" },
|
|
59
|
+
{ field: "amount", order: "asc" },
|
|
60
|
+
],
|
|
61
|
+
limit: 5,
|
|
62
|
+
offset: 1,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
res = await sqlitedb.get(
|
|
66
|
+
"games",
|
|
67
|
+
{ type: "paid" },
|
|
68
|
+
{
|
|
69
|
+
sort: [
|
|
70
|
+
{ field: "amount", order: "desc" },
|
|
71
|
+
{ field: "timeStamp", order: "desc" },
|
|
72
|
+
],
|
|
73
|
+
}
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
res = await sqlitedb.get(
|
|
77
|
+
"games",
|
|
78
|
+
{ amount: 400 },
|
|
79
|
+
{
|
|
80
|
+
apply: {
|
|
81
|
+
field: "timeStamp",
|
|
82
|
+
sort: "desc",
|
|
83
|
+
ineq: {
|
|
84
|
+
op: ">=",
|
|
85
|
+
value: 1,
|
|
50
86
|
},
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
87
|
+
},
|
|
88
|
+
sort: [
|
|
89
|
+
{ field: "amount", order: "asc" },
|
|
90
|
+
{ field: "timeStamp", order: "desc" },
|
|
91
|
+
],
|
|
92
|
+
limit: 2,
|
|
93
|
+
offset: 1,
|
|
94
|
+
}
|
|
95
|
+
);
|
|
54
96
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
97
|
+
console.log("SQLite DB Tests Successfull");
|
|
98
|
+
console.log(sqlitedb.metrics.getStatus());
|
|
99
|
+
checkTestsCompleted();
|
|
58
100
|
}
|
|
59
101
|
|
|
60
|
-
|
|
61
102
|
async function testFireStore() {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
103
|
+
try {
|
|
104
|
+
require("../creds.json");
|
|
105
|
+
} catch (e) {
|
|
106
|
+
console.log("testFireStore", "creds.json not found");
|
|
107
|
+
checkTestsCompleted();
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
var firebasedb = new FireStoreDB(require("../creds.json"));
|
|
111
|
+
console.log(firebasedb.metrics.getStatus());
|
|
112
|
+
|
|
113
|
+
var gm = new Game("IndVSPak", Date.now(), "Dhoni", 67.33, "free");
|
|
114
|
+
firebasedb.loglevel = 1;
|
|
115
|
+
var res = await firebasedb.create("games", gm);
|
|
116
|
+
res = await firebasedb.insert("games", gm, Date.now());
|
|
117
|
+
res = await firebasedb.insert("games", gm, Date.now());
|
|
118
|
+
gm.amount = 1;
|
|
119
|
+
res = await firebasedb.insert("games", gm);
|
|
120
|
+
res = await firebasedb.get("games", { amount: 67.33 });
|
|
121
|
+
res = await firebasedb.getOne("games", { amount: 1 });
|
|
122
|
+
res = await firebasedb.update(
|
|
123
|
+
"games",
|
|
124
|
+
{ userid: "Dhoni" },
|
|
125
|
+
{ userid: "ABC123" }
|
|
126
|
+
);
|
|
127
|
+
res = await firebasedb.update("games", { id: "IndVSPak" }, { amount: 1000 });
|
|
128
|
+
await firebasedb.delete("games", { type: "free" });
|
|
129
|
+
|
|
130
|
+
res = await firebasedb.insert(
|
|
131
|
+
"games",
|
|
132
|
+
new Game("IndVSPak1", Date.now(), "Dhoni", 100, "free")
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
res = await firebasedb.insert(
|
|
136
|
+
"games",
|
|
137
|
+
new Game("IndVSPak2", Date.now(), "Dhoni", 200, "free")
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
res = await firebasedb.insert(
|
|
141
|
+
"games",
|
|
142
|
+
new Game("IndVSPak3", Date.now(), "Dhoni", 300, "paid")
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
res = await firebasedb.insert(
|
|
146
|
+
"games",
|
|
147
|
+
new Game("IndVSPak4", Date.now(), "Dhoni", 400, "paid")
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
res = await firebasedb.get("games", undefined, {
|
|
151
|
+
sort: [{ field: "amount", order: "desc" }],
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
res = await firebasedb.get("games", undefined, {
|
|
155
|
+
sort: [{ field: "amount", order: "desc" }],
|
|
156
|
+
limit: 2,
|
|
157
|
+
offset: 1,
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
// res = await firebasedb.get('games', {type:'paid'}, { sort: [{ field: 'amount', order: 'asc' }, { field: 'timeStamp', order: 'desc' }] })
|
|
161
|
+
|
|
162
|
+
res = await firebasedb.get(
|
|
163
|
+
"games",
|
|
164
|
+
{ amount: 100 },
|
|
165
|
+
{
|
|
166
|
+
apply: {
|
|
167
|
+
field: "timeStamp",
|
|
168
|
+
sort: "desc",
|
|
169
|
+
ineq: {
|
|
170
|
+
op: ">=",
|
|
171
|
+
value: 1,
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
sort: [
|
|
175
|
+
{ field: "amount", order: "asc" },
|
|
176
|
+
{ field: "timeStamp", order: "desc" },
|
|
177
|
+
],
|
|
178
|
+
limit: 2,
|
|
179
|
+
offset: 1,
|
|
69
180
|
}
|
|
70
|
-
|
|
71
|
-
console.log(firebasedb.metrics.getStatus())
|
|
72
|
-
|
|
73
|
-
var gm = new Game('IndVSPak', Date.now(), 'Dhoni', 67.33, 'free')
|
|
74
|
-
firebasedb.loglevel = 1
|
|
75
|
-
var res = await firebasedb.create('games', gm);
|
|
76
|
-
res = await firebasedb.insert('games', gm, Date.now());
|
|
77
|
-
res = await firebasedb.insert('games', gm, Date.now());
|
|
78
|
-
gm.amount = 1
|
|
79
|
-
res = await firebasedb.insert('games', gm);
|
|
80
|
-
res = await firebasedb.get('games', { amount: 67.33 });
|
|
81
|
-
res = await firebasedb.getOne('games', { amount: 1 });
|
|
82
|
-
res = await firebasedb.update('games', { userid: 'Dhoni' }, { userid: 'ABC123' });
|
|
83
|
-
res = await firebasedb.update('games', { id: 'IndVSPak' }, { amount: 1000 });
|
|
84
|
-
await firebasedb.delete('games', { type: 'free' })
|
|
85
|
-
|
|
86
|
-
res = await firebasedb.insert('games', new Game('IndVSPak1', Date.now(), 'Dhoni', 100, 'free'));
|
|
87
|
-
|
|
88
|
-
res = await firebasedb.insert('games', new Game('IndVSPak2', Date.now(), 'Dhoni', 200, 'free'));
|
|
89
|
-
|
|
90
|
-
res = await firebasedb.insert('games', new Game('IndVSPak3', Date.now(), 'Dhoni', 300, 'paid'));
|
|
181
|
+
);
|
|
91
182
|
|
|
92
|
-
|
|
183
|
+
console.log("Firestore DB Tests Successfull");
|
|
184
|
+
console.log(firebasedb.metrics.getStatus());
|
|
93
185
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
res = await firebasedb.get('games', undefined, { sort: [{ field: 'amount', order: 'desc' }], limit: 2, offset: 1 })
|
|
97
|
-
|
|
98
|
-
// res = await firebasedb.get('games', {type:'paid'}, { sort: [{ field: 'amount', order: 'asc' }, { field: 'timeStamp', order: 'desc' }] })
|
|
186
|
+
checkTestsCompleted();
|
|
187
|
+
}
|
|
99
188
|
|
|
100
|
-
|
|
189
|
+
async function testMongo() {
|
|
190
|
+
try {
|
|
191
|
+
require("../creds.json");
|
|
192
|
+
} catch (e) {
|
|
193
|
+
console.log("testMongo", "creds.json not found");
|
|
194
|
+
checkTestsCompleted();
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
var crd = require("../creds.json");
|
|
198
|
+
var mongodb = new MongoDB(crd.mongourl, crd.mongodbname);
|
|
199
|
+
console.log(mongodb.metrics.getStatus());
|
|
200
|
+
|
|
201
|
+
if (mongodb.db == undefined) {
|
|
202
|
+
await mongodb._connect();
|
|
203
|
+
}
|
|
204
|
+
try {
|
|
205
|
+
var gm = new Game("IndVSPak", Date.now(), "Dhoni", 67.33, "free");
|
|
206
|
+
mongodb.loglevel = 1;
|
|
207
|
+
var res = await mongodb.create("games", gm);
|
|
208
|
+
res = await mongodb.insert("games", gm);
|
|
209
|
+
res = await mongodb.insert(
|
|
210
|
+
"games",
|
|
211
|
+
new Game("IndVSPak", Date.now(), "Dhoni", 67.33, "free")
|
|
212
|
+
);
|
|
213
|
+
res = await mongodb.get("games", { amount: 67.33 });
|
|
214
|
+
res = await mongodb.getOne("games", { amount: 1 });
|
|
215
|
+
res = await mongodb.update(
|
|
216
|
+
"games",
|
|
217
|
+
{ userid: "Dhoni" },
|
|
218
|
+
{ userid: "ABC123" }
|
|
219
|
+
);
|
|
220
|
+
res = await mongodb.update("games", { id: "IndVSPak" }, { amount: 1000 });
|
|
221
|
+
|
|
222
|
+
res = await mongodb.insert(
|
|
223
|
+
"games",
|
|
224
|
+
new Game("IndVSPak1", Date.now(), "Dhoni", 100, "free")
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
res = await mongodb.insert(
|
|
228
|
+
"games",
|
|
229
|
+
new Game("IndVSPak2", Date.now(), "Dhoni", 200, "free")
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
res = await mongodb.insert(
|
|
233
|
+
"games",
|
|
234
|
+
new Game("IndVSPak3", Date.now(), "Dhoni", 300, "paid")
|
|
235
|
+
);
|
|
236
|
+
|
|
237
|
+
res = await mongodb.insert(
|
|
238
|
+
"games",
|
|
239
|
+
new Game("IndVSPak4", Date.now(), "Dhoni", 400, "paid")
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
res = await mongodb.get("games", undefined, {
|
|
243
|
+
sort: [{ field: "amount", order: "desc" }],
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
res = await mongodb.get("games", undefined, {
|
|
247
|
+
sort: [
|
|
248
|
+
{ field: "timeStamp", order: "asc" },
|
|
249
|
+
{ field: "amount", order: "asc" },
|
|
250
|
+
],
|
|
251
|
+
limit: 5,
|
|
252
|
+
offset: 1,
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
res = await mongodb.get(
|
|
256
|
+
"games",
|
|
257
|
+
{ type: "paid" },
|
|
258
|
+
{
|
|
259
|
+
sort: [
|
|
260
|
+
{ field: "amount", order: "asc" },
|
|
261
|
+
{ field: "timeStamp", order: "desc" },
|
|
262
|
+
],
|
|
263
|
+
}
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
res = await mongodb.get(
|
|
267
|
+
"games",
|
|
268
|
+
{ amount: 400 },
|
|
269
|
+
{
|
|
101
270
|
apply: {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
271
|
+
field: "timeStamp",
|
|
272
|
+
sort: "desc",
|
|
273
|
+
ineq: {
|
|
274
|
+
op: ">=",
|
|
275
|
+
value: 1,
|
|
276
|
+
},
|
|
108
277
|
},
|
|
109
|
-
sort: [
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
278
|
+
sort: [
|
|
279
|
+
{ field: "amount", order: "asc" },
|
|
280
|
+
{ field: "timeStamp", order: "desc" },
|
|
281
|
+
],
|
|
282
|
+
limit: 2,
|
|
283
|
+
offset: 1,
|
|
284
|
+
}
|
|
285
|
+
);
|
|
286
|
+
await mongodb.delete("games", { type: "free" });
|
|
287
|
+
console.log("Mongo DB Tests Successfull");
|
|
288
|
+
console.log(mongodb.metrics.getStatus());
|
|
289
|
+
} finally {
|
|
290
|
+
mongodb._close();
|
|
116
291
|
checkTestsCompleted();
|
|
117
|
-
|
|
292
|
+
}
|
|
118
293
|
}
|
|
119
294
|
|
|
120
|
-
|
|
121
|
-
async function testMongo() {
|
|
122
|
-
try {
|
|
123
|
-
require('../creds.json')
|
|
124
|
-
|
|
125
|
-
} catch (e) {
|
|
126
|
-
console.log('testMongo', 'creds.json not found');
|
|
127
|
-
checkTestsCompleted();
|
|
128
|
-
return
|
|
129
|
-
}
|
|
130
|
-
var crd = require('../creds.json')
|
|
131
|
-
var mongodb = new MongoDB(crd.mongourl, crd.mongodbname);
|
|
132
|
-
console.log(mongodb.metrics.getStatus())
|
|
133
|
-
|
|
134
|
-
if (mongodb.db == undefined) {
|
|
135
|
-
await mongodb._connect();
|
|
136
|
-
}
|
|
137
|
-
try {
|
|
138
|
-
var gm = new Game('IndVSPak', Date.now(), 'Dhoni', 67.33, 'free')
|
|
139
|
-
mongodb.loglevel = 1
|
|
140
|
-
var res = await mongodb.create('games', gm);
|
|
141
|
-
res = await mongodb.insert('games', gm);
|
|
142
|
-
res = await mongodb.insert('games',
|
|
143
|
-
new Game('IndVSPak', Date.now(), 'Dhoni', 67.33, 'free'));
|
|
144
|
-
res = await mongodb.get('games', { amount: 67.33 });
|
|
145
|
-
res = await mongodb.getOne('games', { amount: 1 });
|
|
146
|
-
res = await mongodb.update('games', { userid: 'Dhoni' }, { userid: 'ABC123' });
|
|
147
|
-
res = await mongodb.update('games', { id: 'IndVSPak' }, { amount: 1000 });
|
|
148
|
-
|
|
149
|
-
res = await mongodb.insert('games', new Game('IndVSPak1', Date.now(), 'Dhoni', 100, 'free'));
|
|
150
|
-
|
|
151
|
-
res = await mongodb.insert('games', new Game('IndVSPak2', Date.now(), 'Dhoni', 200, 'free'));
|
|
152
|
-
|
|
153
|
-
res = await mongodb.insert('games', new Game('IndVSPak3', Date.now(), 'Dhoni', 300, 'paid'));
|
|
154
|
-
|
|
155
|
-
res = await mongodb.insert('games', new Game('IndVSPak4', Date.now(), 'Dhoni', 400, 'paid'));
|
|
156
|
-
|
|
157
|
-
res = await mongodb.get('games', undefined, { sort: [{ field: 'amount', order: 'desc' }] })
|
|
158
|
-
|
|
159
|
-
res = await mongodb.get('games', undefined, { sort: [{ field: 'timeStamp', order: 'asc' }, { field: 'amount', order: 'asc' }], limit: 5, offset: 1 })
|
|
160
|
-
|
|
161
|
-
res = await mongodb.get('games', { type: 'paid' }, { sort: [{ field: 'amount', order: 'asc' }, { field: 'timeStamp', order: 'desc' }] })
|
|
162
|
-
|
|
163
|
-
res = await mongodb.get('games', { amount: 400 }, {
|
|
164
|
-
apply: {
|
|
165
|
-
field: 'timeStamp',
|
|
166
|
-
sort: 'desc',
|
|
167
|
-
ineq: {
|
|
168
|
-
op: '>=',
|
|
169
|
-
value: 1
|
|
170
|
-
}
|
|
171
|
-
},
|
|
172
|
-
sort: [{ field: 'amount', order: 'asc' }, { field: 'timeStamp', order: 'desc' }],
|
|
173
|
-
limit: 2, offset: 1
|
|
174
|
-
})
|
|
175
|
-
await mongodb.delete('games', { type: 'free' })
|
|
176
|
-
console.log('Mongo DB Tests Successfull')
|
|
177
|
-
console.log(mongodb.metrics.getStatus())
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
} finally {
|
|
181
|
-
mongodb._close();
|
|
182
|
-
checkTestsCompleted();
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
295
|
async function testOracleDb() {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
296
|
+
let creds = require("../creds/oracle/creds.json");
|
|
297
|
+
creds.wallet_dir = path.join(__dirname, "../creds/oracle");
|
|
298
|
+
var oracledb = new OracleDB(creds);
|
|
299
|
+
console.log(oracledb.metrics.getStatus());
|
|
300
|
+
var gm = new Game("IndVSPak", Date.now(), "Dhoni", 67.33, "paid");
|
|
301
|
+
gm.completed = true;
|
|
302
|
+
gm.runs = ["a", "b"];
|
|
303
|
+
gm.extras = {
|
|
304
|
+
location: "India",
|
|
305
|
+
raining: false,
|
|
306
|
+
match: 1,
|
|
307
|
+
};
|
|
308
|
+
oracledb.loglevel = 1;
|
|
309
|
+
await oracledb.connect();
|
|
310
|
+
test(oracledb);
|
|
204
311
|
}
|
|
205
312
|
|
|
206
|
-
|
|
207
|
-
|
|
208
313
|
async function testMysqlDb() {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
314
|
+
let creds = require("../creds/mysql.json");
|
|
315
|
+
var mysqldb = new MySQLDB(creds);
|
|
316
|
+
console.log(mysqldb.metrics.getStatus());
|
|
317
|
+
mysqldb.loglevel = 1;
|
|
318
|
+
await mysqldb.connect();
|
|
319
|
+
test(mysqldb);
|
|
215
320
|
}
|
|
216
321
|
|
|
322
|
+
async function testHanaDb() {
|
|
323
|
+
let creds = require("../creds/hana.json");
|
|
324
|
+
var hana = new HanaDB(creds);
|
|
325
|
+
console.log(hana.metrics.getStatus());
|
|
326
|
+
hana.loglevel = 1;
|
|
327
|
+
await hana.connect();
|
|
328
|
+
test(hana);
|
|
329
|
+
}
|
|
217
330
|
|
|
218
331
|
async function test(db) {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
332
|
+
testCount++;
|
|
333
|
+
var gm = new Game("IndVSPak", Date.now(), "Dhoni", 67.33, "paid");
|
|
334
|
+
gm.completed = true;
|
|
335
|
+
gm.runs = ["a", "b"];
|
|
336
|
+
gm.extras = {
|
|
337
|
+
location: "India",
|
|
338
|
+
raining: false,
|
|
339
|
+
match: 1,
|
|
340
|
+
};
|
|
341
|
+
var res = await db.create("games", gm).catch((e) => {
|
|
342
|
+
if (!e.message.includes("duplicate")) {
|
|
343
|
+
throw e;
|
|
227
344
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
345
|
+
});
|
|
346
|
+
gm.id = Date.now();
|
|
347
|
+
res = await db.insert("games", gm);
|
|
348
|
+
gm.id = Date.now();
|
|
349
|
+
|
|
350
|
+
res = await db.insert("games", gm);
|
|
351
|
+
gm.amount = 1;
|
|
352
|
+
gm.id = Date.now();
|
|
353
|
+
|
|
354
|
+
res = await db.insert("games", gm);
|
|
355
|
+
res = await db.get("games", { amount: 1 });
|
|
356
|
+
res = await db.getOne("games", { amount: 1 });
|
|
357
|
+
res = await db.update("games", { amount: 1 }, { userid: "xxxx" });
|
|
358
|
+
res = await db.getOne("games", { userid: "xxxx" });
|
|
359
|
+
|
|
360
|
+
res = await db.insert(
|
|
361
|
+
"games",
|
|
362
|
+
new Game("IndVSPak1", Date.now(), "Dhoni", 100, "free")
|
|
363
|
+
);
|
|
364
|
+
|
|
365
|
+
res = await db.insert(
|
|
366
|
+
"games",
|
|
367
|
+
new Game("IndVSPak2", Date.now(), "Dhoni", 200, "free")
|
|
368
|
+
);
|
|
369
|
+
|
|
370
|
+
res = await db.insert(
|
|
371
|
+
"games",
|
|
372
|
+
new Game("IndVSPak3", Date.now(), "Dhoni", 300, "paid")
|
|
373
|
+
);
|
|
374
|
+
|
|
375
|
+
res = await db.insert(
|
|
376
|
+
"games",
|
|
377
|
+
new Game("IndVSPak4", Date.now(), "Dhoni", 400, "paid")
|
|
378
|
+
);
|
|
379
|
+
|
|
380
|
+
res = await db.get("games", undefined, {
|
|
381
|
+
sort: [
|
|
382
|
+
{ field: "timeStamp", order: "asc" },
|
|
383
|
+
{ field: "amount", order: "asc" },
|
|
384
|
+
],
|
|
385
|
+
limit: 5,
|
|
386
|
+
offset: 1,
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
res = await db.get(
|
|
390
|
+
"games",
|
|
391
|
+
{ type: "paid" },
|
|
392
|
+
{
|
|
393
|
+
sort: [
|
|
394
|
+
{ field: "amount", order: "desc" },
|
|
395
|
+
{ field: "timeStamp", order: "desc" },
|
|
396
|
+
],
|
|
397
|
+
}
|
|
398
|
+
);
|
|
399
|
+
|
|
400
|
+
res = await db.get(
|
|
401
|
+
"games",
|
|
402
|
+
{ amount: 400 },
|
|
403
|
+
{
|
|
404
|
+
apply: {
|
|
405
|
+
field: "timeStamp",
|
|
406
|
+
sort: "desc",
|
|
407
|
+
ineq: {
|
|
408
|
+
op: ">=",
|
|
409
|
+
value: 1,
|
|
263
410
|
},
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
411
|
+
},
|
|
412
|
+
sort: [
|
|
413
|
+
{ field: "amount", order: "asc" },
|
|
414
|
+
{ field: "timeStamp", order: "desc" },
|
|
415
|
+
],
|
|
416
|
+
limit: 2,
|
|
417
|
+
offset: 1,
|
|
418
|
+
}
|
|
419
|
+
);
|
|
267
420
|
|
|
268
|
-
|
|
269
|
-
|
|
421
|
+
res = await db.delete("games", { id: "IndVSPak1" });
|
|
422
|
+
res = await db.getOne("games", { id: "IndVSPak1" });
|
|
270
423
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
424
|
+
console.log("SQLite DB Tests Successfull");
|
|
425
|
+
console.log(db.metrics.getStatus());
|
|
426
|
+
checkTestsCompleted();
|
|
274
427
|
}
|
|
275
428
|
|
|
276
429
|
// testSqlite();
|
|
277
430
|
// testFireStore();
|
|
278
431
|
// testMongo();
|
|
279
432
|
// testOracleDb()
|
|
280
|
-
testMysqlDb()
|
|
433
|
+
// testMysqlDb();
|
|
434
|
+
testHanaDb();
|