Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Not relative imports between inherited entity not supported #1224

Open
AmirDavoodi opened this issue Dec 9, 2024 · 1 comment
Open

Comments

@AmirDavoodi
Copy link
Contributor

Versions

  • Node: v18.20.5
  • OS: Ubuntu 24.04 noble

Reproduction

I am having entities in different directory which are using inheritance.

  • MasterEntity: Located inside src/common/entity/master.entity.ts
  • BaseEntity: Located inside src/common/entity/base.entity.ts
  • UserEntity: Located inside src/user/entity/user.entity.ts
  • OtpEntity: Located inside src/sms/entity/otp.entity.ts

I am importing the parent entity using the normal importing using the vscode.
for example:

import { BaseEntity } from 'src/common/entity/base.entity';
export class UserEntity extends BaseEntity<UserEntity> {
    ...
}

This type of importing the parent entity gives me Not Found error when I run the seed:run

Error: Cannot find module 'src/common/entity/base.entity'
code: 'MODULE_NOT_FOUND'
Additional Details
root@fa9931f4e061:/app# npm run seed:run create-permissions.seeder.ts

> [email protected] seed:run
> ts-node ./node_modules/typeorm-extension/bin/cli.cjs seed:run -r ./ -d src/config/typeorm-datasource.ts -n create-permissions.seeder.ts

ℹ DataSource Directory: /app/src/config                                                                         11:29:51 PM
ℹ DataSource Name: typeorm-datasource.ts                                                                        11:29:51 PM
ℹ Seed Name: create-permissions.seeder.ts                                                                       11:29:54 PM
typeorm-extension seed:run

Populate the database with an initial data set or generated data by a factory.

Options:
  -h, --help               Show help                                   [boolean]
      --preserveFilePaths  This option indicates if file paths should be
                           preserved.                 [boolean] [default: false]
  -r, --root               Root directory of the project.      [default: "/app"]
      --tsconfig, --tc     Name (or relative path incl. name) of the tsconfig
                           file.                      [default: "tsconfig.json"]
  -d, --dataSource         Name (or relative path incl. name) of the data-source
                           file.                        [default: "data-source"]
  -n, --name               Name (or relative path incl. name) of the seeder.
  -v, --version            Show version number                         [boolean]

Error: Cannot find module 'src/common/entity/base.entity'
Require stack:
- /app/src/sms/entity/otp.entity.ts
- /app/node_modules/typeorm/util/ImportUtils.js
- /app/node_modules/typeorm/connection/ConnectionOptionsReader.js
- /app/node_modules/typeorm/globals.js
- /app/node_modules/typeorm/index.js
- /app/node_modules/typeorm-extension/dist/index.cjs
- /app/node_modules/typeorm-extension/bin/cli.cjs
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1140:15)
    at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/app/node_modules/@cspotcode/source-map-support/source-map-support.js:811:30)
    at Function.Module._load (node:internal/modules/cjs/loader:981:27)
    at Module.require (node:internal/modules/cjs/loader:1231:19)
    at require (node:internal/modules/helpers:177:18)
    at Object.<anonymous> (/app/src/sms/entity/otp.entity.ts:1:1)
    at Module._compile (node:internal/modules/cjs/loader:1364:14)
    at Module.m._compile (/app/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1422:10)
    at Object.require.extensions.<computed> [as .ts] (/app/node_modules/ts-node/src/index.ts:1621:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/app/src/sms/entity/otp.entity.ts',
    '/app/node_modules/typeorm/util/ImportUtils.js',
    '/app/node_modules/typeorm/connection/ConnectionOptionsReader.js',
    '/app/node_modules/typeorm/globals.js',
    '/app/node_modules/typeorm/index.js',
    '/app/node_modules/typeorm-extension/dist/index.cjs',
    '/app/node_modules/typeorm-extension/bin/cli.cjs'
  ]
}

The only working solution that I found is to replace all the import path to a relative path which does not being supported by the vscode. By the way, I am coding using NestJS framework.

Thank you in advance for any help or suggestions.

Steps to reproduce

Just created an import without a relative path to your entities file and then try to run the seed:run command. You see the Not Found error.

What is Expected?

Supporting the not relative path by providing the project root path. I already provided the -r options but not working.
`"seed:run": "ts-node ./node_modules/typeorm-extension/bin/cli.cjs seed:run -r ./ -d src/config/typeorm-datasource.ts -n create-permissions.seeder.ts"

What is actually happening?

Not being able to report the import without relative path.
For example, the path should be change as bellow

  • import { BaseEntity } from 'src/common/entity/base.entity';
  • import { BaseEntity } from '../../common/entity/base.entity';
@AmirDavoodi
Copy link
Contributor Author

By the way, here is my datasource options for my postgres db.

import { DataSource, DataSourceOptions } from 'typeorm';
import { ConfigService } from './config.service';
import { SeederOptions } from 'typeorm-extension';

const configService = new ConfigService();
const options: DataSourceOptions & SeederOptions = {
  ...configService.database.postgres,
  subscribers: ['src/**/subscriber/*.subscriber.{ts,js}'],
  entities: ['src/**/entity/*.entity.{ts,js}'],
  migrations: ['src/database/migration/*.{ts,js}'],
  seeds: ['src/database/seeds/**/*.seeder.{.ts,.js}'],
  factories: ['src/database/factories/**/*.factory.{.ts,.js}'],
  synchronize: false,
};

const TypeOrmDataSource = new DataSource(options);
export default TypeOrmDataSource;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant