Home > Enterprise >  Ormconfig to DataSource in typeorm 0.3?
Ormconfig to DataSource in typeorm 0.3?

Time:12-02

I have a project with an API using TypeScript, NestJS and typeorm.

I am using typeorm version 0.2 but cause of security issues I have to upgrade to version 0.3 and there were a lot of changes. Now I am not sure, if I understand the DataSource correctly, which I haven't used before.

Before I used a ormconfig.ts file, not a json:

// ormconfig.ts

import "dotenv/config";
import * as path from "path";
import { ConnectionOptions } from "typeorm";

const connectionOptions: ConnectionOptions = {
    type: "postgres",
    host: process.env.TYPEORM_HOST,
    port: parseInt(process.env.TYPEORM_PORT as string),
    username: process.env.TYPEORM_USERNAME,
    password: process.env.TYPEORM_PASSWORD,
    database: process.env.TYPEORM_DATABASE,
    entities: [path.join(__dirname, "**", "*.entity.{ts,js}")],
    cache: true,
    synchronize: false,
    migrationsRun: false,
    migrations: ["build/src/migration/*.js"],
    cli: {
        migrationsDir: "./migration",
    },
};

export = connectionOptions;

And use it in app.module.ts like this:

// app.module.ts

import connectionOptions = require("./ormconfig");

@Module({
    imports: [
        TypeOrmModule.forRoot(connectionOptions),
    ],
})
export class AppModule {}

And if I need a transaction I used Injection:

// my-entity.service.ts

@Injectable()
export class MyEntityService {
    constructor(
        @InjectRepository(MyEntity)
        @InjectConnection() private connection: Connection
    ) {}
}

But now Connection is depreacted, the cli property isn't available for PostgreSQL, and you should use DataSource.

I am doing this now and using this connection in app.module.ts like before:

// ormconfig.ts

import "dotenv/config";
import * as path from "path";
import { PostgresConnectionOptions } from "typeorm/driver/postgres/PostgresConnectionOptions";

const connectionOptions: PostgresConnectionOptions = {
    type: "postgres",
    host: process.env.TYPEORM_HOST,
    port: parseInt(process.env.TYPEORM_PORT as string),
    username: process.env.TYPEORM_USERNAME,
    password: process.env.TYPEORM_PASSWORD,
    database: process.env.TYPEORM_DATABASE,
    entities: [path.join(__dirname, "**", "*.entity.{ts,js}")],
    logging: process.env.TYPEORM_LOGGING === "true",
    cache: true,
    // Migrations
    synchronize: false,
    migrationsRun: false,
    migrations: ["build/src/migration/*.js"],
};

export = connectionOptions;

I've added a data-source.ts:

// data-source.ts

import { DataSource } from "typeorm";
import connectionOptions = require("./ormconfig");

export const AppDataSource = new DataSource(connectionOptions);

I've changed Connection to DataSource:

// my-entity.service.ts

@Injectable()
export class MyEntityService {
    constructor(
        @InjectRepository(MyEntity)
        @InjectConnection() private dataSource: DataSource
    ) {}
}

And cause of the missing cli property in the ConnectionOptions, I am using now the new CLI command with the migrations path and the data source instead of my old ormconfig file:

typeorm migration:generate ./src/migrations/update-my-entity-table -d ./src/data-source.ts

It seems all to work. Is this the right way? Does the @InjectConnection() and changing from Connection to DataSource do all the job I needed? Cause I never use the exported AppDataSource anywhere.

I am also not using [import reflect-metadata][1] anywhere like the typeorm documentation says.

Am I missing something?

CodePudding user response:

Nest will take the options you've passed and create a DataSource instance, so @InjectDataSource() still works just fine.The AppDataSource you have should pretty much just be for your CLI for ease of use.

Also, @nestjs/common imports reflect-metadata so you shouldn't have to worry about that either.

  • Related