Tutorial

02. 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 of 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 all these resources.

Adapters

AdminBro uses "Database Adapters" to manage different kinds of resources.

How to use an adapter

  1. First, you have to install the 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 so given Adapter can 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 tutorial: 03. Customize resources.

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

Example of 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 - this is how Mongoose does this.
// Most probably you will have them defined in a separate file
const User = mongoose.model('User', { name: String, email: String, surname: String })
const Admin = mongoose.model('Admin', { name: String, email: String})
// we have to connect with the database first so we wrap it with async function
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,
  // also with an additional options for admin resource
  const adminBro = new AdminBro({
    resources: [User, {
      resource: Admin,
      options: {
        //...
      },
    }],
  })
}
run()
// ...

The way how each Adapter handles initialization differs. That is why make sure to read its documentation first.

Resources customization

The biggest advantage of using AdminBro is the ability to fully customize how it works. Visit 03. Customize resources to see how you can change the behavior of selected resources.

What's next?

Now let see how you can modify the resource in tutorial 03. Customize resources.

SoftwareBrothers

Proudly built and maintained by SoftwareBrothers

Software House with a passion for both JavaScript and TypeScript.

See what we do See what we believe in

Proudly built and maintained by

SoftwareBrothers