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 @@ -1,7 +1,7 @@ // @flow import localforage from 'localforage'; -import initSqlJs from 'sql.js'; +import initSqlJs, { type SqliteDatabase } from 'sql.js'; import { type SharedWorkerMessageEvent, @@ -23,7 +23,7 @@ }; localforage.config(localforageConfig); -let sqliteDb = null; +let sqliteDb: ?SqliteDatabase = null; async function initDatabase(sqljsFilePath: string, sqljsFilename: ?string) { const content = await localforage.getItem(SQLITE_CONTENT); @@ -45,8 +45,12 @@ } const versionData = sqliteDb.exec('PRAGMA user_version;'); - if (versionData.length && versionData[0].values.length) { - console.info(`Db version: ${versionData[0].values[0]}`); + if (!versionData.length || !versionData[0].values.length) { + throw new Error('Error while retrieving database version'); + } + const [dbVersion] = versionData[0].values[0]; + if (typeof dbVersion === 'number') { + console.info(`Db version: ${dbVersion}`); } else { throw new Error('Error while retrieving database version'); } diff --git a/web/flow-typed/npm/sql.js_vx.x.x.js b/web/flow-typed/npm/sql.js_vx.x.x.js new file mode 100644 --- /dev/null +++ b/web/flow-typed/npm/sql.js_vx.x.x.js @@ -0,0 +1,89 @@ +// @flow + +// flow-typed signature: f355493becad56880bf30832260c5460 +// flow-typed version: <>/sql.js_v1.8.0/flow_v0.182.0 + +declare module 'sql.js' { + declare export type SqlValue = number | string | Uint8Array | null; + declare export type ParamsObject = { +[key: string]: SqlValue }; + declare export type ParamsCallback = (obj: ParamsObject) => void; + + declare export type BindParams = + | $ReadOnlyArray + | ParamsObject + | null; + + declare export type QueryExecResult = { + columns: string[], + values: SqlValue[][], + }; + + declare export type StatementIteratorResult = { + +done: boolean, + +value: SqliteStatement, + }; + + declare export type SqlJsStatic = { + +Database: Class, + +Statement: Class, + }; + + declare type EmscriptenModule = { + +locateFile: (path: string, prefix?: string) => string, + ... + }; + + declare type InitSqlJsStatic = { + (config?: EmscriptenModule): Promise, + }; + + declare export class SqliteDatabase { + constructor(data?: Array | Uint8Array | Buffer | null): this; + close(): void; + + create_function( + name: string, + func: (...args: $ReadOnlyArray) => R, + ): SqliteDatabase; + each( + sql: string, + params: BindParams, + callback: ParamsCallback, + done: () => void, + ): SqliteDatabase; + each( + sql: string, + callback: ParamsCallback, + done: () => void, + ): SqliteDatabase; + exec(sql: string, params?: BindParams): QueryExecResult[]; + export(): Uint8Array; + getRowsModified(): number; + handleError(): null | empty; + iterateStatements(sql: string): StatementIterator; + prepare(sql: string, params?: BindParams): SqliteStatement; + run(sql: string, params?: BindParams): SqliteDatabase; + } + + declare export class SqliteStatement { + bind(values?: BindParams): boolean; + free(): boolean; + freemem(): void; + get(params?: BindParams): $ReadOnlyArray; + getAsObject(params?: BindParams): ParamsObject; + getColumnNames(): $ReadOnlyArray; + getNormalizedSQL(): string; + getSQL(): string; + reset(): void; + run(values?: BindParams): void; + step(): boolean; + } + + declare export class StatementIterator implements Iterable { + @@iterator: () => Iterator; + getRemainingSql(): string; + next(): StatementIteratorResult; + } + + declare export default InitSqlJsStatic; +}