phase-8g: add sqllite.
This commit is contained in:
committed by
saikiranvella
parent
d1556f2a67
commit
c7e39c3e4e
@@ -1,39 +1,63 @@
|
||||
import { readFileSync, writeFileSync, existsSync } from 'fs';
|
||||
import type { Db } from '../db/index';
|
||||
import type { PortfolioData, PortfolioHolding } from '../types';
|
||||
|
||||
interface HoldingRow {
|
||||
ticker: string;
|
||||
shares: number;
|
||||
cost_basis: number;
|
||||
type: string;
|
||||
source: string;
|
||||
}
|
||||
|
||||
export class PortfolioRepository {
|
||||
private static readonly PORTFOLIO_PATH = './portfolio.json';
|
||||
constructor(private readonly db: Db) {}
|
||||
|
||||
exists(): boolean {
|
||||
return existsSync(PortfolioRepository.PORTFOLIO_PATH);
|
||||
const row = this.db.prepare('SELECT COUNT(*) AS n FROM holdings').get() as { n: number };
|
||||
return row.n > 0;
|
||||
}
|
||||
|
||||
read(): PortfolioData {
|
||||
if (!this.exists()) return { holdings: [] };
|
||||
return JSON.parse(readFileSync(PortfolioRepository.PORTFOLIO_PATH, 'utf8')) as PortfolioData;
|
||||
}
|
||||
|
||||
write(data: PortfolioData): void {
|
||||
writeFileSync(PortfolioRepository.PORTFOLIO_PATH, JSON.stringify(data, null, 2), 'utf8');
|
||||
const rows = this.db.prepare('SELECT * FROM holdings ORDER BY ticker').all() as HoldingRow[];
|
||||
return { holdings: rows.map(PortfolioRepository.toHolding) };
|
||||
}
|
||||
|
||||
upsert(entry: PortfolioHolding): PortfolioHolding {
|
||||
const data = this.read();
|
||||
const normalized = entry.ticker.toUpperCase().trim();
|
||||
const idx = data.holdings.findIndex((h) => h.ticker.toUpperCase() === normalized);
|
||||
const record: PortfolioHolding = { ...entry, ticker: normalized };
|
||||
if (idx >= 0) data.holdings[idx] = record;
|
||||
else data.holdings.push(record);
|
||||
this.write(data);
|
||||
return record;
|
||||
const ticker = entry.ticker.toUpperCase().trim();
|
||||
this.db
|
||||
.prepare(
|
||||
`INSERT INTO holdings (ticker, shares, cost_basis, type, source)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
ON CONFLICT(ticker) DO UPDATE SET
|
||||
shares = excluded.shares,
|
||||
cost_basis = excluded.cost_basis,
|
||||
type = excluded.type,
|
||||
source = excluded.source`,
|
||||
)
|
||||
.run(
|
||||
ticker,
|
||||
entry.shares,
|
||||
entry.costBasis ?? 0,
|
||||
entry.type ?? 'stock',
|
||||
entry.source ?? 'Manual',
|
||||
);
|
||||
return { ...entry, ticker };
|
||||
}
|
||||
|
||||
remove(ticker: string): boolean {
|
||||
const data = this.read();
|
||||
const before = data.holdings.length;
|
||||
data.holdings = data.holdings.filter((h) => h.ticker.toUpperCase() !== ticker.toUpperCase());
|
||||
if (data.holdings.length === before) return false;
|
||||
this.write(data);
|
||||
return true;
|
||||
const result = this.db
|
||||
.prepare('DELETE FROM holdings WHERE ticker = ?')
|
||||
.run(ticker.toUpperCase());
|
||||
return result.changes > 0;
|
||||
}
|
||||
|
||||
private static toHolding(row: HoldingRow): PortfolioHolding {
|
||||
return {
|
||||
ticker: row.ticker,
|
||||
shares: row.shares,
|
||||
costBasis: row.cost_basis,
|
||||
type: row.type as PortfolioHolding['type'],
|
||||
source: row.source,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user