Tutorial

03. Adding resources

Idea of Resources

What is a Resource in AdmiBro? - Resource is everything that you can manage (CRUD - create, read, update, destroy). Most often it is a Model from your ORM.

The entire idea od AdminBro is to manage resources of all kinds. It doesn't matter if you use MongoDB with mongoose or PostgreSQL with Sequelize. AdminBro should give you the ability to manage records in those resources.

Adapters

AdminBro uses different database adapters in order to handle different kind of resources.

How to use an adapter

  1. First, you have to install adapter locally using the npm/yarn.
  2. Next, you have to register this adapter using the AdminBro.registerAdapter that AdminBro could recognize resources of its type.
const AdminBro = require('admin-bro')
const AdminBroMongoose = require('admin-bro-mongoose')

AdminBro.registerAdapter(AdminBroMongoose)

Passing resources to AminBro

So you know how to register an adapter - now let's take a look of how to add resources that they can be seen in AdminBro.

You have 2 options:

  1. you can either add an entire Database and AdminBro will fetch all resources from it,
  2. or you can pass each Resource one by one.

The first option is very easy, but the second allows you to modify the resources, see: 04-customizing-resources.

Both passing entire Database or each Resource can be done via AdminBro options

Example by using the mongoose adapter:

// ...
const AdminBro = require('admin-bro')
const AdminBroMongoose = require('admin-bro-mongoose')
const mongoose = require('mongoose')
AdminBro.registerAdapter(AdminBroMongoose)

// Initialize Database along with models
const User = mongoose.model('User', { name: String, email: String, surname: String })
const Admin = mongoose.model('Admin', { name: String, email: String})

const run = async () => {
  const mongooseDb = await mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true })

  // Passing resources by giving entire database
  const adminBro = new AdminBro({
    databases: [mongooseDb],
    //... other AdminBroOptions
  })

  // Passing resources one by one
  const adminBro = new AdminBro({
    resources: [User, Admin],
    //... other AdminBroOptions
  })
}

run()
// ... 

Available adapters

Currently we support following Database Adapters:

Resources customization

Each resource could be customized in AdminBro - to see how to do this go to 04. Customize resources