mirror of
https://github.com/actualbudget/actual.git
synced 2026-03-11 12:43:09 -05:00
[TypeScript] Make db.firstSync generic
This commit is contained in:
@@ -35,12 +35,14 @@ function calcBufferedAmount(
|
||||
return buffered + amount;
|
||||
}
|
||||
|
||||
function getBudgetTable(): string {
|
||||
type BudgetTable = 'reflect_budgets' | 'zero_budgets';
|
||||
|
||||
function getBudgetTable(): BudgetTable {
|
||||
return isReflectBudget() ? 'reflect_budgets' : 'zero_budgets';
|
||||
}
|
||||
|
||||
export function isReflectBudget(): boolean {
|
||||
const budgetType = db.firstSync(
|
||||
const budgetType = db.firstSync<Pick<db.DbPreference, 'value'>>(
|
||||
`SELECT value FROM preferences WHERE id = ?`,
|
||||
['budgetType'],
|
||||
);
|
||||
@@ -91,7 +93,7 @@ export function getBudget({
|
||||
month: string;
|
||||
}): number {
|
||||
const table = getBudgetTable();
|
||||
const existing = db.firstSync(
|
||||
const existing = db.firstSync<db.DbZeroBudget | db.DbReflectBudget>(
|
||||
`SELECT * FROM ${table} WHERE month = ? AND category = ?`,
|
||||
[dbMonth(month), category],
|
||||
);
|
||||
@@ -110,10 +112,12 @@ export function setBudget({
|
||||
amount = safeNumber(typeof amount === 'number' ? amount : 0);
|
||||
const table = getBudgetTable();
|
||||
|
||||
const existing = db.firstSync(
|
||||
`SELECT id FROM ${table} WHERE month = ? AND category = ?`,
|
||||
[dbMonth(month), category],
|
||||
);
|
||||
const existing = db.firstSync<
|
||||
Pick<db.DbZeroBudget | db.DbReflectBudget, 'id'>
|
||||
>(`SELECT id FROM ${table} WHERE month = ? AND category = ?`, [
|
||||
dbMonth(month),
|
||||
category,
|
||||
]);
|
||||
if (existing) {
|
||||
return db.update(table, { id: existing.id, amount });
|
||||
}
|
||||
@@ -127,10 +131,12 @@ export function setBudget({
|
||||
|
||||
export function setGoal({ month, category, goal, long_goal }): Promise<void> {
|
||||
const table = getBudgetTable();
|
||||
const existing = db.firstSync(
|
||||
`SELECT id FROM ${table} WHERE month = ? AND category = ?`,
|
||||
[dbMonth(month), category],
|
||||
);
|
||||
const existing = db.firstSync<
|
||||
Pick<db.DbZeroBudget | db.DbReflectBudget, 'id'>
|
||||
>(`SELECT id FROM ${table} WHERE month = ? AND category = ?`, [
|
||||
dbMonth(month),
|
||||
category,
|
||||
]);
|
||||
if (existing) {
|
||||
return db.update(table, {
|
||||
id: existing.id,
|
||||
@@ -148,7 +154,7 @@ export function setGoal({ month, category, goal, long_goal }): Promise<void> {
|
||||
}
|
||||
|
||||
export function setBuffer(month: string, amount: unknown): Promise<void> {
|
||||
const existing = db.firstSync(
|
||||
const existing = db.firstSync<Pick<db.DbZeroBudget, 'id'>>(
|
||||
`SELECT id FROM zero_budget_months WHERE id = ?`,
|
||||
[month],
|
||||
);
|
||||
@@ -167,10 +173,12 @@ function setCarryover(
|
||||
month: string,
|
||||
flag: boolean,
|
||||
): Promise<void> {
|
||||
const existing = db.firstSync(
|
||||
`SELECT id FROM ${table} WHERE month = ? AND category = ?`,
|
||||
[month, category],
|
||||
);
|
||||
const existing = db.firstSync<
|
||||
Pick<db.DbZeroBudget | db.DbReflectBudget, 'id'>
|
||||
>(`SELECT id FROM ${table} WHERE month = ? AND category = ?`, [
|
||||
month,
|
||||
category,
|
||||
]);
|
||||
if (existing) {
|
||||
return db.update(table, { id: existing.id, carryover: flag ? 1 : 0 });
|
||||
}
|
||||
@@ -547,9 +555,10 @@ async function addMovementNotes({
|
||||
|
||||
const monthBudgetNotesId = `budget-${month}`;
|
||||
const existingMonthBudgetNotes = addNewLine(
|
||||
db.firstSync(`SELECT n.note FROM notes n WHERE n.id = ?`, [
|
||||
monthBudgetNotesId,
|
||||
])?.note,
|
||||
db.firstSync<Pick<db.DbNote, 'note'>>(
|
||||
`SELECT n.note FROM notes n WHERE n.id = ?`,
|
||||
[monthBudgetNotesId],
|
||||
)?.note,
|
||||
);
|
||||
|
||||
const displayDay = monthUtils.format(monthUtils.currentDate(), 'MMMM dd');
|
||||
|
||||
@@ -174,12 +174,9 @@ export async function first<T>(sql, params?: (string | number)[]) {
|
||||
|
||||
// The underlying sql system is now sync, but we can't update `first` yet
|
||||
// without auditing all uses of it
|
||||
export function firstSync(sql, params?: (string | number)[]) {
|
||||
const arr = runQuery(sql, params, true);
|
||||
// TODO: In the next phase, we will make this function generic
|
||||
// and pass the type of the return type to `runQuery`.
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return arr.length === 0 ? null : (arr[0] as any);
|
||||
export function firstSync<T>(sql, params?: (string | number)[]) {
|
||||
const arr = runQuery<T>(sql, params, true);
|
||||
return arr.length === 0 ? null : arr[0];
|
||||
}
|
||||
|
||||
// This function is marked as async because `runQuery` is no longer
|
||||
|
||||
Reference in New Issue
Block a user