diff --git a/web/database/queries/db-queries.js b/web/database/queries/db-queries.js new file mode 100644 --- /dev/null +++ b/web/database/queries/db-queries.js @@ -0,0 +1,17 @@ +// @flow + +function getDbVersion(db: SqliteDatabase): number { + const versionData = db.exec('PRAGMA user_version;'); + if (!versionData.length || !versionData[0].values.length) { + throw new Error('Error while retrieving database version: empty result'); + } + const dbVersions: SqlValue = versionData[0].values[0][0]; + if (typeof dbVersions !== 'number') { + throw new Error( + 'Error while retrieving database version: invalid type returned', + ); + } + return dbVersions; +} + +export { getDbVersion }; diff --git a/web/database/worker/db-worker.js b/web/database/worker/db-worker.js --- a/web/database/worker/db-worker.js +++ b/web/database/worker/db-worker.js @@ -11,6 +11,7 @@ workerResponseMessageTypes, type WorkerRequestProxyMessage, } from '../../types/worker-types.js'; +import { getDbVersion } from '../queries/db-queries.js'; import { SQLITE_CONTENT, SQLITE_ENCRYPTION_KEY } from '../utils/constants.js'; import { generateDatabaseCryptoKey } from '../utils/worker-crypto-utils.js'; @@ -44,12 +45,8 @@ sqliteDb = new SQL.Database(); } - const versionData = sqliteDb.exec('PRAGMA user_version;'); - if (versionData.length && versionData[0].values.length) { - console.info(`Db version: ${versionData[0].values[0]}`); - } else { - throw new Error('Error while retrieving database version'); - } + const dbVersion = getDbVersion(sqliteDb); + console.info(`Db version: ${dbVersion}`); } async function processAppRequest(