@tstdl/base 0.89.5 → 0.89.7
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.
|
@@ -7,9 +7,10 @@ import type { FunctionModuleFunction } from '../module/modules/function.module.j
|
|
|
7
7
|
import type { OneOrMany, Type } from '../types.js';
|
|
8
8
|
export type BootstrapFn = () => void | Promise<void>;
|
|
9
9
|
export type RunOptions = {
|
|
10
|
-
bootstrap?: BootstrapFn
|
|
10
|
+
bootstrap?: OneOrMany<BootstrapFn>;
|
|
11
11
|
};
|
|
12
|
-
export
|
|
12
|
+
export type ApplicationArgument = LoggerArgument;
|
|
13
|
+
export declare class Application implements Resolvable<ApplicationArgument> {
|
|
13
14
|
#private;
|
|
14
15
|
static _instance: Application | undefined;
|
|
15
16
|
private static get instance();
|
|
@@ -16,6 +16,7 @@ import { ModuleState } from '../module/module.js';
|
|
|
16
16
|
import { FunctionModule } from '../module/modules/function.module.js';
|
|
17
17
|
import { getShutdownSignal, getShutdownToken } from '../process-shutdown.js';
|
|
18
18
|
import { DeferredPromise } from '../promise/deferred-promise.js';
|
|
19
|
+
import { toArray } from '../utils/array/array.js';
|
|
19
20
|
import { mapAsync } from '../utils/async-iterable-helpers/map.js';
|
|
20
21
|
import { toArrayAsync } from '../utils/async-iterable-helpers/to-array.js';
|
|
21
22
|
import { isDefined, isFunction, isObject, isUndefined } from '../utils/type-guards.js';
|
|
@@ -68,6 +69,9 @@ let Application = class Application {
|
|
|
68
69
|
this.registerModule(module);
|
|
69
70
|
}
|
|
70
71
|
run(...optionsFunctionsAndModules) {
|
|
72
|
+
if (this.#shutdownToken.isSet) {
|
|
73
|
+
throw new Error('Application was shut down.');
|
|
74
|
+
}
|
|
71
75
|
const options = ((optionsFunctionsAndModules.length > 0) && isObject(optionsFunctionsAndModules[0])) ? optionsFunctionsAndModules[0] : undefined;
|
|
72
76
|
const functionsAndModules = (isUndefined(options) ? optionsFunctionsAndModules : optionsFunctionsAndModules.slice(1));
|
|
73
77
|
void this._run(functionsAndModules, options);
|
|
@@ -95,7 +99,9 @@ let Application = class Application {
|
|
|
95
99
|
}
|
|
96
100
|
}
|
|
97
101
|
if (isDefined(options.bootstrap)) {
|
|
98
|
-
|
|
102
|
+
for (const fn of toArray(options.bootstrap)) {
|
|
103
|
+
await runInInjectionContext(this.#injector, fn);
|
|
104
|
+
}
|
|
99
105
|
}
|
|
100
106
|
const modules = await toArrayAsync(mapAsync(this.#moduleTypesAndInstances, async (instanceOrType) => (isFunction(instanceOrType) ? this.#injector.resolveAsync(instanceOrType) : instanceOrType)));
|
|
101
107
|
try {
|
package/migration/migrator.d.ts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import { LockProvider } from '../lock/index.js';
|
|
2
|
-
import { Logger } from '../logger/index.js';
|
|
3
|
-
import { MigrationStateRepository } from './migration-state-repository.js';
|
|
4
1
|
export type MigrationDefinition<T = void> = {
|
|
5
2
|
name: string;
|
|
6
3
|
migrations: Migration<T>[];
|
|
@@ -21,9 +18,6 @@ export type MigrationResult<T> = {
|
|
|
21
18
|
restartRequested: boolean;
|
|
22
19
|
};
|
|
23
20
|
export declare class Migrator {
|
|
24
|
-
private
|
|
25
|
-
private readonly lockProvider;
|
|
26
|
-
private readonly logger;
|
|
27
|
-
constructor(migrationStateRepository: MigrationStateRepository, lockProvider: LockProvider, logger: Logger);
|
|
21
|
+
#private;
|
|
28
22
|
migrate<T>({ name, migrations }: MigrationDefinition<T>): Promise<MigrationResult<T>[]>;
|
|
29
23
|
}
|
package/migration/migrator.js
CHANGED
|
@@ -4,14 +4,8 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
4
4
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
5
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
6
|
};
|
|
7
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
-
};
|
|
10
|
-
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
|
-
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
|
-
};
|
|
13
7
|
import { CancellationToken } from '../cancellation/index.js';
|
|
14
|
-
import {
|
|
8
|
+
import { Injector, Singleton, inject, runInInjectionContext } from '../injector/index.js';
|
|
15
9
|
import { LockProvider } from '../lock/index.js';
|
|
16
10
|
import { Logger } from '../logger/index.js';
|
|
17
11
|
import { toArray } from '../utils/array/array.js';
|
|
@@ -21,20 +15,16 @@ import { Timer } from '../utils/timer.js';
|
|
|
21
15
|
import { isDefined } from '../utils/type-guards.js';
|
|
22
16
|
import { MigrationStateRepository } from './migration-state-repository.js';
|
|
23
17
|
let Migrator = class Migrator {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
this.migrationStateRepository = migrationStateRepository;
|
|
29
|
-
this.lockProvider = lockProvider;
|
|
30
|
-
this.logger = logger;
|
|
31
|
-
}
|
|
18
|
+
#injector = inject(Injector);
|
|
19
|
+
#migrationStateRepository = inject(MigrationStateRepository);
|
|
20
|
+
#lockProvider = inject(LockProvider, 'migrator:');
|
|
21
|
+
#logger = inject(Logger, 'Migrator');
|
|
32
22
|
// eslint-disable-next-line max-statements, max-lines-per-function
|
|
33
23
|
async migrate({ name, migrations }) {
|
|
34
24
|
if (migrations.length == 0) {
|
|
35
|
-
throw new Error('
|
|
25
|
+
throw new Error('No migrations provided.');
|
|
36
26
|
}
|
|
37
|
-
const lock = this
|
|
27
|
+
const lock = this.#lockProvider.get(`${name}`);
|
|
38
28
|
// eslint-disable-next-line max-statements, max-lines-per-function
|
|
39
29
|
const { result } = await lock.use(30000, true, async () => {
|
|
40
30
|
const results = [];
|
|
@@ -43,7 +33,7 @@ let Migrator = class Migrator {
|
|
|
43
33
|
const control = {
|
|
44
34
|
restart: () => restartToken.set()
|
|
45
35
|
};
|
|
46
|
-
const currentState = await this
|
|
36
|
+
const currentState = await this.#migrationStateRepository.tryLoadByFilter({ name });
|
|
47
37
|
const currentRevision = currentState?.revision ?? 'init';
|
|
48
38
|
const latestRevision = migrations.sort(compareByValueSelectionDescending((migration) => migration.to))[0].to;
|
|
49
39
|
if (currentRevision == latestRevision) {
|
|
@@ -51,29 +41,29 @@ let Migrator = class Migrator {
|
|
|
51
41
|
}
|
|
52
42
|
const suitableMigrations = migrations.filter((migration) => toArray(migration.from).includes(currentRevision));
|
|
53
43
|
if (suitableMigrations.length == 0) {
|
|
54
|
-
throw new Error(`
|
|
44
|
+
throw new Error(`No suitable migration path from current revision ${currentRevision} to latest revision ${latestRevision} found.`);
|
|
55
45
|
}
|
|
56
46
|
const migration = suitableMigrations.sort(compareByValueSelectionDescending((m) => m.to))[0];
|
|
57
|
-
this
|
|
47
|
+
this.#logger.warn(`Starting migration for "${name}" from revision ${currentRevision} to ${migration.to}.`);
|
|
58
48
|
let migratorResult;
|
|
59
|
-
const time = await Timer.measureAsync(async () => (migratorResult = await migration.migrator(control)));
|
|
49
|
+
const time = await Timer.measureAsync(async () => (migratorResult = await runInInjectionContext(this.#injector, async () => migration.migrator(control))));
|
|
60
50
|
results.push({ from: currentRevision, to: migration.to, time, result: migratorResult, restartRequested: restartToken.isSet });
|
|
61
51
|
if (restartToken.isSet) {
|
|
62
|
-
this
|
|
63
|
-
this
|
|
52
|
+
this.#logger.warn(`Finished migration in ${round(time / 1000, 2)} seconds.`);
|
|
53
|
+
this.#logger.warn('Migration-restart requested.');
|
|
64
54
|
continue;
|
|
65
55
|
}
|
|
66
56
|
if (isDefined(currentState)) {
|
|
67
|
-
await this
|
|
57
|
+
await this.#migrationStateRepository.patchByFilter({ name }, { revision: migration.to });
|
|
68
58
|
}
|
|
69
59
|
else {
|
|
70
60
|
const newState = {
|
|
71
61
|
name,
|
|
72
62
|
revision: migration.to
|
|
73
63
|
};
|
|
74
|
-
await this
|
|
64
|
+
await this.#migrationStateRepository.insert(newState);
|
|
75
65
|
}
|
|
76
|
-
this
|
|
66
|
+
this.#logger.warn(`Finished migration in ${round(time / 1000, 2)} seconds.`);
|
|
77
67
|
}
|
|
78
68
|
return results;
|
|
79
69
|
});
|
|
@@ -81,9 +71,6 @@ let Migrator = class Migrator {
|
|
|
81
71
|
}
|
|
82
72
|
};
|
|
83
73
|
Migrator = __decorate([
|
|
84
|
-
Singleton()
|
|
85
|
-
__param(1, ResolveArg('migrator:')),
|
|
86
|
-
__param(2, ResolveArg('Migrator')),
|
|
87
|
-
__metadata("design:paramtypes", [MigrationStateRepository, LockProvider, Logger])
|
|
74
|
+
Singleton()
|
|
88
75
|
], Migrator);
|
|
89
76
|
export { Migrator };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tstdl/base",
|
|
3
|
-
"version": "0.89.
|
|
3
|
+
"version": "0.89.7",
|
|
4
4
|
"author": "Patrick Hein",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -130,7 +130,7 @@
|
|
|
130
130
|
"typescript": "5.2"
|
|
131
131
|
},
|
|
132
132
|
"peerDependencies": {
|
|
133
|
-
"@elastic/elasticsearch": "^8.
|
|
133
|
+
"@elastic/elasticsearch": "^8.10",
|
|
134
134
|
"@koa/router": "^12.0",
|
|
135
135
|
"@tstdl/angular": "^0.89",
|
|
136
136
|
"@zxcvbn-ts/core": "^3.0",
|
|
@@ -146,7 +146,7 @@
|
|
|
146
146
|
"mongodb": "^6.1",
|
|
147
147
|
"nodemailer": "^6.9",
|
|
148
148
|
"playwright": "^1.38",
|
|
149
|
-
"preact": "^10.
|
|
149
|
+
"preact": "^10.18",
|
|
150
150
|
"preact-render-to-string": "^6.2",
|
|
151
151
|
"undici": "^5.25",
|
|
152
152
|
"urlpattern-polyfill": "^9.0"
|