multi-db-orm 3.0.12 → 3.1.0
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/.vscode/launch.json +29 -29
- package/.vscode/settings.json +10 -10
- package/Dockerfile +17 -17
- 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/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 +1 -1
- 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/engines/sqlitedb.js
CHANGED
|
@@ -1,166 +1,166 @@
|
|
|
1
|
-
const { MultiDbORM } = require("./multidb");
|
|
2
|
-
var fs = require("fs");
|
|
3
|
-
|
|
4
|
-
class SQLiteDB extends MultiDbORM {
|
|
5
|
-
sqlite3;
|
|
6
|
-
dataMap = {
|
|
7
|
-
string: "TEXT",
|
|
8
|
-
number: "REAL",
|
|
9
|
-
boolean: "BOOLEAN",
|
|
10
|
-
};
|
|
11
|
-
constructor(filepath) {
|
|
12
|
-
super();
|
|
13
|
-
var sqlite3 = require("sqlite3");
|
|
14
|
-
this.sqlite3 = sqlite3;
|
|
15
|
-
if (filepath == undefined) filepath = ":memory:";
|
|
16
|
-
else {
|
|
17
|
-
var currentPath = process.cwd();
|
|
18
|
-
if (!fs.existsSync(filepath)) {
|
|
19
|
-
filepath = currentPath + "/" + filepath;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
this.db = new sqlite3.Database(filepath);
|
|
23
|
-
console.log("SQLite3 Initialized");
|
|
24
|
-
this.dbType = "sqlite3";
|
|
25
|
-
this.reqMade = 0;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
async run(query) {
|
|
29
|
-
var db = this.db;
|
|
30
|
-
var that = this;
|
|
31
|
-
this.reqMade++;
|
|
32
|
-
return new Promise(function (resolve, reject) {
|
|
33
|
-
db.all(query, function (err, resp) {
|
|
34
|
-
if (err) reject(err);
|
|
35
|
-
else resolve(resp);
|
|
36
|
-
if (that.loglevel > 3) console.log("Query ", query, " -> ", resp);
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async get(modelname, filter, options) {
|
|
42
|
-
this.metrics.get(modelname, filter, options);
|
|
43
|
-
var where = "";
|
|
44
|
-
for (var key in filter) {
|
|
45
|
-
where = where + `${key} = '${filter[key]}' AND `;
|
|
46
|
-
}
|
|
47
|
-
where = where + " 1 ";
|
|
48
|
-
var sort = "";
|
|
49
|
-
if (options) {
|
|
50
|
-
if (options.apply) {
|
|
51
|
-
if (options.apply.ineq) {
|
|
52
|
-
where =
|
|
53
|
-
where +
|
|
54
|
-
` AND '${options.apply.field}' ${options.apply.ineq.op} '${options.apply.ineq.value}'`;
|
|
55
|
-
}
|
|
56
|
-
if (options.apply.sort) {
|
|
57
|
-
sort = `ORDER BY ${options.apply.field} ${options.apply.sort}`;
|
|
58
|
-
}
|
|
59
|
-
} else if (options.sort) {
|
|
60
|
-
sort = `ORDER BY`;
|
|
61
|
-
for (let i = 0; i < options.sort.length; i++) {
|
|
62
|
-
sort = sort + ` ${options.sort[i].field} ${options.sort[i].order}`;
|
|
63
|
-
if (i < options.sort.length - 1) {
|
|
64
|
-
sort = sort + " , ";
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
var query = `SELECT * FROM ${modelname} WHERE ${where} ${sort} ;`;
|
|
70
|
-
return (await this.run(query)) || [];
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async getOne(modelname, filter) {
|
|
74
|
-
this.metrics.getOne(modelname, filter);
|
|
75
|
-
var where = "";
|
|
76
|
-
for (var key in filter) {
|
|
77
|
-
where = where + `${key} = '${filter[key]}' AND `;
|
|
78
|
-
}
|
|
79
|
-
where = where + " 1 ";
|
|
80
|
-
var query = `SELECT * FROM ${modelname} WHERE ${where} LIMIT 1;`;
|
|
81
|
-
var row = await this.run(query);
|
|
82
|
-
return row[0];
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
async create(modelname, sampleObject) {
|
|
86
|
-
this.sync.create(modelname, sampleObject);
|
|
87
|
-
this.metrics.create(modelname, sampleObject);
|
|
88
|
-
|
|
89
|
-
var cols = "";
|
|
90
|
-
for (var key in sampleObject) {
|
|
91
|
-
var type = this.dataMap[typeof sampleObject[key]] || "TEXT";
|
|
92
|
-
cols = cols + `${key} ${type},`;
|
|
93
|
-
}
|
|
94
|
-
cols = cols.substring(0, cols.length - 1);
|
|
95
|
-
var query = `CREATE TABLE IF NOT EXISTS ${modelname} (${cols});`;
|
|
96
|
-
try {
|
|
97
|
-
return await this.run(query);
|
|
98
|
-
} catch (err) {
|
|
99
|
-
console.log(err);
|
|
100
|
-
return undefined;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
async insert(modelname, object) {
|
|
105
|
-
this.sync.insert(modelname, object);
|
|
106
|
-
this.metrics.insert(modelname, object);
|
|
107
|
-
var cols = "";
|
|
108
|
-
var vals = "";
|
|
109
|
-
for (var key in object) {
|
|
110
|
-
cols = cols + `${key},`;
|
|
111
|
-
vals = vals + `'${object[key]}',`;
|
|
112
|
-
}
|
|
113
|
-
cols = cols.substring(0, cols.length - 1);
|
|
114
|
-
vals = vals.substring(0, vals.length - 1);
|
|
115
|
-
|
|
116
|
-
var query = `INSERT INTO ${modelname} (${cols}) VALUES(${vals});`;
|
|
117
|
-
|
|
118
|
-
try {
|
|
119
|
-
return await this.run(query);
|
|
120
|
-
} catch (err) {
|
|
121
|
-
if (
|
|
122
|
-
err.message &&
|
|
123
|
-
err.message.indexOf("SQLITE_ERROR: no such table: ") > -1
|
|
124
|
-
) {
|
|
125
|
-
await this.create(modelname, object);
|
|
126
|
-
return await this.run(query);
|
|
127
|
-
} else throw err;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
async update(modelname, filter, object) {
|
|
132
|
-
this.sync.update(modelname, filter, object);
|
|
133
|
-
this.metrics.update(modelname, filter, object);
|
|
134
|
-
|
|
135
|
-
var where = "";
|
|
136
|
-
var vals = "";
|
|
137
|
-
for (var key in filter) {
|
|
138
|
-
where = where + `${key} = '${filter[key]}' AND `;
|
|
139
|
-
}
|
|
140
|
-
for (var key in object) {
|
|
141
|
-
vals = vals + ` ${key} = '${object[key]}',`;
|
|
142
|
-
}
|
|
143
|
-
where = where + " 1 ";
|
|
144
|
-
vals = vals.substring(0, vals.length - 1);
|
|
145
|
-
|
|
146
|
-
var query = `UPDATE ${modelname} SET ${vals} WHERE ${where};`;
|
|
147
|
-
return await this.run(query);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
async delete(modelname, filter) {
|
|
151
|
-
this.sync.delete(modelname, filter);
|
|
152
|
-
this.metrics.delete(modelname, filter);
|
|
153
|
-
|
|
154
|
-
var where = "";
|
|
155
|
-
for (var key in filter) {
|
|
156
|
-
where = where + `${key} = '${filter[key]}' AND `;
|
|
157
|
-
}
|
|
158
|
-
where = where + " 1 ";
|
|
159
|
-
var query = `DELETE FROM ${modelname} WHERE ${where};`;
|
|
160
|
-
return await this.run(query);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
module.exports = {
|
|
165
|
-
SQLiteDB,
|
|
166
|
-
};
|
|
1
|
+
const { MultiDbORM } = require("./multidb");
|
|
2
|
+
var fs = require("fs");
|
|
3
|
+
|
|
4
|
+
class SQLiteDB extends MultiDbORM {
|
|
5
|
+
sqlite3;
|
|
6
|
+
dataMap = {
|
|
7
|
+
string: "TEXT",
|
|
8
|
+
number: "REAL",
|
|
9
|
+
boolean: "BOOLEAN",
|
|
10
|
+
};
|
|
11
|
+
constructor(filepath) {
|
|
12
|
+
super();
|
|
13
|
+
var sqlite3 = require("sqlite3");
|
|
14
|
+
this.sqlite3 = sqlite3;
|
|
15
|
+
if (filepath == undefined) filepath = ":memory:";
|
|
16
|
+
else {
|
|
17
|
+
var currentPath = process.cwd();
|
|
18
|
+
if (!fs.existsSync(filepath)) {
|
|
19
|
+
filepath = currentPath + "/" + filepath;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
this.db = new sqlite3.Database(filepath);
|
|
23
|
+
console.log("SQLite3 Initialized");
|
|
24
|
+
this.dbType = "sqlite3";
|
|
25
|
+
this.reqMade = 0;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async run(query) {
|
|
29
|
+
var db = this.db;
|
|
30
|
+
var that = this;
|
|
31
|
+
this.reqMade++;
|
|
32
|
+
return new Promise(function (resolve, reject) {
|
|
33
|
+
db.all(query, function (err, resp) {
|
|
34
|
+
if (err) reject(err);
|
|
35
|
+
else resolve(resp);
|
|
36
|
+
if (that.loglevel > 3) console.log("Query ", query, " -> ", resp);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async get(modelname, filter, options) {
|
|
42
|
+
this.metrics.get(modelname, filter, options);
|
|
43
|
+
var where = "";
|
|
44
|
+
for (var key in filter) {
|
|
45
|
+
where = where + `${key} = '${filter[key]}' AND `;
|
|
46
|
+
}
|
|
47
|
+
where = where + " 1 ";
|
|
48
|
+
var sort = "";
|
|
49
|
+
if (options) {
|
|
50
|
+
if (options.apply) {
|
|
51
|
+
if (options.apply.ineq) {
|
|
52
|
+
where =
|
|
53
|
+
where +
|
|
54
|
+
` AND '${options.apply.field}' ${options.apply.ineq.op} '${options.apply.ineq.value}'`;
|
|
55
|
+
}
|
|
56
|
+
if (options.apply.sort) {
|
|
57
|
+
sort = `ORDER BY ${options.apply.field} ${options.apply.sort}`;
|
|
58
|
+
}
|
|
59
|
+
} else if (options.sort) {
|
|
60
|
+
sort = `ORDER BY`;
|
|
61
|
+
for (let i = 0; i < options.sort.length; i++) {
|
|
62
|
+
sort = sort + ` ${options.sort[i].field} ${options.sort[i].order}`;
|
|
63
|
+
if (i < options.sort.length - 1) {
|
|
64
|
+
sort = sort + " , ";
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
var query = `SELECT * FROM ${modelname} WHERE ${where} ${sort} ;`;
|
|
70
|
+
return (await this.run(query)) || [];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async getOne(modelname, filter) {
|
|
74
|
+
this.metrics.getOne(modelname, filter);
|
|
75
|
+
var where = "";
|
|
76
|
+
for (var key in filter) {
|
|
77
|
+
where = where + `${key} = '${filter[key]}' AND `;
|
|
78
|
+
}
|
|
79
|
+
where = where + " 1 ";
|
|
80
|
+
var query = `SELECT * FROM ${modelname} WHERE ${where} LIMIT 1;`;
|
|
81
|
+
var row = await this.run(query);
|
|
82
|
+
return row[0];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async create(modelname, sampleObject) {
|
|
86
|
+
this.sync.create(modelname, sampleObject);
|
|
87
|
+
this.metrics.create(modelname, sampleObject);
|
|
88
|
+
|
|
89
|
+
var cols = "";
|
|
90
|
+
for (var key in sampleObject) {
|
|
91
|
+
var type = this.dataMap[typeof sampleObject[key]] || "TEXT";
|
|
92
|
+
cols = cols + `${key} ${type},`;
|
|
93
|
+
}
|
|
94
|
+
cols = cols.substring(0, cols.length - 1);
|
|
95
|
+
var query = `CREATE TABLE IF NOT EXISTS ${modelname} (${cols});`;
|
|
96
|
+
try {
|
|
97
|
+
return await this.run(query);
|
|
98
|
+
} catch (err) {
|
|
99
|
+
console.log(err);
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async insert(modelname, object) {
|
|
105
|
+
this.sync.insert(modelname, object);
|
|
106
|
+
this.metrics.insert(modelname, object);
|
|
107
|
+
var cols = "";
|
|
108
|
+
var vals = "";
|
|
109
|
+
for (var key in object) {
|
|
110
|
+
cols = cols + `${key},`;
|
|
111
|
+
vals = vals + `'${object[key]}',`;
|
|
112
|
+
}
|
|
113
|
+
cols = cols.substring(0, cols.length - 1);
|
|
114
|
+
vals = vals.substring(0, vals.length - 1);
|
|
115
|
+
|
|
116
|
+
var query = `INSERT INTO ${modelname} (${cols}) VALUES(${vals});`;
|
|
117
|
+
|
|
118
|
+
try {
|
|
119
|
+
return await this.run(query);
|
|
120
|
+
} catch (err) {
|
|
121
|
+
if (
|
|
122
|
+
err.message &&
|
|
123
|
+
err.message.indexOf("SQLITE_ERROR: no such table: ") > -1
|
|
124
|
+
) {
|
|
125
|
+
await this.create(modelname, object);
|
|
126
|
+
return await this.run(query);
|
|
127
|
+
} else throw err;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async update(modelname, filter, object) {
|
|
132
|
+
this.sync.update(modelname, filter, object);
|
|
133
|
+
this.metrics.update(modelname, filter, object);
|
|
134
|
+
|
|
135
|
+
var where = "";
|
|
136
|
+
var vals = "";
|
|
137
|
+
for (var key in filter) {
|
|
138
|
+
where = where + `${key} = '${filter[key]}' AND `;
|
|
139
|
+
}
|
|
140
|
+
for (var key in object) {
|
|
141
|
+
vals = vals + ` ${key} = '${object[key]}',`;
|
|
142
|
+
}
|
|
143
|
+
where = where + " 1 ";
|
|
144
|
+
vals = vals.substring(0, vals.length - 1);
|
|
145
|
+
|
|
146
|
+
var query = `UPDATE ${modelname} SET ${vals} WHERE ${where};`;
|
|
147
|
+
return await this.run(query);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async delete(modelname, filter) {
|
|
151
|
+
this.sync.delete(modelname, filter);
|
|
152
|
+
this.metrics.delete(modelname, filter);
|
|
153
|
+
|
|
154
|
+
var where = "";
|
|
155
|
+
for (var key in filter) {
|
|
156
|
+
where = where + `${key} = '${filter[key]}' AND `;
|
|
157
|
+
}
|
|
158
|
+
where = where + " 1 ";
|
|
159
|
+
var query = `DELETE FROM ${modelname} WHERE ${where};`;
|
|
160
|
+
return await this.run(query);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
module.exports = {
|
|
165
|
+
SQLiteDB,
|
|
166
|
+
};
|
package/index.js
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
const {
|
|
2
|
-
MultiDBSafe,
|
|
3
|
-
FireStoreDB,
|
|
4
|
-
MongoDB,
|
|
5
|
-
SQLiteDB,
|
|
6
|
-
OracleDB,
|
|
7
|
-
MySQLDB,
|
|
8
|
-
HanaDB,
|
|
9
|
-
BigQueryDB,
|
|
10
|
-
} = require("./databases");
|
|
11
|
-
const { Sync } = require("./sync");
|
|
12
|
-
|
|
13
|
-
module.exports = {
|
|
14
|
-
MultiDBSafe,
|
|
15
|
-
FireStoreDB,
|
|
16
|
-
MongoDB,
|
|
17
|
-
SQLiteDB,
|
|
18
|
-
OracleDB,
|
|
19
|
-
MySQLDB,
|
|
20
|
-
HanaDB,
|
|
21
|
-
Sync,
|
|
22
|
-
BigQueryDB,
|
|
23
|
-
};
|
|
1
|
+
const {
|
|
2
|
+
MultiDBSafe,
|
|
3
|
+
FireStoreDB,
|
|
4
|
+
MongoDB,
|
|
5
|
+
SQLiteDB,
|
|
6
|
+
OracleDB,
|
|
7
|
+
MySQLDB,
|
|
8
|
+
HanaDB,
|
|
9
|
+
BigQueryDB,
|
|
10
|
+
} = require("./databases");
|
|
11
|
+
const { Sync } = require("./sync");
|
|
12
|
+
|
|
13
|
+
module.exports = {
|
|
14
|
+
MultiDBSafe,
|
|
15
|
+
FireStoreDB,
|
|
16
|
+
MongoDB,
|
|
17
|
+
SQLiteDB,
|
|
18
|
+
OracleDB,
|
|
19
|
+
MySQLDB,
|
|
20
|
+
HanaDB,
|
|
21
|
+
Sync,
|
|
22
|
+
BigQueryDB,
|
|
23
|
+
};
|
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
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
|
`)
|
package/restore.js
CHANGED
|
@@ -1,102 +1,102 @@
|
|
|
1
|
-
|
|
2
|
-
const mongoose = require('mongoose');
|
|
3
|
-
var fs = require('fs');
|
|
4
|
-
async function log(arg) {
|
|
5
|
-
update(arg)
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
var maxC = 0;
|
|
9
|
-
var curC = 1;
|
|
10
|
-
async function processCollection(collectionName) {
|
|
11
|
-
|
|
12
|
-
try {
|
|
13
|
-
|
|
14
|
-
var collData = data[collectionName];
|
|
15
|
-
var coll = db.collection(collectionName);
|
|
16
|
-
collData.forEach((cd) => {
|
|
17
|
-
cd._id = mongoose.Types.ObjectId(cd._id)
|
|
18
|
-
})
|
|
19
|
-
await coll.insertMany(collData)
|
|
20
|
-
var count = await coll.countDocuments()
|
|
21
|
-
log(collectionName, " = ", count);
|
|
22
|
-
if(removeDups)
|
|
23
|
-
dups(collectionName)
|
|
24
|
-
|
|
25
|
-
} catch (er) {
|
|
26
|
-
log(er.message)
|
|
27
|
-
log("error in " + collectionName);
|
|
28
|
-
}
|
|
29
|
-
log('Processed '+collectionName+"("+(curC+"/"+maxC)+")");
|
|
30
|
-
if (curC++ < maxC) {
|
|
31
|
-
curC;
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
log("\n\nAll Data restore completed !!");
|
|
35
|
-
finish('Restore Complete !')
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
async function dups(collectionName) {
|
|
41
|
-
var coll = db.collection(collectionName);
|
|
42
|
-
await coll.aggregate(
|
|
43
|
-
{ "$group": { "_id": "$name", "count": { "$sum": 1 } } },
|
|
44
|
-
{ "$match": { "_id": { "$ne": null }, "count": { "$gt": 1 } } },
|
|
45
|
-
{ "$project": { "name": "$_id", "_id": 0 } }
|
|
46
|
-
)
|
|
47
|
-
.toArray((err, results) => {
|
|
48
|
-
if (err) throw err;
|
|
49
|
-
log("===", results);
|
|
50
|
-
|
|
51
|
-
})
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
var dumpFile = process.argv[2];
|
|
55
|
-
var targetDb = process.argv[3];
|
|
56
|
-
var deDupDo = process.argv[4];
|
|
57
|
-
var db;
|
|
58
|
-
var data;
|
|
59
|
-
var update=(msg)=>{
|
|
60
|
-
console.log(msg)
|
|
61
|
-
}
|
|
62
|
-
var finish=(msg)=>{
|
|
63
|
-
log(msg)
|
|
64
|
-
process.exit()
|
|
65
|
-
}
|
|
66
|
-
var removeDups=false;
|
|
67
|
-
if (dumpFile && targetDb) {
|
|
68
|
-
data = JSON.parse(fs.readFileSync(dumpFile));
|
|
69
|
-
mongoose.Promise = global.Promise;
|
|
70
|
-
|
|
71
|
-
mongoose.connect(targetDb, {
|
|
72
|
-
useUnifiedTopology: true,
|
|
73
|
-
useNewUrlParser: true,
|
|
74
|
-
}).then(() => {
|
|
75
|
-
log("Successfully connected to the database");
|
|
76
|
-
readCollectionsFromDump(mongoose.connection.db,JSON.parse(fs.readFileSync(dumpFile)),deDupDo);
|
|
77
|
-
}).catch(err => {
|
|
78
|
-
log('Could not connect to the database. Exiting now...', err);
|
|
79
|
-
process.exit();
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
async function readCollectionsFromDump(connectedDB, dumpData,deDup) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
if(deDup==1){
|
|
86
|
-
removeDups=true;
|
|
87
|
-
log("Removing duplicates as well")
|
|
88
|
-
}
|
|
89
|
-
db = connectedDB;
|
|
90
|
-
data = (dumpData);
|
|
91
|
-
|
|
92
|
-
var v = Object.keys(dumpData)
|
|
93
|
-
log('Found '+v.length+' Collections to restore');
|
|
94
|
-
// log(v);
|
|
95
|
-
maxC = v.length;
|
|
96
|
-
console.log(v.length)
|
|
97
|
-
v.forEach(async function(coll){
|
|
98
|
-
await processCollection(coll)
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
1
|
+
|
|
2
|
+
const mongoose = require('mongoose');
|
|
3
|
+
var fs = require('fs');
|
|
4
|
+
async function log(arg) {
|
|
5
|
+
update(arg)
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
var maxC = 0;
|
|
9
|
+
var curC = 1;
|
|
10
|
+
async function processCollection(collectionName) {
|
|
11
|
+
|
|
12
|
+
try {
|
|
13
|
+
|
|
14
|
+
var collData = data[collectionName];
|
|
15
|
+
var coll = db.collection(collectionName);
|
|
16
|
+
collData.forEach((cd) => {
|
|
17
|
+
cd._id = mongoose.Types.ObjectId(cd._id)
|
|
18
|
+
})
|
|
19
|
+
await coll.insertMany(collData)
|
|
20
|
+
var count = await coll.countDocuments()
|
|
21
|
+
log(collectionName, " = ", count);
|
|
22
|
+
if(removeDups)
|
|
23
|
+
dups(collectionName)
|
|
24
|
+
|
|
25
|
+
} catch (er) {
|
|
26
|
+
log(er.message)
|
|
27
|
+
log("error in " + collectionName);
|
|
28
|
+
}
|
|
29
|
+
log('Processed '+collectionName+"("+(curC+"/"+maxC)+")");
|
|
30
|
+
if (curC++ < maxC) {
|
|
31
|
+
curC;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
log("\n\nAll Data restore completed !!");
|
|
35
|
+
finish('Restore Complete !')
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
async function dups(collectionName) {
|
|
41
|
+
var coll = db.collection(collectionName);
|
|
42
|
+
await coll.aggregate(
|
|
43
|
+
{ "$group": { "_id": "$name", "count": { "$sum": 1 } } },
|
|
44
|
+
{ "$match": { "_id": { "$ne": null }, "count": { "$gt": 1 } } },
|
|
45
|
+
{ "$project": { "name": "$_id", "_id": 0 } }
|
|
46
|
+
)
|
|
47
|
+
.toArray((err, results) => {
|
|
48
|
+
if (err) throw err;
|
|
49
|
+
log("===", results);
|
|
50
|
+
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
var dumpFile = process.argv[2];
|
|
55
|
+
var targetDb = process.argv[3];
|
|
56
|
+
var deDupDo = process.argv[4];
|
|
57
|
+
var db;
|
|
58
|
+
var data;
|
|
59
|
+
var update=(msg)=>{
|
|
60
|
+
console.log(msg)
|
|
61
|
+
}
|
|
62
|
+
var finish=(msg)=>{
|
|
63
|
+
log(msg)
|
|
64
|
+
process.exit()
|
|
65
|
+
}
|
|
66
|
+
var removeDups=false;
|
|
67
|
+
if (dumpFile && targetDb) {
|
|
68
|
+
data = JSON.parse(fs.readFileSync(dumpFile));
|
|
69
|
+
mongoose.Promise = global.Promise;
|
|
70
|
+
|
|
71
|
+
mongoose.connect(targetDb, {
|
|
72
|
+
useUnifiedTopology: true,
|
|
73
|
+
useNewUrlParser: true,
|
|
74
|
+
}).then(() => {
|
|
75
|
+
log("Successfully connected to the database");
|
|
76
|
+
readCollectionsFromDump(mongoose.connection.db,JSON.parse(fs.readFileSync(dumpFile)),deDupDo);
|
|
77
|
+
}).catch(err => {
|
|
78
|
+
log('Could not connect to the database. Exiting now...', err);
|
|
79
|
+
process.exit();
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
async function readCollectionsFromDump(connectedDB, dumpData,deDup) {
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
if(deDup==1){
|
|
86
|
+
removeDups=true;
|
|
87
|
+
log("Removing duplicates as well")
|
|
88
|
+
}
|
|
89
|
+
db = connectedDB;
|
|
90
|
+
data = (dumpData);
|
|
91
|
+
|
|
92
|
+
var v = Object.keys(dumpData)
|
|
93
|
+
log('Found '+v.length+' Collections to restore');
|
|
94
|
+
// log(v);
|
|
95
|
+
maxC = v.length;
|
|
96
|
+
console.log(v.length)
|
|
97
|
+
v.forEach(async function(coll){
|
|
98
|
+
await processCollection(coll)
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
}
|
package/sync.d.ts
CHANGED
|
@@ -1,6 +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;
|
|
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
6
|
}
|