multi-db-orm 2.1.1 → 2.1.3

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
@@ -10,6 +10,7 @@ Supported databases:
10
10
  2. Google Firestore
11
11
  3. SQlite3
12
12
  4. Oracle
13
+ 4. MySQL
13
14
 
14
15
  ### Install
15
16
  The package is available on npm
@@ -24,11 +25,12 @@ npm install --save mongodb
24
25
  npm install --save firebase-admin
25
26
  npm install --save sqlite3
26
27
  npm install --save oracledb oracle-instantclient
28
+ npm install --save mysql
27
29
  ```
28
30
 
29
31
  Configure the database
30
32
  ```
31
- const { MultiDbORM, FireStoreDB, MongoDB, SQLiteDB, Sync } = require("multi-db-orm");
33
+ const { MultiDbORM, FireStoreDB, MongoDB, SQLiteDB, MySQLDB, Sync } = require("multi-db-orm");
32
34
 
33
35
  // You can choose to initialize any or all of the supported databases in singe app
34
36
 
@@ -53,6 +55,15 @@ var oracledb = new OracleDB({
53
55
  connection_pool_name:'your-conn-pool-name' //optional
54
56
  });
55
57
 
58
+ // MySQLDB
59
+ var mysqldb = new MySQLDB({
60
+ "host": "db.mysql.com",
61
+ "port": "3306",
62
+ "username": "test",
63
+ "password": "Password@123",
64
+ "database": "test"
65
+ });
66
+
56
67
  var db = firebasedb;
57
68
  ```
58
69
 
@@ -0,0 +1,39 @@
1
+ import { MultiDbORM, MultiDbORMOptions } from './MultiDbORM';
2
+
3
+ interface ServiceAccount {
4
+ type: string;
5
+ project_id: string;
6
+ private_key_id: string;
7
+ private_key: string;
8
+ client_email: string;
9
+ client_id: string;
10
+ auth_uri: string;
11
+ token_uri: string;
12
+ auth_provider_x509_cert_url: string;
13
+ client_x509_cert_url: string;
14
+ }
15
+
16
+ export declare class FireStoreDB extends MultiDbORM {
17
+ admin: any; // Replace 'any' with actual Firebase Admin library types
18
+ serviceAccount: ServiceAccount;
19
+
20
+ constructor(serviceAccount: ServiceAccount, appname?: string);
21
+
22
+ async run(query: string): Promise<any>;
23
+
24
+ attachOptions(modelref: any, options: any): any;
25
+
26
+ async _get(modelname: string, filter: any, options?: any): Promise<any>;
27
+
28
+ async get(modelname: string, filter: any, options?: any): Promise<any>;
29
+
30
+ async getOne(modelname: string, filter: any, id?: string, options?: any): Promise<any>;
31
+
32
+ async create(modelname: string, sampleObject: any): Promise<any>;
33
+
34
+ async insert(modelname: string, object: any, id?: string): Promise<any>;
35
+
36
+ async update(modelname: string, filter: any, object: any, id?: string): Promise<any>;
37
+
38
+ async delete(modelname: string, filter: any, id?: string): Promise<any>;
39
+ }
@@ -0,0 +1,5 @@
1
+ export { MultiDbORM } from './engines/multidb';
2
+ export { SQLiteDB } from './engines/sqlitedb';
3
+ export { MongoDB } from './engines/mongodb';
4
+ export { FireStoreDB } from './engines/firestoredb';
5
+ export { OracleDB } from './engines/oracledb';
@@ -0,0 +1,9 @@
1
+ export class Metrics {
2
+ constructor(loglevel: number);
3
+ get(modelname: string, filter: any, options: any): void;
4
+ getOne(modelname: string, filter: any): void;
5
+ create(modelname: string, sampleObject: any): void;
6
+ insert(modelname: string, object: any): void;
7
+ update(modelname: string, filter: any, object: any): void;
8
+ delete(modelname: string, filter: any): void;
9
+ }
@@ -0,0 +1,18 @@
1
+ import MongoClient from 'mongodb/lib/mongo_client';
2
+ import { MultiDbORM, MultiDbORMOptions } from './MultiDbORM';
3
+
4
+ export declare class MongoDB extends MultiDbORM {
5
+ mongodbname: string;
6
+ dbc: typeof import('mongodb');
7
+ db: typeof import('mongodb');
8
+ client: MongoClient;
9
+ url: string;
10
+
11
+ constructor(secureUrl: string, mongodbname: string);
12
+
13
+ async _close(): Promise<void>;
14
+
15
+ async _connect(): Promise<void>;
16
+
17
+ async run(query: string): Promise<any>;
18
+ }
@@ -0,0 +1,29 @@
1
+ export class MultiDbORM {
2
+ db: any;
3
+ dbType: string;
4
+ reqMade: number;
5
+ lastQLatency: any;
6
+ loglevel: number;
7
+ sync: import('../sync').Sync;
8
+ metrics: import('./metrics').Metrics;
9
+
10
+ constructor(db: any);
11
+
12
+ async connect(): Promise<void>;
13
+
14
+ setdb(db: any): void;
15
+
16
+ getdb(): any;
17
+
18
+ async get(modelname: string, filter: Record<string, any>): Promise<void>;
19
+
20
+ async getOne(modelname: string, filter: Record<string, any>): Promise<void>;
21
+
22
+ async create(modelname: string, object: Record<string, any>): Promise<void>;
23
+
24
+ async insert(modelname: string, object: Record<string, any>): Promise<void>;
25
+
26
+ async update(modelname: string, filter: Record<string, any>, object: Record<string, any>): Promise<void>;
27
+
28
+ async delete(modelname: string, filter: Record<string, any>): Promise<void>;
29
+ }
@@ -0,0 +1,42 @@
1
+ import { MultiDbORM } from './multidb';
2
+
3
+ declare module 'mysql' {
4
+ import { EventEmitter } from 'events';
5
+
6
+ export interface ConnectionConfig {
7
+ host: string;
8
+ port: number;
9
+ user: string;
10
+ password: string;
11
+ database?: string;
12
+ }
13
+
14
+ export class Connection extends EventEmitter {
15
+ constructor(config: ConnectionConfig);
16
+ connect(): void;
17
+ end(): void;
18
+ query(query: string, callback: (error: Error | null, results: any, fields: any) => void): void;
19
+ }
20
+ }
21
+
22
+ export class MySQLDB extends MultiDbORM {
23
+ db: typeof import('mysql')
24
+ mysql: typeof import('mysql');
25
+ connection: import('mysql').Connection;
26
+
27
+ constructor(credentials: {
28
+ host: string;
29
+ port: string;
30
+ username: string;
31
+ password: string;
32
+ database: string;
33
+ connectionLimit?: Number;
34
+ connectTimeout?: Number;
35
+ acquireTimeout?: Number;
36
+ timeout?: Number;
37
+ });
38
+
39
+ run(query: string): Promise<any[]>;
40
+
41
+ closePool(): void;
42
+ }
@@ -0,0 +1,212 @@
1
+ const mysql = require('mysql');
2
+ const { MultiDbORM } = require('./multidb');
3
+
4
+ class MySQLDB extends MultiDbORM {
5
+
6
+ mysql;
7
+ pool;
8
+ dataMap = {
9
+ "id": "VARCHAR(50) NOT NULL PRIMARY KEY",
10
+ "string": "VARCHAR(4000)",
11
+ "number": "DOUBLE",
12
+ "boolean": "BOOL",
13
+ "array": "TEXT",
14
+ "object": "JSON"
15
+ };
16
+
17
+ constructor(credentials) {
18
+ super();
19
+ this.mysql = mysql;
20
+ this.pool = mysql.createPool({
21
+ connectionLimit: credentials.connectionLimit || 10,
22
+ host: credentials.host,
23
+ port: credentials.port,
24
+ user: credentials.username,
25
+ password: credentials.password,
26
+ database: credentials.database,
27
+ waitForConnections: true,
28
+ queueLimit: 0,
29
+ connectTimeout: credentials.connectTimeout || 10000,
30
+ acquireTimeout: credentials.acquireTimeout || 10000,
31
+ timeout: credentials.timeout || 60000
32
+ });
33
+
34
+ this.pool.on('connection', (connection) => {
35
+ if (this.loglevel > 1)
36
+ console.log('MySQLDB: New connection acquired');
37
+ });
38
+
39
+ this.pool.on('error', (err) => {
40
+ if (this.loglevel > 0)
41
+ console.error('MySQLDB: Pool error:', err);
42
+ });
43
+
44
+ this.dbType = 'mysql';
45
+ this.reqMade = 0;
46
+ }
47
+
48
+ async run(query) {
49
+ var that = this;
50
+ this.reqMade++;
51
+ return new Promise(function (resolve, reject) {
52
+ that.pool.query(query, function (err, results) {
53
+ if (err) {
54
+ reject(err);
55
+ } else {
56
+ resolve(results);
57
+ }
58
+ if (that.loglevel > 3) {
59
+ console.log("Query ", query, ' -> ', results);
60
+ }
61
+ });
62
+ });
63
+ }
64
+
65
+ async get(modelname, filter, options) {
66
+ this.metrics.get(modelname, filter, options);
67
+ var where = '';
68
+ for (var key in filter) {
69
+ where = where + `${key} = '${filter[key]}' AND `;
70
+ }
71
+ where = where + " 1 ";
72
+ var sort = "";
73
+ if (options) {
74
+ if (options.apply) {
75
+ if (options.apply.ineq) {
76
+ where = where + ` AND '${options.apply.field}' ${options.apply.ineq.op} '${options.apply.ineq.value}'`;
77
+ }
78
+ if (options.apply.sort) {
79
+ sort = `ORDER BY ${options.apply.field} ${options.apply.sort}`;
80
+ }
81
+ } else if (options.sort) {
82
+ sort = `ORDER BY`;
83
+ for (let i = 0; i < options.sort.length; i++) {
84
+ sort = sort + ` ${options.sort[i].field} ${options.sort[i].order}`;
85
+ if (i < options.sort.length - 1) {
86
+ sort = sort + ' , ';
87
+ }
88
+ }
89
+ }
90
+ }
91
+ var query = `SELECT * FROM ${modelname} WHERE ${where} ${sort} ;`;
92
+ return await this.run(query);
93
+ }
94
+
95
+ async getOne(modelname, filter) {
96
+ this.metrics.getOne(modelname, filter);
97
+ var where = '';
98
+ for (var key in filter) {
99
+ where = where + `${key} = '${filter[key]}' AND `;
100
+ }
101
+ where = where + " 1 ";
102
+ var query = `SELECT * FROM ${modelname} WHERE ${where} LIMIT 1;`;
103
+ var row = await this.run(query);
104
+ return row[0];
105
+ }
106
+
107
+ async create(modelname, sampleObject) {
108
+ this.sync.create(modelname, sampleObject);
109
+ this.metrics.create(modelname, sampleObject);
110
+
111
+ var cols = '';
112
+ for (var key in sampleObject) {
113
+ var type = this.dataMap[typeof (sampleObject[key])] || 'TEXT';
114
+ cols = cols + `${key} ${type},`;
115
+ }
116
+ cols = cols.substring(0, cols.length - 1);
117
+ var query = `CREATE TABLE IF NOT EXISTS ${modelname} (${cols});`;
118
+ try {
119
+ return await this.run(query);
120
+ } catch (err) {
121
+ if (this.loglevel > 0)
122
+ console.log(err);
123
+ throw err
124
+ }
125
+ }
126
+
127
+ async insert(modelname, object) {
128
+ this.sync.insert(modelname, object);
129
+ this.metrics.insert(modelname, object);
130
+ var cols = '';
131
+ var vals = '';
132
+ for (var key in object) {
133
+ cols = cols + `${key},`;
134
+ let val = object[key]
135
+ if (typeof val == 'object')
136
+ val = JSON.stringify(object[key])
137
+ if (typeof val == "undefined")
138
+ vals = vals + `Null,`;
139
+ else if (typeof val == 'boolean')
140
+ vals = vals + `${val},`;
141
+ else
142
+ vals = vals + `'${val}',`;
143
+ }
144
+ cols = cols.substring(0, cols.length - 1);
145
+ vals = vals.substring(0, vals.length - 1);
146
+
147
+ var query = `INSERT INTO ${modelname} (${cols}) VALUES(${vals});`;
148
+
149
+ try {
150
+ return await this.run(query);
151
+ } catch (err) {
152
+ if (err.code && err.code === 'ER_NO_SUCH_TABLE') {
153
+ await this.create(modelname, object);
154
+ return await this.run(query);
155
+ } else {
156
+ throw err;
157
+ }
158
+ }
159
+ }
160
+
161
+ async update(modelname, filter, object) {
162
+ this.sync.update(modelname, filter, object);
163
+ this.metrics.update(modelname, filter, object);
164
+
165
+ var where = '';
166
+ var vals = '';
167
+ for (var key in filter) {
168
+ where = where + `${key} = '${filter[key]}' AND `;
169
+ }
170
+ for (var key in object) {
171
+ vals = vals + ` ${key} = '${object[key]}',`;
172
+ }
173
+ where = where + " 1 ";
174
+ vals = vals.substring(0, vals.length - 1);
175
+
176
+ var query = `UPDATE ${modelname} SET ${vals} WHERE ${where};`;
177
+ return await this.run(query);
178
+ }
179
+
180
+ async delete(modelname, filter) {
181
+ this.sync.delete(modelname, filter);
182
+ this.metrics.delete(modelname, filter);
183
+
184
+ var where = '';
185
+ for (var key in filter) {
186
+ where = where + `${key} = '${filter[key]}' AND `;
187
+ }
188
+ where = where + " 1 ";
189
+ var query = `DELETE FROM ${modelname} WHERE ${where};`;
190
+ return await this.run(query);
191
+ }
192
+
193
+ closePool() {
194
+ return new Promise((res, rej) => {
195
+ this.pool.end((err) => {
196
+ if (err) {
197
+ rej(err)
198
+ if (this.loglevel > 1)
199
+ console.error('Error closing connection pool:', err);
200
+ } else {
201
+ res()
202
+ if (this.loglevel > 1)
203
+ console.log('MySQLDB: Connection pool closed');
204
+ }
205
+ });
206
+ })
207
+ }
208
+ }
209
+
210
+ module.exports = {
211
+ MySQLDB
212
+ };
@@ -0,0 +1,24 @@
1
+ import { MultiDbORM, MultiDbORMOptions } from './MultiDbORM';
2
+
3
+ export interface OracleDBConfig {
4
+ username: string;
5
+ password: string;
6
+ net_service_name: string;
7
+ wallet_dir: string;
8
+ connection_pool_name?: string;
9
+ wallet_password?: string;
10
+ lib_dir?: string;
11
+ }
12
+
13
+ export declare class OracleDB extends MultiDbORM {
14
+ db: typeof import('oracledb')
15
+ connection_pool_name: string;
16
+ schema: string;
17
+ pool_creation: Promise<any>;
18
+
19
+ constructor(config: OracleDBConfig);
20
+
21
+ async connect(): Promise<void>;
22
+
23
+ async run(query: string): Promise<any>;
24
+ }
@@ -28,17 +28,18 @@ class OracleDB extends MultiDbORM {
28
28
  net_service_name,
29
29
  wallet_dir,
30
30
  connection_pool_name,
31
- wallet_password }) {
31
+ wallet_password,
32
+ lib_dir }) {
32
33
  super()
33
34
  const oracledb = require('oracledb')
34
35
  const oracleInstantClient = require("oracle-instantclient");
35
36
  if (!wallet_password)
36
37
  wallet_password = password
37
- process.env.LD_LIBRARY_PATH = oracleInstantClient.path
38
+ process.env.LD_LIBRARY_PATH = lib_dir || oracleInstantClient.path
38
39
  process.env.TS_ADMIN = wallet_dir
39
40
  oracledb.initOracleClient({
40
41
  configDir: wallet_dir,
41
- libDir: oracleInstantClient.path
42
+ libDir: lib_dir || oracleInstantClient.path
42
43
  })
43
44
  oracledb.autoCommit = true;
44
45
  oracledb.outFormat = oracledb.OUT_FORMAT_OBJECT
@@ -0,0 +1,11 @@
1
+ import { MultiDbORM, MultiDbORMOptions } from './MultiDbORM';
2
+
3
+ export declare class SQLiteDB extends MultiDbORM {
4
+ sqlite3: typeof import('sqlite3');
5
+ db: typeof import('sqlite3')
6
+
7
+ constructor(filepath?: string);
8
+
9
+ async run(query: string): Promise<any>;
10
+
11
+ }
package/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export { MultiDbORM } from './engines/multidb';
2
+ export { SQLiteDB } from './engines/sqlitedb';
3
+ export { MongoDB } from './engines/mongodb';
4
+ export { FireStoreDB } from './engines/firestoredb';
5
+ export { OracleDB } from './engines/oracledb';
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "multi-db-orm",
3
- "version": "2.1.1",
3
+ "version": "2.1.3",
4
4
  "description": "CRUD , Backup , Restore and Migration library for multiple databases",
5
5
  "main": "index.js",
6
6
  "dependencies": {
7
- "fs": "0.0.1-security",
8
- "node-firestore-import-export": "^1.1.0"
7
+ "fs": "0.0.1-security"
9
8
  },
10
9
  "devDependencies": {
11
10
  "firebase-admin": "^9.3.0",
12
11
  "mongodb": "^3.6.3",
12
+ "mysql": "^2.18.1",
13
13
  "oracle-instantclient": "^1.0.1",
14
14
  "oracledb": "^6.1.0",
15
15
  "sqlite3": "^5.0.0"
@@ -45,4 +45,4 @@
45
45
  "url": "https://github.com/shiveshnavin/multi-db-orm/issues"
46
46
  },
47
47
  "homepage": "https://github.com/shiveshnavin/multi-db-orm#readme"
48
- }
48
+ }
package/sync.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export class Sync {
2
+ create(modelname: string, sampleObject: any): void;
3
+ insert(modelname: string, object: any): void;
4
+ update(modelname: string, filter: any, object: any): void;
5
+ delete(modelname: string, filter: any): void;
6
+ }
package/test/test.js CHANGED
@@ -1,10 +1,11 @@
1
1
  const { Game } = require("./models");
2
2
  const { MultiDBSafe, FireStoreDB, MongoDB, SQLiteDB, Sync, OracleDB } = require("../index");
3
- const path = require('path')
3
+ const path = require('path');
4
+ const { MySQLDB } = require("../engines/mysqldb");
4
5
 
5
6
 
6
7
 
7
- var testCount = 3
8
+ var testCount = 0
8
9
  function checkTestsCompleted() {
9
10
  if (--testCount <= 0) {
10
11
  process.exit(0)
@@ -199,34 +200,59 @@ async function testOracleDb() {
199
200
  }
200
201
  oracledb.loglevel = 1
201
202
  await oracledb.connect()
202
- var res = await oracledb.create('games', gm);
203
+ test(oracledb)
204
+ }
205
+
206
+
207
+
208
+ async function testMysqlDb() {
209
+ let creds = require('../creds/mysql.json')
210
+ var mysqldb = new MySQLDB(creds);
211
+ console.log(mysqldb.metrics.getStatus())
212
+ mysqldb.loglevel = 1
213
+ await mysqldb.connect()
214
+ test(mysqldb)
215
+ }
216
+
217
+
218
+ async function test(db) {
219
+ testCount++
220
+ var gm = new Game('IndVSPak', Date.now(), 'Dhoni', 67.33, 'paid')
221
+ gm.completed = true
222
+ gm.runs = ['a', 'b']
223
+ gm.extras = {
224
+ location: 'India',
225
+ raining: false,
226
+ match: 1
227
+ }
228
+ var res = await db.create('games', gm);
203
229
  gm.id = Date.now()
204
- res = await oracledb.insert('games', gm);
230
+ res = await db.insert('games', gm);
205
231
  gm.id = Date.now()
206
232
 
207
- res = await oracledb.insert('games', gm);
233
+ res = await db.insert('games', gm);
208
234
  gm.amount = 1
209
235
  gm.id = Date.now()
210
236
 
211
- res = await oracledb.insert('games', gm);
212
- res = await oracledb.get('games', { amount: 1 });
213
- res = await oracledb.getOne('games', { amount: 1 });
214
- res = await oracledb.update('games', { amount: 1 }, { userid: 'xxxx' });
215
- res = await oracledb.getOne('games', { userid: 'xxxx' });
237
+ res = await db.insert('games', gm);
238
+ res = await db.get('games', { amount: 1 });
239
+ res = await db.getOne('games', { amount: 1 });
240
+ res = await db.update('games', { amount: 1 }, { userid: 'xxxx' });
241
+ res = await db.getOne('games', { userid: 'xxxx' });
216
242
 
217
- res = await oracledb.insert('games', new Game('IndVSPak1', Date.now(), 'Dhoni', 100, 'free'));
243
+ res = await db.insert('games', new Game('IndVSPak1', Date.now(), 'Dhoni', 100, 'free'));
218
244
 
219
- res = await oracledb.insert('games', new Game('IndVSPak2', Date.now(), 'Dhoni', 200, 'free'));
245
+ res = await db.insert('games', new Game('IndVSPak2', Date.now(), 'Dhoni', 200, 'free'));
220
246
 
221
- res = await oracledb.insert('games', new Game('IndVSPak3', Date.now(), 'Dhoni', 300, 'paid'));
247
+ res = await db.insert('games', new Game('IndVSPak3', Date.now(), 'Dhoni', 300, 'paid'));
222
248
 
223
- res = await oracledb.insert('games', new Game('IndVSPak4', Date.now(), 'Dhoni', 400, 'paid'));
249
+ res = await db.insert('games', new Game('IndVSPak4', Date.now(), 'Dhoni', 400, 'paid'));
224
250
 
225
- res = await oracledb.get('games', undefined, { sort: [{ field: 'timeStamp', order: 'asc' }, { field: 'amount', order: 'asc' }], limit: 5, offset: 1 })
251
+ res = await db.get('games', undefined, { sort: [{ field: 'timeStamp', order: 'asc' }, { field: 'amount', order: 'asc' }], limit: 5, offset: 1 })
226
252
 
227
- res = await oracledb.get('games', { type: 'paid' }, { sort: [{ field: 'amount', order: 'desc' }, { field: 'timeStamp', order: 'desc' }] })
253
+ res = await db.get('games', { type: 'paid' }, { sort: [{ field: 'amount', order: 'desc' }, { field: 'timeStamp', order: 'desc' }] })
228
254
 
229
- res = await oracledb.get('games', { amount: 400 }, {
255
+ res = await db.get('games', { amount: 400 }, {
230
256
  apply: {
231
257
  field: 'timeStamp',
232
258
  sort: 'desc',
@@ -239,16 +265,16 @@ async function testOracleDb() {
239
265
  limit: 2, offset: 1
240
266
  })
241
267
 
242
- res = await oracledb.delete('games', { id: 'IndVSPak1' });
243
- res = await oracledb.getOne('games', { id: 'IndVSPak1' });
268
+ res = await db.delete('games', { id: 'IndVSPak1' });
269
+ res = await db.getOne('games', { id: 'IndVSPak1' });
244
270
 
245
271
  console.log('SQLite DB Tests Successfull')
246
- console.log(oracledb.metrics.getStatus())
272
+ console.log(db.metrics.getStatus())
247
273
  checkTestsCompleted();
248
274
  }
249
275
 
250
-
251
276
  // testSqlite();
252
277
  // testFireStore();
253
278
  // testMongo();
254
- // testOracleDb()
279
+ // testOracleDb()
280
+ testMysqlDb()