@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,354 @@
|
|
|
1
|
+
import { Crypto, error, isDatabaseName, log } from '@tasenor/common';
|
|
2
|
+
import { randomString, vault } from '..';
|
|
3
|
+
import knex from 'knex';
|
|
4
|
+
import { types } from 'pg';
|
|
5
|
+
import { builtins } from 'pg-types';
|
|
6
|
+
import { create as opaque } from 'ts-opaque';
|
|
7
|
+
// How many times to retry migration before giving up.
|
|
8
|
+
const MIGRATION_RETRY = 20;
|
|
9
|
+
// Fix Knex messing around with the date values and timezones.
|
|
10
|
+
const parseDate = (val) => val;
|
|
11
|
+
types.setTypeParser(builtins.TIMESTAMPTZ, parseDate);
|
|
12
|
+
types.setTypeParser(builtins.TIMESTAMP, parseDate);
|
|
13
|
+
types.setTypeParser(builtins.DATE, parseDate);
|
|
14
|
+
/**
|
|
15
|
+
* Check if the database with the given name exists.
|
|
16
|
+
* @param master The master database.
|
|
17
|
+
* @param name Name of the DB to check.
|
|
18
|
+
* @returns True if it is found.
|
|
19
|
+
*/
|
|
20
|
+
const exists = async (master, name) => {
|
|
21
|
+
return !!(await master('databases').select('*').where({ name }).first());
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Construct a Knex configuration for the given database.
|
|
25
|
+
* @param master
|
|
26
|
+
* @param name
|
|
27
|
+
* @param hostOverride
|
|
28
|
+
* @returns
|
|
29
|
+
*/
|
|
30
|
+
const getConfig = async (master, name, hostOverride = null) => {
|
|
31
|
+
const userDb = await master('databases').select('*').where({ name }).first();
|
|
32
|
+
const password = await Crypto.decrypt(vault.get('SECRET'), userDb.password);
|
|
33
|
+
if (!password) {
|
|
34
|
+
throw new Error('Failed to get password.');
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
client: 'postgresql',
|
|
38
|
+
connection: {
|
|
39
|
+
host: hostOverride === null ? userDb.host : hostOverride,
|
|
40
|
+
port: userDb.port,
|
|
41
|
+
database: userDb.name,
|
|
42
|
+
user: userDb.user,
|
|
43
|
+
password
|
|
44
|
+
},
|
|
45
|
+
pool: {
|
|
46
|
+
min: 1,
|
|
47
|
+
max: 3
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
const cache = {};
|
|
52
|
+
/**
|
|
53
|
+
* Get the cached connection to the given customer database.
|
|
54
|
+
* @param master Master database connection.
|
|
55
|
+
* @param name Name of the database.
|
|
56
|
+
* @param hostOverride If given, use this host instead of the host in database.
|
|
57
|
+
* @returns Knex connection.
|
|
58
|
+
*/
|
|
59
|
+
const get = async (master, name, hostOverride = null) => {
|
|
60
|
+
if (!(await exists(master, name))) {
|
|
61
|
+
delete cache[name];
|
|
62
|
+
throw new Error(`Database '${name}' does not exist.`);
|
|
63
|
+
}
|
|
64
|
+
const cacheName = hostOverride ? `${name}:${hostOverride}` : name;
|
|
65
|
+
if (!cache[cacheName]) {
|
|
66
|
+
const knexConfig = await getConfig(master, name, hostOverride);
|
|
67
|
+
cache[cacheName] = knex(knexConfig);
|
|
68
|
+
}
|
|
69
|
+
return cache[cacheName];
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Disconnect and drop cached connection.
|
|
73
|
+
* @param name Name of the database.
|
|
74
|
+
*/
|
|
75
|
+
const disconnect = async (name) => {
|
|
76
|
+
for (const conn of Object.entries(cache)) {
|
|
77
|
+
if (conn[0].split(':')[0] === name) {
|
|
78
|
+
log(`Disconnecting ${conn[0]}`);
|
|
79
|
+
await conn[1].destroy();
|
|
80
|
+
delete cache[conn[0]];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Get the configuration for the root connection.
|
|
86
|
+
* @returns Configuration object.
|
|
87
|
+
*/
|
|
88
|
+
const getRootConfig = () => {
|
|
89
|
+
const url = new URL(vault.get('DATABASE_URL'));
|
|
90
|
+
const knexConfig = {
|
|
91
|
+
client: 'postgresql',
|
|
92
|
+
connection: {
|
|
93
|
+
host: url.hostname,
|
|
94
|
+
port: parseInt(url.port) || 5432,
|
|
95
|
+
database: 'postgres',
|
|
96
|
+
user: vault.get('DATABASE_ROOT_USER'),
|
|
97
|
+
password: vault.get('DATABASE_ROOT_PASSWORD')
|
|
98
|
+
},
|
|
99
|
+
pool: {
|
|
100
|
+
min: 1,
|
|
101
|
+
max: 1
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
return knexConfig;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Convert database URL to knex config.
|
|
108
|
+
* @param knexUrl
|
|
109
|
+
* @returns
|
|
110
|
+
*/
|
|
111
|
+
const getKnexConfig = (knexUrl) => {
|
|
112
|
+
const url = new URL(knexUrl);
|
|
113
|
+
return {
|
|
114
|
+
client: 'postgresql',
|
|
115
|
+
connection: {
|
|
116
|
+
host: url.hostname,
|
|
117
|
+
port: parseInt(url.port) || 5432,
|
|
118
|
+
database: url.pathname.replace(/^\//, ''),
|
|
119
|
+
user: url.username,
|
|
120
|
+
password: url.password
|
|
121
|
+
},
|
|
122
|
+
pool: {
|
|
123
|
+
min: 1,
|
|
124
|
+
max: 5
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* Get the cached connetion to the root access.
|
|
130
|
+
* @returns Knex instance.
|
|
131
|
+
*/
|
|
132
|
+
let rootCache;
|
|
133
|
+
const getRoot = () => {
|
|
134
|
+
if (rootCache)
|
|
135
|
+
return rootCache;
|
|
136
|
+
rootCache = knex(getRootConfig());
|
|
137
|
+
return rootCache;
|
|
138
|
+
};
|
|
139
|
+
const getMasterConfig = () => {
|
|
140
|
+
return getKnexConfig(vault.get('DATABASE_URL'));
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* Get the master database.
|
|
144
|
+
* @returns A Knex instance.
|
|
145
|
+
*/
|
|
146
|
+
let masterCache;
|
|
147
|
+
const getMaster = () => {
|
|
148
|
+
if (masterCache)
|
|
149
|
+
return masterCache;
|
|
150
|
+
masterCache = knex(getMasterConfig());
|
|
151
|
+
return masterCache;
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* Test if the name is valid database name.
|
|
155
|
+
* @param name
|
|
156
|
+
* @returns True if valid.
|
|
157
|
+
*/
|
|
158
|
+
const isValidName = (name) => {
|
|
159
|
+
return isDatabaseName(name);
|
|
160
|
+
};
|
|
161
|
+
/**
|
|
162
|
+
* Create new customer database.
|
|
163
|
+
* @param db Knex for bookkeeper master database.
|
|
164
|
+
* @param name Name of the new database.
|
|
165
|
+
* @param host A host name of the DB host.
|
|
166
|
+
* @param port A port used by the DB host.
|
|
167
|
+
* @param migrations Path of the migrations file.
|
|
168
|
+
* @returns ID
|
|
169
|
+
*/
|
|
170
|
+
const create = async (masterDb, name, host, port, migrations = null, hostOverride = null) => {
|
|
171
|
+
if (await exists(masterDb, name)) {
|
|
172
|
+
throw new Error(`Database '${name}' exist.`);
|
|
173
|
+
}
|
|
174
|
+
if (!isValidName(name)) {
|
|
175
|
+
throw new Error(`Invalid database name '${name}'.`);
|
|
176
|
+
}
|
|
177
|
+
const rootDb = getRoot();
|
|
178
|
+
const user = 'user' + randomString(20);
|
|
179
|
+
const password = randomString(64);
|
|
180
|
+
const entry = {
|
|
181
|
+
name,
|
|
182
|
+
host,
|
|
183
|
+
port,
|
|
184
|
+
user,
|
|
185
|
+
password: await Crypto.encrypt(vault.get('SECRET'), password),
|
|
186
|
+
config: {}
|
|
187
|
+
};
|
|
188
|
+
await rootDb.raw(`CREATE USER "${user}" WITH PASSWORD '${password}'`);
|
|
189
|
+
await rootDb.raw(`CREATE DATABASE "${name}"`);
|
|
190
|
+
await rootDb.raw(`GRANT ALL PRIVILEGES ON DATABASE "${name}" TO "${user}"`);
|
|
191
|
+
const id = (await masterDb('databases').insert(entry).returning('id'))[0].id;
|
|
192
|
+
if (migrations) {
|
|
193
|
+
await migrate(masterDb, name, migrations, hostOverride);
|
|
194
|
+
}
|
|
195
|
+
return id;
|
|
196
|
+
};
|
|
197
|
+
/**
|
|
198
|
+
* Apply migrations to the named customer database.
|
|
199
|
+
* @param masterDb
|
|
200
|
+
* @param name
|
|
201
|
+
* @param migrations
|
|
202
|
+
* @param hostOverride
|
|
203
|
+
*/
|
|
204
|
+
const migrate = async (masterDb, name, migrations, hostOverride = null) => {
|
|
205
|
+
log(`Migrating database '${name}'.`);
|
|
206
|
+
const conf = await getConfig(masterDb, name, hostOverride);
|
|
207
|
+
conf.migrations = { directory: migrations };
|
|
208
|
+
const db = knex(conf);
|
|
209
|
+
await db.migrate.latest();
|
|
210
|
+
// Do not leave hanging connections.
|
|
211
|
+
await db.destroy();
|
|
212
|
+
};
|
|
213
|
+
/**
|
|
214
|
+
* Apply migrations to the master database.
|
|
215
|
+
*/
|
|
216
|
+
const migrateMaster = async (migrations) => {
|
|
217
|
+
function delay(time) {
|
|
218
|
+
return new Promise(resolve => setTimeout(resolve, time));
|
|
219
|
+
}
|
|
220
|
+
log('Migrating master database.');
|
|
221
|
+
const conf = await getMasterConfig();
|
|
222
|
+
conf.migrations = { directory: migrations };
|
|
223
|
+
const db = knex(conf);
|
|
224
|
+
for (let i = 0; i < MIGRATION_RETRY; i++) {
|
|
225
|
+
try {
|
|
226
|
+
await db.migrate.latest();
|
|
227
|
+
log('Migrating master database successful.');
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
catch (err) {
|
|
231
|
+
if (i < MIGRATION_RETRY - 1) {
|
|
232
|
+
error('Migration failed. Retrying...');
|
|
233
|
+
await delay(1000);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
// Do not leave hanging connections.
|
|
238
|
+
await db.destroy();
|
|
239
|
+
};
|
|
240
|
+
/**
|
|
241
|
+
* Roll bakc last migrations from the named customer database.
|
|
242
|
+
* @param masterDb
|
|
243
|
+
* @param name
|
|
244
|
+
* @param migrations
|
|
245
|
+
* @param hostOverride
|
|
246
|
+
*/
|
|
247
|
+
const rollback = async (masterDb, name, migrations, hostOverride = null) => {
|
|
248
|
+
const conf = await getConfig(masterDb, name, hostOverride);
|
|
249
|
+
conf.migrations = { directory: migrations };
|
|
250
|
+
const db = knex(conf);
|
|
251
|
+
await db.migrate.rollback();
|
|
252
|
+
// Do not leave hanging connections.
|
|
253
|
+
await db.destroy();
|
|
254
|
+
};
|
|
255
|
+
/**
|
|
256
|
+
* Destroy customer database.
|
|
257
|
+
* @param masterDb
|
|
258
|
+
* @param name
|
|
259
|
+
* @param hostOverride
|
|
260
|
+
*/
|
|
261
|
+
const destroy = async (masterDb, name, hostOverride = null) => {
|
|
262
|
+
const db = await masterDb('databases').where({ name }).first();
|
|
263
|
+
if (!db) {
|
|
264
|
+
return 'Database not found.';
|
|
265
|
+
}
|
|
266
|
+
const dbToDelete = await get(masterDb, name, hostOverride);
|
|
267
|
+
await dbToDelete.raw(`DROP OWNED BY ${db.user}`);
|
|
268
|
+
disconnect(name);
|
|
269
|
+
await masterDb('database_users').where({ database_id: db.id }).delete();
|
|
270
|
+
await masterDb('databases').where({ id: db.id }).delete();
|
|
271
|
+
const rootDb = getRoot();
|
|
272
|
+
await rootDb.raw(`DROP OWNED BY ${db.user}`);
|
|
273
|
+
await rootDb.raw(`DROP USER ${db.user}`);
|
|
274
|
+
await rootDb.raw(`DROP DATABASE ${db.name} WITH (FORCE)`);
|
|
275
|
+
return null;
|
|
276
|
+
};
|
|
277
|
+
/**
|
|
278
|
+
* Generate a name for a database that does not yet exist.
|
|
279
|
+
* @param masterDb - Master database.
|
|
280
|
+
* @param init - Initial part of the name.
|
|
281
|
+
* @returns Suitable name.
|
|
282
|
+
*/
|
|
283
|
+
async function findName(masterDb, init) {
|
|
284
|
+
let n = 1;
|
|
285
|
+
let name = init;
|
|
286
|
+
while (await exists(masterDb, opaque(name))) {
|
|
287
|
+
n++;
|
|
288
|
+
name = `${init}${n}`;
|
|
289
|
+
}
|
|
290
|
+
return name;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Terminate all DB connections.
|
|
294
|
+
*/
|
|
295
|
+
async function disconnectAll() {
|
|
296
|
+
if (masterCache) {
|
|
297
|
+
log('Disconnecting master DB.');
|
|
298
|
+
await masterCache.destroy();
|
|
299
|
+
}
|
|
300
|
+
if (rootCache) {
|
|
301
|
+
log('Disconnecting root DB.');
|
|
302
|
+
await rootCache.destroy();
|
|
303
|
+
}
|
|
304
|
+
for (const conn of Object.entries(cache)) {
|
|
305
|
+
log(`Disconnecting ${conn[0]}`);
|
|
306
|
+
await conn[1].destroy();
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Get a list of all customer database connection infos.
|
|
311
|
+
*/
|
|
312
|
+
async function customerDbs(hostOverride = null) {
|
|
313
|
+
const all = [];
|
|
314
|
+
await vault.initialize();
|
|
315
|
+
const master = await getMaster();
|
|
316
|
+
const dbs = await master('databases').select('name', 'host', 'port', 'user', 'password');
|
|
317
|
+
for (const db of dbs) {
|
|
318
|
+
const conf = await getConfig(master, db.name, hostOverride);
|
|
319
|
+
const { host, port, database, user, password } = conf.connection;
|
|
320
|
+
all.push({ host, port, database, user, password });
|
|
321
|
+
}
|
|
322
|
+
return all;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Resolve database host from environment DATABASE_URL if set.
|
|
326
|
+
*/
|
|
327
|
+
function envHost() {
|
|
328
|
+
if (!process.env.DATABASE_URL) {
|
|
329
|
+
return null;
|
|
330
|
+
}
|
|
331
|
+
const url = new URL(process.env.DATABASE_URL);
|
|
332
|
+
return url.hostname;
|
|
333
|
+
}
|
|
334
|
+
export const DB = {
|
|
335
|
+
create,
|
|
336
|
+
customerDbs,
|
|
337
|
+
destroy,
|
|
338
|
+
disconnectAll,
|
|
339
|
+
envHost,
|
|
340
|
+
exists,
|
|
341
|
+
findName,
|
|
342
|
+
get,
|
|
343
|
+
getConfig,
|
|
344
|
+
getKnexConfig,
|
|
345
|
+
getMaster,
|
|
346
|
+
getMasterConfig,
|
|
347
|
+
getRoot,
|
|
348
|
+
getRootConfig,
|
|
349
|
+
isValidName,
|
|
350
|
+
migrate,
|
|
351
|
+
migrateMaster,
|
|
352
|
+
rollback
|
|
353
|
+
};
|
|
354
|
+
//# sourceMappingURL=DB.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DB.js","sourceRoot":"","sources":["../../../src/database/DB.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAgB,KAAK,EAAgB,cAAc,EAAE,GAAG,EAAe,MAAM,iBAAiB,CAAA;AAC7G,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,IAAI,CAAA;AACxC,OAAO,IAAc,MAAM,MAAM,CAAA;AACjC,OAAO,EAAE,KAAK,EAAE,MAAM,IAAI,CAAA;AAC1B,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AACnC,OAAO,EAAE,MAAM,IAAI,MAAM,EAAE,MAAM,WAAW,CAAA;AAE5C,sDAAsD;AACtD,MAAM,eAAe,GAAG,EAAE,CAAA;AAE1B,8DAA8D;AAC9D,MAAM,SAAS,GAAG,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAA;AAC9B,KAAK,CAAC,aAAa,CAAC,QAAQ,CAAC,WAAW,EAAE,SAAS,CAAC,CAAA;AACpD,KAAK,CAAC,aAAa,CAAC,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC,CAAA;AAClD,KAAK,CAAC,aAAa,CAAC,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC,CAAA;AAc7C;;;;;GAKG;AACH,MAAM,MAAM,GAAG,KAAK,EAAE,MAAoB,EAAE,IAAkB,EAAoB,EAAE;IAClF,OAAO,CAAC,CAAC,CAAC,MAAM,MAAM,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC,CAAA;AAC1E,CAAC,CAAA;AAED;;;;;;GAMG;AACH,MAAM,SAAS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;IACb,OAAO;IACP,MAAM;IACN,QAAQ;IACR,GAAG;IACH,SAAS;IACT,aAAa;IACb,SAAS;IACT,eAAe;IACf,OAAO;IACP,aAAa;IACb,WAAW;IACX,OAAO;IACP,aAAa;IACb,QAAQ;CACT,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/database/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,cAAc,sBAAsB,CAAA;AACpC,cAAc,MAAM,CAAA"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* # Tasenor Common for Node
|
|
4
|
+
*
|
|
5
|
+
* Type definitions and functionality implementation for Node.
|
|
6
|
+
*
|
|
7
|
+
* An interactive stateful process is a backend system which handles a process with a
|
|
8
|
+
* states, but the transition from one state to another could need user input from
|
|
9
|
+
* the client - e.g. web browser.
|
|
10
|
+
*
|
|
11
|
+
* ## Sub-modules
|
|
12
|
+
*
|
|
13
|
+
* * [cli](tasenor_common_node_src_cli.html)
|
|
14
|
+
* * [database](tasenor_common_node_src_database.html)
|
|
15
|
+
* * [export](tasenor_common_node_src_export.html)
|
|
16
|
+
* * [import](tasenor_common_node_src_import.html)
|
|
17
|
+
* * [net](tasenor_common_node_src_net.html)
|
|
18
|
+
* * [plugins](tasenor_common_node_src_plugins.html)
|
|
19
|
+
* * [reports](tasenor_common_node_src_reports.html)
|
|
20
|
+
* * [system](tasenor_common_node_src_system.html)
|
|
21
|
+
* * [common](tasenor_common_node_src_common.html)
|
|
22
|
+
* * [error](tasenor_common_node_src_error.html)
|
|
23
|
+
* * [import](tasenor_common_node_src_import.html)
|
|
24
|
+
* * [process](tasenor_common_node_src_process.html)
|
|
25
|
+
* * [server](tasenor_common_node_src_server.html)
|
|
26
|
+
*
|
|
27
|
+
* @module tasenor-common-node
|
|
28
|
+
*/
|
|
29
|
+
export * from './index';
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* # Tasenor Common for Node
|
|
4
|
+
*
|
|
5
|
+
* Type definitions and functionality implementation for Node.
|
|
6
|
+
*
|
|
7
|
+
* An interactive stateful process is a backend system which handles a process with a
|
|
8
|
+
* states, but the transition from one state to another could need user input from
|
|
9
|
+
* the client - e.g. web browser.
|
|
10
|
+
*
|
|
11
|
+
* ## Sub-modules
|
|
12
|
+
*
|
|
13
|
+
* * [cli](tasenor_common_node_src_cli.html)
|
|
14
|
+
* * [database](tasenor_common_node_src_database.html)
|
|
15
|
+
* * [export](tasenor_common_node_src_export.html)
|
|
16
|
+
* * [import](tasenor_common_node_src_import.html)
|
|
17
|
+
* * [net](tasenor_common_node_src_net.html)
|
|
18
|
+
* * [plugins](tasenor_common_node_src_plugins.html)
|
|
19
|
+
* * [reports](tasenor_common_node_src_reports.html)
|
|
20
|
+
* * [system](tasenor_common_node_src_system.html)
|
|
21
|
+
* * [common](tasenor_common_node_src_common.html)
|
|
22
|
+
* * [error](tasenor_common_node_src_error.html)
|
|
23
|
+
* * [import](tasenor_common_node_src_import.html)
|
|
24
|
+
* * [process](tasenor_common_node_src_process.html)
|
|
25
|
+
* * [server](tasenor_common_node_src_server.html)
|
|
26
|
+
*
|
|
27
|
+
* @module tasenor-common-node
|
|
28
|
+
*/
|
|
29
|
+
export * from './index';
|
|
30
|
+
//# sourceMappingURL=doccer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"doccer.js","sourceRoot":"","sources":["../../src/doccer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,cAAc,SAAS,CAAA"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error types for processing and an exception used to ask more information from UI.
|
|
3
|
+
*
|
|
4
|
+
* @module tasenor-common-node/src/error
|
|
5
|
+
*/
|
|
6
|
+
import { TasenorElement } from '@tasenor/common';
|
|
7
|
+
export declare class ProcessingError extends Error {
|
|
8
|
+
}
|
|
9
|
+
export declare class InvalidFile extends ProcessingError {
|
|
10
|
+
}
|
|
11
|
+
export declare class InvalidArgument extends ProcessingError {
|
|
12
|
+
}
|
|
13
|
+
export declare class BadState extends ProcessingError {
|
|
14
|
+
}
|
|
15
|
+
export declare class NotImplemented extends ProcessingError {
|
|
16
|
+
}
|
|
17
|
+
export declare class NotFound extends ProcessingError {
|
|
18
|
+
}
|
|
19
|
+
export declare class DatabaseError extends ProcessingError {
|
|
20
|
+
}
|
|
21
|
+
export declare class SystemError extends ProcessingError {
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Special exception to halt processing in order to require more configuration information from UI.
|
|
25
|
+
*/
|
|
26
|
+
export declare class AskUI extends Error {
|
|
27
|
+
element: TasenorElement;
|
|
28
|
+
constructor(element: TasenorElement);
|
|
29
|
+
}
|
|
30
|
+
export declare function isAskUI(obj: unknown): obj is AskUI;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error types for processing and an exception used to ask more information from UI.
|
|
3
|
+
*
|
|
4
|
+
* @module tasenor-common-node/src/error
|
|
5
|
+
*/
|
|
6
|
+
export class ProcessingError extends Error {
|
|
7
|
+
}
|
|
8
|
+
export class InvalidFile extends ProcessingError {
|
|
9
|
+
}
|
|
10
|
+
export class InvalidArgument extends ProcessingError {
|
|
11
|
+
}
|
|
12
|
+
export class BadState extends ProcessingError {
|
|
13
|
+
}
|
|
14
|
+
export class NotImplemented extends ProcessingError {
|
|
15
|
+
}
|
|
16
|
+
export class NotFound extends ProcessingError {
|
|
17
|
+
}
|
|
18
|
+
export class DatabaseError extends ProcessingError {
|
|
19
|
+
}
|
|
20
|
+
export class SystemError extends ProcessingError {
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Special exception to halt processing in order to require more configuration information from UI.
|
|
24
|
+
*/
|
|
25
|
+
export class AskUI extends Error {
|
|
26
|
+
element;
|
|
27
|
+
constructor(element) {
|
|
28
|
+
super('Need more information from UI.');
|
|
29
|
+
this.element = element;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export function isAskUI(obj) {
|
|
33
|
+
return (obj instanceof Error) && 'element' in obj;
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.js","sourceRoot":"","sources":["../../src/error.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,MAAM,OAAO,eAAgB,SAAQ,KAAK;CAAG;AAC7C,MAAM,OAAO,WAAY,SAAQ,eAAe;CAAG;AACnD,MAAM,OAAO,eAAgB,SAAQ,eAAe;CAAG;AACvD,MAAM,OAAO,QAAS,SAAQ,eAAe;CAAG;AAChD,MAAM,OAAO,cAAe,SAAQ,eAAe;CAAG;AACtD,MAAM,OAAO,QAAS,SAAQ,eAAe;CAAG;AAChD,MAAM,OAAO,aAAc,SAAQ,eAAe;CAAG;AACrD,MAAM,OAAO,WAAY,SAAQ,eAAe;CAAG;AAEnD;;GAEG;AACH,MAAM,OAAO,KAAM,SAAQ,KAAK;IACvB,OAAO,CAAgB;IAE9B,YAAY,OAAuB;QACjC,KAAK,CAAC,gCAAgC,CAAC,CAAA;QACvC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;IACxB,CAAC;CACF;AAED,MAAM,UAAU,OAAO,CAAC,GAAY;IAClC,OAAO,CAAC,GAAG,YAAY,KAAK,CAAC,IAAI,SAAS,IAAI,GAAG,CAAA;AACnD,CAAC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { KnexDatabase } from '..';
|
|
2
|
+
import { ParsedTsvFileData, BookkeeperConfig, DirectoryPath, TsvFilePath, JsonFilePath, Json, TarFilePath } from '@tasenor/common';
|
|
3
|
+
/**
|
|
4
|
+
* Common functionality for exporters.
|
|
5
|
+
*/
|
|
6
|
+
export declare class Exporter {
|
|
7
|
+
/**
|
|
8
|
+
* Version number of the file format produced by this exporter.
|
|
9
|
+
*/
|
|
10
|
+
VERSION: number;
|
|
11
|
+
/**
|
|
12
|
+
* Read all accounts from the database and generate TSV-data.
|
|
13
|
+
* @param db Knex connection to use.
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
getAccounts(db: KnexDatabase): Promise<ParsedTsvFileData>;
|
|
17
|
+
/**
|
|
18
|
+
* Read all periods from the database and generate TSV-data.
|
|
19
|
+
* @param db Knex connection to use.
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
getPeriods(db: KnexDatabase): Promise<ParsedTsvFileData>;
|
|
23
|
+
/**
|
|
24
|
+
* Read all entries and documents from the database and generate TSV-data.
|
|
25
|
+
* @param db Knex connection to use.
|
|
26
|
+
* @returns
|
|
27
|
+
*/
|
|
28
|
+
getEntries(db: KnexDatabase): Promise<ParsedTsvFileData>;
|
|
29
|
+
/**
|
|
30
|
+
* Read configuration information from database and construct compiled configuration.
|
|
31
|
+
* @param db Knex connection to use.
|
|
32
|
+
* @returns
|
|
33
|
+
*/
|
|
34
|
+
getConfig(db: KnexDatabase): Promise<BookkeeperConfig>;
|
|
35
|
+
/**
|
|
36
|
+
* Read all tags from the database and generate TSV-data.
|
|
37
|
+
* @param db Knex connection to use.
|
|
38
|
+
* @param out Directory to write image files.
|
|
39
|
+
* @returns
|
|
40
|
+
*/
|
|
41
|
+
getTags(db: KnexDatabase, out: DirectoryPath): Promise<ParsedTsvFileData>;
|
|
42
|
+
/**
|
|
43
|
+
* Write prepared data to TSV file.
|
|
44
|
+
* @param path Output file path.
|
|
45
|
+
* @param lines Data content.
|
|
46
|
+
*/
|
|
47
|
+
writeTsv(path: TsvFilePath, lines: ParsedTsvFileData): void;
|
|
48
|
+
/**
|
|
49
|
+
* Write prepared data to JSON file.
|
|
50
|
+
* @param path Output file path.
|
|
51
|
+
* @param lines Data content.
|
|
52
|
+
*/
|
|
53
|
+
writeJson(path: JsonFilePath, data: Json): void;
|
|
54
|
+
/**
|
|
55
|
+
* Save complete backup of the Sqlite database to the given directory.
|
|
56
|
+
* @param db Database connection.
|
|
57
|
+
* @param out Directory to store all files.
|
|
58
|
+
* @returns Configuration constructed from the database.
|
|
59
|
+
*/
|
|
60
|
+
dump(db: KnexDatabase, out: DirectoryPath): Promise<BookkeeperConfig>;
|
|
61
|
+
/**
|
|
62
|
+
* Construct a tar-package for the given configuration from the source directory.
|
|
63
|
+
* @param conf Configuration found from the database.
|
|
64
|
+
* @param out Directory containing files extracted as a backup.
|
|
65
|
+
* @param destPath Destionation file name if given.
|
|
66
|
+
* @returns Path to the tar-package.
|
|
67
|
+
*/
|
|
68
|
+
makeTar(conf: BookkeeperConfig, out: DirectoryPath, destPath: DirectoryPath | undefined): Promise<TarFilePath>;
|
|
69
|
+
}
|