-
Notifications
You must be signed in to change notification settings - Fork 2
/
sqlitemongo.ts
79 lines (72 loc) · 2.49 KB
/
sqlitemongo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
export { sqliteMongo as default };
import * as sqlite3 from "sqlite3";
import {MongoClient} from "mongodb";
async function sqliteMongo(sqlitepath: string, mongoURI: string, mongoDbName = 'sqlite3') {
if (typeof sqlitepath !== `string`) {
return console.error(`Expected a valid sqlite3 filepath but instead got ${sqlitepath}`);
}
if (typeof mongoURI !== `string`) {
return console.error(`Expected a valid mongodb URI but instead got ${mongoURI}`);
}
const sqliteDb = new sqlite3.Database(sqlitepath);
const mongoConnected = await MongoClient.connect(mongoURI);
const mongoDb = mongoConnected.db(mongoDbName);
async function uploadAllTablesToMongo() {
const tables = await getSqliteTables();
return Promise.all(tables.map(async function uploadTable(tableName) {
let rows = await getSqliteRows(tableName);
return uploadToMongo(tableName, rows);
}));
}
return uploadAllTablesToMongo().finally(closeDb);
function closeDb() {
sqliteDb.close();
mongoConnected.close();
}
async function uploadToMongo(tableName, rows) {
return new Promise(function resolveLogError(resolve, reject) {
if (rows.length === 0) {
// TODO: Add document validation for Mongodb 3.6 and later
mongoDb.createCollection(tableName, {}, resolve);
return;
}
// TODO: Add --overwrite option (fixes duplicate key errors)
const params = { ordered: false };
mongoDb.collection(tableName).insertMany(rows, params, function insertRes(err, _result) {
if (err) {
return reject(`Failed to insert ${typeof rows} with error ${err}`);
}
resolve(undefined)
});
})
}
async function getSqliteRows(tableName: string) {
return new Promise(function (resolve, reject) {
let sql = `SELECT rowid as _id,* FROM "${tableName}"`;
sqliteDb.all(sql, [], function ifErrorRejectElseResolve(err, rows) {
if(err) {
return reject(`Failed to get sqlite3 table data for ${tableName}.\n${err}`);
}
resolve(rows);
});
});
}
async function getSqliteTables(): Promise<string[]> {
return new Promise(function getTables(resolve,reject) {
const allTablesQuery = `SELECT name FROM sqlite_master
WHERE type ='table' AND name NOT LIKE 'sqlite_%';`
sqliteDb.all(allTablesQuery, [], function resolveTables(err, tables) {
if (err) {
return reject(err);
}
if (!Array.isArray(tables)) {
return reject(`There are no sqlite tables to upload to mongo`);
}
tables = tables.map(function mapToName(table) {
return table.name;
});
resolve(tables);
});
})
}
}