Security News
The Dark Side of Open Source
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
b-generator
Advanced tools
Readme
A simple and useful Admin generator and report generator for nodejs
Using npm:
$ npm install b-generator
Using bower:
$ bower install b-generator
Using yarn:
$ yarn add b-generator
Using pnpm:
$ pnpm add b-generator
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
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);
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.
You should have folders that you put data for generators and reports schema. you can name it "generators" and "reports".
These are some modules inside b-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.
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;
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": [
{
"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:
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:
note: you can use i18next for field labels
FAQs
A simple and useful nodejs and express package to create admin panels
The npm package b-generator receives a total of 1 weekly downloads. As such, b-generator popularity was classified as not popular.
We found that b-generator demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
Research
Security News
The Socket Research team found this npm package includes code for collecting sensitive developer information, including your operating system username, Git username, and Git email.
Security News
OpenJS is warning of social engineering takeovers targeting open source projects after receiving a credible attempt on the foundation.