Tutorial

03. Adding resources

Idea of Resources

What is a Resource in AdmiBro? - Resource is everything what 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 Postgress with Sequelize. AdminBro should give you the ability to manage records in those resources.

Adapters

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

How to use an adapter

  1. First, you have to install adapter localy 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 regiser 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 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. Customize 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)

// Initializ 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 customised in AdminBro - to see how to do this go to 04. Customize resources