typeorm: Typeorm CLI not works perfectly after 0.3.0
Issue Description
After version 0.3.0 cli does not work perfectly. when I try to create an entity or migration using cli is create entity or migration in the root directory. However, in the old version, it works perfectly. It shows this error with the new version 0.3.0
$ npm run typeorm -- entity:create -n user
> nest-typeorm@0.0.1 typeorm D:\Learning\nodejs\nest-tuotrial\nest-typeorm
> ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/ormconfig.ts "entity:create" "-n" "user"
bin.js entity:create <path>
Generates a new entity.
Options:
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
Not enough non-option arguments: got 0, need at least 1
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! nest-typeorm@0.0.1 typeorm: `ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/ormconfig.ts "entity:create" "-n" "user"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the nest-typeorm@0.0.1 typeorm script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\hamja\AppData\Roaming\npm-cache\_logs\2022-03-27T06_06_50_058Z-debug.log
Expected Behavior
while when I use 0.2.17 version. it works with same command.
$ npm run typeorm -- entity:create -n user
> nest-typeorm@0.0.1 typeorm D:\Learning\nodejs\nest-tuotrial\nest-typeorm
> ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/ormconfig.ts "entity:create" "-n" "user"
Entity D:\Learning\nodejs\nest-tuotrial\nest-typeorm/src/entities/user.ts has been created successfully.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 14
- Comments: 42 (5 by maintainers)
Okay… so it took me SOME HOURS but it’s finally working right now. My scripts in package.json are like that:
My ormconfig.ts (attention to the .ts) looks like that:
And I’m running the command to generate migrations like the following:
Now, some things I struggled and I would like to share with you guys. First of all, it seems like the support for ormconfig.js or ormconfig.json is no longer working as it were. I’m not completely sure on that, but I gave a few shots and none of them worked. But, what seems to be working properly and what seems to be the new standard is the usage of TypeORM DataSource to configure the connection. As it already was, the migrations property in the configurations of TypeORM (being set here through the ormconfig.ts) state the folder where the CLI will read the migrations, but not the folder where it will store them once generated. I tried setting the property cli.migrationsDir but it was being ignored. I even saw some threads stating this particular issue. It seems that, when generating a new migration, we need to pass not only its name but also its path. That’s why I’ve set in my migration:generate script in package.json a template-like solution. I’m leaving hardcoded the path and just using the parameter name passed when running the script.
I’m honestly disappointed by TypeORM documentation with those changes and with the overall support to community. I hope to see some improvements soon.
Syntax has changed. You need to execute command in a following manner `typeorm entity:create path/to/entity". CLI gives you a hint about it:
We didn’t update the documentation yet. Feel free to contribute.
The documentation is a joke. I wish I tried different orm
Guys, for real now. Documentation should be taken seriously. I think this is the third or fourth time i’m struggling with TypeORM Cli. The documentations should be updated ASAP and provide more examples
This issue should have more visibility, it could save hours!!
I found some information here https://github.com/typeorm/typeorm/releases/tag/0.3.0 My version 0.3.5
DEPRECATIONS
entities,migrations,subscribersoptions insideDataSourceOptionsaccepting string directories support is deprecated. You’ll be only able to pass entity references in the future versions.Like this:
Scripts:
Usage:
It works.
no, you need to provide full path now.
I had the same issue with migrations, and was only able to resolve it thanks to you guys.
Old:
New:
Hey guys…faced the same issue and hated it very much since now anyone can specify a random path. I fixed this like detailed below:
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js","migration:create": "npm run typeorm migration:create ./migrations/$npm_config_name",Usage:
npm run migration:create --name=testThis will generate the migration under the migrations folder
‘cli’ does not exist in type ‘PostgresConnectionOptions’
You guys, can use this. https://github.com/misostack/nestjs/blob/master/src/config/environment.ts
./src/database/datasource.ts
@IlayMorozoff I managed to get it working on windows using the following
"migration:create": "typeorm-ts-node-esm migration:create ./src/database/migrations/%npm_config_name%", "migration:generate": "npm run typeorm migration:generate ./src/database/migrations/%npm_config_name%",$ typeorm entity:create --help typeorm entity:create <path> Generates a new entity.
I just started using TypeORM this week. Although it’s free work and we should be grateful for it, it’s so confusing to read documentation that doesn’t work at all! You can pin a version to your documentation so new users will at least have a clue.
We definitely need some clarity on why glob support is being removed. It is extremely useful and prevents a ton of extra development busy work. I am NOT looking forward to maintaining those arrays manually, especially when migrations are typically generated for us using the CLI.
Documentation is not updated yet … cli does not exist in
DataSourceOptions?? path for migration??I ended up rolling my own solutions to this ever since migrating to 0.3+. I’ll try to share it as best I can with this chat in hopes to help others who don’t want to maintain those arrays manually while also being able to generate migrations with the CLI…
The Issue
My understanding with the new change is that we are not able to pass in a glob path anymore into the
DataSource()initialization. This requires us to manually maintain both theentitiesarray as well as themigrationsarray in the config file, which I felt was a huge regression in terms of developer experience, since we now have to generate files using the CLI, and then manually remember to update that config file with new entries anytime we want to generate a migration.Instead, I tried to incorporate a script into my CLI workflow that handles this for me.
My Solution
What I decided to do is “intercept” the CLI
migrate:generatecommand with my own custom command that I wrote into amigrate.tsfile and keep in thesrc/directory of my project. In this way, I can change mypackage.jsoncommands to call that custom command in place of the vanillatypeormmigration generation command. I know this sounds brittle, but I think this is actually a pretty stable solution, as it doesn’t require you to make any changes to the typeorm source. Also, just a note, I am usingdotenvto pass in my configuration, but this solution should work without it. Here is a quick screenshot of my directory structure for clarity (I cropped out my other src folders):1. Add a
migrate.tsfileHere is the
migrate.tsscript you will need to put in your/srcdirectory.You should not need to modify this unless you are using a non-default (/src/migration) migration directoryView File
migrate.ts2. Modify your
package.jsonNow that we have our script, we want to us this in our
package.jsonfile any time we use the typeorm CLI commands. This is how your typeorm commands should look like. Notice how themigrate:generatecommand now routes through themigrate.tsfile:package.json3. Modify your
configuration.tsfileLast step is to add some fields to our
configuration.tsfile so themigrate.tsscript knows where to find your entities and migration folder. Here is what mine looks like. You’ll notice I have the migration, subscriber, and entity folder duplicated in this config. Because I am using typescipt, my ACTUAL files would end up in the/distfolder, but the files I used to generate migrations were my original*.tssource files. If anyone can think of a better way to do this let me know, but since I do not plan on ever changing those directories from the default, I felt it wasn’t that big of a deal to have both entries in the config file. I don’t see me ever having to modify those in the future.View File
configuration.ts4. Use the CLI
Now whenever you want to generate migrations, you can simply use the npm script like you always would have done anyway, but the
migrate.tsfile handles filling in the entities for you. Here is how you would generate a migration:You can still use all the other migrations commands as you normally would as well!
I know this was a long winded explanation, but let me know if anyone has any questions. I am happy to help. Hopefully we can either get some answers as to why glob support was removed, or better, we can get the feature back. I was pretty discouraged from using
0.3after reading that change in the changelog. If you are inspired by my solution, and have come up with something better, let me know as well!NOTICE:
typeorm-ts-node-esmandtypeorm-ts-node-commonjscannot registertsconfig-paths/register. so nestjs project may not work.still using
node -r tsconfig-paths/register -r ts-node/register ./node_modules/typeorm/cli.jsSo maybe I’m getting it wrongs, but its looks like it means we will have to add new entities and migrations to the
DataSourcemanually everytime we create ones?Seems like a regression to me but maybe I’m not fully understanding the vision.
The thing that is destroying my mind is that I cannot understand the reason to provide the full path when trying to create/generate a migration.
Why cannot something like:
instead of the current:
With maybe
process.cwd()as default value ifpathis not provided.Hello friends, especially NestJS friends.
Like many of you, we recently upgraded to TypeORM 0.3.x and ran into issues with the way that the docs wants you to use the CLI. We decided to use Nest’s Standalone Application feature to grab the DataSourceOptions instead of having to reimplement how our app is loading.
Here’s the list of things that we found troublesome:
DataSource#initializeeven if the DataSource is already initialized. This is a problem if you use NestJS, which automatically initializes the DataSource.-nflag.migrationsis a good one. Having to register each migration you generate as a manual step is not just tedious, it goes against tons of other ORM patterns.Anyway, here’s the method we’ve used:
in ormconfig.ts
in package.json
If I were to suggest anything, perhaps a
if (dataSource.isInitialized)check could be added to the commands in here. That would prevent having to destroy the datasource in the ormconfig being loaded from Nest.I would also suggest separating the name of the migration and the folder to store it in like it used to be. Especially because it’s not particularly intuitive that
typeorm migration:create src/migrations/Testgenerates a file insrc/migrations/<timestamp>-Test.ts$npm_config_namedoes not work with yarn. npm or pnpm should be fine.And how to make the file name change when it is created? Otherwise, this does not work in this configuration and the name remains the current $npm_config_name. I would be grateful if you clarify