adminjs-typeorm icon indicating copy to clipboard operation
adminjs-typeorm copied to clipboard

Is there a way to work with Entities created with EntitySchema?

Open CaioTeixeira95 opened this issue 4 years ago • 1 comments

Hello folks!

I have the following code:

// acount.ts

export class Account {
  id?: number
  exchangeAccountId: string
  externalUserId: string
  apiKey: string
  apiSecret: string
  isActive: boolean

  constructor(
    exchangeAccountId: string,
    externalUserId: string,
    apiKey: string,
    apiSecret: string,
    isActive: boolean,
    id?: number
  ) {
    this.id = id
    this.exchangeAccountId = exchangeAccountId
    this.externalUserId = externalUserId
    this.apiKey = apiKey
    this.apiSecret = apiSecret
    this.isActive = isActive
  }
}
// account-entity.ts

import { Account } from './account'

export const AccountEntity = new EntitySchema<Account>({
  name: 'Account',
  tableName: 'accounts',
  target: Account,
  columns: {
    id: {
      type: number,
      name: 'id',
      primary: true,
      generated: true,
    },
    exchangeAccountId: {
      type: String,
      name: 'exchange_account_id',
      nullable: true,
    },
    externalUserId: {
      type: String,
      name: 'external_user_id',
    },
    apiKey: {
      type: String,
      name: 'api_key',
      nullable: true,
    },
    apiSecret: {
      type: Number,
      name: 'api_secret',
      nullable: true,
    },
    isActive: {
      type: Boolean,
      name: 'is_active',
      default: false,
    },
  },
})
// account-repository.ts

import { Account } from './account'
import { AccountEntity } from './account-entity'
import { AbstractRepository, EntityRepository } from 'typeorm'

interface AccountRepositoryInterface {
  findById(id: number): Promise<Account | undefined>
  findAll(): Promise<Account[]>
  save(account: Account): Promise<Account | undefined>
}

@EntityRepository(AccountEntity)
export class AccountRepository extends AbstractRepository<Account> implements AccountRepositoryInterface {
  async findById(id: number): Promise<Account | undefined> {
    return this.manager.findOne(Account, { id })
  }

  async save(account: Account): Promise<Account> {
    return this.manager.save(Account, account)
  }

  async findAll(): Promise<Account[]> {
    return this.manager.find(Account)
  }
}
// app.ts

import AdminBroExpress from '@admin-bro/express'
import * as AdminBroTypeOrm from '@admin-bro/typeorm'
import AdminBro from 'admin-bro'
import { createConnection } from 'typeorm'
import { Router } from 'express'
import express from 'express'

import { AccountEntity } from './account-entity'

interface AdminBroConfig {
  adminBro: AdminBro
  adminBroRouter: Router
}

async function setupAdminBro(): Promise<AdminBroConfig> {
  AdminBro.registerAdapter({ Database, Resource })

  const adminBro = new AdminBro({
    resources: [{ resource: AccountEntity }],
    rootPath: '/admin',
    branding: {
      companyName: 'My company',
    },
  })

  const adminBroRouter = AdminBroExpress.buildRouter(adminBro)

  return { adminBro, adminBroRouter } as AdminBroConfig
}

const app = express()

createConnection()
  .then(setupAdminBro)
  .then(({ adminBro, adminBroRouter }) => app.use(adminBro.options.rootPath, adminBroRouter))

// And so on...

And I'm getting the following error:

NoResourceAdapterError: There are no adapters supporting one of the resource you provided
    at /path/to/project/node_modules/admin-bro/lib/backend/utils/resources-factory/resources-factory.js:99:15
    at Array.map (<anonymous>)
    at ResourcesFactory._convertResources (/path/to/project/node_modules/admin-bro/lib/backend/utils/resources-factory/resources-factory.js:93:22)
    at ResourcesFactory.buildResources (/path/to/project/node_modules/admin-bro/lib/backend/utils/resources-factory/resources-factory.js:48:35)
    at new AdminBro (/path/to/project/node_modules/admin-bro/lib/admin-bro.js:101:39)
    at /path/to/project/src/app.ts:16:20
    at Generator.next (<anonymous>)
    at /path/to/project/node_modules/tslib/tslib.js:115:75
    at new Promise (<anonymous>)
    at __awaiter (/path/to/project/node_modules/tslib/tslib.js:111:16)

Because of the project's architecture, I can't use the decorators (@ Entity, @ Column...). That said, could you guys help me with how can I make AdminBro work with my project that uses EntitySchema?

Thank you in advantage!

CaioTeixeira95 avatar Nov 24 '21 15:11 CaioTeixeira95

I would also really appreciate entity schema support. I was able to hack something together that gets the job done and I wanted to basically reintegrate my additions into @adminjs/typeorm but I was unable to get the repo to work... Maybe some of the maintainers can jump in and help out?

romandecker avatar Jan 05 '22 14:01 romandecker