Structure coming along

This commit is contained in:
Casey Timm
2025-07-05 10:07:47 -04:00
parent 7900ecaaf8
commit 824a23765e
15 changed files with 1115 additions and 67 deletions

170
src/lib/db.js Normal file
View File

@ -0,0 +1,170 @@
// db.js
import postgres from 'postgres'
const db = postgres({ host:'192.168.1.126', username:'budget', password:'budget', database:'budget'}) // will use psql environment variables
export async function createBudgetTable() {
return await db`
create table if not exists budget (
id serial primary key,
name text not null,
amount numeric(10,2) not null,
notes text
)
`
}
export async function createBudgetTransactionTable() {
return await db`
create table if not exists budget_transaction (
id serial primary key,
budget_id integer not null references budget(id),
transaction_id text not null references transaction(id),
amount numeric(10,2) not null
)`
}
export async function addBudget(name, amount, notes) {
const result = await db`
insert into budget (name, amount, notes)
values (${name}, ${amount}, ${notes})
returning id
`
// result = Result [{ id: 1 }]
return result[0].id
}
export async function deleteBudget(id) {
const result = await db`
delete from budget
where id = ${id}
`
// result = Result [{ id: 1 }]
return result
}
export async function updateBudget(id, name, amount, notes) {
const result = await db`
update budget
set name = ${name},
amount = ${amount},
notes = ${notes}
where id = ${id}
`
// result = Result [{ id: 1 }]
return result
}
export async function getBudgetTransactions(id) {
// Fetch all transactions associated with a specific budget
try {
const transactions = await db`
select
transaction.id as id,
transaction.posted as posted,
transaction.amount as amount,
transaction.description as description,
transaction.pending as pending,
transaction.notes as notes,
budget_transaction.amount as budget_amount,
budget_transaction.id as budget_transaction_id
from budget_transaction
join transaction on budget_transaction.transaction_id = transaction.id
where budget_transaction.budget_id = ${id}
order by transaction.posted desc
`
// transactions = Result [{ id: 1, posted: 1633036800, amount: 50.00, description: "Grocery Store", pending: false, notes: "Weekly groceries" }, ...]
return { transactions }
}
catch {
await createBudgetTransactionTable();
return []
}
}
export async function deleteBudgetTransaction(budgetId, transactionId) {
// Delete a transaction from a budget
const result = await db`
delete from budget_transaction
where budget_id = ${budgetId} and transaction_id = ${transactionId}
`
// result = Result [{ id: 1 }]
return result
}
export async function addBudgetTransaction(budgetId, transactionId, amount) {
// Add a transaction to a budget
const result = await db`
insert into budget_transaction (budget_id, transaction_id, amount)
values (${budgetId}, ${transactionId}, ${amount})
returning id
`
// result = Result [{ id: 1 }]
return result[0].id
}
export async function getBudgets() {
const budgets = await db`
select
budget.id as id,
budget.name as name,
budget.amount as amount,
budget.notes as notes
from budget
`
if (!budgets) {
await createBudgetTable();
return await getBudgets()
}
// budgets = Result [{ name: "Walter", age: 80 }, { name: 'Murray', age: 68 }, ...]
return budgets
}
export async function getAccounts(age) {
const accounts = await db`
select
account.id as id,
account.name as name,
org.name as org_name,
balance,
available_balance,
balance_date
from account
left join org on org.id = account.org_id
`
// users = Result [{ name: "Walter", age: 80 }, { name: 'Murray', age: 68 }, ...]
return accounts
}
export async function getTransactions(accountId) {
let transactions = await db`
select
transaction.id as id,
transaction.posted as posted,
transaction.amount as amount,
transaction.description as description,
transaction.pending as pending,
transaction.notes as notes
from transaction
where account_id = ${accountId}
order by posted desc
`
transactions = transactions.map((t) => ({
...t, date: new Date(t.posted * 1000)
}));
return transactions
}
export async function setTransactionNote(transactionId, note) {
const result = await db`
update transaction
set notes = ${note}
where id = ${transactionId}
`
return result
}
export default db

5
src/lib/simplefin.js Normal file
View File

@ -0,0 +1,5 @@
export async function updateAccounts()
{
fetch
}