Generating a migration with typeorm

Viewed 40

I try to generate a new migration using typeORM, but I get the following error:

Error during migration generation:
Error: Unable to open file: "libs/backend/user/orm.config.ts". ⨯ Unable to compile TypeScript:
libs/backend/user/orm.config.ts:1:38 - error TS2307: Cannot find module '@app/shared/orm-helper' or its corresponding type declarations.

1 import { ormConfigWithPresets } from '@app/shared/orm-helper';
                                       ~~~~~~~~~~~~~~~~~~~~~~~~

I am using the following command:

typeorm-ts-node-commonjs migration:generate -d libs/backend/user/orm.config.ts ./libs/backend/user/src/lib/infrastructure/migrations

And my orm.config file looks as follows:

import { ormConfigWithPresets } from '@app/shared/orm-helper';
import { UserSchemas } from './src/lib/infrastructure/schemas/schemas';
import migrations from './src/lib/infrastructure/migrations/migrations';
import {DataSource} from 'typeorm';

const config = ormConfigWithPresets(
    'default',
    ...
    UserSchemas,
    migrations,
    'libs/backend/user/src/lib/infrastructure/migrations',
);

export default new DataSource(config);

It points to the following function:

import { SqlServerConnectionOptions } from 'typeorm/driver/sqlserver/SqlServerConnectionOptions';
import OrmPresets from './orm-presets';
import { EntityClassOrSchema } from '@nestjs/typeorm/dist/interfaces/entity-class-or-schema.type';

export function ormConfigWithPresets(
    name: string,
    secretConfig: OrmSecretConfig,
    host: string,
    port: number,
    database: string,
    entityPrefix: string,
    entities: EntityClassOrSchema[],
    migrations: unknown[],
    migrationsDir: string,
    allowSelfSignedCertificates = OrmPresets.allowSelfSignedCertificate,
    dropSchema = OrmPresets.dropSchema,
): SqlServerConnectionOptions {
    return {
        name,
        type: 'mssql',
        host,
        port,
        username: secretConfig.username,
        password: secretConfig.password,
        database,
        dropSchema,
        synchronize: OrmPresets.synchronize,
        entityPrefix,
        entities,
        logging: OrmPresets.logging,
        options: OrmPresets.options,
        migrationsRun: OrmPresets.migrationsRun,
        pool: OrmPresets.pool,
        migrations,
        cli: {
            migrationsDir,
        },
        extra: {
            trustServerCertificate: allowSelfSignedCertificates,
        },
    } as SqlServerConnectionOptions;
}

export class OrmSecretConfig {
    username: string;
    password: string;
}

I have a tsconfig.base.json file at the root of my project where the path is defined, but which he somehow this way cannot recognize:

{
  "compileOnSave": false,
  "compilerOptions": {
    ...,
    "paths": {
      ...,
      "@app/shared/orm-helper": ["libs/shared/orm-helper/src/index.ts"],
      ...,
    },
  },
}

Is there a better way to approach this problem? I am fairly new and using some legacy documentation.

TypeOrm v0.3.x

0 Answers
Related