Changeset View
Changeset View
Standalone View
Standalone View
web/database/utils/db-utils.js
- This file was added.
// @flow | |||||||||
import type { QueryExecResult } from 'sql.js'; | |||||||||
function parseSQLiteQueryResult<T>(result: QueryExecResult): T[] { | |||||||||
const { columns, values } = result; | |||||||||
return values.map(rowResult => { | |||||||||
const row: any = Object.fromEntries( | |||||||||
columns.map((key, index) => [key, rowResult[index]]), | |||||||||
); | |||||||||
return row; | |||||||||
}); | |||||||||
} | |||||||||
// NOTE: sql.js has behavior that when there are multiple statements in query | |||||||||
// e.g. "statement1; statement2; statement3;" | |||||||||
// and statement2 will not return anything, the result will be: | |||||||||
// [result1, result3], not [result1, undefined, result3] | |||||||||
function parseSQLiteResult<T>( | |||||||||
ashoat: It might be better to name this something more specific, eg. `parseMultiStatementSQLiteResult`… | |||||||||
ashoatUnsubmitted Not Done Inline Actions
Or I guess SQLite always returns an array of statements with an array of results with an array of columns inside it? It might be helpful to assert that T must be an Object. This will help with readability too, I think ashoat: Or I guess SQLite always returns an array of statements with an array of results with an array… | |||||||||
rawResult: $ReadOnlyArray<QueryExecResult>, | |||||||||
): T[][] { | |||||||||
return rawResult.map((queryResult: QueryExecResult) => | |||||||||
parseSQLiteQueryResult<T>(queryResult), | |||||||||
); | |||||||||
} | |||||||||
export { parseSQLiteResult }; |
It might be better to name this something more specific, eg. parseMultiStatementSQLiteResult or something