Typescript migration

* db/database.js
* utils/databaseUtil.js
This commit is contained in:
baldo 2020-04-08 22:05:53 +02:00
parent c91d892780
commit 9e29eb924e
6 changed files with 67 additions and 42 deletions

26
package-lock.json generated
View file

@ -89,6 +89,12 @@
"integrity": "sha512-0stqrMZB7vxsRTe//XEpr6A9+fThL2y/g6qzDG5ZgMJwuOceqOBDfh+g99tuN/XS58V52aPNTfWVS49Xvh842w==", "integrity": "sha512-0stqrMZB7vxsRTe//XEpr6A9+fThL2y/g6qzDG5ZgMJwuOceqOBDfh+g99tuN/XS58V52aPNTfWVS49Xvh842w==",
"dev": true "dev": true
}, },
"@types/events": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/@types/events/-/events-3.0.0.tgz",
"integrity": "sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g==",
"dev": true
},
"@types/express": { "@types/express": {
"version": "4.17.4", "version": "4.17.4",
"resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.4.tgz", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.4.tgz",
@ -111,6 +117,17 @@
"@types/range-parser": "*" "@types/range-parser": "*"
} }
}, },
"@types/glob": {
"version": "7.1.1",
"resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.1.1.tgz",
"integrity": "sha512-1Bh06cbWJUHMC97acuD6UMG29nMt0Aqz1vF3guLfG+kHHJhy3AyohZFFxYk2f7Q1SQIrNwvncxAE0N/9s70F2w==",
"dev": true,
"requires": {
"@types/events": "*",
"@types/minimatch": "*",
"@types/node": "*"
}
},
"@types/graceful-fs": { "@types/graceful-fs": {
"version": "4.1.3", "version": "4.1.3",
"resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.3.tgz", "resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.3.tgz",
@ -181,6 +198,15 @@
"@types/mime": "*" "@types/mime": "*"
} }
}, },
"@types/sqlite3": {
"version": "3.1.6",
"resolved": "https://registry.npmjs.org/@types/sqlite3/-/sqlite3-3.1.6.tgz",
"integrity": "sha512-OBsK0KIGUICExQ/ZvnPY4cKx5Kz4NcrVyGTIvOL5y4ajXu7r++RfBajfpGfGDmDVCKcoCDX1dO84/oeyeITnxA==",
"dev": true,
"requires": {
"@types/node": "*"
}
},
"Base64": { "Base64": {
"version": "0.2.1", "version": "0.2.1",
"resolved": "https://registry.npmjs.org/Base64/-/Base64-0.2.1.tgz", "resolved": "https://registry.npmjs.org/Base64/-/Base64-0.2.1.tgz",

View file

@ -56,10 +56,12 @@
"@types/compression": "^1.7.0", "@types/compression": "^1.7.0",
"@types/deep-extend": "^0.4.31", "@types/deep-extend": "^0.4.31",
"@types/express": "^4.17.4", "@types/express": "^4.17.4",
"@types/glob": "^7.1.1",
"@types/graceful-fs": "^4.1.3", "@types/graceful-fs": "^4.1.3",
"@types/lodash": "^4.14.149", "@types/lodash": "^4.14.149",
"@types/node": "^13.11.0", "@types/node": "^13.11.0",
"@types/nodemailer": "^6.4.0", "@types/nodemailer": "^6.4.0",
"@types/sqlite3": "^3.1.6",
"bower": "^1.8.8", "bower": "^1.8.8",
"escape-string-regexp": "^2.0.0", "escape-string-regexp": "^2.0.0",
"grunt": "^1.0.4", "grunt": "^1.0.4",

View file

@ -1,17 +1,21 @@
'use strict'; import util from "util";
import fs from "graceful-fs";
import glob from "glob";
import path from "path";
const util = require('util'); import sqlite from "sqlite";
const fs = require('graceful-fs'); import sqlite3 from "sqlite3";
const glob = util.promisify(require('glob'));
const path = require('path');
const config = require('../config').config; import {config} from "../config";
const Logger = require('../logger'); import Logger from "../logger";
async function applyPatch(db, file) { const pglob = util.promisify(glob);
const pReadFile = util.promisify(fs.readFile);
async function applyPatch(db: sqlite.Database, file: string): Promise<void> {
Logger.tag('database', 'migration').info('Checking if patch need to be applied: %s', file); Logger.tag('database', 'migration').info('Checking if patch need to be applied: %s', file);
const contents = await util.promisify(fs.readFile)(file); const contents = await pReadFile(file);
const version = path.basename(file, '.sql'); const version = path.basename(file, '.sql');
const row = await db.get('SELECT * FROM schema_version WHERE version = ?', version); const row = await db.get('SELECT * FROM schema_version WHERE version = ?', version);
@ -31,7 +35,7 @@ async function applyPatch(db, file) {
Logger.tag('database', 'migration').info('Patch successfully applied: %s', file); Logger.tag('database', 'migration').info('Patch successfully applied: %s', file);
} }
async function applyMigrations(db) { async function applyMigrations(db: sqlite.Database): Promise<void> {
Logger.tag('database', 'migration').info('Migrating database...'); Logger.tag('database', 'migration').info('Migrating database...');
const sql = 'BEGIN TRANSACTION; CREATE TABLE IF NOT EXISTS schema_version (\n' + const sql = 'BEGIN TRANSACTION; CREATE TABLE IF NOT EXISTS schema_version (\n' +
@ -41,20 +45,17 @@ async function applyMigrations(db) {
await db.exec(sql); await db.exec(sql);
const files = await glob(__dirname + '/patches/*.sql'); const files = await pglob(__dirname + '/patches/*.sql');
for (const file of files) { for (const file of files) {
await applyPatch(db, file) await applyPatch(db, file)
} }
} }
async function init() { export async function init(): Promise<void> {
const sqlite = require('sqlite');
const SQLite3 = require('sqlite3');
const file = config.server.databaseFile; const file = config.server.databaseFile;
Logger.tag('database').info('Setting up database: %s', file); Logger.tag('database').info('Setting up database: %s', file);
let db; let db: sqlite.Database;
try { try {
db = await sqlite.open(file); db = await sqlite.open(file);
} }
@ -73,18 +74,18 @@ async function init() {
throw error; throw error;
} }
let legacyDB; await db.close()
try {
legacyDB = new SQLite3.Database(file);
}
catch (error) {
Logger.tag('database').error('Error initialzing legacy database lib:', error);
throw error;
}
module.exports.db = legacyDB;
} }
module.exports = { Logger.tag('database').info('Setting up legacy database: %s', config.server.databaseFile);
init
}; let legacyDB: sqlite3.Database;
try {
legacyDB = new sqlite3.Database(config.server.databaseFile);
}
catch (error) {
Logger.tag('database').error('Error initialzing legacy database lib:', error);
throw error;
}
export const db = legacyDB;

View file

@ -1,7 +1,7 @@
import "./init" import "./init"
import { config } from "./config" import { config } from "./config"
import Logger from "./logger" import Logger from "./logger"
import db from "./db/database" import * as db from "./db/database"
import scheduler from "./jobs/scheduler" import scheduler from "./jobs/scheduler"
import { init as initRouter } from "./router" import { init as initRouter } from "./router"
import app from "./app" import app from "./app"

View file

@ -1,12 +0,0 @@
'use strict';
const _ = require('lodash')
module.exports = {
inCondition (field, list) {
return {
query: '(' + field + ' IN (' + _.join(_.times(list.length, _.constant('?')), ', ') + '))',
params: list
}
}
}

View file

@ -0,0 +1,8 @@
import _ from "lodash";
export function inCondition<T>(field: string, list: T[]): {query: string, params: T[]} {
return {
query: '(' + field + ' IN (' + _.join(_.times(list.length, _.constant('?')), ', ') + '))',
params: list,
}
}