diff --git a/packages/loot-core/src/shared/query.ts b/packages/loot-core/src/shared/query.ts index 0dd129b586..92c814ae96 100644 --- a/packages/loot-core/src/shared/query.ts +++ b/packages/loot-core/src/shared/query.ts @@ -1,7 +1,11 @@ +import { WithRequired } from '../types/util'; + // @ts-strict-ignore export type QueryState = { + table: string; + tableOptions: object; filterExpressions: Array; - selectExpressions: Array; + selectExpressions: Array | unknown | '*' | string; groupExpressions: Array; orderExpressions: Array; calculation: boolean; @@ -15,8 +19,9 @@ export type QueryState = { export class Query { state: QueryState; - constructor(state) { + constructor(state: WithRequired, 'table'>) { this.state = { + tableOptions: state.tableOptions || {}, filterExpressions: state.filterExpressions || [], selectExpressions: state.selectExpressions || [], groupExpressions: state.groupExpressions || [], @@ -48,12 +53,22 @@ export class Query { }); } - select(exprs: Array | unknown = []) { + select( + exprs: + | Array + | Array> + | Record + | '*' + | string = [], + ) { + let exprsToUse: Array; if (!Array.isArray(exprs)) { - exprs = [exprs]; + exprsToUse = [exprs]; + } else { + exprsToUse = exprs; } - const query = new Query({ ...this.state, selectExpressions: exprs }); + const query = new Query({ ...this.state, selectExpressions: exprsToUse }); query.state.calculation = false; return query; } @@ -86,11 +101,11 @@ export class Query { }); } - limit(num) { + limit(num: number) { return new Query({ ...this.state, limit: num }); } - offset(num) { + offset(num: number) { return new Query({ ...this.state, offset: num }); } @@ -106,7 +121,7 @@ export class Query { return new Query({ ...this.state, validateRefs: false }); } - options(opts) { + options(opts: object) { return new Query({ ...this.state, tableOptions: opts }); }