Skip to content

Instantly share code, notes, and snippets.

@mrmurphy
Created February 23, 2019 05:10
Show Gist options
  • Save mrmurphy/8f9e841d10527ac56cfdbcf6254b5cac to your computer and use it in GitHub Desktop.
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
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