Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
CaioTeixeira95 opened this issue Nov 24, 2021 · 1 comment
Open
Labels
enhancement New feature or request

Comments

@CaioTeixeira95
Copy link

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!

@romandecker
Copy link

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?

@dziraf dziraf added the enhancement New feature or request label Feb 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants