Comments (11)
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.
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.
@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.
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.
@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.
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.
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.
@eugenio165
I have created a simple sample repo here: https://github.com/xcymax/nestjs-typeorm-seeding-sample - appreciate any help.
from typeorm-seeding.
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.
hello guys
When I do seed:run in my NestJS app I get the following error:
This is my seed:config:
This is my database.config.ts file
from typeorm-seeding.
@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)
- update major faker version please HOT 1
- Faker npm package not supported HOT 1
- Faker got abandoned and NPM audit complains about it HOT 1
- TypeError: Cannot read properties of undefined(reading 'number') HOT 3
- Support [email protected] HOT 2
- how to truncate tables? HOT 1
- There is a typo for the shebang in the dist/cli.js HOT 1
- Example for how to seed ManyToMany relation HOT 1
- [Request] Support for typeorm 0.3 and nestjs/typeorm v8.1.0 HOT 1
- Database connection failed! Check your typeORM config file. MissingDriverError: Wrong driver: "undefined" given. HOT 4
- this.subQuery is not a function HOT 1
- [Request] Support for typeorm 0.3 and nestjs/typeorm v8.1.0 HOT 20
- Replace `faker` with `@faker-js/faker` HOT 2
- Hello. I just have a question HOT 1
- TypeError: Cannot read properties of undefined (reading 'name')
- prevent seed:config to log any output
- How to seed data with monorepo workspace in nestjs HOT 4
- unable to resolve dependency tree HOT 8
- Publish 1.6.2 HOT 2
- Doesn't finish command
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typeorm-seeding.