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
> [email protected] 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! [email protected] 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 [email protected] 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
> [email protected] 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.
typeorm 0.3.4 version too.
so, i decide downgrade package version from 0.3.4 to 0.2.45.
it works.
CLI works fine till version - 0.2.45. After the version bump, above mentioned message is displayed. I had upgraded my entire project to 0.3 seeing the deprecated changes for the connection configurations (deprecated createconnection), but now migration is blocked. Does anyone know what the new command should be like for version 0.3+ or is this a bug?
Looping in (@pleerock)
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:
bin.js entity:create <path>
We didn't update the documentation yet. Feel free to contribute.
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:
bin.js entity:create <path>We didn't update the documentation yet. Feel free to contribute.
Thanks now it works. it there any way to create entity in specific folder without giving full path.
it there any way to create entity in specific folder without giving full path.
no, you need to provide full path now.
'cli' does not exist in type 'PostgresConnectionOptions'
Documentation is not updated yet .. cli does not exist in DataSourceOptions?? path for migration??
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:
bin.js entity:create <path>We didn't update the documentation yet. Feel free to contribute.
$ 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.
This issue should have more visibility, it could save hours!!
I had the same issue with migrations, and was only able to resolve it thanks to you guys.
Old:
// Generate
ts-node ./node_modules/typeorm/cli migration:generate --config build/src/configurations/ormconfig --name update
// Run
typeorm migration:run --config build/src/configurations/ormconfig
New:
// Generate
ts-node ./node_modules/typeorm/cli migration:generate ./src/orm/migration/update -d ./src/orm/datasource.ts
// Run (I had to switch to ts-node since it now runs migrations from .ts instead of .js)
ts-node ./node_modules/typeorm/cli migration:run -d ./src/orm/datasource.ts
// Revert
ts-node ./node_modules/typeorm/cli migration:revert -d ./src/orm/datasource.ts
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=test
This will generate the migration under the migrations folder
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
Okay... so it took me SOME HOURS but it's finally working right now. My scripts in package.json are like that:
...
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js",
"migration:generate": "./node_modules/.bin/ts-node ./node_modules/.bin/typeorm migration:generate -d ormconfig.ts ./src/api/Environment/database/migrations/$npm_config_name",
"migration:up": "./node_modules/.bin/ts-node ./node_modules/.bin/typeorm migration:run -d ormconfig.ts",
"migration:down": "./node_modules/.bin/ts-node ./node_modules/.bin/typeorm migration:revert -d ormconfig.ts"
...
My ormconfig.ts (attention to the .ts) looks like that:
import { DataSource, DataSourceOptions } from 'typeorm';
export const connectionSource = new DataSource({
migrationsTableName: 'migrations',
type: 'mysql',
host: 'localhost',
port: 3306,
username: '-------',
password: '-------',
database: '------',
logging: true,
synchronize: false,
entities: ['src/api/3 - Domain/entities/*.entity.ts'],
migrations: ['src/api/Environment/database/migrations/*.ts'],
} as DataSourceOptions);
And I'm running the command to generate migrations like the following:
ᛟ npm run migration:generate --name="teste"
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.
Thanks this is the solution for the issue of the migrations
I'm also having some problems here, with NestJS, with how the middleware handle the DataSource and npm commands.
I can understand the change on how the DataSource is handled right now (but I don't like). What I can't understand is the change of how the CLI works right now. As we can see, there are a lot of problems, not only with documentation, also with templating on the script just to create a single migration.
Is there any way to help with this? It supposes that I can't upgrade the package in my projects, too many problems without any sense.
In this moment i am work with ts and typeorm, and my database config is
export const dbConnection = new DataSource({ name: 'default', type: 'postgres', host: DB_HOST, port: parseInt(DB_PORT), username: DB_USER, password: DB_PASSWORD, database: DB_DATABASE, logging: false, entities: [join(__dirname, '../**/*.entity{.ts,.js}')], });
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:
ts-node ./node_modules/typeorm/cli migration:generate --path src/orm/migrations -d ./src/orm/datasource.ts <migrationName>
instead of the current:
ts-node ./node_modules/typeorm/cli migration:generate src/orm/migrations/<migrationName> -d ./src/orm/datasource.ts <migrationName>
With maybe process.cwd() as default value if path is not provided.
Got stuck also but found a solution, try this:
I used
npx typeorm entity: create -n User
This will generate the entity in the route folder of your project. To avoid this
Do:
npx typeorm entity:create ./src/entities/User
The User in the path is the name of the entity. This should create an entity at the given path.You can repeat the above process for migrations.
In my case, I had typeOrm installed both globally and locally.
For more information checkout
I found some information here https://github.com/typeorm/typeorm/releases/tag/0.3.0 My version 0.3.5
DEPRECATIONS
entities, migrations, subscribers options inside DataSourceOptions accepting string directories support is deprecated.
You'll be only able to pass entity references in the future versions.
Like this:
// ormconfig.ts
const connectionSource = new DataSource({
...
entities: [ExamplesEntity],
migrations: [ExamleMigration1656076568327],
...
})
export default connectionSource;
Scripts:
{
"scripts": {
"typeorm": "typeorm-ts-node-esm -d ./src/db/ormconfig.ts",
"migration:create": "typeorm-ts-node-esm migration:create ./src/db/migrations/$npm_config_name",
"migration:generate": "npm run typeorm migration:generate ./src/db/migrations/$npm_config_name",
"migration:show": "npm run typeorm migration:show",
"migration:run": "npm run typeorm migration:run",
"migration:revert": "npm run typeorm migration:revert"
}
}
Usage:
npm run migration:generate --name=example_migration
npm run migration:create --name=example_migration
npm run migration:run
It works.
The documentation is a joke. I wish I tried different orm
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:
// ormconfig.ts const connectionSource = new DataSource({ ... entities: [ExamplesEntity], migrations: [ExamleMigration1656076568327], ... }) export default connectionSource;
So maybe I'm getting it wrongs, but its looks like it means we will have to add new entities and migrations to the DataSource manually everytime we create ones?
Seems like a regression to me but maybe I'm not fully understanding the vision.
You guys, can use this. https://github.com/misostack/nestjs/blob/master/src/config/environment.ts
import 'dotenv/config';
import { DataSource, DataSourceOptions } from 'typeorm';
export const DATABASE_CONFIG = {
MAIN_DB_TYPE: 'mysql',
MAIN_DB_URL: process.env.MYSQL_DB_URL || 'nestjs',
MAIN_DB_CHARSET: 'utf8mb4_unicode_ci',
MAIN_DB_TABLE_PREFIX: 'nestjs',
};
export class Environment {
static getMainDatabaseConfiguration(dirname): any {
return {
type: DATABASE_CONFIG.MAIN_DB_TYPE,
url: DATABASE_CONFIG.MAIN_DB_URL,
entities: [dirname + '/**/entities/*.entity{.js,.ts}'],
// MYSQL will store Timestamp in GMT ( UTC = 0)
timezone: 'Z', // must have this, if the response date will be marked as current timezone
charset: DATABASE_CONFIG.MAIN_DB_CHARSET,
// must not be synchronize automaticall, use data migration instea
synchronize: false,
// migrations
migrations: [dirname + '/database/migrations/*.ts'],
// cli
cli: {
migrationsDir: dirname + '/database/migrations',
},
};
}
static getMainDatasourceConfiguration(dirname): any {
return new DataSource({
...Environment.getMainDatabaseConfiguration(dirname),
} as DataSourceOptions);
}
}
./src/database/datasource.ts
import { Environment } from '../config/environment';
export default Environment.getMainDatasourceConfiguration('./src');
"typeorm2": "node --require tsconfig-paths/register -r ts-node/register ./node_modules/typeorm/cli.js --config ./src/database/ormconfig.ts",
"typeorm": "node --require tsconfig-paths/register -r ts-node/register ./node_modules/typeorm/cli.js -d ./src/database/datasource.ts"
npm run migration:create --name=test
this worked for me but the migration name is static ($npm_config_name) migration file could not be named on creating it
Yes, I would like to see updated documentation with more examples, otherwise it causes pain and suffering
Я нашел некоторую информацию здесь https://github.com/typeorm/typeorm/releases/tag/0.3.0 Моя версия 0.3.5
УСТАРЕВШИЕ
entities,migrations,subscribersпараметры внутриDataSourceOptionsподдержки строковых каталогов устарели. В будущих версиях вы сможете передавать только ссылки на сущности .Как это:
// ormconfig.ts const connectionSource = new DataSource({ ... entities: [ExamplesEntity], migrations: [ExamleMigration1656076568327], ... }) export default connectionSource;Скрипты:
{ "scripts": { "typeorm": "typeorm-ts-node-esm -d ./src/db/ormconfig.ts", "migration:create": "typeorm-ts-node-esm migration:create ./src/db/migrations/$npm_config_name", "migration:generate": "npm run typeorm migration:generate ./src/db/migrations/$npm_config_name", "migration:show": "npm run typeorm migration:show", "migration:run": "npm run typeorm migration:run", "migration:revert": "npm run typeorm migration:revert" } }Применение:
npm run migration:generate --name=example_migration npm run migration:create --name=example_migration npm run migration:runОно работает.
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
$npm_config_name does not work with yarn.
npm or pnpm should be fine.
@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%",
$npm_config_namedoes not work with yarn. npm or pnpm should be fine.
pnpm does not work.
NOTICE: typeorm-ts-node-esm and typeorm-ts-node-commonjs cannot register tsconfig-paths/register. so nestjs project may not work.
still using node -r tsconfig-paths/register -r ts-node/register ./node_modules/typeorm/cli.js
Temporary use this bash one-liner:
read -p 'Name: ' name && yarn migration:generate src/database/migrations/$name
This is a crap but works TypeORM developers, WTF!?