GithubHelp home page GithubHelp logo

Comments (11)

eugenio165 avatar eugenio165 commented on May 22, 2024 24

Hi xcymax, i've had this problem in the past. This happens because you're using absolute paths. You should use relative paths in all your imports, so that when you run your code in prod mode, which would normally be something like node dist/main it all works correctly.

Also, in my experience i could not get my typeorm configuration to work with both .ts and .js files. If the above solution doesn't work, i recommend you use your configuration with only .js files and use nest cli's start command to start your project. This way, nest builds and executes your application straight with javascript, using the dist folder.

from typeorm-seeding.

ponahoum avatar ponahoum commented on May 22, 2024 22

Would recommend to use npm install --save-dev tsconfig-paths and in the seeding commands add -r tsconfig-paths/register, ie:

In the package.json

"fixtures:run:seed": "ts-node -r tsconfig-paths/register ./node_modules/typeorm-seeding/dist/cli.js seed -n src/fixtures/ormFixturesConfig.ts"

My config:

export default {
  type: 'mysql',
  url: process.env.DB_URL,
  logging: true,
  entities: ['src/**/*.entity.ts'],
  seeds: ['src/fixtures/seeds/**/*.seed.ts'],
  factories: ['src/fixtures/factories/**/*.factory.ts'],
};

It solved my issue. I think using .js files is a terrible idea, as often the dist folder content is not immediatly updated and the files may accumulate in it if you update the file names, causing strange effects when launching the fixtures.

from typeorm-seeding.

eugenio165 avatar eugenio165 commented on May 22, 2024 11

@xcymax
The only thing i had to do was change the configs to route to the .js files in the dist folder, like i said earlier...
Good Luck!

{
  "type": "postgres",
  "host": "localhost",
  "port": 5432,
  "username": "docker",
  "password": "docker",
  "database": "postgres",
  "entities": ["dist/**/*.entity.js"],
  "migrations": ["dist/**/migration/*.js"],
  "cli": {
    "migrationsDir": "migration"
  },
  "synchronize": true,
  "factories": [ "dist/**/database/factories/**/*.js" ],
  "seeds": [ "dist/**/database/seeds/**/*.js" ]
}

from typeorm-seeding.

xcymax avatar xcymax commented on May 22, 2024 6

Oh now I understand, I was only focused on the entities setting in the config. It works now when factories and seeds are also pointing to the dist location and .js only.

For clarification:

  "factories": ["dist/**/database/factories/**/*.js"],
  "seeds": ["dist/**/database/seeds/**/*.js"],

Thanks for the help!

from typeorm-seeding.

eugenio165 avatar eugenio165 commented on May 22, 2024 3

@xcymax
Make sure your using nest cli to run your project, have all the configurations, even typeorm-seeding configs routed to the .js files in the dist folder and make sure your entity files are correct. If that doesnt work, send a few lines worth of code from your entity file, your factory, your seed and your config so we can see whats going on.

from typeorm-seeding.

xcymax avatar xcymax commented on May 22, 2024

Thank you @eugenio165 ! Changing all my imports to relative paths fixed that but I am now seeing a new error which I believe you were alluding to:

�  TypeORM Seeding v1.6.1
- Loading ormconfig
✔ ORM Config loaded
- Import Factories
✔ Factories are imported
- Importing Seeders
✔ Seeders are imported
- Connecting to the database
✔ Database connected
- Executing CreateUsers Seeder
❌  Could not save entity
EntityMetadataNotFound: No metadata for "User" was found.
    at new EntityMetadataNotFoundError (C:\Users\User\Development\example\example-api\src\error\EntityMetadataNotFoundError.ts:9:9)
    at Connection.getMetadata (C:\Users\User\Development\example\example-api\src\connection\Connection.ts:333:19)
    at C:\Users\User\Development\example\example-api\src\persistence\EntityPersistExecutor.ts:77:55
    at Array.forEach (<anonymous>)
    at EntityPersistExecutor.<anonymous> (C:\Users\User\Development\example\example-api\src\persistence\EntityPersistExecutor.ts:71:30)
    at step (C:\Users\User\Development\example\example-api\node_modules\typeorm\node_modules\tslib\tslib.js:141:27)
    at Object.next (C:\Users\User\Development\example\example-api\node_modules\typeorm\node_modules\tslib\tslib.js:122:57)
    at C:\Users\User\Development\example\example-api\node_modules\typeorm\node_modules\tslib\tslib.js:115:75
    at new Promise (<anonymous>)
    at Object.__awaiter (C:\Users\User\Development\example\example-api\node_modules\typeorm\node_modules\tslib\tslib.js:111:16) {
  name: 'EntityMetadataNotFound',
  message: 'No metadata for "User" was found.'
}
✖ Could not run the seed CreateUsers!
Error: Could not save entity
    at EntityFactory.<anonymous> (C:\Users\User\Development\example\example-api\node_modules\typeorm-seeding\src\entity-factory.ts:52:15)
    at step (C:\Users\User\Development\example\example-api\node_modules\tslib\tslib.js:140:27)
    at Object.throw (C:\Users\User\Development\example\example-api\node_modules\tslib\tslib.js:121:57)
    at rejected (C:\Users\User\Development\example\example-api\node_modules\tslib\tslib.js:112:69)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

I tried running with entities: [ 'dist/**/*.entity.js' ], in my configuration and nest cli as you mentioned but am still seeing the same error.

Just getting into Nest/TypeORM so baby steps :).

from typeorm-seeding.

rbutera avatar rbutera commented on May 22, 2024

Thank you @eugenio165 ! Changing all my imports to relative paths fixed that but I am now seeing a new error which I believe you were alluding to:

�  TypeORM Seeding v1.6.1
- Loading ormconfig
✔ ORM Config loaded
- Import Factories
✔ Factories are imported
- Importing Seeders
✔ Seeders are imported
- Connecting to the database
✔ Database connected
- Executing CreateUsers Seeder
❌  Could not save entity
EntityMetadataNotFound: No metadata for "User" was found.
    at new EntityMetadataNotFoundError (C:\Users\User\Development\example\example-api\src\error\EntityMetadataNotFoundError.ts:9:9)
    at Connection.getMetadata (C:\Users\User\Development\example\example-api\src\connection\Connection.ts:333:19)
    at C:\Users\User\Development\example\example-api\src\persistence\EntityPersistExecutor.ts:77:55
    at Array.forEach (<anonymous>)
    at EntityPersistExecutor.<anonymous> (C:\Users\User\Development\example\example-api\src\persistence\EntityPersistExecutor.ts:71:30)
    at step (C:\Users\User\Development\example\example-api\node_modules\typeorm\node_modules\tslib\tslib.js:141:27)
    at Object.next (C:\Users\User\Development\example\example-api\node_modules\typeorm\node_modules\tslib\tslib.js:122:57)
    at C:\Users\User\Development\example\example-api\node_modules\typeorm\node_modules\tslib\tslib.js:115:75
    at new Promise (<anonymous>)
    at Object.__awaiter (C:\Users\User\Development\example\example-api\node_modules\typeorm\node_modules\tslib\tslib.js:111:16) {
  name: 'EntityMetadataNotFound',
  message: 'No metadata for "User" was found.'
}
✖ Could not run the seed CreateUsers!
Error: Could not save entity
    at EntityFactory.<anonymous> (C:\Users\User\Development\example\example-api\node_modules\typeorm-seeding\src\entity-factory.ts:52:15)
    at step (C:\Users\User\Development\example\example-api\node_modules\tslib\tslib.js:140:27)
    at Object.throw (C:\Users\User\Development\example\example-api\node_modules\tslib\tslib.js:121:57)
    at rejected (C:\Users\User\Development\example\example-api\node_modules\tslib\tslib.js:112:69)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

I tried running with entities: [ 'dist/**/*.entity.js' ], in my configuration and nest cli as you mentioned but am still seeing the same error.

Just getting into Nest/TypeORM so baby steps :).

were you able to resolve this issue? trying to set up Nest / TypeORM integration tests and it seems like this topic is mostly unsupported in the nest ecosystem

from typeorm-seeding.

xcymax avatar xcymax commented on May 22, 2024

@eugenio165
I have created a simple sample repo here: https://github.com/xcymax/nestjs-typeorm-seeding-sample - appreciate any help.

from typeorm-seeding.

m3h0w avatar m3h0w commented on May 22, 2024

Hi xcymax, i've had this problem in the past. This happens because you're using absolute paths. You should use relative paths in all your imports, so that when you run your code in prod mode, which would normally be something like node dist/main it all works correctly.

That sounds like people who use absolute imports have never ran their code in production.

Is there any way to make typeorm-seeding work with absolute paths?

from typeorm-seeding.

svJariwala avatar svJariwala commented on May 22, 2024

hello guys

When I do seed:run in my NestJS app I get the following error:

image

This is my seed:config:

image

This is my database.config.ts file

image

from typeorm-seeding.

Lobaton2020 avatar Lobaton2020 commented on May 22, 2024

@xcymax The only thing i had to do was change the configs to route to the .js files in the dist folder, like i said earlier... Good Luck!

{
  "type": "postgres",
  "host": "localhost",
  "port": 5432,
  "username": "docker",
  "password": "docker",
  "database": "postgres",
  "entities": ["dist/**/*.entity.js"],
  "migrations": ["dist/**/migration/*.js"],
  "cli": {
    "migrationsDir": "migration"
  },
  "synchronize": true,
  "factories": [ "dist/**/database/factories/**/*.js" ],
  "seeds": [ "dist/**/database/seeds/**/*.js" ]
}

Appreciate your help, works for me

from typeorm-seeding.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.