npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

b-generator

v3.1.0

Published

A simple and useful nodejs and express package to create admin panels

Downloads

13

Readme

b-generator

A simple and useful Admin generator and report generator for nodejs

Table of contents

  1. Installation
  2. Usage
  3. CRUD Generator
  4. Report Generator

Installation

Using npm:

$ npm install b-generator

Using bower:

$ bower install b-generator

Using yarn:

$ yarn add b-generator

Using pnpm:

$ pnpm add b-generator

Usage

Structure

You should create two file for each model. the first is a model definition, and the second is repository file which extends Base Repository inside b-generator package

Repository

const BGenerator = require("b-generator");
const BaseRepository = BGenerator.BaseRepository;
const SampleModel = require("Path_to_SampleModel");

class SampleRepository extends BaseRepository {
    // Your relations and ...
}

module.exports = new SampleRepository(SampleModel);

Routing

Inside routes folder you can use a macro that is implemented for creating CRUDs.

const BGenerator = require("b-generator");
const bGeneratorRouter = BGenerator.bGeneratorRouter;

bGeneratorRouter.crud(router, [], 'SampleController', (new SampleController()), 'api/crud/sample');
bGeneratorRouter.crudSettings(router, [], 'SampleSettingsController', (new SampleController()), 'api/crud/sample-settings');

The difference between crud and crudSettings is that crudSettings will remove delete route and that is used for settings pages where we want to show one form instead of list with all CRUD options.

Inside Resources

You should have folders that you put data for generators and reports schema. you can name it "generators" and "reports".

Modules

These are some modules inside b-generator:

  1. CRUD Generator: It creates an api based crud that can be used by react and vue.js.
  2. Report Generator: It creates an api based report generator with ability to create filters and export as excel.

CRUD Generator

There are two types of controllers. The simple one is CRUD controller that you can add simple CRUD like user management and etc.

The other one is crud parent controller that manipulates data that are subset of another data. For example the posts that belong to user. or comments that belong to a post.

Controller

const BGenerator = require("b-generator");
const BGenController = BGenerator.CrudController;
// for using parent controller
// const BGenController = BGenerator.CrudParentController;

const configFile = require("path_to_config.json");

const sequelize = require("sequelize");
const bGenConfig = {
    wrapper_api_base: "/api/",
    sequelize,
    base_app_path: __dirname + "/../../"  /* PATH TO YOUR APP FOLDER */
}

class SampleController extends BGenController {
    constructor() {
        super(configFile, bGenConfig);
    }
}

module.exports = SampleController;

Admin Config File

Inside your config file you should address model and repository of the file that you need.

The path starts from "base_app_path" that you gave earlier inside controller constructor.

{
    "params": {
        "model": "Path_to_model",
        "repository": "path_to_repository",
        "customView": "backoffice/simple"
    },

    "fields": [
        { "name": "sample_field_1", "label": "language:labels.sample_field_1" },
        { "name": "sample_field_2", "label": "Sample Field 2" }
    ],

        "form": {
        "display": [
          "sample_field_1",
          "sample_field_2"
        ]
    },

    "filter": {
        "display": [
          "sample_field_1",
          "sample_field_2"
        ]
    },

    "list": {
        "title": "Sample list title",
            "display": [
              "sample_field_1",
              "sample_field_2"
            ],
            "objectActions": [
            { "name": "edit", "label": "Edit", "route": "/api/admin/v1/sample/load/:id", "type": "action", "class": "btn btn-sm btn-default g-bg-white border", "icon": "fa fa-edit" },
            { "name": "delete", "label": "Delete", "route": "/api/admin/v1/sample/delete/:id", "type": "action", "view": false, "class": "btn btn-sm btn-danger border", "icon": "fa fa-trash", "confirm": "Are you sure?"}
        ],
            "actions": [
            { "name": "create", "label": "Create", "route": "/api/admin/v1/sample/load", "type": "action", "class": "btn btn-info abstract-list-btn-standard" }
        ],
            "generalActions": [],
            "batchActions": []
    },

    "new": {
        "title" : "New Sample",
            "display": [
              "sample_field_1",
              "sample_field_2"
            ],
            "actions": [
            { "name": "list", "label": "Back", "route": "/api/admin/v1/sample", "type": "action", "class": "btn btn-default abstract-list-btn-standard" },
            { "name": "create", "label": "Save", "route": "/api/admin/v1/sample/create", "type": "action", "view": false, "class": "btn btn-primary abstract-list-btn-standard"},
            { "name": "save_and_add", "label": "Save and Add", "route": "/api/admin/v1/sample/create", "view": false, "type": "action", "class": "btn btn-primary c-btn-save-and-add"}
        ]
    },

    "edit": {
        "title" : "Edit Sample",
            "actions": [
            { "name": "list", "label": "Back", "route": "/api/admin/v1/sample", "type": "action", "class": "btn btn-default abstract-list-btn-standard" },
            { "name": "delete", "label": "Delete", "route": "/api/admin/v1/sample/delete/:id", "type": "action", "view": false, "class": "btn btn-danger abstract-list-btn-standard", "confirm": "Are you Sure?"},
            { "name": "update", "label": "Save", "route": "/api/admin/v1/sample/update/:id", "type": "action", "view": false, "class": "btn btn-primary abstract-list-btn-standard"}
        ]
    },

    "show": {
        "title" : "Show Sample",
            "display": [],
            "actions": []
    },

    "excel": {
        "display": []
    }
}

Fields structure

{
    "fields": [
      {
        "name": "sample_field_1",
        "label": "language:labels.sample_field_1",
        "dataProvider": "aMethodNameInsideRepository (used to fetch options inside list by its id. it is only used when you have micro services or have no access to parts of your data inside your current package)",
        "form": {
          "type": "text|select|checkbox|color|hidden|file|password|autocomplete",
          "multiple": "true|false (in case of type=select)",
          "expanded": "true|false (in case of type=select)",
          "dataProvider": "aMethodNameInsideRepository (used to fetch select options and validate) or a route (in case of type=autocomplete)",
          "placeholder": "some text",
          "choices": {
            "key1": "in case of predefined choices in select you can use this and remove dataProvider",
            "key2": "value2"
          },
          "validation": ["nullable", "required", "min:5", "max:10"]
        },
        "filter": {
          "type": "text|select|checkbox|color|hidden|file|password|autocomplete",
          "multiple": "true|false (in case of type=select)",
          "expanded": "true|false (in case of type=select)",
          "dataProvider": "aMethodNameInsideRepository (used to fetch select options and validate) or a route (in case of type=autocomplete)",
          "placeholder": "some text",
          "choices": {
            "key1": "in case of predefined choices in select you can use this and remove dataProvider",
            "key2": "value2"
          },
          "validation": ["nullable", "required", "min:5", "max:10"]
        }
      }
    ]
}

Notes:

  1. Check Validator for all validation options
  2. You can use ":base" inside "dataProvider" for example ":base/getUsers". This is automatically replaced with "wrapper_api_base" that you passed inside constructor of controller. It is a good practice is to put "wrapper_api_base" into .env file.

Display Fields inside List

Once you defined fields you can add options for displaying list.

{
  
    "filter": {
        "display": ["sample_field_1", "sample_field_2"]
    },

    "list": {
        "title": "Sample list title",
        "display": ["sample_field_1", "_sample_field_2"],
        "objectActionsDisplay": "collapsed|expanded",
        "objectActions": [
            { "name": "edit", "label": "Edit", "route": ":base/sample/load/:id", "type": "action", "class": "btn btn-sm btn-default g-bg-white border", "icon": "fa fa-edit" },
            { "name": "delete", "label": "Delete", "route": ":base/sample/delete/:id", "type": "action", "view": false, "class": "btn btn-sm btn-danger border", "icon": "fa fa-trash", "confirm": "Are you sure?"}
        ],
        "actions": [
            { "name": "create", "label": "Create", "route": ":base/sample/load", "type": "action", "class": "btn btn-info abstract-list-btn-standard" }
        ],
       "generalActions": [],
       "batchActions": []
    }

}

Note:

  1. If you want to customize display of data inside table, You can put underscore at the beginning of every field name inside display. After that you should create a list_FIELD_NAME.jade file inside resources_folder/views/YOUR_CUSTOM_VIEW_VALUE_INSIDE_PARAMS. in this file you can access to a variable named "item" that refers to the object that you need to show.
  2. you can refer :FIELD_NAME inside routes. this will be automatically replaced to item[FIELD_NAME].
  3. You can access this API by sending a post request to "/api/crud/sample"

Report Generator

note: you can use i18next for field labels

License

ISC