@tasenor/common-node 1.9.32 → 1.9.34
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.
- package/dist/migrations/01_init.js +60 -0
- package/dist/src/cli.d.ts +81 -0
- package/dist/src/cli.js +242 -0
- package/dist/src/cli.js.map +1 -0
- package/dist/src/commands/account.d.ts +12 -0
- package/dist/src/commands/account.js +58 -0
- package/dist/src/commands/account.js.map +1 -0
- package/dist/src/commands/balance.d.ts +11 -0
- package/dist/src/commands/balance.js +117 -0
- package/dist/src/commands/balance.js.map +1 -0
- package/dist/src/commands/db.d.ts +14 -0
- package/dist/src/commands/db.js +69 -0
- package/dist/src/commands/db.js.map +1 -0
- package/dist/src/commands/entry.d.ts +13 -0
- package/dist/src/commands/entry.js +106 -0
- package/dist/src/commands/entry.js.map +1 -0
- package/dist/src/commands/import.d.ts +17 -0
- package/dist/src/commands/import.js +140 -0
- package/dist/src/commands/import.js.map +1 -0
- package/dist/src/commands/importer.d.ts +13 -0
- package/dist/src/commands/importer.js +71 -0
- package/dist/src/commands/importer.js.map +1 -0
- package/dist/src/commands/index.d.ts +191 -0
- package/dist/src/commands/index.js +482 -0
- package/dist/src/commands/index.js.map +1 -0
- package/dist/src/commands/period.d.ts +12 -0
- package/dist/src/commands/period.js +48 -0
- package/dist/src/commands/period.js.map +1 -0
- package/dist/src/commands/plugin.d.ts +15 -0
- package/dist/src/commands/plugin.js +78 -0
- package/dist/src/commands/plugin.js.map +1 -0
- package/dist/src/commands/report.d.ts +11 -0
- package/dist/src/commands/report.js +96 -0
- package/dist/src/commands/report.js.map +1 -0
- package/dist/src/commands/settings.d.ts +10 -0
- package/dist/src/commands/settings.js +64 -0
- package/dist/src/commands/settings.js.map +1 -0
- package/dist/src/commands/stock.d.ts +8 -0
- package/dist/src/commands/stock.js +73 -0
- package/dist/src/commands/stock.js.map +1 -0
- package/dist/src/commands/tag.d.ts +13 -0
- package/dist/src/commands/tag.js +89 -0
- package/dist/src/commands/tag.js.map +1 -0
- package/dist/src/commands/tx.d.ts +12 -0
- package/dist/src/commands/tx.js +81 -0
- package/dist/src/commands/tx.js.map +1 -0
- package/dist/src/commands/user.d.ts +12 -0
- package/dist/src/commands/user.js +52 -0
- package/dist/src/commands/user.js.map +1 -0
- package/dist/src/database/BookkeeperImporter.d.ts +77 -0
- package/dist/src/database/BookkeeperImporter.js +343 -0
- package/dist/src/database/BookkeeperImporter.js.map +1 -0
- package/dist/src/database/DB.d.ts +51 -0
- package/dist/src/database/DB.js +354 -0
- package/dist/src/database/DB.js.map +1 -0
- package/dist/src/database/index.d.ts +7 -0
- package/dist/src/database/index.js +8 -0
- package/dist/src/database/index.js.map +1 -0
- package/dist/src/doccer.d.ts +29 -0
- package/dist/src/doccer.js +30 -0
- package/dist/src/doccer.js.map +1 -0
- package/dist/src/error.d.ts +30 -0
- package/dist/src/error.js +35 -0
- package/dist/src/error.js.map +1 -0
- package/dist/src/export/Exporter.d.ts +69 -0
- package/dist/src/export/Exporter.js +123 -0
- package/dist/src/export/Exporter.js.map +1 -0
- package/dist/src/export/TasenorExporter.d.ts +55 -0
- package/dist/src/export/TasenorExporter.js +135 -0
- package/dist/src/export/TasenorExporter.js.map +1 -0
- package/dist/src/export/TilitinExporter.d.ts +71 -0
- package/dist/src/export/TilitinExporter.js +290 -0
- package/dist/src/export/TilitinExporter.js.map +1 -0
- package/dist/src/export/index.d.ts +8 -0
- package/dist/src/export/index.js +9 -0
- package/dist/src/export/index.js.map +1 -0
- package/dist/src/import/TextFileProcessHandler.d.ts +104 -0
- package/dist/src/import/TextFileProcessHandler.js +354 -0
- package/dist/src/import/TextFileProcessHandler.js.map +1 -0
- package/dist/src/import/TransactionImportConnector.d.ts +38 -0
- package/dist/src/import/TransactionImportConnector.js +27 -0
- package/dist/src/import/TransactionImportConnector.js.map +1 -0
- package/dist/src/import/TransactionImportHandler.d.ts +174 -0
- package/dist/src/import/TransactionImportHandler.js +736 -0
- package/dist/src/import/TransactionImportHandler.js.map +1 -0
- package/dist/src/import/TransactionRules.d.ts +238 -0
- package/dist/src/import/TransactionRules.js +522 -0
- package/dist/src/import/TransactionRules.js.map +1 -0
- package/dist/src/import/TransactionUI.d.ts +181 -0
- package/dist/src/import/TransactionUI.js +482 -0
- package/dist/src/import/TransactionUI.js.map +1 -0
- package/dist/src/import/TransferAnalyzer.d.ts +324 -0
- package/dist/src/import/TransferAnalyzer.js +1379 -0
- package/dist/src/import/TransferAnalyzer.js.map +1 -0
- package/dist/src/import/index.d.ts +11 -0
- package/dist/src/import/index.js +12 -0
- package/dist/src/import/index.js.map +1 -0
- package/dist/src/index.d.ts +11 -0
- package/dist/src/index.js +12 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/net/crypto.d.ts +33 -0
- package/dist/src/net/crypto.js +63 -0
- package/dist/src/net/crypto.js.map +1 -0
- package/dist/src/net/git.d.ts +49 -0
- package/dist/src/net/git.js +137 -0
- package/dist/src/net/git.js.map +1 -0
- package/dist/src/net/index.d.ts +10 -0
- package/dist/src/net/index.js +11 -0
- package/dist/src/net/index.js.map +1 -0
- package/dist/src/net/middleware.d.ts +61 -0
- package/dist/src/net/middleware.js +220 -0
- package/dist/src/net/middleware.js.map +1 -0
- package/dist/src/net/tokens.d.ts +50 -0
- package/dist/src/net/tokens.js +141 -0
- package/dist/src/net/tokens.js.map +1 -0
- package/dist/src/net/vault.d.ts +67 -0
- package/dist/src/net/vault.js +145 -0
- package/dist/src/net/vault.js.map +1 -0
- package/dist/src/plugins/BackendPlugin.d.ts +91 -0
- package/dist/src/plugins/BackendPlugin.js +165 -0
- package/dist/src/plugins/BackendPlugin.js.map +1 -0
- package/dist/src/plugins/DataPlugin.d.ts +13 -0
- package/dist/src/plugins/DataPlugin.js +26 -0
- package/dist/src/plugins/DataPlugin.js.map +1 -0
- package/dist/src/plugins/ImportPlugin.d.ts +188 -0
- package/dist/src/plugins/ImportPlugin.js +204 -0
- package/dist/src/plugins/ImportPlugin.js.map +1 -0
- package/dist/src/plugins/ReportPlugin.d.ts +132 -0
- package/dist/src/plugins/ReportPlugin.js +393 -0
- package/dist/src/plugins/ReportPlugin.js.map +1 -0
- package/dist/src/plugins/SchemePlugin.d.ts +34 -0
- package/dist/src/plugins/SchemePlugin.js +47 -0
- package/dist/src/plugins/SchemePlugin.js.map +1 -0
- package/dist/src/plugins/ServicePlugin.d.ts +80 -0
- package/dist/src/plugins/ServicePlugin.js +168 -0
- package/dist/src/plugins/ServicePlugin.js.map +1 -0
- package/dist/src/plugins/ToolPlugin.d.ts +27 -0
- package/dist/src/plugins/ToolPlugin.js +37 -0
- package/dist/src/plugins/ToolPlugin.js.map +1 -0
- package/dist/src/plugins/index.d.ts +13 -0
- package/dist/src/plugins/index.js +14 -0
- package/dist/src/plugins/index.js.map +1 -0
- package/dist/src/plugins/plugins.d.ts +101 -0
- package/dist/src/plugins/plugins.js +292 -0
- package/dist/src/plugins/plugins.js.map +1 -0
- package/dist/src/process/Process.d.ts +108 -0
- package/dist/src/process/Process.js +335 -0
- package/dist/src/process/Process.js.map +1 -0
- package/dist/src/process/ProcessConnector.d.ts +24 -0
- package/dist/src/process/ProcessConnector.js +28 -0
- package/dist/src/process/ProcessConnector.js.map +1 -0
- package/dist/src/process/ProcessFile.d.ts +69 -0
- package/dist/src/process/ProcessFile.js +145 -0
- package/dist/src/process/ProcessFile.js.map +1 -0
- package/dist/src/process/ProcessHandler.d.ts +60 -0
- package/dist/src/process/ProcessHandler.js +73 -0
- package/dist/src/process/ProcessHandler.js.map +1 -0
- package/dist/src/process/ProcessStep.d.ts +52 -0
- package/dist/src/process/ProcessStep.js +78 -0
- package/dist/src/process/ProcessStep.js.map +1 -0
- package/dist/src/process/ProcessingSystem.d.ts +60 -0
- package/dist/src/process/ProcessingSystem.js +182 -0
- package/dist/src/process/ProcessingSystem.js.map +1 -0
- package/dist/src/process/index.d.ts +11 -0
- package/dist/src/process/index.js +12 -0
- package/dist/src/process/index.js.map +1 -0
- package/dist/src/reports/conversions.d.ts +8 -0
- package/dist/src/reports/conversions.js +47 -0
- package/dist/src/reports/conversions.js.map +1 -0
- package/dist/src/reports/index.d.ts +6 -0
- package/dist/src/reports/index.js +7 -0
- package/dist/src/reports/index.js.map +1 -0
- package/dist/src/server/ISPDemoServer.d.ts +43 -0
- package/dist/src/server/ISPDemoServer.js +112 -0
- package/dist/src/server/ISPDemoServer.js.map +1 -0
- package/dist/src/server/api.d.ts +15 -0
- package/dist/src/server/api.js +27 -0
- package/dist/src/server/api.js.map +1 -0
- package/dist/src/server/index.d.ts +7 -0
- package/dist/src/server/index.js +8 -0
- package/dist/src/server/index.js.map +1 -0
- package/dist/src/server/router.d.ts +5 -0
- package/dist/src/server/router.js +37 -0
- package/dist/src/server/router.js.map +1 -0
- package/dist/src/system.d.ts +27 -0
- package/dist/src/system.js +95 -0
- package/dist/src/system.js.map +1 -0
- package/dist/tests/TransactionRules.spec.d.ts +1 -0
- package/dist/tests/TransactionRules.spec.js +64 -0
- package/dist/tests/TransactionRules.spec.js.map +1 -0
- package/dist/tests/password.spec.d.ts +1 -0
- package/dist/tests/password.spec.js +8 -0
- package/dist/tests/password.spec.js.map +1 -0
- package/dist/tests/tokens.spec.d.ts +1 -0
- package/dist/tests/tokens.spec.js +49 -0
- package/dist/tests/tokens.spec.js.map +1 -0
- package/dist/tests/vault.spec.d.ts +1 -0
- package/dist/tests/vault.spec.js +19 -0
- package/dist/tests/vault.spec.js.map +1 -0
- package/package.json +3 -3
- package/src/import/TransactionImportHandler.ts +5 -1
- package/src/index.ts +0 -1
- package/src/testing/ProcessingSystemMock.ts +0 -45
- package/src/testing/UnitTestImportConnector.ts +0 -86
- package/src/testing/UnitTester.ts +0 -231
- package/src/testing/index.ts +0 -4
- package/src/testing/test-handlers.ts +0 -47
- package/tests/TransferAnalyzer-account-address.spec.ts +0 -87
- package/tests/TransferAnalyzer-buying-and-selling.spec.ts +0 -354
- package/tests/TransferAnalyzer-loans.spec.ts +0 -197
- package/tests/TransferAnalyzer-multiple-null-amounts.spec.ts +0 -181
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/* eslint-disable camelcase */
|
|
2
|
+
import { sprintf } from 'sprintf-js';
|
|
3
|
+
import { Command } from '.';
|
|
4
|
+
import { log } from '@tasenor/common';
|
|
5
|
+
class TxCommand extends Command {
|
|
6
|
+
addArguments(parser) {
|
|
7
|
+
const sub = parser.add_subparsers();
|
|
8
|
+
const ls = sub.add_parser('ls', { help: 'List all transactions' });
|
|
9
|
+
ls.set_defaults({ subCommand: 'ls' });
|
|
10
|
+
ls.add_argument('db', { help: 'Name of the database' });
|
|
11
|
+
ls.add_argument('period', { help: 'Period year, date or ID' });
|
|
12
|
+
const rm = sub.add_parser('rm', { help: 'Delete a transaction' });
|
|
13
|
+
rm.set_defaults({ subCommand: 'rm' });
|
|
14
|
+
rm.add_argument('db', { help: 'Name of the database' });
|
|
15
|
+
rm.add_argument('id', { help: 'ID of the transaction' });
|
|
16
|
+
const create = sub.add_parser('create', { help: 'Create a transaction' });
|
|
17
|
+
create.set_defaults({ subCommand: 'create' });
|
|
18
|
+
create.add_argument('--force', { help: 'Allow invalid transactions.', action: 'store_true', required: false });
|
|
19
|
+
create.add_argument('--data', { help: 'Define additional data field for all entries as JSON.', required: false });
|
|
20
|
+
create.add_argument('db', { help: 'Name of the database' });
|
|
21
|
+
create.add_argument('date', { help: 'The transaction date' });
|
|
22
|
+
create.add_argument('entry', { nargs: '+', help: 'A transaction line as string, e.g "1234 Description +12,00" or "1234 Description +12,00 {<data>}"' });
|
|
23
|
+
}
|
|
24
|
+
async ls() {
|
|
25
|
+
const { db, period } = this.args;
|
|
26
|
+
const periodId = await this.periodId(db, period);
|
|
27
|
+
const resp = await this.get(`/db/${db}/document?period=${periodId}&entries`);
|
|
28
|
+
await this.readAccounts(db);
|
|
29
|
+
this.out('document', resp);
|
|
30
|
+
}
|
|
31
|
+
print(data) {
|
|
32
|
+
for (const doc of data.sort((a, b) => (a.number || 0) - (b.number || 0))) {
|
|
33
|
+
const { number, date } = doc;
|
|
34
|
+
console.log(`#${number} ${date}`);
|
|
35
|
+
if (doc.entries) {
|
|
36
|
+
for (const entry of doc.entries) {
|
|
37
|
+
console.log(' ', this.accountsById[entry.account_id || -1].number, sprintf('%.2f', entry.debit ? entry.amount / 100 : entry.amount / -100), entry.description);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async rm() {
|
|
43
|
+
const { db, id } = this.args;
|
|
44
|
+
await this.delete(`/db/${db}/document/${id}`);
|
|
45
|
+
log(`Document ${id} deleted successfully.`);
|
|
46
|
+
}
|
|
47
|
+
async create() {
|
|
48
|
+
const { db, date, data, entry, force } = this.args;
|
|
49
|
+
if (!db) {
|
|
50
|
+
throw new Error(`Invalid database argument ${JSON.stringify(db)}`);
|
|
51
|
+
}
|
|
52
|
+
const periodId = await this.periodId(db, date);
|
|
53
|
+
let entries = await this.entries(db, entry);
|
|
54
|
+
if (data) {
|
|
55
|
+
const extras = await this.jsonData(data);
|
|
56
|
+
entries = entries.map(e => ({ ...e, data: extras }));
|
|
57
|
+
}
|
|
58
|
+
const sum = entries.reduce((prev, cur) => prev + cur.amount, 0);
|
|
59
|
+
if (sum && !force) {
|
|
60
|
+
throw new Error(`Transaction total must be zero. Got ${sum} from ${JSON.stringify(entries)}.`);
|
|
61
|
+
}
|
|
62
|
+
const document = await this.post(`/db/${db}/document`, { period_id: periodId, date: this.date(date) });
|
|
63
|
+
log(`Created a document #${document.id} on ${date}.`);
|
|
64
|
+
for (const e of entries) {
|
|
65
|
+
const out = await this.post(`/db/${db}/entry`, {
|
|
66
|
+
document_id: document.id,
|
|
67
|
+
account_id: e.account_id,
|
|
68
|
+
debit: e.amount > 0,
|
|
69
|
+
amount: Math.abs(e.amount),
|
|
70
|
+
description: e.description,
|
|
71
|
+
data: e.data
|
|
72
|
+
});
|
|
73
|
+
log(`Created an entry #${out.id} for ${e.number} ${e.description} ${sprintf('%.2f', e.amount / 100)}${e.data ? ' ' + JSON.stringify(e.data) : ''}.`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async run() {
|
|
77
|
+
await this.runBy('subCommand');
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
export default TxCommand;
|
|
81
|
+
//# sourceMappingURL=tx.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tx.js","sourceRoot":"","sources":["../../../src/commands/tx.ts"],"names":[],"mappings":"AAAA,8BAA8B;AAC9B,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,GAAG,CAAA;AAE3B,OAAO,EAAqC,GAAG,EAAE,MAAM,iBAAiB,CAAA;AAExE,MAAM,SAAU,SAAQ,OAAO;IAE7B,YAAY,CAAC,MAAsB;QACjC,MAAM,GAAG,GAAG,MAAM,CAAC,cAAc,EAAE,CAAA;QAEnC,MAAM,EAAE,GAAG,GAAG,CAAC,UAAU,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,uBAAuB,EAAE,CAAC,CAAA;QAClE,EAAE,CAAC,YAAY,CAAC,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAA;QACrC,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,sBAAsB,EAAE,CAAC,CAAA;QACvD,EAAE,CAAC,YAAY,CAAC,QAAQ,EAAE,EAAE,IAAI,EAAE,yBAAyB,EAAE,CAAC,CAAA;QAE9D,MAAM,EAAE,GAAG,GAAG,CAAC,UAAU,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,sBAAsB,EAAE,CAAC,CAAA;QACjE,EAAE,CAAC,YAAY,CAAC,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAA;QACrC,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,sBAAsB,EAAE,CAAC,CAAA;QACvD,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,EAA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AE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC;gBAC1B,WAAW,EAAE,CAAC,CAAC,WAAW;gBAC1B,IAAI,EAAE,CAAC,CAAC,IAAI;aACb,CAAC,CAAA;YACF,GAAG,CAAC,qBAAqB,GAAG,CAAC,EAAE,QAAQ,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC,WAAW,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,CAAA;SACrJ;IACH,CAAC;IAED,KAAK,CAAC,GAAG;QACP,MAAM,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAA;IAChC,CAAC;CACF;AAED,eAAe,SAAS,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Command } from '.';
|
|
2
|
+
import { ArgumentParser } from 'argparse';
|
|
3
|
+
declare class UserCommand extends Command {
|
|
4
|
+
addArguments(parser: ArgumentParser): void;
|
|
5
|
+
ls(): Promise<void>;
|
|
6
|
+
print(data: any): void;
|
|
7
|
+
rm(): Promise<void>;
|
|
8
|
+
create(): Promise<void>;
|
|
9
|
+
add(): Promise<void>;
|
|
10
|
+
run(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
export default UserCommand;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { log } from '@tasenor/common';
|
|
2
|
+
import { Command } from '.';
|
|
3
|
+
class UserCommand extends Command {
|
|
4
|
+
addArguments(parser) {
|
|
5
|
+
const sub = parser.add_subparsers();
|
|
6
|
+
const ls = sub.add_parser('ls', { help: 'List all users' });
|
|
7
|
+
ls.set_defaults({ subCommand: 'ls' });
|
|
8
|
+
const rm = sub.add_parser('rm', { help: 'Delete a user' });
|
|
9
|
+
rm.set_defaults({ subCommand: 'rm' });
|
|
10
|
+
rm.add_argument('email', { help: 'Email address of the user' });
|
|
11
|
+
const create = sub.add_parser('create', { help: 'Create a user' });
|
|
12
|
+
create.set_defaults({ subCommand: 'create' });
|
|
13
|
+
create.add_argument('name', { help: 'Full name of the user' });
|
|
14
|
+
create.add_argument('passwd', { help: 'Initial password for the user' });
|
|
15
|
+
create.add_argument('email', { help: 'Email address of the user' });
|
|
16
|
+
const add = sub.add_parser('add', { help: 'Add a user to the database' });
|
|
17
|
+
add.set_defaults({ subCommand: 'add' });
|
|
18
|
+
add.add_argument('email', { help: 'Email address of the user' });
|
|
19
|
+
add.add_argument('db', { help: 'Name of the database' });
|
|
20
|
+
}
|
|
21
|
+
async ls() {
|
|
22
|
+
const resp = await this.get('/admin/user');
|
|
23
|
+
this.out('user', resp);
|
|
24
|
+
}
|
|
25
|
+
print(data) {
|
|
26
|
+
for (const user of data.sort((a, b) => (a.id || 0) - (b.id || 0))) {
|
|
27
|
+
const { id, name, email, config } = user;
|
|
28
|
+
console.log(`#${id} ${name} ${email} ${JSON.stringify(config)}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async rm() {
|
|
32
|
+
const { email } = this.args;
|
|
33
|
+
await this.delete(`/admin/user/${email}`);
|
|
34
|
+
log(`User ${email} deleted successfully.`);
|
|
35
|
+
}
|
|
36
|
+
async create() {
|
|
37
|
+
const { name, passwd, email } = this.args;
|
|
38
|
+
const params = { name, password: passwd, email };
|
|
39
|
+
await this.post('/admin/user', params);
|
|
40
|
+
log(`User ${name} created successfully.`);
|
|
41
|
+
}
|
|
42
|
+
async add() {
|
|
43
|
+
const { email, db } = this.args;
|
|
44
|
+
log(`Adding user ${email} to database ${db}`);
|
|
45
|
+
await this.post(`/admin/user/${email}/databases`, { database: this.str(db) });
|
|
46
|
+
}
|
|
47
|
+
async run() {
|
|
48
|
+
await this.runBy('subCommand');
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export default UserCommand;
|
|
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E,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC,IAAI,CAAA;QACzC,MAAM,MAAM,GAAG,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,CAAA;QAChD,MAAM,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,MAAM,CAAC,CAAA;QACtC,GAAG,CAAC,QAAQ,IAAI,wBAAwB,CAAC,CAAA;IAC3C,CAAC;IAED,KAAK,CAAC,GAAG;QACP,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE,GAAG,IAAI,CAAC,IAAI,CAAA;QAC/B,GAAG,CAAC,eAAe,KAAK,gBAAgB,EAAE,EAAE,CAAC,CAAA;QAC7C,MAAM,IAAI,CAAC,IAAI,CAAC,eAAe,KAAK,YAAY,EAAE,EAAE,QAAQ,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,CAAA;IAC/E,CAAC;IAED,KAAK,CAAC,GAAG;QACP,MAAM,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAA;IAChC,CAAC;CACF;AAED,eAAe,WAAW,CAAA"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { BookkeeperConfig, DirectoryPath, Hostname, ProcessedTsvFileData, TarFilePath, TextFilePath, TsvFilePath } from '@tasenor/common';
|
|
2
|
+
import { KnexDatabase } from './DB';
|
|
3
|
+
/**
|
|
4
|
+
* Implementation of the database backup file reading.
|
|
5
|
+
*/
|
|
6
|
+
export declare class BookkeeperImporter {
|
|
7
|
+
VERSION: number | null;
|
|
8
|
+
/**
|
|
9
|
+
* Read in a TSV-file and construct list of objects.
|
|
10
|
+
* @param file Path to the TSV-file.
|
|
11
|
+
* @returns List of objects using texts in header line as keys.
|
|
12
|
+
*/
|
|
13
|
+
readTsv(file: TsvFilePath): Promise<ProcessedTsvFileData>;
|
|
14
|
+
/**
|
|
15
|
+
* Read the version number from the file.
|
|
16
|
+
* @param file Path to the version file.
|
|
17
|
+
*/
|
|
18
|
+
setVersion(file: TextFilePath): void;
|
|
19
|
+
/**
|
|
20
|
+
* Read the account information from the tsv file.
|
|
21
|
+
* @param file A tsv file to read.
|
|
22
|
+
*/
|
|
23
|
+
readAccountTsv(file: TsvFilePath): Promise<Record<string, unknown>[]>;
|
|
24
|
+
/**
|
|
25
|
+
* Read the account information in to the database.
|
|
26
|
+
* @param db Database connection.
|
|
27
|
+
* @param files A list of files to read.
|
|
28
|
+
*/
|
|
29
|
+
setAccounts(db: KnexDatabase, files: TsvFilePath[]): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Read the period information in to the database.
|
|
32
|
+
* @param db Database connection.
|
|
33
|
+
* @param file Path to the period file.
|
|
34
|
+
*/
|
|
35
|
+
setPeriods(db: any, file: TsvFilePath): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Read and store all documents and entries found from the TSV-file.
|
|
38
|
+
* @param db Database connection.
|
|
39
|
+
* @param file Path to the transaction file.
|
|
40
|
+
* @param conf Database configuration.
|
|
41
|
+
*/
|
|
42
|
+
setEntries(db: KnexDatabase, file: TsvFilePath, conf: BookkeeperConfig): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Set the configuration for database.
|
|
45
|
+
* @param db Database connection.
|
|
46
|
+
* @param name Name of the database to update.
|
|
47
|
+
* @param conf Database configuration.
|
|
48
|
+
*/
|
|
49
|
+
setConfig(db: KnexDatabase, config: BookkeeperConfig): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Read in tag data and files from the backup.
|
|
52
|
+
* @param db Database connection.
|
|
53
|
+
* @param file Path to the tag file. Also its directory is assumed where images can be found.
|
|
54
|
+
*/
|
|
55
|
+
setTags(db: KnexDatabase, file: TsvFilePath): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Remove all data from all tables.
|
|
58
|
+
* @param db Database connection.
|
|
59
|
+
*/
|
|
60
|
+
clearEverything(db: KnexDatabase): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Clear the given database and restore everything from the directory.
|
|
63
|
+
* @param masterDb Master DB connection.
|
|
64
|
+
* @param dbName Name of the database to use.
|
|
65
|
+
* @param out The directory containing unpacked backup.
|
|
66
|
+
* @param hostOverride If set, use this hostname instead of the one in database, when connecting to target DB.
|
|
67
|
+
*/
|
|
68
|
+
restore(masterDb: KnexDatabase, dbName: string, out: DirectoryPath, hostOverride?: Hostname | null): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Clear the given database and restore everything from the directory.
|
|
71
|
+
* @param masterDb Master DB connection.
|
|
72
|
+
* @param dbName Name of the database to use.
|
|
73
|
+
* @param out The directory containing unpacked backup.
|
|
74
|
+
* @param hostOverride If set, use this hostname instead of the one in database, when connecting to target DB.
|
|
75
|
+
*/
|
|
76
|
+
run(masterDb: KnexDatabase, dbName: string, tarPath: TarFilePath, out: DirectoryPath, hostOverride?: Hostname | null): Promise<void>;
|
|
77
|
+
}
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
import glob from 'fast-glob';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import { system } from '..';
|
|
5
|
+
import { log, error } from '@tasenor/common';
|
|
6
|
+
import { DB } from './DB';
|
|
7
|
+
import { create } from 'ts-opaque';
|
|
8
|
+
/**
|
|
9
|
+
* Implementation of the database backup file reading.
|
|
10
|
+
*/
|
|
11
|
+
export class BookkeeperImporter {
|
|
12
|
+
VERSION = null;
|
|
13
|
+
/**
|
|
14
|
+
* Read in a TSV-file and construct list of objects.
|
|
15
|
+
* @param file Path to the TSV-file.
|
|
16
|
+
* @returns List of objects using texts in header line as keys.
|
|
17
|
+
*/
|
|
18
|
+
async readTsv(file) {
|
|
19
|
+
log(`Reading ${file}.`);
|
|
20
|
+
const content = fs.readFileSync(file).toString('utf-8').trim();
|
|
21
|
+
const lines = content.split('\n').map(s => s.split('\t'));
|
|
22
|
+
const headers = lines[0];
|
|
23
|
+
headers[0] = headers[0].replace(/^#\s+/, '');
|
|
24
|
+
const objects = [];
|
|
25
|
+
for (let i = 1; i < lines.length; i++) {
|
|
26
|
+
const obj = {};
|
|
27
|
+
for (let j = 0; j < headers.length; j++) {
|
|
28
|
+
obj[headers[j]] = lines[i][j] || '';
|
|
29
|
+
}
|
|
30
|
+
objects.push(obj);
|
|
31
|
+
}
|
|
32
|
+
return objects;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Read the version number from the file.
|
|
36
|
+
* @param file Path to the version file.
|
|
37
|
+
*/
|
|
38
|
+
setVersion(file) {
|
|
39
|
+
this.VERSION = JSON.parse(fs.readFileSync(file).toString('utf-8'));
|
|
40
|
+
log(`Found file format version ${this.VERSION}.`);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Read the account information from the tsv file.
|
|
44
|
+
* @param file A tsv file to read.
|
|
45
|
+
*/
|
|
46
|
+
async readAccountTsv(file) {
|
|
47
|
+
const match = /([a-z][a-z])-([A-Z][A-Z][A-Z])\.tsv$/.exec(file);
|
|
48
|
+
if (!match) {
|
|
49
|
+
throw new Error(`File name ${file} has not correct format.`);
|
|
50
|
+
}
|
|
51
|
+
const entries = [];
|
|
52
|
+
const [, language, currency] = match;
|
|
53
|
+
const accounts = await this.readTsv(file);
|
|
54
|
+
let headings = [];
|
|
55
|
+
for (const account of accounts) {
|
|
56
|
+
if (account.text !== '') {
|
|
57
|
+
const code = (!account.code
|
|
58
|
+
? null
|
|
59
|
+
: (
|
|
60
|
+
// Allow numeric VAT as well.
|
|
61
|
+
/^\d+(\.\d+)$/.test(account.code) ? account.code : account.code.replace(/^_+/, '')));
|
|
62
|
+
let data;
|
|
63
|
+
try {
|
|
64
|
+
data = account.data === undefined || account.data === '' ? {} : JSON.parse(account.data);
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
throw new Error(`Parsing account data failed: ${account.data}.`);
|
|
68
|
+
}
|
|
69
|
+
if (code !== null) {
|
|
70
|
+
data.code = code;
|
|
71
|
+
}
|
|
72
|
+
// Verision 1.
|
|
73
|
+
if (this.VERSION === 1) {
|
|
74
|
+
const flags = new Set(account.flags ? account.flags.split(' ') : []);
|
|
75
|
+
if (flags.has('FAVOURITE')) {
|
|
76
|
+
data.favourite = true;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const entry = {
|
|
80
|
+
language,
|
|
81
|
+
currency,
|
|
82
|
+
number: account['number / title'],
|
|
83
|
+
name: account.text,
|
|
84
|
+
type: account.type,
|
|
85
|
+
data
|
|
86
|
+
};
|
|
87
|
+
if (headings.length) {
|
|
88
|
+
for (const heading of headings) {
|
|
89
|
+
heading.number = entry.number;
|
|
90
|
+
entries.push(heading);
|
|
91
|
+
}
|
|
92
|
+
headings = [];
|
|
93
|
+
}
|
|
94
|
+
entries.push(entry);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
const spaces = /^(_*)/.exec(account['number / title']);
|
|
98
|
+
const entry = {
|
|
99
|
+
text: account['number / title'].replace(/^_+/, ''),
|
|
100
|
+
number: null,
|
|
101
|
+
level: spaces ? spaces[1].length : 0
|
|
102
|
+
};
|
|
103
|
+
headings.push(entry);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return entries;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Read the account information in to the database.
|
|
110
|
+
* @param db Database connection.
|
|
111
|
+
* @param files A list of files to read.
|
|
112
|
+
*/
|
|
113
|
+
async setAccounts(db, files) {
|
|
114
|
+
let count = 0;
|
|
115
|
+
for (const file of files) {
|
|
116
|
+
const accounts = await this.readAccountTsv(file);
|
|
117
|
+
for (const entry of accounts) {
|
|
118
|
+
if (entry.text) {
|
|
119
|
+
await db('heading').insert(entry).catch(err => {
|
|
120
|
+
error(`Failed to insert a heading ${JSON.stringify(entry)}`);
|
|
121
|
+
throw err;
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
await db('account').insert(entry).catch(err => {
|
|
126
|
+
error(`Failed to insert an account ${JSON.stringify(entry)}`);
|
|
127
|
+
throw err;
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
count++;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
log(`Inserted ${count} rows to the database.`);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Read the period information in to the database.
|
|
137
|
+
* @param db Database connection.
|
|
138
|
+
* @param file Path to the period file.
|
|
139
|
+
*/
|
|
140
|
+
async setPeriods(db, file) {
|
|
141
|
+
log(`Reading period file ${file}.`);
|
|
142
|
+
let count = 0;
|
|
143
|
+
const periods = await this.readTsv(file);
|
|
144
|
+
for (const period of periods) {
|
|
145
|
+
const entry = {
|
|
146
|
+
start_date: period.start,
|
|
147
|
+
end_date: period.end,
|
|
148
|
+
locked: period.flags === 'LOCKED'
|
|
149
|
+
};
|
|
150
|
+
await db('period').insert(entry);
|
|
151
|
+
count++;
|
|
152
|
+
}
|
|
153
|
+
log(`Inserted ${count} rows to the database.`);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Read and store all documents and entries found from the TSV-file.
|
|
157
|
+
* @param db Database connection.
|
|
158
|
+
* @param file Path to the transaction file.
|
|
159
|
+
* @param conf Database configuration.
|
|
160
|
+
*/
|
|
161
|
+
async setEntries(db, file, conf) {
|
|
162
|
+
log(`Reading entry file ${file}.`);
|
|
163
|
+
let count = 0;
|
|
164
|
+
const periods = await db('period').select('id').orderBy('start_date');
|
|
165
|
+
const periodMap = {};
|
|
166
|
+
const accounts = await db('account').select('id', 'number').where({ language: conf.language });
|
|
167
|
+
const accountMap = accounts.reduce((prev, cur) => ({ [cur.number]: cur.id, ...prev }), {});
|
|
168
|
+
let n = 1;
|
|
169
|
+
for (const period of periods) {
|
|
170
|
+
periodMap[n++] = period.id;
|
|
171
|
+
}
|
|
172
|
+
const data = await this.readTsv(file);
|
|
173
|
+
let periodId, docId, rowNumber;
|
|
174
|
+
for (const line of data) {
|
|
175
|
+
// Period line.
|
|
176
|
+
const check = /^Period (\d+)/.exec(line.number);
|
|
177
|
+
if (check) {
|
|
178
|
+
periodId = periodMap[parseInt(check[1])];
|
|
179
|
+
if (!periodId) {
|
|
180
|
+
throw Error(`Inconsistent periods. Cannot find period number ${n}.`);
|
|
181
|
+
}
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
// Document line.
|
|
185
|
+
if (line.number !== '') {
|
|
186
|
+
const entry = {
|
|
187
|
+
period_id: periodId,
|
|
188
|
+
number: parseInt(line.number),
|
|
189
|
+
date: line['date / account']
|
|
190
|
+
};
|
|
191
|
+
docId = (await db('document').insert(entry).returning('id'))[0].id;
|
|
192
|
+
count++;
|
|
193
|
+
rowNumber = 1;
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
// Entry line.
|
|
197
|
+
if (!accountMap[line['date / account']]) {
|
|
198
|
+
throw Error(`Inconsistent accounts. Cannot account find number ${line['date / account']}.`);
|
|
199
|
+
}
|
|
200
|
+
const amount = parseFloat(line.amount);
|
|
201
|
+
const data = {};
|
|
202
|
+
// Version 1.
|
|
203
|
+
if (this.VERSION === 1) {
|
|
204
|
+
const flags = new Set(line.flags.split(' '));
|
|
205
|
+
if (flags.has('VAT_IGNORE') || flags.has('VAT_RECONCILED')) {
|
|
206
|
+
if (flags.has('VAT_IGNORE')) {
|
|
207
|
+
data.VAT = { ignore: true };
|
|
208
|
+
}
|
|
209
|
+
if (flags.has('VAT_RECONCILED')) {
|
|
210
|
+
data.VAT = { ...(data.VAT || {}), reconciled: true };
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
// Version 2.
|
|
214
|
+
}
|
|
215
|
+
else if (this.VERSION === 2) {
|
|
216
|
+
Object.assign(data, JSON.parse(line.data));
|
|
217
|
+
}
|
|
218
|
+
const entry = {
|
|
219
|
+
document_id: docId,
|
|
220
|
+
account_id: accountMap[line['date / account']],
|
|
221
|
+
debit: !(amount < 0),
|
|
222
|
+
amount: Math.abs(amount),
|
|
223
|
+
description: line.text,
|
|
224
|
+
row_number: rowNumber,
|
|
225
|
+
data
|
|
226
|
+
};
|
|
227
|
+
rowNumber++;
|
|
228
|
+
await db('entry').insert(entry).catch(err => {
|
|
229
|
+
error(`Failed to insert an entry ${JSON.stringify(entry)}`);
|
|
230
|
+
throw err;
|
|
231
|
+
});
|
|
232
|
+
count++;
|
|
233
|
+
}
|
|
234
|
+
log(`Inserted ${count} rows to the database.`);
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Set the configuration for database.
|
|
238
|
+
* @param db Database connection.
|
|
239
|
+
* @param name Name of the database to update.
|
|
240
|
+
* @param conf Database configuration.
|
|
241
|
+
*/
|
|
242
|
+
async setConfig(db, config) {
|
|
243
|
+
log('Saving configuration.');
|
|
244
|
+
// Transform deep structures to dot-names.
|
|
245
|
+
const transform = (config, prefix = '') => {
|
|
246
|
+
const ret = {};
|
|
247
|
+
Object.keys(config).forEach(k => {
|
|
248
|
+
if (config[k] !== null && typeof config[k] === 'object' && config[k].length === undefined) {
|
|
249
|
+
Object.assign(ret, transform(config[k], `${k}.`));
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
ret[`${prefix}${k}`] = config[k];
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
return ret;
|
|
256
|
+
};
|
|
257
|
+
// Save all.
|
|
258
|
+
for (const [k, v] of Object.entries(transform(config))) {
|
|
259
|
+
await db('settings').insert({ name: k, value: JSON.stringify(v) });
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Read in tag data and files from the backup.
|
|
264
|
+
* @param db Database connection.
|
|
265
|
+
* @param file Path to the tag file. Also its directory is assumed where images can be found.
|
|
266
|
+
*/
|
|
267
|
+
async setTags(db, file) {
|
|
268
|
+
log(`Reading tag file ${file}.`);
|
|
269
|
+
const picPath = path.dirname(file);
|
|
270
|
+
let count = 0;
|
|
271
|
+
const tags = await this.readTsv(file);
|
|
272
|
+
for (const tag of tags) {
|
|
273
|
+
const pic = fs.readFileSync(path.join(picPath, tag.picture));
|
|
274
|
+
const entry = {
|
|
275
|
+
tag: tag.tag,
|
|
276
|
+
name: tag.name,
|
|
277
|
+
mime: tag.mime,
|
|
278
|
+
picture: pic,
|
|
279
|
+
type: tag.type,
|
|
280
|
+
order: tag.order
|
|
281
|
+
};
|
|
282
|
+
await db('tags').insert(entry);
|
|
283
|
+
count++;
|
|
284
|
+
}
|
|
285
|
+
log(`Inserted ${count} rows to the database.`);
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Remove all data from all tables.
|
|
289
|
+
* @param db Database connection.
|
|
290
|
+
*/
|
|
291
|
+
async clearEverything(db) {
|
|
292
|
+
log('Deleting all existing data.');
|
|
293
|
+
await db('entry').del();
|
|
294
|
+
await db('document').del();
|
|
295
|
+
await db('account').del();
|
|
296
|
+
await db('heading').del();
|
|
297
|
+
await db('period').del();
|
|
298
|
+
await db('tags').del();
|
|
299
|
+
await db('settings').del();
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Clear the given database and restore everything from the directory.
|
|
303
|
+
* @param masterDb Master DB connection.
|
|
304
|
+
* @param dbName Name of the database to use.
|
|
305
|
+
* @param out The directory containing unpacked backup.
|
|
306
|
+
* @param hostOverride If set, use this hostname instead of the one in database, when connecting to target DB.
|
|
307
|
+
*/
|
|
308
|
+
async restore(masterDb, dbName, out, hostOverride = null) {
|
|
309
|
+
const userDb = await DB.get(masterDb, create(dbName), hostOverride);
|
|
310
|
+
this.setVersion(create(path.join(out, 'VERSION')));
|
|
311
|
+
const conf = JSON.parse(fs.readFileSync(path.join(out, 'settings.json')).toString('utf-8'));
|
|
312
|
+
if (!conf.language) {
|
|
313
|
+
throw new Error('Configuration does not have language.');
|
|
314
|
+
}
|
|
315
|
+
await this.clearEverything(userDb);
|
|
316
|
+
await this.setConfig(userDb, conf);
|
|
317
|
+
const files = glob.sync(path.join(out, 'accounts', '*'));
|
|
318
|
+
await this.setAccounts(userDb, files);
|
|
319
|
+
const periodsPath = path.join(out, 'periods.tsv');
|
|
320
|
+
await this.setPeriods(userDb, create(periodsPath));
|
|
321
|
+
const entriesPath = path.join(out, 'entries.tsv');
|
|
322
|
+
await this.setEntries(userDb, create(entriesPath), conf);
|
|
323
|
+
const tagsPath = path.join(out, 'tags.tsv');
|
|
324
|
+
await this.setTags(userDb, create(tagsPath));
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Clear the given database and restore everything from the directory.
|
|
328
|
+
* @param masterDb Master DB connection.
|
|
329
|
+
* @param dbName Name of the database to use.
|
|
330
|
+
* @param out The directory containing unpacked backup.
|
|
331
|
+
* @param hostOverride If set, use this hostname instead of the one in database, when connecting to target DB.
|
|
332
|
+
*/
|
|
333
|
+
async run(masterDb, dbName, tarPath, out, hostOverride = null) {
|
|
334
|
+
tarPath = create(path.resolve(tarPath));
|
|
335
|
+
if (!fs.existsSync(tarPath)) {
|
|
336
|
+
throw new Error(`Backup ${tarPath} does not exist.`);
|
|
337
|
+
}
|
|
338
|
+
await system(`cd "${out}" && tar xf "${tarPath}"`);
|
|
339
|
+
await this.restore(masterDb, dbName, out, hostOverride);
|
|
340
|
+
await system(`rm -fr "${out}"`);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
//# sourceMappingURL=BookkeeperImporter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BookkeeperImporter.js","sourceRoot":"","sources":["../../../src/database/BookkeeperImporter.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAA;AAC5B,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAE,MAAM,IAAI,CAAA;AACnB,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,CAAA;AAC3B,OAAO,EAAE,GAAG,EAA2G,KAAK,EAAY,MAAM,iBAAiB,CAAA;AAC/J,OAAO,EAAE,EAAE,EAAgB,MAAM,MAAM,CAAA;AACvC,OAAO,EAAE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CAAC,CAAC,CAAA;QAClD,MAAM,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,aAAa,CAAC,CAAA;QACjD,MAAM,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC,WAAW,CAAC,EAAE,IAAI,CAAC,CAAA;QACxD,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,UAAU,CAAC,CAAA;QAC3C,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAA;IAC9C,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,GAAG,CAAC,QAAsB,EAAE,MAAc,EAAE,OAAoB,EAAE,GAAkB,EAAE,eAAgC,IAAI;QAC9H,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAA;QACvC,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE;YAC3B,MAAM,IAAI,KAAK,CAAC,UAAU,OAAO,kBAAkB,CAAC,CAAA;SACrD;QACD,MAAM,MAAM,CAAC,OAAO,GAAG,gBAAgB,OAAO,GAAG,CAAC,CAAA;QAClD,MAAM,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,YAAY,CAAC,CAAA;QACvD,MAAM,MAAM,CAAC,WAAW,GAAG,GAAG,CAAC,CAAA;IACjC,CAAC;CACF"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { DatabaseName, Hostname, ID, Url } from '@tasenor/common';
|
|
2
|
+
import { Knex } from 'knex';
|
|
3
|
+
export type KnexDatabase = Knex<any, any[]>;
|
|
4
|
+
export type KnexConfig = Record<string, any>;
|
|
5
|
+
export type KnexConnectionInfo = {
|
|
6
|
+
host: string;
|
|
7
|
+
port: string | number;
|
|
8
|
+
database: string;
|
|
9
|
+
user: string;
|
|
10
|
+
password: string;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Generate a name for a database that does not yet exist.
|
|
14
|
+
* @param masterDb - Master database.
|
|
15
|
+
* @param init - Initial part of the name.
|
|
16
|
+
* @returns Suitable name.
|
|
17
|
+
*/
|
|
18
|
+
declare function findName(masterDb: KnexDatabase, init: string): Promise<string>;
|
|
19
|
+
/**
|
|
20
|
+
* Terminate all DB connections.
|
|
21
|
+
*/
|
|
22
|
+
declare function disconnectAll(): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Get a list of all customer database connection infos.
|
|
25
|
+
*/
|
|
26
|
+
declare function customerDbs(hostOverride?: null | Hostname): Promise<KnexConnectionInfo[]>;
|
|
27
|
+
/**
|
|
28
|
+
* Resolve database host from environment DATABASE_URL if set.
|
|
29
|
+
*/
|
|
30
|
+
declare function envHost(): string | null;
|
|
31
|
+
export declare const DB: {
|
|
32
|
+
create: (masterDb: KnexDatabase, name: DatabaseName, host: Hostname, port: number, migrations?: string | null, hostOverride?: null | Hostname) => Promise<ID>;
|
|
33
|
+
customerDbs: typeof customerDbs;
|
|
34
|
+
destroy: (masterDb: KnexDatabase, name: DatabaseName, hostOverride?: null | Hostname) => Promise<string | null>;
|
|
35
|
+
disconnectAll: typeof disconnectAll;
|
|
36
|
+
envHost: typeof envHost;
|
|
37
|
+
exists: (master: KnexDatabase, name: DatabaseName) => Promise<boolean>;
|
|
38
|
+
findName: typeof findName;
|
|
39
|
+
get: (master: KnexDatabase, name: DatabaseName, hostOverride?: null | Hostname) => Promise<KnexDatabase>;
|
|
40
|
+
getConfig: (master: KnexDatabase, name: DatabaseName, hostOverride?: null | Hostname) => Promise<KnexConfig>;
|
|
41
|
+
getKnexConfig: (knexUrl: Url) => KnexConfig;
|
|
42
|
+
getMaster: () => KnexDatabase;
|
|
43
|
+
getMasterConfig: () => KnexConfig;
|
|
44
|
+
getRoot: () => KnexDatabase;
|
|
45
|
+
getRootConfig: () => KnexConfig;
|
|
46
|
+
isValidName: (name: string) => boolean;
|
|
47
|
+
migrate: (masterDb: KnexDatabase, name: DatabaseName, migrations: string, hostOverride?: null | Hostname) => Promise<void>;
|
|
48
|
+
migrateMaster: (migrations: string) => Promise<void>;
|
|
49
|
+
rollback: (masterDb: KnexDatabase, name: DatabaseName, migrations: string, hostOverride?: null | Hostname) => Promise<void>;
|
|
50
|
+
};
|
|
51
|
+
export {};
|