@actual-app/api 1.1.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,7 @@ class Query {
8
8
  calculation: false,
9
9
  rawMode: false,
10
10
  withDead: false,
11
+ validateRefs: true,
11
12
  limit: null,
12
13
  offset: null,
13
14
  ...state
@@ -85,6 +86,10 @@ class Query {
85
86
  return new Query({ ...this.state, withDead: true });
86
87
  }
87
88
 
89
+ withoutValidatedRefs() {
90
+ return new Query({ ...this.state, validateRefs: false });
91
+ }
92
+
88
93
  options(opts) {
89
94
  return new Query({ ...this.state, tableOptions: opts });
90
95
  }
@@ -94,6 +99,24 @@ class Query {
94
99
  }
95
100
  }
96
101
 
102
+ function getPrimaryOrderBy(query, defaultOrderBy) {
103
+ let orderExprs = query.serialize().orderExpressions;
104
+ if (orderExprs.length === 0) {
105
+ if (defaultOrderBy) {
106
+ return { order: 'asc', ...defaultOrderBy };
107
+ }
108
+ return null;
109
+ }
110
+
111
+ let firstOrder = orderExprs[0];
112
+ if (typeof firstOrder === 'string') {
113
+ return { field: firstOrder, order: 'asc' };
114
+ }
115
+ // Handle this form: { field: 'desc' }
116
+ let [field] = Object.keys(firstOrder);
117
+ return { field, order: firstOrder[field] };
118
+ }
119
+
97
120
  module.exports = function q(table) {
98
121
  return new Query({ table });
99
- }
122
+ };
Binary file
package/index.js ADDED
@@ -0,0 +1,10 @@
1
+ const { init, shutdown } = require('./local');
2
+ const methods = require('./methods');
3
+ const utils = require('./utils');
4
+
5
+ module.exports = {
6
+ init,
7
+ shutdown,
8
+ utils,
9
+ ...methods
10
+ };
package/injected.js ADDED
@@ -0,0 +1,5 @@
1
+ // TODO: comment on why it works this way
2
+
3
+ let send;
4
+
5
+ module.exports = { send };
package/local.js ADDED
@@ -0,0 +1,28 @@
1
+ let injected = require('./injected');
2
+ let actualApp;
3
+
4
+ async function init({ budgetId, config } = {}) {
5
+ if (actualApp) {
6
+ return;
7
+ }
8
+
9
+ process.traceProcessWarnings = true;
10
+
11
+ global.fetch = require('node-fetch');
12
+ let bundle = require('./app/bundle.api.js');
13
+
14
+ await bundle.init({ budgetId, config });
15
+ actualApp = bundle.lib;
16
+
17
+ injected.send = bundle.lib.send;
18
+ return bundle.lib;
19
+ }
20
+
21
+ async function shutdown() {
22
+ if (actualApp) {
23
+ await actualApp.send('close-budget');
24
+ actualApp = null;
25
+ }
26
+ }
27
+
28
+ module.exports = { init, shutdown };
@@ -1,12 +1,20 @@
1
- const {
2
- init,
3
- send,
4
- disconnect,
5
- runWithBudget,
6
- runImport
7
- } = require('./connection');
8
- const q = require('./query');
9
- const utils = require('./utils');
1
+ const q = require('./app/query');
2
+ const injected = require('./injected');
3
+
4
+ function send(name, args) {
5
+ return injected.send(name, args);
6
+ }
7
+
8
+ async function runImport(name, func) {
9
+ await send('api/start-import', { budgetName: name });
10
+ try {
11
+ await func();
12
+ } catch (e) {
13
+ await send('api/abort-import');
14
+ throw e;
15
+ }
16
+ await send('api/finish-import');
17
+ }
10
18
 
11
19
  async function loadBudget(budgetId) {
12
20
  return send('api/load-budget', { id: budgetId });
@@ -158,11 +166,7 @@ function deletePayeeRule(id) {
158
166
  }
159
167
 
160
168
  module.exports = {
161
- init,
162
- disconnect,
163
- runWithBudget,
164
169
  runImport,
165
- utils,
166
170
 
167
171
  runQuery,
168
172
  q,
File without changes
@@ -0,0 +1,5 @@
1
+ BEGIN TRANSACTION;
2
+
3
+ DROP TABLE db_version;
4
+
5
+ COMMIT;
@@ -0,0 +1,23 @@
1
+ BEGIN TRANSACTION;
2
+
3
+ CREATE TABLE payees
4
+ (id TEXT PRIMARY KEY,
5
+ name TEXT,
6
+ category TEXT,
7
+ tombstone INTEGER DEFAULT 0,
8
+ transfer_acct TEXT);
9
+
10
+ CREATE TABLE payee_rules
11
+ (id TEXT PRIMARY KEY,
12
+ payee_id TEXT,
13
+ type TEXT,
14
+ value TEXT,
15
+ tombstone INTEGER DEFAULT 0);
16
+
17
+ CREATE INDEX payee_rules_lowercase_index ON payee_rules(LOWER(value));
18
+
19
+ CREATE TABLE payee_mapping
20
+ (id TEXT PRIMARY KEY,
21
+ targetId TEXT);
22
+
23
+ COMMIT;
@@ -0,0 +1,25 @@
1
+ BEGIN TRANSACTION;
2
+
3
+ CREATE TEMPORARY TABLE category_groups_tmp
4
+ (id TEXT PRIMARY KEY,
5
+ name TEXT UNIQUE,
6
+ is_income INTEGER DEFAULT 0,
7
+ sort_order REAL,
8
+ tombstone INTEGER DEFAULT 0);
9
+
10
+ INSERT INTO category_groups_tmp SELECT * FROM category_groups;
11
+
12
+ DROP TABLE category_groups;
13
+
14
+ CREATE TABLE category_groups
15
+ (id TEXT PRIMARY KEY,
16
+ name TEXT,
17
+ is_income INTEGER DEFAULT 0,
18
+ sort_order REAL,
19
+ tombstone INTEGER DEFAULT 0);
20
+
21
+ INSERT INTO category_groups SELECT * FROM category_groups_tmp;
22
+
23
+ DROP TABLE category_groups_tmp;
24
+
25
+ COMMIT;
@@ -0,0 +1,7 @@
1
+ BEGIN TRANSACTION;
2
+
3
+ CREATE INDEX trans_category_date ON transactions(category, date);
4
+ CREATE INDEX trans_category ON transactions(category);
5
+ CREATE INDEX trans_date ON transactions(date);
6
+
7
+ COMMIT;
@@ -0,0 +1,38 @@
1
+ BEGIN TRANSACTION;
2
+
3
+ DELETE FROM spreadsheet_cells WHERE
4
+ name NOT LIKE '%!budget\_%' ESCAPE '\' AND
5
+ name NOT LIKE '%!carryover\_%' ESCAPE '\' AND
6
+ name NOT LIKE '%!buffered';
7
+
8
+ UPDATE OR REPLACE spreadsheet_cells SET name = REPLACE(name, '_', '-');
9
+
10
+ UPDATE OR REPLACE spreadsheet_cells SET
11
+ name =
12
+ SUBSTR(name, 1, 28) ||
13
+ '-' ||
14
+ SUBSTR(name, 29, 4) ||
15
+ '-' ||
16
+ SUBSTR(name, 33, 4) ||
17
+ '-' ||
18
+ SUBSTR(name, 37, 4) ||
19
+ '-' ||
20
+ SUBSTR(name, 41, 12)
21
+ WHERE name LIKE '%!budget-%' AND LENGTH(name) = 52;
22
+
23
+ UPDATE OR REPLACE spreadsheet_cells SET
24
+ name =
25
+ SUBSTR(name, 1, 31) ||
26
+ '-' ||
27
+ SUBSTR(name, 32, 4) ||
28
+ '-' ||
29
+ SUBSTR(name, 36, 4) ||
30
+ '-' ||
31
+ SUBSTR(name, 40, 4) ||
32
+ '-' ||
33
+ SUBSTR(name, 44, 12)
34
+ WHERE name LIKE '%!carryover-%' AND LENGTH(name) = 55;
35
+
36
+ UPDATE spreadsheet_cells SET expr = SUBSTR(expr, 2) WHERE name LIKE '%!carryover-%';
37
+
38
+ COMMIT;
@@ -0,0 +1,6 @@
1
+ BEGIN TRANSACTION;
2
+
3
+ ALTER TABLE transactions ADD COLUMN cleared INTEGER DEFAULT 1;
4
+ ALTER TABLE transactions ADD COLUMN pending INTEGER DEFAULT 0;
5
+
6
+ COMMIT;
@@ -0,0 +1,10 @@
1
+ BEGIN TRANSACTION;
2
+
3
+ CREATE TABLE rules
4
+ (id TEXT PRIMARY KEY,
5
+ stage TEXT,
6
+ conditions TEXT,
7
+ actions TEXT,
8
+ tombstone INTEGER DEFAULT 0);
9
+
10
+ COMMIT;
@@ -0,0 +1,13 @@
1
+ BEGIN TRANSACTION;
2
+
3
+ ALTER TABLE transactions ADD COLUMN parent_id TEXT;
4
+
5
+ UPDATE transactions SET
6
+ parent_id = CASE
7
+ WHEN isChild THEN SUBSTR(id, 1, INSTR(id, '/') - 1)
8
+ ELSE NULL
9
+ END;
10
+
11
+ CREATE INDEX trans_parent_id ON transactions(parent_id);
12
+
13
+ COMMIT;
@@ -0,0 +1,56 @@
1
+ BEGIN TRANSACTION;
2
+
3
+ DROP VIEW IF EXISTS v_transactions_layer2;
4
+ CREATE VIEW v_transactions_layer2 AS
5
+ SELECT
6
+ t.id AS id,
7
+ t.isParent AS is_parent,
8
+ t.isChild AS is_child,
9
+ t.acct AS account,
10
+ CASE WHEN t.isChild = 0 THEN NULL ELSE t.parent_id END AS parent_id,
11
+ CASE WHEN t.isParent = 1 THEN NULL ELSE cm.transferId END AS category,
12
+ pm.targetId AS payee,
13
+ t.imported_description AS imported_payee,
14
+ IFNULL(t.amount, 0) AS amount,
15
+ t.notes AS notes,
16
+ t.date AS date,
17
+ t.financial_id AS imported_id,
18
+ t.error AS error,
19
+ t.starting_balance_flag AS starting_balance_flag,
20
+ t.transferred_id AS transfer_id,
21
+ t.sort_order AS sort_order,
22
+ t.cleared AS cleared,
23
+ t.tombstone AS tombstone
24
+ FROM transactions t
25
+ LEFT JOIN category_mapping cm ON cm.id = t.category
26
+ LEFT JOIN payee_mapping pm ON pm.id = t.description
27
+ WHERE
28
+ t.date IS NOT NULL AND
29
+ t.acct IS NOT NULL;
30
+
31
+ CREATE INDEX trans_sorted ON transactions(date desc, starting_balance_flag, sort_order desc, id);
32
+
33
+ DROP VIEW IF EXISTS v_transactions_layer1;
34
+ CREATE VIEW v_transactions_layer1 AS
35
+ SELECT t.* FROM v_transactions_layer2 t
36
+ LEFT JOIN transactions t2 ON (t.is_child = 1 AND t2.id = t.parent_id)
37
+ WHERE IFNULL(t.tombstone, 0) = 0 AND IFNULL(t2.tombstone, 0) = 0;
38
+
39
+ DROP VIEW IF EXISTS v_transactions;
40
+ CREATE VIEW v_transactions AS
41
+ SELECT t.* FROM v_transactions_layer1 t
42
+ ORDER BY t.date desc, t.starting_balance_flag, t.sort_order desc, t.id;
43
+
44
+
45
+ DROP VIEW IF EXISTS v_categories;
46
+ CREATE VIEW v_categories AS
47
+ SELECT
48
+ id,
49
+ name,
50
+ is_income,
51
+ cat_group AS "group",
52
+ sort_order,
53
+ tombstone
54
+ FROM categories;
55
+
56
+ COMMIT;
@@ -0,0 +1,7 @@
1
+ BEGIN TRANSACTION;
2
+
3
+ CREATE INDEX messages_crdt_search ON messages_crdt(dataset, row, column, timestamp);
4
+
5
+ ANALYZE;
6
+
7
+ COMMIT;
@@ -0,0 +1,33 @@
1
+ BEGIN TRANSACTION;
2
+
3
+ -- This adds the isChild/parent_id constraint in `where`
4
+ DROP VIEW IF EXISTS v_transactions_layer2;
5
+ CREATE VIEW v_transactions_layer2 AS
6
+ SELECT
7
+ t.id AS id,
8
+ t.isParent AS is_parent,
9
+ t.isChild AS is_child,
10
+ t.acct AS account,
11
+ CASE WHEN t.isChild = 0 THEN NULL ELSE t.parent_id END AS parent_id,
12
+ CASE WHEN t.isParent = 1 THEN NULL ELSE cm.transferId END AS category,
13
+ pm.targetId AS payee,
14
+ t.imported_description AS imported_payee,
15
+ IFNULL(t.amount, 0) AS amount,
16
+ t.notes AS notes,
17
+ t.date AS date,
18
+ t.financial_id AS imported_id,
19
+ t.error AS error,
20
+ t.starting_balance_flag AS starting_balance_flag,
21
+ t.transferred_id AS transfer_id,
22
+ t.sort_order AS sort_order,
23
+ t.cleared AS cleared,
24
+ t.tombstone AS tombstone
25
+ FROM transactions t
26
+ LEFT JOIN category_mapping cm ON cm.id = t.category
27
+ LEFT JOIN payee_mapping pm ON pm.id = t.description
28
+ WHERE
29
+ t.date IS NOT NULL AND
30
+ t.acct IS NOT NULL AND
31
+ (t.isChild = 0 OR t.parent_id IS NOT NULL);
32
+
33
+ COMMIT;
@@ -0,0 +1,10 @@
1
+ BEGIN TRANSACTION;
2
+
3
+ CREATE TABLE __meta__ (key TEXT PRIMARY KEY, value TEXT);
4
+
5
+ DROP VIEW IF EXISTS v_transactions_layer2;
6
+ DROP VIEW IF EXISTS v_transactions_layer1;
7
+ DROP VIEW IF EXISTS v_transactions;
8
+ DROP VIEW IF EXISTS v_categories;
9
+
10
+ COMMIT;
@@ -0,0 +1,5 @@
1
+ BEGIN TRANSACTION;
2
+
3
+ ALTER TABLE accounts ADD COLUMN sort_order REAL;
4
+
5
+ COMMIT;
@@ -0,0 +1,28 @@
1
+ BEGIN TRANSACTION;
2
+
3
+ CREATE TABLE schedules
4
+ (id TEXT PRIMARY KEY,
5
+ rule TEXT,
6
+ active INTEGER DEFAULT 0,
7
+ completed INTEGER DEFAULT 0,
8
+ posts_transaction INTEGER DEFAULT 0,
9
+ tombstone INTEGER DEFAULT 0);
10
+
11
+ CREATE TABLE schedules_next_date
12
+ (id TEXT PRIMARY KEY,
13
+ schedule_id TEXT,
14
+ local_next_date INTEGER,
15
+ local_next_date_ts INTEGER,
16
+ base_next_date INTEGER,
17
+ base_next_date_ts INTEGER);
18
+
19
+ CREATE TABLE schedules_json_paths
20
+ (schedule_id TEXT PRIMARY KEY,
21
+ payee TEXT,
22
+ account TEXT,
23
+ amount TEXT,
24
+ date TEXT);
25
+
26
+ ALTER TABLE transactions ADD COLUMN schedule TEXT;
27
+
28
+ COMMIT;
@@ -0,0 +1,135 @@
1
+ export default async function runMigration(db, uuid) {
2
+ function getValue(node) {
3
+ return node.expr != null ? node.expr : node.cachedValue;
4
+ }
5
+
6
+ db.execQuery(`
7
+ CREATE TABLE zero_budget_months
8
+ (id TEXT PRIMARY KEY,
9
+ buffered INTEGER DEFAULT 0);
10
+
11
+ CREATE TABLE zero_budgets
12
+ (id TEXT PRIMARY KEY,
13
+ month INTEGER,
14
+ category TEXT,
15
+ amount INTEGER DEFAULT 0,
16
+ carryover INTEGER DEFAULT 0);
17
+
18
+ CREATE TABLE reflect_budgets
19
+ (id TEXT PRIMARY KEY,
20
+ month INTEGER,
21
+ category TEXT,
22
+ amount INTEGER DEFAULT 0,
23
+ carryover INTEGER DEFAULT 0);
24
+
25
+ CREATE TABLE notes
26
+ (id TEXT PRIMARY KEY,
27
+ note TEXT);
28
+
29
+ CREATE TABLE kvcache (key TEXT PRIMARY KEY, value TEXT);
30
+ CREATE TABLE kvcache_key (id INTEGER PRIMARY KEY, key REAL);
31
+ `);
32
+
33
+ // Migrate budget amounts and carryover
34
+ let budget = db.runQuery(
35
+ `SELECT * FROM spreadsheet_cells WHERE name LIKE 'budget%!budget-%'`,
36
+ [],
37
+ true
38
+ );
39
+ db.transaction(() => {
40
+ budget.map(monthBudget => {
41
+ let match = monthBudget.name.match(
42
+ /^(budget-report|budget)(\d+)!budget-(.+)$/
43
+ );
44
+ if (match == null) {
45
+ console.log('Warning: invalid budget month name', monthBudget.name);
46
+ return;
47
+ }
48
+
49
+ let type = match[1];
50
+ let month = match[2].slice(0, 4) + '-' + match[2].slice(4);
51
+ let dbmonth = parseInt(match[2]);
52
+ let cat = match[3];
53
+
54
+ let amount = parseInt(getValue(monthBudget));
55
+ if (isNaN(amount)) {
56
+ amount = 0;
57
+ }
58
+
59
+ let sheetName = monthBudget.name.split('!')[0];
60
+ let carryover = db.runQuery(
61
+ 'SELECT * FROM spreadsheet_cells WHERE name = ?',
62
+ [`${sheetName}!carryover-${cat}`],
63
+ true
64
+ );
65
+
66
+ let table = type === 'budget-report' ? 'reflect_budgets' : 'zero_budgets';
67
+ db.runQuery(
68
+ `INSERT INTO ${table} (id, month, category, amount, carryover) VALUES (?, ?, ?, ?, ?)`,
69
+ [
70
+ `${month}-${cat}`,
71
+ dbmonth,
72
+ cat,
73
+ amount,
74
+ carryover.length > 0 && getValue(carryover[0]) === 'true' ? 1 : 0
75
+ ]
76
+ );
77
+ });
78
+ });
79
+
80
+ // Migrate buffers
81
+ let buffers = db.runQuery(
82
+ `SELECT * FROM spreadsheet_cells WHERE name LIKE 'budget%!buffered'`,
83
+ [],
84
+ true
85
+ );
86
+ db.transaction(() => {
87
+ buffers.map(buffer => {
88
+ let match = buffer.name.match(/^budget(\d+)!buffered$/);
89
+ if (match) {
90
+ let month = match[1].slice(0, 4) + '-' + match[1].slice(4);
91
+ let amount = parseInt(getValue(buffer));
92
+ if (isNaN(amount)) {
93
+ amount = 0;
94
+ }
95
+
96
+ db.runQuery(
97
+ `INSERT INTO zero_budget_months (id, buffered) VALUES (?, ?)`,
98
+ [month, amount]
99
+ );
100
+ }
101
+ });
102
+ });
103
+
104
+ // Migrate notes
105
+ let notes = db.runQuery(
106
+ `SELECT * FROM spreadsheet_cells WHERE name LIKE 'notes!%'`,
107
+ [],
108
+ true
109
+ );
110
+
111
+ let parseNote = str => {
112
+ try {
113
+ let value = JSON.parse(str);
114
+ return value && value !== '' ? value : null;
115
+ } catch (e) {
116
+ return null;
117
+ }
118
+ };
119
+
120
+ db.transaction(() => {
121
+ notes.forEach(note => {
122
+ let parsed = parseNote(getValue(note));
123
+ if (parsed) {
124
+ let [, id] = note.name.split('!');
125
+ db.runQuery(`INSERT INTO notes (id, note) VALUES (?, ?)`, [id, parsed]);
126
+ }
127
+ });
128
+ });
129
+
130
+ db.execQuery(`
131
+ DROP TABLE spreadsheet_cells;
132
+ ANALYZE;
133
+ VACUUM;
134
+ `);
135
+ }
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@actual-app/api",
3
- "version": "1.1.1",
3
+ "version": "2.0.0",
4
4
  "description": "An API for Actual",
5
5
  "main": "api.js",
6
6
  "license": "MIT",
7
7
  "dependencies": {
8
- "node-ipc": "9.1.1",
9
- "uuid": "3.3.2"
8
+ "better-sqlite3": "^7.5.0",
9
+ "uuid": "3.3.2",
10
+ "node-fetch": "^1.6.3"
10
11
  }
11
12
  }
package/test.js ADDED
@@ -0,0 +1,8 @@
1
+ let api = require('./index');
2
+
3
+ async function run() {
4
+ let app = await api.init({ config: { dataDir: '/tmp' } });
5
+ await app.send('create-budget', { testMode: true });
6
+ }
7
+
8
+ run();