Skip to content

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

Open
@CaioTeixeira95

Description

@CaioTeixeira95

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions