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