typeorm / typeorm

ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.
http://typeorm.io
MIT License
33.54k stars 6.22k forks source link

Unable to load entities after update to version 0.3.12 #9766

Closed heldercunha closed 1 year ago

heldercunha commented 1 year ago

Issue description

After update the typeorm 0.3.11 to version 0.3.12, entities classes cannot be found by path

Expected Behavior

Found all entities classes to load.

All classes found using provided glob pattern "D:\projects\app\src\entities\**\*.entity{.ts,.js}"

Actual Behavior

No entities classes found

No classes were found using the provided glob pattern: "D:\projects\app\src\entities\**\*.entity{.ts,.js}"

Steps to reproduce

Create a new DataSource, configure the DataSource connection, and the entities path.

    const postgresqlDataSource: DataSource = new DataSource({
        type: 'postgres',
        applicationName: 'app,
        host: postgreSQL.host,
        port: postgreSQL.port,
        database: postgreSQL.database,
        username: postgreSQL.username,
        password: postgreSQL.password,
        useUTC: true,
        poolSize: 10,
        connectTimeoutMS: 0,
        dropSchema: false,
        synchronize: false,
        migrationsRun: false,
        uuidExtension: 'uuid-ossp',
        logging: true,
        entities: [join(__dirname, './entities/**/*.entity{.ts,.js}')],
        subscribers: [],
        migrations: [],
    });

My Environment

| Operating System | Windows 10 | | Node.js version | 18.14.0 | | Typescript version | 4.9.5 | | TypeORM version | 0.3.12 |

Additional Context

No response

Relevant Database Driver(s)

Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, but I don't know how to start. I would need guidance.

truemogician commented 1 year ago

I've encountered this bug as well. I debugged into TypeORM and figured out that it was caused by the update of package glob, which is responsible for resolving the glob pattern used in the entity configuration.
As mentioned in this issue, the update breaks its compatibility on Windows, interpreting "\" as an escape character instead of a valid path separator.
This is the origin of this bug, but another point also contributes to this problem: TypeORM will normalize the path before passing it to glob (util/DirectoryExportedClassesLoader), which will replace all forward slashes with backslashes (because backslash is the primary path separator on Windows). That's why this bug will still occur even if you use forward slashes in your config file.

truemogician commented 1 year ago

Some temporary solutions before a new patch version release:

pleerock commented 1 year ago

Thanks @truemogician for investigation and detailed explanation.

MaximBaltak commented 1 year ago

проблема не только с сущностями но и с миграциями естественно, тоже не ищет

tada5hi commented 1 year ago

This also causes problems with loading migrations. About a soon release of the fix I would be very happy @pleerock :v: :relaxed:

potier97 commented 1 year ago

Esto también se convierte en un problema si contamos con distintos ambientes (develop, stage, preprod, production), nos obliga a tener un archivo de migración por cada ambiente, si seria bueno una solución para esto

C0kkie commented 1 year ago

Is that feature not anyway deprecated or did i read wrong in the past changelogs?

amjmhs commented 1 year ago

0.3.13 fixes the issue for me and all migrations are found again. Thank you 🙏

Kolobamanacas commented 7 months ago

Is that feature not anyway deprecated or did i read wrong in the past changelogs?

Yes, it is deprecated and not recommended to be used anymore. From the changelog:

DEPRECATIONS

Which means, as I understand it, that from version 0.3.0 we need to list real entities in the array of entities and not a list of string paths to files:

import { MyEntity1 } from '@/path/to/MyEntity1'
import { MyEntity2 } from '@/path/to/MyEntity2'

new DataSource({
  entities: [MyEntity1, MyEntity2]
})
Mnigos commented 3 months ago

still occurs on 0.3.19

eder-abbvie commented 2 months ago

Same here on 0.3.20

Kolobamanacas commented 2 months ago

@eder-abbvie, @Mnigos

And it will be for all the eternity. As mentioned above, the feature of providing entities be file paths is deprecated. All entities are now needed to be imported and added to an array explicitly.

Mnigos commented 2 months ago

@Kolobamanacas

So now the only way to load entities is to manually import them and insert into array like this?

import { User } from '@modules/users'
import { Artist } from '@modules/artists'

export const typeOrmConfig: TypeOrmModuleOptions = {
  entities: [User, Artist],
}

If yes, please update the documentation, because this is quite confusing.

And how about migrations? Do we need to import them manually too?

Mnigos commented 2 months ago

Documentation fix - #10784

Kolobamanacas commented 2 months ago

So now the only way to load entities is to manually import them and insert into array like this? And how about migrations? Do we need to import them manually too?

Yes and yes. :)

Btw, I'm not maintainer yet, I've just faced the same issue during the update.

flyzss commented 2 weeks ago

Do not use the letter i as the beginning of the file name.