@rsdk/db.typeorm 2.5.1 → 3.0.1-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +73 -1
- package/dist/data-source-extractor.d.ts +20 -0
- package/dist/data-source-extractor.js +43 -0
- package/dist/data-source-extractor.js.map +1 -0
- package/dist/modules/migration-checker.module.d.ts +9 -0
- package/dist/modules/migration-checker.module.js +42 -0
- package/dist/modules/migration-checker.module.js.map +1 -0
- package/dist/typeorm.plugin.d.ts +2 -0
- package/dist/typeorm.plugin.js +17 -0
- package/dist/typeorm.plugin.js.map +1 -1
- package/package.json +11 -8
- package/src/data-source-extractor.ts +50 -0
- package/src/modules/migration-checker.module.ts +24 -0
- package/src/typeorm.plugin.ts +21 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,7 +3,79 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
-
## [
|
|
6
|
+
## [3.0.1-next.0](https://github.com/R-Vision/rsdk/compare/v3.0.0...v3.0.1-next.0) (2023-08-07)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* **deps:** correctly peerDependencies ([7483e35](https://github.com/R-Vision/rsdk/commit/7483e3560b5cf23ebebe6149bd41ebb517821e99))
|
|
11
|
+
|
|
12
|
+
## [3.0.0](https://github.com/R-Vision/rsdk/compare/v3.0.0-next.0...v3.0.0) (2023-08-04)
|
|
13
|
+
|
|
14
|
+
**Note:** Version bump only for package @rsdk/db.typeorm
|
|
15
|
+
|
|
16
|
+
## [3.0.0-next.0](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.16...v3.0.0-next.0) (2023-08-04)
|
|
17
|
+
|
|
18
|
+
**Note:** Version bump only for package @rsdk/db.typeorm
|
|
19
|
+
|
|
20
|
+
# [2.6.0-next.16](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.15...v2.6.0-next.16) (2023-08-04)
|
|
21
|
+
|
|
22
|
+
**Note:** Version bump only for package @rsdk/db.typeorm
|
|
23
|
+
|
|
24
|
+
# [2.6.0-next.15](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.14...v2.6.0-next.15) (2023-08-04)
|
|
25
|
+
|
|
26
|
+
**Note:** Version bump only for package @rsdk/db.typeorm
|
|
27
|
+
|
|
28
|
+
# [2.6.0-next.13](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.12...v2.6.0-next.13) (2023-08-01)
|
|
29
|
+
|
|
30
|
+
**Note:** Version bump only for package @rsdk/db.typeorm
|
|
31
|
+
|
|
32
|
+
# [2.6.0-next.12](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.11...v2.6.0-next.12) (2023-07-28)
|
|
33
|
+
|
|
34
|
+
**Note:** Version bump only for package @rsdk/db.typeorm
|
|
35
|
+
|
|
36
|
+
# [2.6.0-next.11](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.10...v2.6.0-next.11) (2023-07-26)
|
|
37
|
+
|
|
38
|
+
**Note:** Version bump only for package @rsdk/db.typeorm
|
|
39
|
+
|
|
40
|
+
# [2.6.0-next.10](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.9...v2.6.0-next.10) (2023-07-25)
|
|
41
|
+
|
|
42
|
+
**Note:** Version bump only for package @rsdk/db.typeorm
|
|
43
|
+
|
|
44
|
+
# [2.6.0-next.9](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.8...v2.6.0-next.9) (2023-07-25)
|
|
45
|
+
|
|
46
|
+
**Note:** Version bump only for package @rsdk/db.typeorm
|
|
47
|
+
|
|
48
|
+
# [2.6.0-next.8](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.7...v2.6.0-next.8) (2023-07-23)
|
|
49
|
+
|
|
50
|
+
**Note:** Version bump only for package @rsdk/db.typeorm
|
|
51
|
+
|
|
52
|
+
# [2.6.0-next.7](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.6...v2.6.0-next.7) (2023-07-23)
|
|
53
|
+
|
|
54
|
+
**Note:** Version bump only for package @rsdk/db.typeorm
|
|
55
|
+
|
|
56
|
+
# [2.6.0-next.6](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.5...v2.6.0-next.6) (2023-07-22)
|
|
57
|
+
|
|
58
|
+
**Note:** Version bump only for package @rsdk/db.typeorm
|
|
59
|
+
|
|
60
|
+
# [2.6.0-next.5](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.4...v2.6.0-next.5) (2023-07-22)
|
|
61
|
+
|
|
62
|
+
### Features
|
|
63
|
+
|
|
64
|
+
* **db.typeorm:** change default migrationDir ([a647fcb](https://github.com/R-Vision/rsdk/commit/a647fcb3e94299a98511957b6dd36d6c625354ae))
|
|
65
|
+
|
|
66
|
+
# [2.6.0-next.4](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.3...v2.6.0-next.4) (2023-07-10)
|
|
67
|
+
|
|
68
|
+
### Features
|
|
69
|
+
|
|
70
|
+
* **db.typeorm:** added default migrations option ([#79](https://github.com/R-Vision/rsdk/issues/79)) ([c40b578](https://github.com/R-Vision/rsdk/commit/c40b578d6b1a9a18191bf3953b48d25539e67542))
|
|
71
|
+
|
|
72
|
+
# [2.6.0-next.3](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.2...v2.6.0-next.3) (2023-07-07)
|
|
73
|
+
|
|
74
|
+
### Features
|
|
75
|
+
|
|
76
|
+
* add data-source extractor from PlatformApp ([#76](https://github.com/R-Vision/rsdk/issues/76)) ([c668f12](https://github.com/R-Vision/rsdk/commit/c668f12142a009ac7c4bc5adc84b6fa8b85d413a))
|
|
77
|
+
|
|
78
|
+
# [2.6.0-next.1](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.0...v2.6.0-next.1) (2023-07-04)
|
|
7
79
|
|
|
8
80
|
**Note:** Version bump only for package @rsdk/db.typeorm
|
|
9
81
|
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { PlatformApp } from '@rsdk/core';
|
|
2
|
+
import { DataSource } from 'typeorm';
|
|
3
|
+
/**
|
|
4
|
+
* Нужен прежде всего для использования в связке с typeorm cli и указания результата вывода в файле
|
|
5
|
+
* Структура файлов
|
|
6
|
+
* ```
|
|
7
|
+
* src
|
|
8
|
+
* typeorm
|
|
9
|
+
* data-source.ts
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* // src/typeorm/data-source.ts
|
|
14
|
+
* import { app } from '../app'
|
|
15
|
+
* export default DataSourceExtractor.extract(app)
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare class DataSourceExtractor {
|
|
19
|
+
static extract(app: PlatformApp): Promise<DataSource>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DataSourceExtractor = void 0;
|
|
4
|
+
const core_1 = require("@nestjs/core");
|
|
5
|
+
const typeorm_core_module_1 = require("@nestjs/typeorm/dist/typeorm-core.module");
|
|
6
|
+
const core_2 = require("@rsdk/core");
|
|
7
|
+
const nest_tools_1 = require("@rsdk/nest-tools");
|
|
8
|
+
const typeorm_1 = require("typeorm");
|
|
9
|
+
/**
|
|
10
|
+
* Нужен прежде всего для использования в связке с typeorm cli и указания результата вывода в файле
|
|
11
|
+
* Структура файлов
|
|
12
|
+
* ```
|
|
13
|
+
* src
|
|
14
|
+
* typeorm
|
|
15
|
+
* data-source.ts
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* // src/typeorm/data-source.ts
|
|
20
|
+
* import { app } from '../app'
|
|
21
|
+
* export default DataSourceExtractor.extract(app)
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
class DataSourceExtractor {
|
|
25
|
+
static async extract(app) {
|
|
26
|
+
const root = await app.context.getRoot();
|
|
27
|
+
const iterator = new nest_tools_1.NestDefinitionIterator(root);
|
|
28
|
+
const nestModule = await iterator.find((definition) => definition === typeorm_core_module_1.TypeOrmCoreModule ||
|
|
29
|
+
('module' in definition && definition.module === typeorm_core_module_1.TypeOrmCoreModule), nest_tools_1.NestDefinitionType.Module);
|
|
30
|
+
if (!nestModule) {
|
|
31
|
+
throw new core_2.InternalException('Datasource not found');
|
|
32
|
+
}
|
|
33
|
+
const applicationContext = await core_1.NestFactory.createApplicationContext(new core_2.PlatformContext({
|
|
34
|
+
modules: [nestModule],
|
|
35
|
+
}).getRoot());
|
|
36
|
+
const dataSource = applicationContext.get(typeorm_1.DataSource);
|
|
37
|
+
// typeorm cli always call `initialize()`, dont check isInitialized
|
|
38
|
+
dataSource.isInitialized && (await dataSource.destroy());
|
|
39
|
+
return dataSource;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.DataSourceExtractor = DataSourceExtractor;
|
|
43
|
+
//# sourceMappingURL=data-source-extractor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"data-source-extractor.js","sourceRoot":"","sources":["../src/data-source-extractor.ts"],"names":[],"mappings":";;;AAAA,uCAA2C;AAC3C,kFAA6E;AAG7E,qCAAgE;AAChE,iDAA8E;AAC9E,qCAAqC;AAErC;;;;;;;;;;;;;;GAcG;AACH,MAAa,mBAAmB;IAC9B,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,GAAgB;QACnC,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;QACzC,MAAM,QAAQ,GAAG,IAAI,mCAAsB,CAAC,IAAI,CAAC,CAAC;QAElD,MAAM,UAAU,GAAG,MAAM,QAAQ,CAAC,IAAI,CACpC,CAAC,UAAU,EAAE,EAAE,CACb,UAAU,KAAK,uCAAiB;YAChC,CAAC,QAAQ,IAAI,UAAU,IAAI,UAAU,CAAC,MAAM,KAAK,uCAAiB,CAAC,EACrE,+BAAkB,CAAC,MAAM,CAC1B,CAAC;QACF,IAAI,CAAC,UAAU,EAAE;YACf,MAAM,IAAI,wBAAiB,CAAC,sBAAsB,CAAC,CAAC;SACrD;QACD,MAAM,kBAAkB,GAAG,MAAM,kBAAW,CAAC,wBAAwB,CACnE,IAAI,sBAAe,CAAC;YAClB,OAAO,EAAE,CAAC,UAAU,CAAC;SACtB,CAAC,CAAC,OAAO,EAAE,CACb,CAAC;QAEF,MAAM,UAAU,GAAG,kBAAkB,CAAC,GAAG,CAAC,oBAAU,CAAC,CAAC;QAEtD,mEAAmE;QACnE,UAAU,CAAC,aAAa,IAAI,CAAC,MAAM,UAAU,CAAC,OAAO,EAAE,CAAC,CAAC;QACzD,OAAO,UAAU,CAAC;IACpB,CAAC;CACF;AA1BD,kDA0BC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { OnApplicationBootstrap } from '@nestjs/common';
|
|
2
|
+
import { ILogger } from '@rsdk/logging';
|
|
3
|
+
import { DataSource } from 'typeorm';
|
|
4
|
+
export declare class MigrationCheckerModule implements OnApplicationBootstrap {
|
|
5
|
+
private dataSource;
|
|
6
|
+
private logger;
|
|
7
|
+
constructor(dataSource: DataSource, logger: ILogger);
|
|
8
|
+
onApplicationBootstrap(): Promise<void>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
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;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
var MigrationCheckerModule_1;
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.MigrationCheckerModule = void 0;
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const core_1 = require("@rsdk/core");
|
|
19
|
+
const typeorm_1 = require("typeorm");
|
|
20
|
+
let MigrationCheckerModule = exports.MigrationCheckerModule = MigrationCheckerModule_1 = class MigrationCheckerModule {
|
|
21
|
+
dataSource;
|
|
22
|
+
logger;
|
|
23
|
+
constructor(dataSource, logger) {
|
|
24
|
+
this.dataSource = dataSource;
|
|
25
|
+
this.logger = logger;
|
|
26
|
+
}
|
|
27
|
+
async onApplicationBootstrap() {
|
|
28
|
+
const migrationExecutor = new typeorm_1.MigrationExecutor(this.dataSource);
|
|
29
|
+
const migrations = await migrationExecutor.getPendingMigrations();
|
|
30
|
+
if (migrations.length > 0) {
|
|
31
|
+
this.logger.warn('Found pending migrations!', {
|
|
32
|
+
migrations,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
exports.MigrationCheckerModule = MigrationCheckerModule = MigrationCheckerModule_1 = __decorate([
|
|
38
|
+
(0, common_1.Module)({}),
|
|
39
|
+
__param(1, (0, core_1.InjectLogger)(MigrationCheckerModule_1)),
|
|
40
|
+
__metadata("design:paramtypes", [typeorm_1.DataSource, Object])
|
|
41
|
+
], MigrationCheckerModule);
|
|
42
|
+
//# sourceMappingURL=migration-checker.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migration-checker.module.js","sourceRoot":"","sources":["../../src/modules/migration-checker.module.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AACA,2CAAwC;AACxC,qCAA0C;AAE1C,qCAAwD;AAGjD,IAAM,sBAAsB,+DAA5B,MAAM,sBAAsB;IAEvB;IACsC;IAFhD,YACU,UAAsB,EACgB,MAAe;QADrD,eAAU,GAAV,UAAU,CAAY;QACgB,WAAM,GAAN,MAAM,CAAS;IAC5D,CAAC;IAEJ,KAAK,CAAC,sBAAsB;QAC1B,MAAM,iBAAiB,GAAG,IAAI,2BAAiB,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QACjE,MAAM,UAAU,GAAG,MAAM,iBAAiB,CAAC,oBAAoB,EAAE,CAAC;QAElE,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE;YACzB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,2BAA2B,EAAE;gBAC5C,UAAU;aACX,CAAC,CAAC;SACJ;IACH,CAAC;CACF,CAAA;iCAhBY,sBAAsB;IADlC,IAAA,eAAM,EAAC,EAAE,CAAC;IAIN,WAAA,IAAA,mBAAY,EAAC,wBAAsB,CAAC,CAAA;qCADjB,oBAAU;GAFrB,sBAAsB,CAgBlC"}
|
package/dist/typeorm.plugin.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type { AppropriateTransports, IErrorsTransformer, PlatformAppPlugin } fro
|
|
|
4
4
|
import type { TypeOrmPluginOptions } from './types';
|
|
5
5
|
export declare class TypeOrmPlugin implements PlatformAppPlugin {
|
|
6
6
|
private readonly options;
|
|
7
|
+
private static readonly logger;
|
|
7
8
|
/**
|
|
8
9
|
* По умолчанию парсит тип драйвера из env %PACKAGE_NAME%_DB_URL
|
|
9
10
|
*/
|
|
@@ -12,4 +13,5 @@ export declare class TypeOrmPlugin implements PlatformAppPlugin {
|
|
|
12
13
|
forTransports(): AppropriateTransports;
|
|
13
14
|
modules(): (Constructor | DynamicModule)[];
|
|
14
15
|
errorTransformers(): IErrorsTransformer[];
|
|
16
|
+
private getMigrationsOption;
|
|
15
17
|
}
|
package/dist/typeorm.plugin.js
CHANGED
|
@@ -3,11 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.TypeOrmPlugin = void 0;
|
|
4
4
|
const typeorm_1 = require("@nestjs/typeorm");
|
|
5
5
|
const core_1 = require("@rsdk/core");
|
|
6
|
+
const logging_1 = require("@rsdk/logging");
|
|
7
|
+
const node_path_1 = require("node:path");
|
|
6
8
|
const initializer_1 = require("./internal/initializer");
|
|
9
|
+
const migration_checker_module_1 = require("./modules/migration-checker.module");
|
|
7
10
|
const providers_1 = require("./providers");
|
|
8
11
|
const typeorm_errors_transformer_1 = require("./typeorm.errors-transformer");
|
|
9
12
|
class TypeOrmPlugin {
|
|
10
13
|
options;
|
|
14
|
+
static logger = logging_1.LoggerFactory.create(TypeOrmPlugin);
|
|
11
15
|
/**
|
|
12
16
|
* По умолчанию парсит тип драйвера из env %PACKAGE_NAME%_DB_URL
|
|
13
17
|
*/
|
|
@@ -22,6 +26,10 @@ class TypeOrmPlugin {
|
|
|
22
26
|
return 'any';
|
|
23
27
|
}
|
|
24
28
|
modules() {
|
|
29
|
+
const migrations = this.getMigrationsOption();
|
|
30
|
+
if (migrations.length === 0) {
|
|
31
|
+
TypeOrmPlugin.logger.warn('migrations not found');
|
|
32
|
+
}
|
|
25
33
|
return [
|
|
26
34
|
typeorm_1.TypeOrmModule.forRootAsync({
|
|
27
35
|
imports: [
|
|
@@ -54,14 +62,23 @@ class TypeOrmPlugin {
|
|
|
54
62
|
min: config.poolMin,
|
|
55
63
|
max: config.poolMax,
|
|
56
64
|
},
|
|
65
|
+
migrations,
|
|
66
|
+
migrationsTableName: 'typeorm_migrations',
|
|
57
67
|
...this.options.overrideConfig,
|
|
58
68
|
}),
|
|
59
69
|
}),
|
|
70
|
+
migration_checker_module_1.MigrationCheckerModule,
|
|
60
71
|
];
|
|
61
72
|
}
|
|
62
73
|
errorTransformers() {
|
|
63
74
|
return [new typeorm_errors_transformer_1.TypeormErrorTransformer()];
|
|
64
75
|
}
|
|
76
|
+
getMigrationsOption() {
|
|
77
|
+
const mainFileName = require.main?.filename;
|
|
78
|
+
return mainFileName
|
|
79
|
+
? [(0, node_path_1.resolve)((0, node_path_1.dirname)(mainFileName), 'typeorm/migrations/**')]
|
|
80
|
+
: [];
|
|
81
|
+
}
|
|
65
82
|
}
|
|
66
83
|
exports.TypeOrmPlugin = TypeOrmPlugin;
|
|
67
84
|
//# sourceMappingURL=typeorm.plugin.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typeorm.plugin.js","sourceRoot":"","sources":["../src/typeorm.plugin.ts"],"names":[],"mappings":";;;AAEA,6CAAgD;AAOhD,qCAA4D;
|
|
1
|
+
{"version":3,"file":"typeorm.plugin.js","sourceRoot":"","sources":["../src/typeorm.plugin.ts"],"names":[],"mappings":";;;AAEA,6CAAgD;AAOhD,qCAA4D;AAC5D,2CAA8C;AAC9C,yCAA6C;AAE7C,wDAAqD;AACrD,iFAA4E;AAC5E,2CAIqB;AACrB,6EAAuE;AAGvE,MAAa,aAAa;IAMK;IALrB,MAAM,CAAU,MAAM,GAAG,uBAAa,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;IAErE;;OAEG;IACH,YAA6B,OAA6B;QAA7B,YAAO,GAAP,OAAO,CAAsB;QACxD,yBAAW,CAAC,UAAU,EAAE,CAAC;IAC3B,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,EAAE,OAAO,EAAE,QAAQ,CAAC;IACjC,CAAC;IAED,aAAa;QACX,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO;QACL,MAAM,UAAU,GAAG,IAAI,CAAC,mBAAmB,EAAE,CAAC;QAC9C,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE;YAC3B,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,sBAAsB,CAAC,CAAC;SACnD;QAED,OAAO;YACL,uBAAa,CAAC,YAAY,CAAC;gBACzB,OAAO,EAAE;oBACP,2BAAoB,CAAC,UAAU,CAAC,+BAAmB,CAAC;oBACpD;wBACE,MAAM,EAAE,uBAAa;wBACrB,SAAS,EAAE,CAAC,kCAAsB,EAAE,gCAAoB,CAAC;wBACzD,OAAO,EAAE,CAAC,gCAAoB,CAAC;qBAChC;iBACF;gBACD,MAAM,EAAE,CAAC,+BAAmB,EAAE,gCAAoB,EAAE,eAAQ,CAAC;gBAC7D,UAAU,EAAE,CACV,MAA2B,EAC3B,MAA4B,EAC5B,OAAe,EACO,EAAE,CAAC,CAAC;oBAC1B;;;;uBAIG;oBACH,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI;oBACvB,GAAG,EAAE,MAAM,CAAC,GAAG,CAAC,QAAQ,EAAE;oBAC1B,GAAG,CAAC,MAAM,CAAC,MAAM,IAAI;wBACnB,MAAM,EAAE,MAAM,CAAC,MAAM;qBACtB,CAAC;oBACF,MAAM;oBACN,GAAG,CAAC,IAAI,CAAC,QAAQ,IAAI,EAAE,QAAQ,EAAE,IAAI,CAAC,QAAQ,EAAE,CAAC;oBACjD,gBAAgB,EAAE,CAAC,IAAI,CAAC,QAAQ;oBAChC,UAAU,EAAE,MAAM,CAAC,iBAAiB,CAAC,MAAM,EAAE;oBAC7C,aAAa,EAAE,MAAM,CAAC,iBAAiB;oBACvC,eAAe,EAAE,OAAO,GAAG,UAAU;oBACrC,KAAK,EAAE;wBACL,GAAG,EAAE,MAAM,CAAC,OAAO;wBACnB,GAAG,EAAE,MAAM,CAAC,OAAO;qBACpB;oBACD,UAAU;oBACV,mBAAmB,EAAE,oBAAoB;oBACzC,GAAI,IAAI,CAAC,OAAO,CAAC,cAAsB;iBACxC,CAAC;aACH,CAAC;YACF,iDAAsB;SACvB,CAAC;IACJ,CAAC;IAED,iBAAiB;QACf,OAAO,CAAC,IAAI,oDAAuB,EAAE,CAAC,CAAC;IACzC,CAAC;IAEO,mBAAmB;QACzB,MAAM,YAAY,GAAG,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC;QAE5C,OAAO,YAAY;YACjB,CAAC,CAAC,CAAC,IAAA,mBAAO,EAAC,IAAA,mBAAO,EAAC,YAAY,CAAC,EAAE,uBAAuB,CAAC,CAAC;YAC3D,CAAC,CAAC,EAAE,CAAC;IACT,CAAC;;AA/EH,sCAgFC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsdk/db.typeorm",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.1-next.0",
|
|
4
4
|
"description": "Typeorm module for platform",
|
|
5
5
|
"license": "Apache License 2.0",
|
|
6
6
|
"publishConfig": {
|
|
@@ -14,13 +14,16 @@
|
|
|
14
14
|
"prepublishOnly": "npm run build"
|
|
15
15
|
},
|
|
16
16
|
"peerDependencies": {
|
|
17
|
-
"@nestjs/common": "^
|
|
18
|
-
"@nestjs/
|
|
19
|
-
"@
|
|
20
|
-
"@rsdk/
|
|
21
|
-
"@rsdk/
|
|
22
|
-
"@rsdk/
|
|
17
|
+
"@nestjs/common": "^10.1.3",
|
|
18
|
+
"@nestjs/core": "*",
|
|
19
|
+
"@nestjs/typeorm": "^10.0.0",
|
|
20
|
+
"@rsdk/common": "^3.0.0",
|
|
21
|
+
"@rsdk/common.nestjs": "^3.0.0",
|
|
22
|
+
"@rsdk/core": "^3.0.1-next.0",
|
|
23
|
+
"@rsdk/db": "^3.0.1-next.0",
|
|
24
|
+
"@rsdk/logging": "^3.0.0",
|
|
25
|
+
"@rsdk/nest-tools": "^3.0.0",
|
|
23
26
|
"typeorm": "^0.3.12"
|
|
24
27
|
},
|
|
25
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "fc4e33568c2e79d62977fc6e17175e08893cb196"
|
|
26
29
|
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { NestFactory } from '@nestjs/core';
|
|
2
|
+
import { TypeOrmCoreModule } from '@nestjs/typeorm/dist/typeorm-core.module';
|
|
3
|
+
import type { NestModuleDefinition } from '@rsdk/common.nestjs';
|
|
4
|
+
import type { PlatformApp } from '@rsdk/core';
|
|
5
|
+
import { InternalException, PlatformContext } from '@rsdk/core';
|
|
6
|
+
import { NestDefinitionIterator, NestDefinitionType } from '@rsdk/nest-tools';
|
|
7
|
+
import { DataSource } from 'typeorm';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Нужен прежде всего для использования в связке с typeorm cli и указания результата вывода в файле
|
|
11
|
+
* Структура файлов
|
|
12
|
+
* ```
|
|
13
|
+
* src
|
|
14
|
+
* typeorm
|
|
15
|
+
* data-source.ts
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* // src/typeorm/data-source.ts
|
|
20
|
+
* import { app } from '../app'
|
|
21
|
+
* export default DataSourceExtractor.extract(app)
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export class DataSourceExtractor {
|
|
25
|
+
static async extract(app: PlatformApp): Promise<DataSource> {
|
|
26
|
+
const root = await app.context.getRoot();
|
|
27
|
+
const iterator = new NestDefinitionIterator(root);
|
|
28
|
+
|
|
29
|
+
const nestModule = await iterator.find<NestModuleDefinition>(
|
|
30
|
+
(definition) =>
|
|
31
|
+
definition === TypeOrmCoreModule ||
|
|
32
|
+
('module' in definition && definition.module === TypeOrmCoreModule),
|
|
33
|
+
NestDefinitionType.Module,
|
|
34
|
+
);
|
|
35
|
+
if (!nestModule) {
|
|
36
|
+
throw new InternalException('Datasource not found');
|
|
37
|
+
}
|
|
38
|
+
const applicationContext = await NestFactory.createApplicationContext(
|
|
39
|
+
new PlatformContext({
|
|
40
|
+
modules: [nestModule],
|
|
41
|
+
}).getRoot(),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
const dataSource = applicationContext.get(DataSource);
|
|
45
|
+
|
|
46
|
+
// typeorm cli always call `initialize()`, dont check isInitialized
|
|
47
|
+
dataSource.isInitialized && (await dataSource.destroy());
|
|
48
|
+
return dataSource;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { OnApplicationBootstrap } from '@nestjs/common';
|
|
2
|
+
import { Module } from '@nestjs/common';
|
|
3
|
+
import { InjectLogger } from '@rsdk/core';
|
|
4
|
+
import { ILogger } from '@rsdk/logging';
|
|
5
|
+
import { DataSource, MigrationExecutor } from 'typeorm';
|
|
6
|
+
|
|
7
|
+
@Module({})
|
|
8
|
+
export class MigrationCheckerModule implements OnApplicationBootstrap {
|
|
9
|
+
constructor(
|
|
10
|
+
private dataSource: DataSource,
|
|
11
|
+
@InjectLogger(MigrationCheckerModule) private logger: ILogger,
|
|
12
|
+
) {}
|
|
13
|
+
|
|
14
|
+
async onApplicationBootstrap(): Promise<void> {
|
|
15
|
+
const migrationExecutor = new MigrationExecutor(this.dataSource);
|
|
16
|
+
const migrations = await migrationExecutor.getPendingMigrations();
|
|
17
|
+
|
|
18
|
+
if (migrations.length > 0) {
|
|
19
|
+
this.logger.warn('Found pending migrations!', {
|
|
20
|
+
migrations,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/typeorm.plugin.ts
CHANGED
|
@@ -8,8 +8,11 @@ import type {
|
|
|
8
8
|
PlatformAppPlugin,
|
|
9
9
|
} from '@rsdk/core';
|
|
10
10
|
import { APP_NAME, PlatformConfigModule } from '@rsdk/core';
|
|
11
|
+
import { LoggerFactory } from '@rsdk/logging';
|
|
12
|
+
import { dirname, resolve } from 'node:path';
|
|
11
13
|
|
|
12
14
|
import { Initializer } from './internal/initializer';
|
|
15
|
+
import { MigrationCheckerModule } from './modules/migration-checker.module';
|
|
13
16
|
import {
|
|
14
17
|
TypeOrmHealthIndicator,
|
|
15
18
|
TypeormLoggerAdapter,
|
|
@@ -19,6 +22,8 @@ import { TypeormErrorTransformer } from './typeorm.errors-transformer';
|
|
|
19
22
|
import type { TypeOrmPluginOptions } from './types';
|
|
20
23
|
|
|
21
24
|
export class TypeOrmPlugin implements PlatformAppPlugin {
|
|
25
|
+
private static readonly logger = LoggerFactory.create(TypeOrmPlugin);
|
|
26
|
+
|
|
22
27
|
/**
|
|
23
28
|
* По умолчанию парсит тип драйвера из env %PACKAGE_NAME%_DB_URL
|
|
24
29
|
*/
|
|
@@ -35,6 +40,11 @@ export class TypeOrmPlugin implements PlatformAppPlugin {
|
|
|
35
40
|
}
|
|
36
41
|
|
|
37
42
|
modules(): (Constructor | DynamicModule)[] {
|
|
43
|
+
const migrations = this.getMigrationsOption();
|
|
44
|
+
if (migrations.length === 0) {
|
|
45
|
+
TypeOrmPlugin.logger.warn('migrations not found');
|
|
46
|
+
}
|
|
47
|
+
|
|
38
48
|
return [
|
|
39
49
|
TypeOrmModule.forRootAsync({
|
|
40
50
|
imports: [
|
|
@@ -71,13 +81,24 @@ export class TypeOrmPlugin implements PlatformAppPlugin {
|
|
|
71
81
|
min: config.poolMin,
|
|
72
82
|
max: config.poolMax,
|
|
73
83
|
},
|
|
84
|
+
migrations,
|
|
85
|
+
migrationsTableName: 'typeorm_migrations',
|
|
74
86
|
...(this.options.overrideConfig as any),
|
|
75
87
|
}),
|
|
76
88
|
}),
|
|
89
|
+
MigrationCheckerModule,
|
|
77
90
|
];
|
|
78
91
|
}
|
|
79
92
|
|
|
80
93
|
errorTransformers(): IErrorsTransformer[] {
|
|
81
94
|
return [new TypeormErrorTransformer()];
|
|
82
95
|
}
|
|
96
|
+
|
|
97
|
+
private getMigrationsOption(): string[] {
|
|
98
|
+
const mainFileName = require.main?.filename;
|
|
99
|
+
|
|
100
|
+
return mainFileName
|
|
101
|
+
? [resolve(dirname(mainFileName), 'typeorm/migrations/**')]
|
|
102
|
+
: [];
|
|
103
|
+
}
|
|
83
104
|
}
|