[Nest] 171 - 08/31/2022, 8:35:42 PM ERROR [ExceptionHandler] Cannot read properties of undefined (reading 'getRepository') tenant-node | TypeError: Cannot read properties of undefined (reading 'getRepository') tenant-node | at InstanceWrapper.useFactory [as metatype] (/var/www/app/src/infrastruture/providers/tenant.provider.ts:6:56) tenant-node | at Injector.instantiateClass (/var/www/app/node_modules/@nestjs/core/injector/injector.js:343:55) tenant-node | at callback (/var/www/app/node_modules/@nestjs/core/injector/injector.js:53:45) tenant-node | at processTicksAndRejections (node:internal/process/task_queues:95:5) tenant-node | at Injector.resolveConstructorParams (/var/www/app/node_modules/@nestjs/core/injector/injector.js:132:24) tenant-node | at Injector.loadInstance (/var/www/app/node_modules/@nestjs/core/injector/injector.js:57:13) tenant-node | at Injector.loadProvider (/var/www/app/node_modules/@nestjs/core/injector/injector.js:84:9) tenant-node | at async Promise.all (index 3) tenant-node |
at InstanceLoader.createInstancesOfProviders (/var/www/app/node_modules/@nestjs/core/injector/instance-loader.js:47:9) tenant-node | at /var/www/app/node_modules/@nestjs/core/injector/instance-loader.js:32:13
Content in my tenant.provider.ts:
import { DataSource, getRepository, Repository } from "typeorm";
import { TenantSchema } from "../db/TenantSchemas.schema";
export const TenantProviders = [{
provide: 'TENANT_REPOSITORY',
useFactory: (dataSource: DataSource) => dataSource.getRepository(TenantSchema),
Inject: ['DATA_SOURCE']
}, ];
Inclusion within my app.module.ts:
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
const envTenant = ConfigModule.forRoot({
isGlobal: true
})
import { TenantModule } from './tenant.module';
import { TenantProviders } from '../infrastruture/providers/tenant.provider';
import { TenantTypeOrmRepository } from '../infrastruture/db/TypeOrmRepository';
import { DBTenant } from '../infrastruture/db/connections/db.tenant';
import { TenantController } from './tenant.controller';
@Module({
imports: [
envTenant,
DBTenant,
//TenantModule
],
controllers: [TenantController],
providers: [
...TenantProviders,
TenantTypeOrmRepository
],
})
export class AppModule {}
My TenantSchema.schema.ts:
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class TenantSchema {
constructor(tRepository?: Partial<TenantSchema>) {
this.id = tRepository?.id;
this.name = tRepository?.name;
this.tenant = tRepository?.tenant;
this.isActive = tRepository?.isActive;
}
@PrimaryGeneratedColumn()
id: number;
@Column({ length: 128 })
name:string;
@Column({ length: 10 })
tenant:string;
@Column({ default: false })
isActive:boolean;
}
Database provider setup in db.provider.ts
import { appDataSource } from './app.source';
export const dbProviders = [
{
provide: 'DATA_SOURCE',
useFactory: async () => {
return appDataSource.initialize();
},
},
];