Adminbro、急行、MongoDB、マングースで5分で管理領域をつくること


管理領域を設定し、実際にすべての管理ルートとコントローラを構築せずにデータを5分で作業を開始する方法があります.ここに...
我々が必要とするすべてはモデルです、そして、それから我々はAdminBro パッケージは完全に動作ダッシュボードを実行するために我々のモデル以外に基づいて.
まず、Expressサーバを設定する必要があります.
mkdir server 
cd server 
npm init
エクスプレスと管理broパッケージをインストールしましょう
npm i @adminjs/express @adminjs/mongoose adminjs express mongoose            
今モデルのフォルダを作成する必要があります
mkdir models
そして、モデルのためのファイル、我々は製品とカテゴリーのモデルを作ると言いましょう
touch models/products.js models/categories.js
製品のスキーマを定義しましょうmodels/products.js :
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const productsSchema = new Schema({
  product: {
    type: String,
    required: true,
    unique: true
  },
  price: {
    type: Number,
    required: true
  },
  categoryId: {
    type: Schema.Types.ObjectId, ref: 'categories',
    required: true
  },
});

module.exports = mongoose.model('products', productsSchema);
と内部のカテゴリmodels/categories.js :
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const categoriesSchema = new Schema({
    category: {
        type: String,
        required: true,
        unique: true
    }
},
{strictQuery: false}
)
module.exports =  mongoose.model('categories', categoriesSchema);
ではメインサーバファイルを作りましょうindex.js インサイドserver フォルダ
touch index.js
そして、この基本的なベアボーンコードを追加します.
// GENERAL CONFIG
const app = require('express')();
const port = process.env.PORT || 5050;

// CONNECTING TO DB
const mongoose = require('mongoose');
(async function () {
  try {
    await mongoose.connect('mongodb://127.0.0.1/ourDatabase');
    console.log('Your DB is running');
  } catch (error) {
    console.log('your DB is not running. Start it up!');
  }
})();

app.listen(port, () => console.log(`🚀 Server running on port ${port} 🚀`));
今、我々はサーバーを実行することができますnodemon そして、それがアップして、ローカルMongoデータベースに接続して実行してください.
今、最後のステップ-私たちは私たちのモデルをインポートする必要がありますし、管理者broは残りを行います.
これを加えるindex.js DBに接続した後のファイル
// ADMIN BRO
const AdminJS = require('adminjs');
const AdminJSExpress = require('@adminjs/express')
// We have to tell AdminJS that we will manage mongoose resources with it
AdminJS.registerAdapter(require('@adminjs/mongoose'));
// Import all the project's models
const Categories = require('./models/categories'); // replace this for your model
const Products = require('./models/products'); // replace this for your model
// Pass configuration settings  and models to AdminJS
const adminJS = new AdminJS({
  resources: [Categories, Products],
  rootPath: '/admin'
});
// Build and use a router which will handle all AdminJS routes
const router = AdminJSExpress.buildRouter(adminJS);
app.use(adminJS.options.rootPath, router);
// END ADMIN BRO
管理broをインポートした後に見ることができますように、我々のモデルが必要です.
const Categories = require('./models/categories'); // replace this for your model
const Products = require('./models/products'); // replace this for your model
それから、Categories and Products ) この例ではadmin bro )
const adminJS = new AdminJS({
  resources: [Categories, Products],
  rootPath: '/admin'
});
さらにダッシュボードのパスを設定するrootPath: '/admin'指定されたポート(この例では5050)でサーバを開き、管理者URLに移動します/admin ) この例では、我々のデータを使用する準備が整いました.
Demo repo on GitHub