Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | 54x 54x 784x 137604x 137604x 137604x 137604x 137604x 784x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 784x 49345x 39x 39x 39x 49345x 784x 49345x 49345x 49345x 10988x 10988x 10988x 10988x 10988x 49345x 784x 14944x 14944x 14944x 14944x 34401x 34401x 34401x 34401x 34401x 34401x 34401x 34401x 34401x 34403x 34401x 14944x 14944x 784x 112638x 112638x 1751x 1751x 112638x 14944x 14944x 112638x 93368x 93368x 112638x 1629x 1629x 112638x 54x 54x 112638x 892x 892x 892x 892x 112638x 112638x 112638x 784x 391x 391x 391x 391x 391x 391x 112638x 112638x 14944x 14944x 112635x 14944x 14944x 34401x 34401x 34401x 34393x 34393x 34393x 34393x 34401x 34401x 34401x 34401x 34401x 34401x 206362x 206362x 206362x 34401x 34401x 14944x 16909x 14941x 14941x 14941x 112638x 391x 391x 784x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 361x 361x 391x 391x 405x 405x 405x 391x 391x 391x 391x 391x 391x 391x 391x 112638x 112638x 112638x 112638x 112638x 391x 391x 493x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 784x 54x | 'use strict';
module.exports = ({ numeric: { decimal: { compareDecimals, parseDecimal }, }, accounting: { prepareJournal }, decimalSqlFunctions: { registerDecimalFunctions }, postingBalanceMaterializer: { materializePostingBalances }, valuationPriceMaterializer: { materializeValuationPrices }, databaseMigration: { SCHEMA_VERSION, migrateDatabase }, external: { path, sqlite: Database }, }) => {
function amountFields(amount) {
return {
quantity: amount ? amount.quantity : null,
commodity: amount ? amount.commodity : null,
};
}
function prepareStatements(database) {
return {
databaseMetadata: database.prepare('INSERT INTO database_metadata (key, value) VALUES (?, ?)'),
sourceFile: database.prepare(`
INSERT INTO source_files (id, path, sha256, size)
VALUES (?, ?, ?, ?)
`),
fileComment: database.prepare(`
INSERT INTO file_comments (entry_id, text)
VALUES (?, ?)
`),
journalEntry: database.prepare(`
INSERT INTO journal_entries (id, source_file_id, line)
VALUES (?, ?, ?)
`),
transaction: database.prepare(`
INSERT INTO transactions
(entry_id, date, description)
VALUES (?, ?, ?)
`),
posting: database.prepare(`
INSERT INTO postings
(id, transaction_id, position, posting_date, account,
amount_quantity, amount_commodity,
lot_cost_quantity, lot_cost_commodity, lot_cost_is_total,
cost_quantity, cost_commodity, cost_is_total,
balance_quantity, balance_commodity)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`),
comment: database.prepare(`
INSERT INTO comments (transaction_id, posting_id, position, text)
VALUES (?, ?, ?, ?)
`),
entryTag: database.prepare(`
INSERT INTO tags (transaction_id, posting_id, position, ordinal, name, value)
VALUES (?, ?, ?, ?, ?, ?)
`),
resolvedPostingAmount: database.prepare(`
INSERT INTO resolved_posting_amounts (id, posting_id, position, amount_quantity, amount_commodity)
VALUES (?, ?, ?, ?, ?)
`),
price: database.prepare(`
INSERT INTO prices
(entry_id, date, base_commodity, quote_quantity, quote_commodity, comment)
VALUES (?, ?, ?, ?, ?, ?)
`),
account: database.prepare(`
INSERT INTO account_declarations (entry_id, name, comment, used) VALUES (?, ?, ?, ?)
`),
tag: database.prepare(`
INSERT INTO tag_declarations (entry_id, name, comment, used) VALUES (?, ?, ?, ?)
`),
commodity: database.prepare(`
INSERT INTO commodity_declarations (entry_id, symbol, comment, format, is_default, used)
VALUES (?, ?, ?, ?, ?, ?)
`),
};
}
function insertComments(statement, comments, ownerLine, transactionId, postingId) {
for (const comment of comments) {
const position = comment.location.line - ownerLine;
statement.run(transactionId, postingId, position, comment.text);
}
}
function insertTags(statement, tags, ownerLine, transactionId, postingId) {
let previousPosition = -1;
let ordinal = 0;
for (const tag of tags) {
const position = tag.location.line - ownerLine;
ordinal = position === previousPosition ? ordinal + 1 : 0;
statement.run(transactionId, postingId, position, ordinal, tag.name, tag.value);
previousPosition = position;
}
}
function insertTransaction(statements, entryId, entry, counters, resolvedPostings) {
statements.transaction.run(entryId, entry.date, entry.description);
insertComments(statements.comment, entry.comments, entry.location.line, entryId, null);
insertTags(statements.entryTag, (entry.tags || []), entry.location.line, entryId, null);
entry.postings.forEach((posting, position) => {
const amount = amountFields(posting.amount);
const lotCost = amountFields(posting.lotCost && posting.lotCost.amount);
const cost = amountFields(posting.cost && posting.cost.amount);
const balance = amountFields(posting.balanceAssignment || posting.balanceAssertion);
const postingId = ++counters.posting;
statements.posting.run(postingId, entryId, position, posting.postingDate || entry.date, posting.account, amount.quantity, amount.commodity, lotCost.quantity, lotCost.commodity, posting.lotCost ? Number(posting.lotCost.total) : null, cost.quantity, cost.commodity, posting.cost ? Number(posting.cost.total) : null, balance.quantity, balance.commodity);
insertComments(statements.comment, posting.comments, posting.location.line, null, postingId);
insertTags(statements.entryTag, (posting.tags || []), posting.location.line, null, postingId);
resolvedPostings[position].forEach((resolvedAmount, amountPosition) => {
statements.resolvedPostingAmount.run(++counters.resolvedAmount, postingId, amountPosition, resolvedAmount.quantity, resolvedAmount.commodity);
});
});
}
function insertEntry(statements, entryId, entry, counters, resolvedTransactions, ignoredProperties, usage) {
switch (entry.type) {
case 'comment':
statements.fileComment.run(entryId, entry.text);
break;
case 'transaction':
insertTransaction(statements, entryId, entry, counters, resolvedTransactions.get(entry));
break;
case 'price':
statements.price.run(entryId, entry.date, entry.commodity, entry.price.quantity, entry.price.commodity, entry.comment);
break;
case 'account':
statements.account.run(entryId, entry.name, entry.comment, Number(usage.accounts.has(entry.name)));
break;
case 'tag':
statements.tag.run(entryId, entry.name, entry.comment, Number(usage.tags.has(entry.name)));
break;
case 'commodity': {
const properties = entry.properties.filter((property) => !ignoredProperties.has(property));
statements.commodity.run(entryId, entry.symbol, entry.comment, properties.slice().reverse().find((property) => property.name === 'format')?.value ?? null, Number(properties.some((property) => property.name === 'default')), Number(usage.commodities.has(entry.symbol)));
break;
}
default:
throw new Error(`Cannot store unsupported journal entry type: ${entry.type}`);
}
}
function declarationUsage(entries, resolvedTransactions) {
const usage = {
accounts: new Set(),
commodities: new Set(),
tags: new Set(),
};
for (const entry of entries) {
if (entry.type !== 'transaction')
continue;
const resolvedPostings = resolvedTransactions.get(entry);
if (!resolvedPostings)
continue;
let transactionUsed = false;
entry.postings.forEach((posting, position) => {
const postingUsed = resolvedPostings[position].some(({ quantity }) => compareDecimals(parseDecimal(quantity), parseDecimal('0')) !== 0);
if (!postingUsed)
return;
transactionUsed = true;
usage.accounts.add(posting.account);
for (const amount of [
posting.amount,
posting.lotCost?.amount,
posting.cost?.amount,
posting.balanceAssignment,
posting.balanceAssertion,
...resolvedPostings[position],
]) {
if (amount?.commodity)
usage.commodities.add(amount.commodity);
}
for (const tag of posting.tags || [])
usage.tags.add(tag.name);
});
if (transactionUsed) {
for (const tag of entry.tags || [])
usage.tags.add(tag.name);
}
}
return usage;
}
function writeJournalDatabase(databasePath, journal) {
const { entries: storableEntries, resolvedTransactions, valuationCommodity, ignoredProperties, warnings, } = prepareJournal(journal);
const usage = declarationUsage(storableEntries, resolvedTransactions);
const resolvedDatabasePath = path.resolve(databasePath);
const database = new Database(resolvedDatabasePath);
database.pragma('foreign_keys = ON');
registerDecimalFunctions(database);
try {
migrateDatabase(database);
const statements = prepareStatements(database);
const insertWarning = database.prepare(`
INSERT INTO ingestion_warnings
(position, code, message, source, line, column, start_line, end_line)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
`);
const replaceContents = database.transaction(() => {
database.exec(`
DELETE FROM valuation_prices;
DELETE FROM ingestion_warnings;
DELETE FROM journal_entries;
DELETE FROM source_files;
DELETE FROM database_metadata;
`);
statements.databaseMetadata.run('schema_version', SCHEMA_VERSION);
statements.databaseMetadata.run('root_path', journal.journalPath);
statements.databaseMetadata.run('built_at', new Date().toISOString());
if (valuationCommodity) {
statements.databaseMetadata.run('valuation_commodity', valuationCommodity);
}
const sourceIds = new Map();
journal.files.forEach((file, index) => {
const fileId = index + 1;
statements.sourceFile.run(fileId, file.path, file.sha256, file.size);
sourceIds.set(file.path, fileId);
});
const counters = {
posting: 0,
resolvedAmount: 0,
postingBalance: 0,
valuationPrice: 0,
};
storableEntries.forEach((entry, index) => {
const sourceFileId = sourceIds.get(entry.location.source);
if (sourceFileId === undefined) {
throw new Error(`Journal entry refers to an unregistered source file: ${entry.location.source}`);
}
const entryId = index + 1;
statements.journalEntry.run(entryId, sourceFileId, entry.location.line);
insertEntry(statements, entryId, entry, counters, resolvedTransactions, ignoredProperties, usage);
});
warnings.forEach((warning, position) => {
insertWarning.run(position, warning.code, warning.message, warning.source, warning.line, warning.column, warning.startLine, warning.endLine);
});
counters.postingBalance = materializePostingBalances(database);
counters.valuationPrice = materializeValuationPrices(database, valuationCommodity);
return counters;
});
const counters = replaceContents.immediate();
return {
databasePath: resolvedDatabasePath,
journalPath: journal.journalPath,
files: journal.files.length,
entries: storableEntries.length,
transactions: storableEntries.filter((entry) => entry.type === 'transaction').length,
postings: counters.posting,
postingAmounts: counters.resolvedAmount,
postingBalances: counters.postingBalance,
prices: storableEntries.filter((entry) => entry.type === 'price').length,
valuationCommodity,
valuationPrices: counters.valuationPrice,
warnings: warnings,
};
}
finally {
database.close();
}
}
return { writeJournalDatabase };
};
|