Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 7b25592

Browse files
committed
chore(db-compare): port to TypeScript
1 parent 4b38eda commit 7b25592

File tree

6 files changed

+223
-36
lines changed

6 files changed

+223
-36
lines changed

apps/db-compare/package-lock.json

Lines changed: 160 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/db-compare/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,9 @@
6767
}
6868
}
6969
}
70+
},
71+
"devDependencies": {
72+
"@types/colors": "1.2.4",
73+
"@types/diff": "^7.0.2"
7074
}
7175
}

apps/db-compare/src/compare.js renamed to apps/db-compare/src/compare.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"use strict";
22

3-
require('colors');
4-
const jsDiff = require('diff');
5-
const sqlite = require('sqlite');
6-
const sqlite3 = require('sqlite3');
7-
const sql = require('./sql');
3+
import jsDiff from "diff";
4+
import * as sqlite from "sqlite";
5+
import * as sqlite3 from "sqlite3";
6+
import sql from "./sql.js";
87

9-
function printDiff(one, two) {
8+
import "colors";
9+
10+
function printDiff(one: string, two: string) {
1011
const diff = jsDiff.diffChars(one, two);
1112

1213
diff.forEach(function(part){
@@ -20,23 +21,23 @@ function printDiff(one, two) {
2021
console.log("");
2122
}
2223

23-
function checkMissing(table, name, ids1, ids2) {
24+
function checkMissing(table: string, name: string, ids1: string[], ids2: string[]) {
2425
const missing = ids1.filter(item => ids2.indexOf(item) < 0);
2526

2627
if (missing.length > 0) {
2728
console.log("Missing IDs from " + name + " table " + table + ": ", missing);
2829
}
2930
}
3031

31-
function handleBuffer(obj) {
32+
function handleBuffer(obj: { content: Buffer | string }) {
3233
if (obj && Buffer.isBuffer(obj.content)) {
3334
obj.content = obj.content.toString();
3435
}
3536

3637
return obj;
3738
}
3839

39-
function compareRows(table, rsLeft, rsRight, column) {
40+
function compareRows(table: string, rsLeft: Record<string, any>, rsRight: Record<string, any>, column: string) {
4041
const leftIds = Object.keys(rsLeft);
4142
const rightIds = Object.keys(rsRight);
4243

@@ -72,7 +73,7 @@ async function main() {
7273
const dbLeft = await sqlite.open({filename: dbLeftPath, driver: sqlite3.Database});
7374
const dbRight = await sqlite.open({filename: dbRightPath, driver: sqlite3.Database});
7475

75-
async function compare(table, column, query) {
76+
async function compare(table: string, column: string, query: string) {
7677
const rsLeft = await sql.getIndexed(dbLeft, column, query);
7778
const rsRight = await sql.getIndexed(dbRight, column, query);
7879

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
"use strict";
22

3-
async function getSingleResult(db, query, params = []) {
3+
import type { Database } from "sqlite";
4+
5+
async function getSingleResult(db: Database, query: string, params: any[] = []) {
46
return await wrap(db, async db => db.get(query, ...params));
57
}
68

7-
async function getSingleResultOrNull(db, query, params = []) {
9+
async function getSingleResultOrNull(db: Database, query: string, params: any[] = []) {
810
const all = await wrap(db, async db => db.all(query, ...params));
911

1012
return all.length > 0 ? all[0] : null;
1113
}
1214

13-
async function getSingleValue(db, query, params = []) {
15+
async function getSingleValue(db: Database, query: string, params: any[] = []) {
1416
const row = await getSingleResultOrNull(db, query, params);
1517

1618
if (!row) {
@@ -20,14 +22,14 @@ async function getSingleValue(db, query, params = []) {
2022
return row[Object.keys(row)[0]];
2123
}
2224

23-
async function getResults(db, query, params = []) {
25+
async function getResults(db: Database, query: string, params: any[] = []) {
2426
return await wrap(db, async db => db.all(query, ...params));
2527
}
2628

27-
async function getIndexed(db, column, query, params = []) {
29+
async function getIndexed(db: Database, column: string, query: string, params: any[] = []) {
2830
const results = await getResults(db, query, params);
2931

30-
const map = {};
32+
const map: Record<string, any> = {};
3133

3234
for (const row of results) {
3335
map[row[column]] = row;
@@ -36,8 +38,8 @@ async function getIndexed(db, column, query, params = []) {
3638
return map;
3739
}
3840

39-
async function getMap(db, query, params = []) {
40-
const map = {};
41+
async function getMap(db: Database, query: string, params: any[] = []) {
42+
const map: Record<string, any> = {};
4143
const results = await getResults(db, query, params);
4244

4345
for (const row of results) {
@@ -49,7 +51,7 @@ async function getMap(db, query, params = []) {
4951
return map;
5052
}
5153

52-
async function getFlattenedResults(db, key, query, params = []) {
54+
async function getFlattenedResults(db: Database, key: string, query: string, params: any[] = []) {
5355
const list = [];
5456
const result = await getResults(db, query, params);
5557

@@ -60,24 +62,23 @@ async function getFlattenedResults(db, key, query, params = []) {
6062
return list;
6163
}
6264

63-
async function execute(db, query, params = []) {
65+
async function execute(db: Database, query: string, params: any[] = []) {
6466
return await wrap(db, async db => db.run(query, ...params));
6567
}
6668

67-
async function wrap(db, func) {
69+
async function wrap<T>(db: Database, func: (db: Database) => Promise<T>) {
6870
const thisError = new Error();
6971

7072
try {
7173
return await func(db);
72-
}
73-
catch (e) {
74+
} catch (e: any) {
7475
console.error("Error executing query. Inner exception: " + e.stack + thisError.stack);
7576

7677
throw thisError;
7778
}
7879
}
7980

80-
module.exports = {
81+
export default {
8182
getSingleValue,
8283
getSingleResult,
8384
getSingleResultOrNull,
@@ -86,4 +87,4 @@ module.exports = {
8687
getMap,
8788
getFlattenedResults,
8889
execute
89-
};
90+
};

apps/db-compare/tsconfig.app.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"node"
77
],
88
"rootDir": "src",
9-
"tsBuildInfoFile": "dist/tsconfig.app.tsbuildinfo"
9+
"tsBuildInfoFile": "dist/tsconfig.app.tsbuildinfo",
10+
"verbatimModuleSyntax": false
1011
},
1112
"include": [
1213
"src/**/*.ts"

0 commit comments

Comments
 (0)