multi-db-orm 1.3.0 → 1.3.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.
@@ -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.js CHANGED
@@ -1,10 +1,10 @@
1
- const { MultiDBSafe,FireStoreDB,MongoDB,SQLiteDB } = require("./databases");
2
- const { Sync } = require("./sync");
3
-
4
- module.exports = {
5
- MultiDBSafe,
6
- FireStoreDB,
7
- MongoDB,
8
- SQLiteDB,
9
- Sync
1
+ const { MultiDBSafe,FireStoreDB,MongoDB,SQLiteDB } = require("./databases");
2
+ const { Sync } = require("./sync");
3
+
4
+ module.exports = {
5
+ MultiDBSafe,
6
+ FireStoreDB,
7
+ MongoDB,
8
+ SQLiteDB,
9
+ Sync
10
10
  }
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,44 +1,44 @@
1
- {
2
- "name": "multi-db-orm",
3
- "version": "1.3.0",
4
- "description": "CRUD , Backup , Restore and Migration library for multiple databases",
5
- "main": "index.js",
6
- "dependencies": {
7
- "firebase-admin": "^9.3.0",
8
- "fs": "0.0.1-security",
9
- "mongodb": "^3.6.3",
10
- "node-firestore-import-export": "^1.1.0",
11
- "sqlite3": "^5.0.0"
12
- },
13
- "devDependencies": {},
14
- "scripts": {
15
- "test": "node test/test.js"
16
- },
17
- "repository": {
18
- "type": "git",
19
- "url": "git+https://github.com/shiveshnavin/multi-db-orm.git"
20
- },
21
- "keywords": [
22
- "database",
23
- "backup",
24
- "restore",
25
- "db",
26
- "backup",
27
- "db",
28
- "restore",
29
- "mongodb",
30
- "firestore",
31
- "backup",
32
- "mongodb",
33
- "restore",
34
- "mongodb",
35
- "sql",
36
- "mysql"
37
- ],
38
- "author": "Shivesh Navin",
39
- "license": "MIT",
40
- "bugs": {
41
- "url": "https://github.com/shiveshnavin/multi-db-orm/issues"
42
- },
43
- "homepage": "https://github.com/shiveshnavin/multi-db-orm#readme"
44
- }
1
+ {
2
+ "name": "multi-db-orm",
3
+ "version": "1.3.1",
4
+ "description": "CRUD , Backup , Restore and Migration library for multiple databases",
5
+ "main": "index.js",
6
+ "dependencies": {
7
+ "firebase-admin": "^9.3.0",
8
+ "fs": "0.0.1-security",
9
+ "mongodb": "^3.6.3",
10
+ "node-firestore-import-export": "^1.1.0",
11
+ "sqlite3": "^5.0.0"
12
+ },
13
+ "devDependencies": {},
14
+ "scripts": {
15
+ "test": "node test/test.js"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/shiveshnavin/multi-db-orm.git"
20
+ },
21
+ "keywords": [
22
+ "database",
23
+ "backup",
24
+ "restore",
25
+ "db",
26
+ "backup",
27
+ "db",
28
+ "restore",
29
+ "mongodb",
30
+ "firestore",
31
+ "backup",
32
+ "mongodb",
33
+ "restore",
34
+ "mongodb",
35
+ "sql",
36
+ "mysql"
37
+ ],
38
+ "author": "Shivesh Navin",
39
+ "license": "MIT",
40
+ "bugs": {
41
+ "url": "https://github.com/shiveshnavin/multi-db-orm/issues"
42
+ },
43
+ "homepage": "https://github.com/shiveshnavin/multi-db-orm#readme"
44
+ }