multi-db-orm 2.1.6 → 2.1.7

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.
@@ -1,173 +1,173 @@
1
- const { MultiDbORM } = require("./multidb");
2
- var fs = require('fs')
3
-
4
- class SQLiteDB extends MultiDbORM {
5
-
6
- sqlite3
7
- dataMap = {
8
- "string": "TEXT",
9
- "number": "REAL"
10
- }
11
- constructor(filepath) {
12
- super()
13
- var sqlite3 = require('sqlite3')
14
- this.sqlite3 = sqlite3
15
- if (filepath == undefined)
16
- filepath = ':memory:'
17
- else {
18
- var currentPath = process.cwd();
19
- if (!fs.existsSync(filepath)) {
20
- filepath = currentPath + '/' + filepath
21
- }
22
- }
23
- this.db = new sqlite3.Database(filepath);
24
- console.log("SQLite3 Initialized");
25
- this.dbType = 'sqlite3'
26
- this.reqMade = 0
27
- }
28
-
29
- async run(query) {
30
- var db = this.db;
31
- var that = this
32
- this.reqMade++
33
- return new Promise(function (resolve, reject) {
34
- db.all(query, function (err, resp) {
35
- if (err)
36
- reject(err)
37
- else
38
- resolve(resp);
39
- if (that.loglevel > 3)
40
- console.log("Query ", query, ' -> ', resp)
41
- });
42
- })
43
-
44
-
45
- }
46
-
47
- async get(modelname, filter, options) {
48
- this.metrics.get(modelname, filter, options)
49
- var where = ''
50
- for (var key in filter) {
51
- where = where + `${key} = '${filter[key]}' AND `
52
- }
53
- where = where + " 1 ";
54
- var sort = "";
55
- if (options) {
56
- if (options.apply) {
57
- if (options.apply.ineq) {
58
- where = where + ` AND '${options.apply.field}' ${options.apply.ineq.op} '${options.apply.ineq.value}'`;
59
- }
60
- if (options.apply.sort) {
61
- sort = `ORDER BY ${options.apply.field} ${options.apply.sort}`
62
- }
63
- }
64
- else if (options.sort) {
65
- sort = `ORDER BY`
66
- for (let i = 0; i < options.sort.length; i++) {
67
- sort = sort + ` ${options.sort[i].field} ${options.sort[i].order}`;
68
- if (i < options.sort.length - 1) {
69
- sort = sort + ' , ';
70
- }
71
-
72
- }
73
- }
74
- }
75
- var query = `SELECT * FROM ${modelname} WHERE ${where} ${sort} ;`
76
- return await this.run(query)
77
- }
78
-
79
- async getOne(modelname, filter) {
80
- this.metrics.getOne(modelname, filter)
81
- var where = ''
82
- for (var key in filter) {
83
- where = where + `${key} = '${filter[key]}' AND `
84
- }
85
- where = where + " 1 ";
86
- var query = `SELECT * FROM ${modelname} WHERE ${where} LIMIT 1;`
87
- var row = await this.run(query)
88
- return row[0];
89
- }
90
-
91
- async create(modelname, sampleObject) {
92
- this.sync.create(modelname, sampleObject)
93
- this.metrics.create(modelname, sampleObject)
94
-
95
- var cols = ''
96
- for (var key in sampleObject) {
97
- var type = this.dataMap[typeof (sampleObject[key])] || 'TEXT'
98
- cols = cols + `${key} ${type},`
99
- }
100
- cols = cols.substring(0, cols.length - 1)
101
- var query = `CREATE TABLE IF NOT EXISTS ${modelname} (${cols});`
102
- try {
103
- return await this.run(query)
104
- } catch (err) {
105
- console.log(err)
106
- return undefined;
107
- }
108
- }
109
-
110
- async insert(modelname, object) {
111
- this.sync.insert(modelname, object)
112
- this.metrics.insert(modelname, object)
113
- var cols = ''
114
- var vals = ''
115
- for (var key in object) {
116
- cols = cols + `${key},`
117
- vals = vals + `'${object[key]}',`
118
- }
119
- cols = cols.substring(0, cols.length - 1)
120
- vals = vals.substring(0, vals.length - 1)
121
-
122
- var query = `INSERT INTO ${modelname} (${cols}) VALUES(${vals});`
123
-
124
- try {
125
- return await this.run(query)
126
- } catch (err) {
127
- if (err.message && err.message.indexOf('SQLITE_ERROR: no such table: ') > -1) {
128
- await this.create(modelname, object);
129
- return await this.run(query)
130
- }
131
- else
132
- throw err;
133
- }
134
- }
135
-
136
- async update(modelname, filter, object) {
137
- this.sync.update(modelname, filter, object)
138
- this.metrics.update(modelname, filter, object)
139
-
140
- var where = ''
141
- var vals = ''
142
- for (var key in filter) {
143
- where = where + `${key} = '${filter[key]}' AND `
144
- }
145
- for (var key in object) {
146
- vals = vals + ` ${key} = '${object[key]}',`
147
- }
148
- where = where + " 1 ";
149
- vals = vals.substring(0, vals.length - 1)
150
-
151
- var query = `UPDATE ${modelname} SET ${vals} WHERE ${where};`
152
- return await this.run(query)
153
- }
154
-
155
- async delete(modelname, filter) {
156
- this.sync.delete(modelname, filter)
157
- this.metrics.delete(modelname, filter)
158
-
159
- var where = ''
160
- for (var key in filter) {
161
- where = where + `${key} = '${filter[key]}' AND `
162
- }
163
- where = where + " 1 ";
164
- var query = `DELETE FROM ${modelname} WHERE ${where};`
165
- return await this.run(query)
166
- }
167
- }
168
-
169
-
170
-
171
- module.exports = {
172
- SQLiteDB
1
+ const { MultiDbORM } = require("./multidb");
2
+ var fs = require('fs')
3
+
4
+ class SQLiteDB extends MultiDbORM {
5
+
6
+ sqlite3
7
+ dataMap = {
8
+ "string": "TEXT",
9
+ "number": "REAL"
10
+ }
11
+ constructor(filepath) {
12
+ super()
13
+ var sqlite3 = require('sqlite3')
14
+ this.sqlite3 = sqlite3
15
+ if (filepath == undefined)
16
+ filepath = ':memory:'
17
+ else {
18
+ var currentPath = process.cwd();
19
+ if (!fs.existsSync(filepath)) {
20
+ filepath = currentPath + '/' + filepath
21
+ }
22
+ }
23
+ this.db = new sqlite3.Database(filepath);
24
+ console.log("SQLite3 Initialized");
25
+ this.dbType = 'sqlite3'
26
+ this.reqMade = 0
27
+ }
28
+
29
+ async run(query) {
30
+ var db = this.db;
31
+ var that = this
32
+ this.reqMade++
33
+ return new Promise(function (resolve, reject) {
34
+ db.all(query, function (err, resp) {
35
+ if (err)
36
+ reject(err)
37
+ else
38
+ resolve(resp);
39
+ if (that.loglevel > 3)
40
+ console.log("Query ", query, ' -> ', resp)
41
+ });
42
+ })
43
+
44
+
45
+ }
46
+
47
+ async get(modelname, filter, options) {
48
+ this.metrics.get(modelname, filter, options)
49
+ var where = ''
50
+ for (var key in filter) {
51
+ where = where + `${key} = '${filter[key]}' AND `
52
+ }
53
+ where = where + " 1 ";
54
+ var sort = "";
55
+ if (options) {
56
+ if (options.apply) {
57
+ if (options.apply.ineq) {
58
+ where = where + ` AND '${options.apply.field}' ${options.apply.ineq.op} '${options.apply.ineq.value}'`;
59
+ }
60
+ if (options.apply.sort) {
61
+ sort = `ORDER BY ${options.apply.field} ${options.apply.sort}`
62
+ }
63
+ }
64
+ else if (options.sort) {
65
+ sort = `ORDER BY`
66
+ for (let i = 0; i < options.sort.length; i++) {
67
+ sort = sort + ` ${options.sort[i].field} ${options.sort[i].order}`;
68
+ if (i < options.sort.length - 1) {
69
+ sort = sort + ' , ';
70
+ }
71
+
72
+ }
73
+ }
74
+ }
75
+ var query = `SELECT * FROM ${modelname} WHERE ${where} ${sort} ;`
76
+ return await this.run(query)
77
+ }
78
+
79
+ async getOne(modelname, filter) {
80
+ this.metrics.getOne(modelname, filter)
81
+ var where = ''
82
+ for (var key in filter) {
83
+ where = where + `${key} = '${filter[key]}' AND `
84
+ }
85
+ where = where + " 1 ";
86
+ var query = `SELECT * FROM ${modelname} WHERE ${where} LIMIT 1;`
87
+ var row = await this.run(query)
88
+ return row[0];
89
+ }
90
+
91
+ async create(modelname, sampleObject) {
92
+ this.sync.create(modelname, sampleObject)
93
+ this.metrics.create(modelname, sampleObject)
94
+
95
+ var cols = ''
96
+ for (var key in sampleObject) {
97
+ var type = this.dataMap[typeof (sampleObject[key])] || 'TEXT'
98
+ cols = cols + `${key} ${type},`
99
+ }
100
+ cols = cols.substring(0, cols.length - 1)
101
+ var query = `CREATE TABLE IF NOT EXISTS ${modelname} (${cols});`
102
+ try {
103
+ return await this.run(query)
104
+ } catch (err) {
105
+ console.log(err)
106
+ return undefined;
107
+ }
108
+ }
109
+
110
+ async insert(modelname, object) {
111
+ this.sync.insert(modelname, object)
112
+ this.metrics.insert(modelname, object)
113
+ var cols = ''
114
+ var vals = ''
115
+ for (var key in object) {
116
+ cols = cols + `${key},`
117
+ vals = vals + `'${object[key]}',`
118
+ }
119
+ cols = cols.substring(0, cols.length - 1)
120
+ vals = vals.substring(0, vals.length - 1)
121
+
122
+ var query = `INSERT INTO ${modelname} (${cols}) VALUES(${vals});`
123
+
124
+ try {
125
+ return await this.run(query)
126
+ } catch (err) {
127
+ if (err.message && err.message.indexOf('SQLITE_ERROR: no such table: ') > -1) {
128
+ await this.create(modelname, object);
129
+ return await this.run(query)
130
+ }
131
+ else
132
+ throw err;
133
+ }
134
+ }
135
+
136
+ async update(modelname, filter, object) {
137
+ this.sync.update(modelname, filter, object)
138
+ this.metrics.update(modelname, filter, object)
139
+
140
+ var where = ''
141
+ var vals = ''
142
+ for (var key in filter) {
143
+ where = where + `${key} = '${filter[key]}' AND `
144
+ }
145
+ for (var key in object) {
146
+ vals = vals + ` ${key} = '${object[key]}',`
147
+ }
148
+ where = where + " 1 ";
149
+ vals = vals.substring(0, vals.length - 1)
150
+
151
+ var query = `UPDATE ${modelname} SET ${vals} WHERE ${where};`
152
+ return await this.run(query)
153
+ }
154
+
155
+ async delete(modelname, filter) {
156
+ this.sync.delete(modelname, filter)
157
+ this.metrics.delete(modelname, filter)
158
+
159
+ var where = ''
160
+ for (var key in filter) {
161
+ where = where + `${key} = '${filter[key]}' AND `
162
+ }
163
+ where = where + " 1 ";
164
+ var query = `DELETE FROM ${modelname} WHERE ${where};`
165
+ return await this.run(query)
166
+ }
167
+ }
168
+
169
+
170
+
171
+ module.exports = {
172
+ SQLiteDB
173
173
  }
package/index.d.ts CHANGED
@@ -1,7 +1,7 @@
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';
6
- export { MySQLDB } from './engines/mysqldb';
7
-
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';
6
+ export { MySQLDB } from './engines/mysqldb';
7
+
package/index.js CHANGED
@@ -1,12 +1,12 @@
1
- const { MultiDBSafe, FireStoreDB, MongoDB, SQLiteDB, OracleDB, MySQLDB } = require("./databases");
2
- const { Sync } = require("./sync");
3
-
4
- module.exports = {
5
- MultiDBSafe,
6
- FireStoreDB,
7
- MongoDB,
8
- SQLiteDB,
9
- OracleDB,
10
- MySQLDB,
11
- Sync
1
+ const { MultiDBSafe, FireStoreDB, MongoDB, SQLiteDB, OracleDB, MySQLDB } = require("./databases");
2
+ const { Sync } = require("./sync");
3
+
4
+ module.exports = {
5
+ MultiDBSafe,
6
+ FireStoreDB,
7
+ MongoDB,
8
+ SQLiteDB,
9
+ OracleDB,
10
+ MySQLDB,
11
+ Sync
12
12
  }
package/migrate.sh CHANGED
@@ -1,12 +1,12 @@
1
- #!/bin/bash
2
- SRCDB=$1
3
- TARDB=$2
4
- DEDUPDB=$3
5
-
6
- echo "Source DB $SRCDB"
7
- echo "Target DB $TARDB"
8
- echo "Dedup DB $DEDUPDB"
9
-
10
- node backup.js $SRCDB dump.json
11
-
1
+ #!/bin/bash
2
+ SRCDB=$1
3
+ TARDB=$2
4
+ DEDUPDB=$3
5
+
6
+ echo "Source DB $SRCDB"
7
+ echo "Target DB $TARDB"
8
+ echo "Dedup DB $DEDUPDB"
9
+
10
+ node backup.js $SRCDB dump.json
11
+
12
12
  node restore.js dump.json $TARDB $DEDUPDB
package/package.json CHANGED
@@ -1,49 +1,49 @@
1
- {
2
- "name": "multi-db-orm",
3
- "version": "2.1.6",
4
- "description": "CRUD , Backup , Restore and Migration library for multiple databases",
5
- "main": "index.js",
6
- "dependencies": {
7
- "fs": "0.0.1-security"
8
- },
9
- "devDependencies": {
10
- "firebase-admin": "^9.3.0",
11
- "mongodb": "^3.6.3",
12
- "mysql": "^2.18.1",
13
- "oracle-instantclient": "^1.0.1",
14
- "oracledb": "^6.1.0",
15
- "sqlite3": "^5.0.0"
16
- },
17
- "scripts": {
18
- "release": "git add -A && git commit -m Update-Files | npm version patch && git add -A && git commit -m Update-Version && git push && npm publish",
19
- "postinstall": "node postinstall.js",
20
- "test": "node test/test.js"
21
- },
22
- "repository": {
23
- "type": "git",
24
- "url": "git+https://github.com/shiveshnavin/multi-db-orm.git"
25
- },
26
- "keywords": [
27
- "database",
28
- "backup",
29
- "restore",
30
- "db",
31
- "backup",
32
- "db",
33
- "restore",
34
- "mongodb",
35
- "firestore",
36
- "backup",
37
- "mongodb",
38
- "restore",
39
- "mongodb",
40
- "sql",
41
- "mysql"
42
- ],
43
- "author": "Shivesh Navin",
44
- "license": "MIT",
45
- "bugs": {
46
- "url": "https://github.com/shiveshnavin/multi-db-orm/issues"
47
- },
48
- "homepage": "https://github.com/shiveshnavin/multi-db-orm#readme"
49
- }
1
+ {
2
+ "name": "multi-db-orm",
3
+ "version": "2.1.7",
4
+ "description": "CRUD , Backup , Restore and Migration library for multiple databases",
5
+ "main": "index.js",
6
+ "dependencies": {
7
+ "fs": "0.0.1-security"
8
+ },
9
+ "devDependencies": {
10
+ "firebase-admin": "^9.3.0",
11
+ "mongodb": "^3.6.3",
12
+ "mysql": "^2.18.1",
13
+ "oracle-instantclient": "^1.0.1",
14
+ "oracledb": "^6.1.0",
15
+ "sqlite3": "^5.0.0"
16
+ },
17
+ "scripts": {
18
+ "release": "git add -A && git commit -m Update-Files | npm version patch && git add -A && git commit -m Update-Version && git push && npm publish",
19
+ "postinstall": "node postinstall.js",
20
+ "test": "node test/test.js"
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/shiveshnavin/multi-db-orm.git"
25
+ },
26
+ "keywords": [
27
+ "database",
28
+ "backup",
29
+ "restore",
30
+ "db",
31
+ "backup",
32
+ "db",
33
+ "restore",
34
+ "mongodb",
35
+ "firestore",
36
+ "backup",
37
+ "mongodb",
38
+ "restore",
39
+ "mongodb",
40
+ "sql",
41
+ "mysql"
42
+ ],
43
+ "author": "Shivesh Navin",
44
+ "license": "MIT",
45
+ "bugs": {
46
+ "url": "https://github.com/shiveshnavin/multi-db-orm/issues"
47
+ },
48
+ "homepage": "https://github.com/shiveshnavin/multi-db-orm#readme"
49
+ }
package/postinstall.js CHANGED
@@ -1,9 +1,9 @@
1
- console.log(`
2
- !!!!! MULTI DB ORM !!!
3
- !!!!! Make sure to install one of the target dependencies !!!!
4
- npm install --save mongodb
5
- npm install --save firebase-admin
6
- npm install --save sqlite3
7
- npm install --save oracledb oracle-instantclient
8
- !!!!! MULTI DB ORM !!!
1
+ console.log(`
2
+ !!!!! MULTI DB ORM !!!
3
+ !!!!! Make sure to install one of the target dependencies !!!!
4
+ npm install --save mongodb
5
+ npm install --save firebase-admin
6
+ npm install --save sqlite3
7
+ npm install --save oracledb oracle-instantclient
8
+ !!!!! MULTI DB ORM !!!
9
9
  `)