Cannot add cli parameters to DataSourceOptions in TypeORM

Viewed 2416

In the typeORM documentation a cli parameter can be added to DataSourceOptions according to https://github.com/typeorm/typeorm/blob/master/docs/data-source-options.md. The example I saw on https://typeorm.io/using-cli looks was

{
    cli: {
        entitiesDir: "src/entity",
        subscribersDir: "src/subscriber",
        migrationsDir: "src/migration"
    }
}

I tried this in my code as follows:

let dataSource = new DataSource(
  {
        type: 'postgres',
        host: 'localhost',
        port: 5432,
        database: 'website',
        username: 'test',
        password: 'test',
        logging: true,
        synchronize: false,
        entities: [User, Posts],
        cli: {
            entitiesDir: "src/entity",
            subscribersDir: "src/subscriber",
            migrationsDir: "src/migration"
        }
  })

However I get the following error: Argument of type '{ type: "postgres"; host: string; port: number; database: string; username: string; password: string; logging: true; synchronize: false; entities: (typeof User | typeof Wallet)[]; cli: { entitiesDir: string; subscribersDir: string; migrationsDir: string; }; }' is not assignable to parameter of type 'DataSourceOptions'. Object literal may only specify known properties, and 'cli' does not exist in type 'PostgresConnectionOptions'.ts(2345)

4 Answers

I had the same problem. To solve this issue, you can simply remove the cli key from the Datasource options and specify the path when creating a new migration

typeorm migration:create -n UrlMigration -d src/migrations 

-d option is used to specify the directory where your migration will be created

"scripts": {
  ...
  "typeorm": "typeorm-ts-node-commonjs -d ormconfig.ts"
}

NB : Replace ormconfig.ts with your datasource filename

refer to TypeORM CLI help

TypeORM and TypeORM CLI not works perfectly after 0.3.0. I had the same problem, so I advice you to downgrade to version 0.2

also, I had a similar issue typeorm version: 0.3.7

what I did:

  1. I created in src/config/ormconfig-migrations.ts
import { DataSource } from 'typeorm';

const configMigration = new DataSource({
  type: 'postgres',
  host: 'localhost',
  port: 5432,
  username: 'mediumclone',
  password: 'qwerty',
  database: 'mediumclone',
  entities: [__dirname + '/**/*.entity{.ts,.js}'],
  synchronize: false,
  migrations: [__dirname + '/migrations/**/*{.ts,.js}'],
});

export default configMigration;

in src/comfig/ormconfig.ts

import { DataSourceOptions } from 'typeorm';

const config: DataSourceOptions = {
  type: 'postgres',
  host: 'localhost',
  port: 5432,
  username: 'mediumclone',
  password: 'qwerty',
  database: 'mediumclone',
  entities: [__dirname + '/**/*.entity{.ts,.js}'],
  synchronize: false,
  migrations: [__dirname + '/migrations/**/*{.ts,.js}'],
};

export default config;

i had separeted, because ormconfig i use for connect db on my app, a ormconfig-migrations.ts - l use to migrations

import { Module } from '@nestjs/common';
import { AppController } from '@app/app.controller';
import { AppService } from '@app/app.service';
import { TagModule } from '@app/tag/tag.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import ormconfig from '@app/config/ormconfig';

@Module({
  imports: [TypeOrmModule.forRoot(ormconfig), TagModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
  1. in package.json i added:
"scripts": {
     "typeorm": "typeorm-ts-node-commonjs -d src/config/ormconfig-migrations.ts",
     "db:drop": "yarn run typeorm schema:drop"
}
  1. in a terminal:
yarn run db:drop

but i have a problem with:

yarn run typeorm migration:generate -n

who did it resolve, please if you resolve this, let know

i hope that with npm other people will not have a problems i hope it helps to someone

(15/08/2022) Hi!. I update typeorm to 0.3.7, and typeorm is working with DataSource (in the typeorm web page tutorial is called data-source.ts) instead of ormconfig.ts... and i realized that

cli: { entitiesDir: "src/entity", subscribersDir: "src/subscriber", migrationsDir: "src/migration" }

does not work. But following these steps might help you:

1-- Add these lines in your scripts of your packege.json

"scripts": { ... "typeorm": "typeorm-ts-node-commonjs", "typeorm_src": "typeorm-ts-node-commonjs -d src/data-source.ts" }

"src/data-source.ts" is where you have your database info...

  • (in case of generate migrations) In your terminal located in your root directory, you have to write this line

npm run typeorm_src migration:generate src/migrations/nameOfMyMigration

  • (in case of only create migrations wich is a empty file) In your terminal located in your root directory, you have to write this line

npm run typeorm migration:create src/migrations/nameOfMyMigration

  • In both cases the lines are going to generate a file (in src/migrations/nameOfMyMigration) with the migrations to do and with the name {timestamp}-nameOfMyMigration.ts, after the creation of that file, execute this line

npm run typeorm_src migration:run

it is going to make the migrations in your database previously generated in your {timestamp}-nameOfMyMigration.ts.

SOURCE: https://typeorm.io/

Related