How to use @admin-bro/nestjs with @admin-bro/typeorm and postgres in a right way?
Asked Answered
F

1

5

The admin-bro-nestjs repository contains a comprehensive example with example with mongoose. But I need use it with typeorm and postgres.
I tried to adapt this example for typeorm:

// main.ts
import AdminBro from 'admin-bro';
import { Database, Resource } from '@admin-bro/typeorm';
import { NestFactory } from '@nestjs/core';

import { AppModule } from './app.module';

AdminBro.registerAdapter({ Database, Resource });

const bootstrap = async () => {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();

and

// app.module.ts
import { Module } from '@nestjs/common';
import { AdminModule } from '@admin-bro/nestjs';
import { TypeOrmModule, getRepositoryToken } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UserEntity } from './user/user.entity';

@Module({
  imports: [
    TypeOrmModule.forRoot({
        type: 'postgres',
        host: 'localhost',
        port: 5432,
        username: 'postgres',
        password: 'password',
        database: 'database_test',
        entities: [UserEntity],
        synchronize: true,
        logging: false,
      }),
    AdminModule.createAdminAsync({
      imports: [
        TypeOrmModule.forFeature([UserEntity]),
      ],
      inject: [
        getRepositoryToken(UserEntity),
      ],
      useFactory: (userRepository: Repository<UserEntity>) => ({
        adminBroOptions: {
          rootPath: '/admin',
          resources: [
            { resource: userRepository },
          ],
        },
        auth: {
          authenticate: async (email, password) => Promise.resolve({ email: 'test' }),
          cookieName: 'test',
          cookiePassword: 'testPass',
        },
      }),
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule { }

But on application start I get the following error:

NoResourceAdapterError: There are no adapters supporting one of the resource you provided

Does anyone have any experience putting all these libraries together?

Foreshore answered 21/2, 2021 at 20:27 Comment(0)
K
6

I also got NoResourceAdapterError: There are no adapters supporting one of the resource you provided when I tried to integrate AdminBro with NestJS/Express/TypeORM (though I'm using MySQL instead Postgres). The solution posted in the admin-bro-nestjs Github issue which links to this question didn't help me.

The cause of NoResourceAdapterError for me simply was that UserEntity didn't extend BaseEntity. NestJS/TypeORM seems to work fine without extending BaseEntity but apparently it is required for AdminBro. You can see it used in the examples of the AdminBro TypeORM adapter documentation.

Here is an example which works for me.

// user.entity
import { Entity, BaseEntity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class UserEntity extends BaseEntity{
  @PrimaryGeneratedColumn()
  id: number;
  @Column()
  name: string;
}
// app.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AdminModule } from '@admin-bro/nestjs';
 
import { Database, Resource } from '@admin-bro/typeorm';
import AdminBro from 'admin-bro'
AdminBro.registerAdapter({ Database, Resource });

@Module({
  imports: [
    TypeOrmModule.forRoot({
      // ...
    }),
    AdminModule.createAdmin({
        adminBroOptions: {
          resources: [UserEntity],
          rootPath: '/admin',
        },
    })
  ],
  // ...
})
Kraigkrait answered 22/4, 2021 at 16:46 Comment(3)
this answer worked for me, saved me hours of debugging and checking what's wrongPaulownia
I didn't think this would do anything, but turns out it was the root cause for me also.Appreciative
My entities extends to BaseEntity, still the problem continuesBermudez

© 2022 - 2024 — McMap. All rights reserved.