multi-db-orm 3.0.11 → 3.0.12
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/.github/workflows/npm-publish.yml +27 -24
- package/.vscode/launch.json +29 -29
- package/.vscode/settings.json +10 -10
- package/Dockerfile +17 -17
- package/README.md +274 -274
- package/backup.js +107 -107
- package/databases.js +19 -19
- package/engines/bigquerydb.d.ts +36 -36
- package/engines/bigquerydb.js +236 -236
- package/engines/firestoredb.d.ts +39 -39
- package/engines/firestoredb.js +227 -227
- package/engines/hanadb.d.ts +24 -24
- package/engines/hanadb.js +244 -244
- package/engines/index.d.ts +8 -8
- package/engines/metrics.d.ts +8 -8
- package/engines/metrics.js +77 -77
- package/engines/mongodb.d.ts +18 -18
- package/engines/mongodb.js +148 -148
- package/engines/multidb.d.ts +41 -41
- package/engines/multidb.js +67 -67
- package/engines/mysqldb.d.ts +25 -25
- package/engines/mysqldb.js +1 -0
- package/engines/oracledb.d.ts +24 -24
- package/engines/oracledb.js +250 -250
- package/engines/sqlitedb.d.ts +11 -11
- package/engines/sqlitedb.js +166 -166
- package/index.js +23 -23
- package/migrate.sh +11 -11
- package/package.json +2 -2
- package/postinstall.js +8 -8
- package/restore.js +102 -102
- package/sync.d.ts +5 -5
- package/sync.js +48 -48
- package/test/models.js +23 -23
- package/test/test.js +434 -434
package/README.md
CHANGED
|
@@ -1,274 +1,274 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
//
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
//
|
|
65
|
-
var
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
db
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
var
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
})
|
|
169
|
-
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
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
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
1
|
+
<h1 align="center">Multi DB Object Retrieval and Management</h1>
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
CRUD support for multiple SQL and NoSQL databases like firestore , MongoDB , SQlite, SAP Hana, and Oracle using a single interface, effectively decoupling database dependency from code.
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<img src="https://github.com/shiveshnavin/multi-db-orm/actions/workflows/npm-publish.yml/badge.svg" alt="Build">
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
<p align="center" style="flex-direction:row">
|
|
12
|
+
<img width="457" height="445" alt="image" src="https://github.com/user-attachments/assets/9b85fc44-877b-42b7-a8d2-a14a95c323c3" />
|
|
13
|
+
<img width="457" height="445" src="https://github.com/user-attachments/assets/14363e1d-f09e-43e3-a2e1-653ff54fcb59"
|
|
14
|
+
alt=" CRUD support for multiple SQL and NoSQL databases like firestore , MongoDB , SQlite, SAP Hana, and Oracle using a single interface, effectively decoupling database dependency from code." >
|
|
15
|
+
</p>
|
|
16
|
+
|
|
17
|
+
<br>
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
## 1. Usage
|
|
21
|
+
|
|
22
|
+
Supported databases:
|
|
23
|
+
|
|
24
|
+
1. MongoDB
|
|
25
|
+
2. Google Firestore
|
|
26
|
+
3. SQlite3
|
|
27
|
+
4. Oracle
|
|
28
|
+
5. MySQL
|
|
29
|
+
6. SAP HANA
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
### Install
|
|
33
|
+
|
|
34
|
+
The package is available on npm
|
|
35
|
+
`npm install multi-db-orm`
|
|
36
|
+
|
|
37
|
+
### Initialize
|
|
38
|
+
|
|
39
|
+
Install the target optional database dependencies based on your usage
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
npm install --save mongodb
|
|
43
|
+
npm install --save firebase-admin
|
|
44
|
+
npm install --save sqlite3
|
|
45
|
+
npm install --save oracledb oracle-instantclient
|
|
46
|
+
npm install --save mysql
|
|
47
|
+
npm install --save @sap/hana-client
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Configure the database
|
|
51
|
+
|
|
52
|
+
```
|
|
53
|
+
const { MultiDbORM, FireStoreDB, MongoDB, SQLiteDB, MySQLDB, Sync } = require("multi-db-orm");
|
|
54
|
+
|
|
55
|
+
// You can choose to initialize any or all of the supported databases in singe app
|
|
56
|
+
|
|
57
|
+
// Firestore
|
|
58
|
+
var firebasedb = new FireStoreDB(require("/path/to/serviceAccountFile.json"));
|
|
59
|
+
Note: If you use firebase DB then keys with undefined values will be set to null while insert or update as firebase dosent support undefined values
|
|
60
|
+
|
|
61
|
+
// Sqlite
|
|
62
|
+
var sqlitedb = new SQLiteDB("/path/to/mydatabase.db"); // if no path is passed , an in-memory db is used
|
|
63
|
+
|
|
64
|
+
// MongoDB
|
|
65
|
+
var mongodb = new MongoDB("mongodb+srv://username:PassW0rd@host.server.net/my_db_name","my_db_name");
|
|
66
|
+
|
|
67
|
+
// OracleDB
|
|
68
|
+
// Download client credentials (Wallet) and extract to /path/to/your/extracted/wallet-dir
|
|
69
|
+
// Oracle field names are case insensetive, Always name your fields in snake case
|
|
70
|
+
var oracledb = new OracleDB({
|
|
71
|
+
username: 'your-username',
|
|
72
|
+
password: 'your-password',
|
|
73
|
+
wallet_dir: '/path/to/your/extracted/wallet-dir',
|
|
74
|
+
net_service_name: 'connstring-high', //get any one from tnsnames.ora
|
|
75
|
+
connection_pool_name:'your-conn-pool-name' //optional
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// MySQLDB
|
|
79
|
+
var mysqldb = new MySQLDB({
|
|
80
|
+
"host": "db.mysql.com",
|
|
81
|
+
"port": "3306",
|
|
82
|
+
"username": "test",
|
|
83
|
+
"password": "Password@123",
|
|
84
|
+
"database": "test"
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// HanaDB
|
|
88
|
+
var mysqldb = new HanaDB({
|
|
89
|
+
"host": "db.hana.com",
|
|
90
|
+
"port": "443",
|
|
91
|
+
"username": "test",
|
|
92
|
+
"password": "Password@123"
|
|
93
|
+
});
|
|
94
|
+
var db = firebasedb;
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Usage
|
|
98
|
+
|
|
99
|
+
You can perform Create,Insert,Get,GetOne,Update,Delete queries . You only need to base your code on the interface MultiDbORM
|
|
100
|
+
|
|
101
|
+
<b>Note:</b>
|
|
102
|
+
|
|
103
|
+
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.
|
|
104
|
+
2. All the functions are `async` i.e. they return a promise so you can use `..then()` or `await`
|
|
105
|
+
|
|
106
|
+
#### Create
|
|
107
|
+
|
|
108
|
+
You can create a Table from a sample object in SQlite . NoSQL databases need not create a entity explicitly .
|
|
109
|
+
|
|
110
|
+
```
|
|
111
|
+
// db.create(modelname,sampleObject)
|
|
112
|
+
|
|
113
|
+
var db = new SQLiteDB("/path/to/mydatabase.db"); // if no path is passed , an in-memory db is used
|
|
114
|
+
db.create('game',aSampleGameObject);
|
|
115
|
+
// creates a game table in db .
|
|
116
|
+
// The fields and their data types are extracted from aSampleGameObject but aSampleGameObject is not saved in db
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
#### Insert
|
|
120
|
+
|
|
121
|
+
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 .
|
|
122
|
+
|
|
123
|
+
```
|
|
124
|
+
// db.insert(modelname,object)
|
|
125
|
+
|
|
126
|
+
var res = await db.insert('game', gm);
|
|
127
|
+
|
|
128
|
+
OR
|
|
129
|
+
|
|
130
|
+
db.insert('game', gm).then(response=>{
|
|
131
|
+
console.log(response);
|
|
132
|
+
}).catch(err=>{
|
|
133
|
+
console.log(err);
|
|
134
|
+
});
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
#### Get
|
|
138
|
+
|
|
139
|
+
The code will retrieve object(s) from the database .
|
|
140
|
+
|
|
141
|
+
```
|
|
142
|
+
// db.get(modelname,filter)
|
|
143
|
+
|
|
144
|
+
var games = await db.get('game', { amount: 19.00 , type: 'Hockey' });
|
|
145
|
+
// returns an array of games having amount = 19.00 and type = Hockey
|
|
146
|
+
|
|
147
|
+
var oneGame = await db.getOne('game', { country: 'India' }); // returns single game having country = 19.00
|
|
148
|
+
|
|
149
|
+
var oneGameFr = await db.getOne('game', { country: 'India' },"32g274hfn48vnf"));
|
|
150
|
+
// Only for firestore if docPath is passed optionally , filter is ignored and the object is returned
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
#### Get with Range and sort
|
|
154
|
+
|
|
155
|
+
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.
|
|
156
|
+
|
|
157
|
+
```
|
|
158
|
+
var gamesFr = await mongodb.get('games', { amount: 400 }, {
|
|
159
|
+
apply: {
|
|
160
|
+
field: 'timeStamp',
|
|
161
|
+
sort: 'desc',
|
|
162
|
+
ineq: {
|
|
163
|
+
op: '>=',
|
|
164
|
+
value: 1650398288
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
limit: 2, offset: 1
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
#### Get with Sort , Limit and Offset
|
|
173
|
+
|
|
174
|
+
The code will retrieve object(s) from the database with sort (asc or desc) , limit and offset.
|
|
175
|
+
|
|
176
|
+
```
|
|
177
|
+
var oneGameFr = await mongodb.get('game',
|
|
178
|
+
{ country: 'India' },
|
|
179
|
+
{ sort: [{ field: 'timeStamp', order: 'asc' },
|
|
180
|
+
{ field: 'amount', order: 'asc' }],
|
|
181
|
+
limit: 5, offset: 1
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
Note :
|
|
187
|
+
|
|
188
|
+
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 .
|
|
189
|
+
2. sort is not applicable when using apply and will be ignored
|
|
190
|
+
|
|
191
|
+
#### Update
|
|
192
|
+
|
|
193
|
+
The code will update objects in the database .
|
|
194
|
+
|
|
195
|
+
```
|
|
196
|
+
// db.update(modelname,filter,object)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
var result = await db.update('game', { amount: 19.00 , type: 'Hockey' },{status : "cancelled",closingTime:Date.now()});
|
|
200
|
+
// updates all the games having amount=19.00 and type=Hockey to status=cancelled
|
|
201
|
+
// and closingTime as current time while other fields are not touched
|
|
202
|
+
|
|
203
|
+
var result_fire = await db.update('game', { amount: 19.00 , type: 'Hockey' },"32g274hfn48vnf");
|
|
204
|
+
/* Only for firestore with optional docPath , it will update collection("game").doc("32g274hfn48vnf") .
|
|
205
|
+
The filters amount and type are ignored when docPath is passed */
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
#### Delete
|
|
209
|
+
|
|
210
|
+
The code will delete objects in the database .
|
|
211
|
+
|
|
212
|
+
```
|
|
213
|
+
// db.delete(modelname,filter)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
var result = await db.delete('game', { amount: 19.00 , type: 'Hockey' });
|
|
217
|
+
// deletes all the games having amount=19.00 and type=Hockey
|
|
218
|
+
|
|
219
|
+
var result_fire = await db.delete('game', { amount: 19.00 , type: 'Hockey' },"32g274hfn48vnf");
|
|
220
|
+
/* Only for firestore with optional docPath , it will delete collection("game").doc("32g274hfn48vnf") .
|
|
221
|
+
The filters amount and type are ignored when docPath is passed */
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
## 2. Migration
|
|
225
|
+
|
|
226
|
+
#### Mongo DB
|
|
227
|
+
|
|
228
|
+
Pass your SOURCE and TARGET DB credentials
|
|
229
|
+
|
|
230
|
+
Using Docker
|
|
231
|
+
|
|
232
|
+
```
|
|
233
|
+
docker run shiveshnavin/multi-db-safe 'mongodb://username:paswd@dbhost:13873/sourceDBName' 'mongodb://username:paswd@dbhost:13873/targetDBName' 0
|
|
234
|
+
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Using Shell
|
|
238
|
+
|
|
239
|
+
```
|
|
240
|
+
./migrate.sh 'mongodb://username:paswd@dbhost:13873/sourceDBName' 'mongodb://username:paswd@dbhost:13873/targetDBName' 0
|
|
241
|
+
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
Note : To run deduplication as well set 1 instead of 0 at last cmd line argument
|
|
245
|
+
|
|
246
|
+
## 3. Backup
|
|
247
|
+
|
|
248
|
+
#### Mongo DB
|
|
249
|
+
|
|
250
|
+
```
|
|
251
|
+
node backup.js 'mongodb://username:paswd@dbhost:13873/sourceDBName'
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
This will create a dump file in dumps/
|
|
255
|
+
|
|
256
|
+
## 4. Restore
|
|
257
|
+
|
|
258
|
+
#### Mongo DB
|
|
259
|
+
|
|
260
|
+
```
|
|
261
|
+
Without Deduplication : node restore.js dumpfile.json 'mongodb://username:paswd@dbhost:13873/targetDBName'
|
|
262
|
+
|
|
263
|
+
With Deduplication : node restore.js dumpfile.json 'mongodb://username:paswd@dbhost:13873/targetDBName' 1
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## 5. Work in Progress
|
|
267
|
+
|
|
268
|
+
Working on enhancing the tool with below features in progress. Feel free to contribute and create a PR .
|
|
269
|
+
|
|
270
|
+
- [ ] Add Backup support for other databases
|
|
271
|
+
- [ ] Add Restore support for other databases
|
|
272
|
+
- [x] Range Operations like `>=` `<=`
|
|
273
|
+
- [ ] Aggregations
|
|
274
|
+
- [ ] InsertMany
|