typeorm icon indicating copy to clipboard operation
typeorm copied to clipboard

Typeorm CLI not works perfectly after 0.3.0

Open HamzaT404 opened this issue 4 years ago • 31 comments

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.

HamzaT404 avatar Mar 27 '22 06:03 HamzaT404

typeorm 0.3.4 version too.

so, i decide downgrade package version from 0.3.4 to 0.2.45.

it works.

mintplo avatar Mar 27 '22 07:03 mintplo

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)

Sreejith288 avatar Mar 27 '22 10:03 Sreejith288

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.

pleerock avatar Mar 27 '22 14:03 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.

Thanks now it works. it there any way to create entity in specific folder without giving full path.

HamzaT404 avatar Mar 28 '22 05:03 HamzaT404

it there any way to create entity in specific folder without giving full path.

no, you need to provide full path now.

pleerock avatar Mar 28 '22 17:03 pleerock

'cli' does not exist in type 'PostgresConnectionOptions'

salimzade avatar Apr 09 '22 20:04 salimzade

Documentation is not updated yet .. cli does not exist in DataSourceOptions?? path for migration??

pranavo72bex avatar May 03 '22 06:05 pranavo72bex

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.

ar-work-acc avatar May 08 '22 11:05 ar-work-acc

This issue should have more visibility, it could save hours!!

Vercryger avatar May 14 '22 01:05 Vercryger

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

serg06 avatar May 27 '22 21:05 serg06

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

mbeghephinx avatar Jun 06 '22 20:06 mbeghephinx

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

joaoignaciojvig avatar Jun 07 '22 18:06 joaoignaciojvig

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.

joaoignaciojvig avatar Jun 07 '22 23:06 joaoignaciojvig

Thanks this is the solution for the issue of the migrations

ctangarife avatar Jun 16 '22 17:06 ctangarife

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.

jorgebodega avatar Jun 16 '22 19:06 jorgebodega

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}')], });

ctangarife avatar Jun 16 '22 20:06 ctangarife

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.

jorgebodega avatar Jun 16 '22 20:06 jorgebodega

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

devgwiji avatar Jun 24 '22 08:06 devgwiji

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.

romanmesh avatar Jun 24 '22 13:06 romanmesh

The documentation is a joke. I wish I tried different orm

Sebastp avatar Jul 05 '22 09:07 Sebastp

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;

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.

sam-eah avatar Jul 07 '22 07:07 sam-eah

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"

misostack avatar Jul 07 '22 10:07 misostack

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

emmyduruc avatar Jul 08 '22 11:07 emmyduruc

Yes, I would like to see updated documentation with more examples, otherwise it causes pain and suffering

IlayMorozoff avatar Jul 10 '22 16:07 IlayMorozoff

Я нашел некоторую информацию здесь 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

IlayMorozoff avatar Jul 10 '22 16:07 IlayMorozoff

$npm_config_name does not work with yarn. npm or pnpm should be fine.

lelinhtinh avatar Jul 10 '22 16:07 lelinhtinh

@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%",

kornect avatar Jul 19 '22 08:07 kornect

$npm_config_name does not work with yarn. npm or pnpm should be fine.

pnpm does not work.

Thanatad avatar Aug 03 '22 01:08 Thanatad

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

Diluka avatar Aug 25 '22 05:08 Diluka

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!?

zolotarev avatar Sep 04 '22 21:09 zolotarev