Created
February 23, 2019 05:10
-
-
Save mrmurphy/8f9e841d10527ac56cfdbcf6254b5cac to your computer and use it in GitHub Desktop.
A little program to save your YNAB transactions to a SQLite Database for fast and easy querying
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ynab = require("ynab"); | |
const SQL = require("sql-template-strings"); | |
const sqlite = require("sqlite"); | |
let personalAccessToken = process.env.YNAB_ACCESS_TOKEN; | |
let client = new ynab.API(personalAccessToken); | |
async function go() { | |
const db = await sqlite.open("./transactions.sqlite"); | |
async function bootstrap() { | |
await db.run(SQL` | |
create table if not exists data ( | |
id text not null, | |
date date not null, | |
amount real not null, | |
memo text, | |
account text, | |
payee text, | |
category text | |
); | |
`); | |
} | |
async function insertData(t) { | |
await db.run(SQL` | |
insert into data (id, date, amount, memo, account, payee, category) values ( | |
${t.id}, | |
${t.date}, | |
${t.amount}, | |
${t.memo}, | |
${t.account_name}, | |
${t.payee_name}, | |
${t.category_name} | |
) | |
`); | |
} | |
await bootstrap(); | |
let { | |
data: { transactions } | |
} = await client.transactions.getTransactions( | |
// You can get this budget ID with the API as well | |
"your-budget-id-here" | |
); | |
await Promise.all(transactions.map(insertData)); | |
} | |
go(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment