mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
feat: log files loaded from glob patterns (#4346)
This new feature logs the files that are loaded using the glob patterns to aid in debugging. Closes: #4162
This commit is contained in:
parent
c8dbf099ba
commit
e12479ed43
16
docs/troubleshooting.md
Normal file
16
docs/troubleshooting.md
Normal file
@ -0,0 +1,16 @@
|
||||
# Troubleshooting
|
||||
|
||||
* [Glob patterns](#glob-patterns)
|
||||
|
||||
## Glob Patterns
|
||||
|
||||
Glob patterns are used in the TypeOrm to specify the locations of entities, migrations, subscriber and other information. Errors in the patterns can lead to the common `RepositoryNotFoundError` and familiar errors. In order to check if any files were loaded by TypeOrm using the glob patterns, all you need to do is set the logging level to `info` such as explained in the [Logging](./logging.md) section of the documentation. This will allow you to have logs in the console that may look like this:
|
||||
|
||||
```bash
|
||||
# in case of an error
|
||||
INFO: No classes were found using the provided glob pattern: "dist/**/*.entity{.ts}"
|
||||
```
|
||||
```bash
|
||||
# when files are found
|
||||
INFO: All classes found using provided glob pattern "dist/**/*.entity{.js,.ts}" : "dist/app/user/user.entity.js | dist/app/common/common.entity.js"
|
||||
```
|
||||
16
docs/zh_CN/troubleshooting.md
Normal file
16
docs/zh_CN/troubleshooting.md
Normal file
@ -0,0 +1,16 @@
|
||||
# 故障排除
|
||||
|
||||
* [全球模式](#全球模式)
|
||||
|
||||
## 全球模式
|
||||
|
||||
在类型中使用全局模式来指定实体,迁移,订户和其他信息的位置。模式中的错误可能导致常见的`RepositoryNotFoundError`和熟悉的错误。为了检查TypeOrm是否使用glob模式加载了任何文件,您需要做的就是将日志级别设置为`info`,如文档的[Logging](./logging.md)部分所述。 这将允许您拥有可能如下所示的日志:
|
||||
|
||||
```bash
|
||||
# 如果出错
|
||||
INFO: No classes were found using the provided glob pattern: "dist/**/*.entity{.ts}"
|
||||
```
|
||||
```bash
|
||||
# 何时找到文件
|
||||
INFO: All classes found using provided glob pattern "dist/**/*.entity{.js,.ts}" : "dist/app/user/user.entity.js | dist/app/common/common.entity.js"
|
||||
```
|
||||
@ -31,7 +31,7 @@ export class ConnectionMetadataBuilder {
|
||||
*/
|
||||
buildMigrations(migrations: (Function|string)[]): MigrationInterface[] {
|
||||
const [migrationClasses, migrationDirectories] = OrmUtils.splitClassesAndStrings(migrations);
|
||||
const allMigrationClasses = [...migrationClasses, ...importClassesFromDirectories(migrationDirectories)];
|
||||
const allMigrationClasses = [...migrationClasses, ...importClassesFromDirectories(this.connection.logger, migrationDirectories)];
|
||||
return allMigrationClasses.map(migrationClass => getFromContainer<MigrationInterface>(migrationClass));
|
||||
}
|
||||
|
||||
@ -40,7 +40,7 @@ export class ConnectionMetadataBuilder {
|
||||
*/
|
||||
buildSubscribers(subscribers: (Function|string)[]): EntitySubscriberInterface<any>[] {
|
||||
const [subscriberClasses, subscriberDirectories] = OrmUtils.splitClassesAndStrings(subscribers || []);
|
||||
const allSubscriberClasses = [...subscriberClasses, ...importClassesFromDirectories(subscriberDirectories)];
|
||||
const allSubscriberClasses = [...subscriberClasses, ...importClassesFromDirectories(this.connection.logger, subscriberDirectories)];
|
||||
return getMetadataArgsStorage()
|
||||
.filterSubscribers(allSubscriberClasses)
|
||||
.map(metadata => getFromContainer<EntitySubscriberInterface<any>>(metadata.target));
|
||||
@ -56,7 +56,7 @@ export class ConnectionMetadataBuilder {
|
||||
const entityClasses: Function[] = entityClassesOrSchemas.filter(entityClass => (entityClass instanceof EntitySchema) === false) as any;
|
||||
const entitySchemas: EntitySchema<any>[] = entityClassesOrSchemas.filter(entityClass => entityClass instanceof EntitySchema) as any;
|
||||
|
||||
const allEntityClasses = [...entityClasses, ...importClassesFromDirectories(entityDirectories)];
|
||||
const allEntityClasses = [...entityClasses, ...importClassesFromDirectories(this.connection.logger, entityDirectories)];
|
||||
allEntityClasses.forEach(entityClass => { // if we have entity schemas loaded from directories
|
||||
if (entityClass instanceof EntitySchema) {
|
||||
entitySchemas.push(entityClass);
|
||||
|
||||
@ -74,11 +74,11 @@ export class AdvancedConsoleLogger implements Logger {
|
||||
switch (level) {
|
||||
case "log":
|
||||
if (this.options === "all" || (this.options instanceof Array && this.options.indexOf("log") !== -1))
|
||||
console.log(message);
|
||||
PlatformTools.log(message);
|
||||
break;
|
||||
case "info":
|
||||
if (this.options === "all" || (this.options instanceof Array && this.options.indexOf("info") !== -1))
|
||||
console.info(message);
|
||||
PlatformTools.logInfo("INFO:", message);
|
||||
break;
|
||||
case "warn":
|
||||
if (this.options === "all" || (this.options instanceof Array && this.options.indexOf("warn") !== -1))
|
||||
|
||||
@ -1,11 +1,14 @@
|
||||
import {PlatformTools} from "../platform/PlatformTools";
|
||||
import {EntitySchema} from "../index";
|
||||
|
||||
import {Logger} from "../logger/Logger";
|
||||
/**
|
||||
* Loads all exported classes from the given directory.
|
||||
*/
|
||||
export function importClassesFromDirectories(directories: string[], formats = [".js", ".ts"]): Function[] {
|
||||
export function importClassesFromDirectories(logger: Logger, directories: string[], formats = [".js", ".ts"]): Function[] {
|
||||
|
||||
const logLevel = "info";
|
||||
const classesNotFoundMessage = "No classes were found using the provided glob pattern: ";
|
||||
const classesFoundMessage = "All classes found using provided glob pattern";
|
||||
function loadFileClasses(exported: any, allLoaded: Function[]) {
|
||||
if (typeof exported === "function" || exported instanceof EntitySchema) {
|
||||
allLoaded.push(exported);
|
||||
@ -24,6 +27,11 @@ export function importClassesFromDirectories(directories: string[], formats = ["
|
||||
return allDirs.concat(PlatformTools.load("glob").sync(PlatformTools.pathNormalize(dir)));
|
||||
}, [] as string[]);
|
||||
|
||||
if (directories.length > 0 && allFiles.length === 0) {
|
||||
logger.log(logLevel, `${classesNotFoundMessage} "${directories}"`);
|
||||
} else if (allFiles.length > 0) {
|
||||
logger.log(logLevel, `${classesFoundMessage} "${directories}" : "${allFiles}"`);
|
||||
}
|
||||
const dirs = allFiles
|
||||
.filter(file => {
|
||||
const dtsExtension = file.substring(file.length - 5, file.length);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user