Issue
I have a NestJS API with TypeORM entities with Postgres DB. What I am trying to do is to set up some initial migration and then to have as series of migrations as the project proceeds. The migration command in package.json is not working. Any help is appreciated.
Here are the errors:
yarn typeorm:cli
yarn run v1.22.18
$ ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli -f src/typeorm/factory/typeorm-migrations.config.ts
Usage: cli <command> [options]
Commands:
cli schema:sync Synchronizes your entities with database
schema. It runs schema update queries on all
connections you have. To run update queries on
a concrete connection use -c option.
cli schema:log Shows sql to be executed by schema:sync
command. It shows sql log only for your default
dataSource. To run update queries on a concrete
connection use -c option.
cli schema:drop Drops all tables in the database on your
default dataSource. To drop table of a concrete
connection's database use -c option.
cli query [query] Executes given SQL query on a default
dataSource. Specify connection name to run
query on a specific dataSource.
cli entity:create <path> Generates a new entity.
cli subscriber:create <path> Generates a new subscriber.
cli migration:create <path> Creates a new migration file.
cli migration:generate <path> Generates a new migration file with sql needs
to be executed to update schema.
cli migration:run Runs all pending migrations.
cli migration:show Show all migrations and whether they have been
run or not
cli migration:revert Reverts last executed migration.
cli version Prints TypeORM version this project uses.
cli cache:clear Clears all data stored in query runner cache.
cli init Generates initial TypeORM project structure. If
name specified then creates files inside
directory called as name. If its not specified
then creates files inside current directory.
Options:
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
Not enough non-option arguments: got 0, need at least 1
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
npm run migration:generate BaseMigration
> npm run typeorm:cli -- migration:generate -n "BaseMigration"
> ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli -f src/typeorm/factory/typeorm-migrations.config.ts "migration:generate" "-n" "BaseMigration"
cli migration:generate <path>
Generates a new migration file with sql needs to be executed to update schema.
Options:
-h, --help Show help [boolean]
-d, --dataSource Path to the file where your DataSource instance is
defined. [string] [required]
-p, --pretty Pretty-print generated SQL [boolean] [default: false]
-o, --outputJs Generate a migration file on Javascript instead of
Typescript [boolean] [default: false]
--dr, --dryrun Prints out the contents of the migration instead of
writing it to a file [boolean] [default: false]
--ch, --check Verifies that the current database is up to date and that
no migrations are needed. Otherwise exits with code 1.
[boolean] [default: false]
-t, --timestamp Custom timestamp for the migration name
[number] [default: false]
-v, --version Show version number [boolean]
Not enough non-option arguments: got 0, need at least 1
package.json
"typeorm:cli": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli -f src/typeorm/factory/typeorm-migrations.config.ts",
"migration:generate": "npm run typeorm:cli -- migration:generate -n"
Here are my config files:
typeorm.config.ts
import { TypeOrmModuleOptions } from "@nestjs/typeorm";
export const typeOrmBaseConfig: TypeOrmModuleOptions = {
type: 'postgres',
synchronize: false,
logging: true,
entities: ['src/typeorm/entities/**/*.ts'],
migrations: ['src/typeorm/migrations/**/*.ts'],
subscribers: ['src/typeorm/subscribers/**/*.ts'],
};
typeorm-migration.config.ts
import { typeOrmBaseConfig } from './typeorm.config';
const migrationConfig = {
...typeOrmBaseConfig,
cli: {
entitiesDir: 'src/typeorm/entities',
migrationsDir: 'src/typeorm/migrations',
subscribersDir: 'src/typeorm/subscribers'
},
host: process.env.DATABASE_HOST,
port: parseInt(process.env.DATABASE_PORT || '3306', 10),
username: process.env.DATABASE_USERNAME,
database: process.env.DATABASE_NAME,
password: process.env.DATABASE_PASSWORD,
};
export = migrationConfig;
typeorm-connection.factory.ts
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { TypeOrmModuleOptions, TypeOrmOptionsFactory } from '@nestjs/typeorm';
import { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConnectionOptions';
import { AwsSecretsFactory } from 'src/aws/aws.secrets.factory';
import { ConfigOptions } from 'src/config/configuration';
import { typeOrmBaseConfig } from './typeorm.config';
@Injectable()
export class TypeOrmConnectionsFactory implements TypeOrmOptionsFactory {
private readonly awsFactory: AwsSecretsFactory;
constructor(private readonly configService: ConfigService) {
this.awsFactory = new AwsSecretsFactory(configService);
}
async createTypeOrmOptions(): Promise<TypeOrmModuleOptions> {
const secret = await this.awsFactory.getSecret();
return {
...typeOrmBaseConfig,
host: this.configService.get(ConfigOptions.DatabaseHost) || secret.host,
port: secret.port,
username: secret.username,
password: secret.password,
database: this.configService.get(ConfigOptions.DatabaseName),
ssl: this.configService.get(ConfigOptions.DatabaseSecureConnect),
} as PostgresConnectionOptions;
}
}
and in app.module.ts
...
@Module({
imports: [
ConfigModule.forRoot({
load: [configuration],
}),
TypeOrmModule.forRootAsync({
useClass: TypeOrmConnectionsFactory,
inject: [],
imports: [ConfigModule],
}),
...
My initial hunch is that the configuration from js files are not kicking in as well as from the command line are ignored. Any help is appreciated.
Solution
The latest version on typeorm has removed these capabilities. So had to downgrade the version of typeorm.
Answered By - Rohith Poyyeri
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.