@muhammedadel/madmin
v1.4.0
Published
- Muhammed Admin package => first step to make framework to node.js # About - this package use to make easy admin panal by only model name and routes guard.
Downloads
2
Maintainers
Readme
MAdmin
- Muhammed Admin package => first step to make framework to node.js
About
- this package use to make easy admin panal by only model name and routes guard.
important to
- app.use(express.json()); or app.use(bodyParser.json())
- app.use(express.urlencoded()); or app.use(bodyParser.urlencoded())
- you must but user in req.user becuase i use req.user.role
Package componenets
- Controller
- guardAppRoutes
- Catalog
- Action, ==> model
- Role, ==> mode
- adminAccountSchema, ==> schema for user account to define role refer to Role Model
explain logic
- every Role have array of acctions
- action is { url: { type: String, required: true }, ===> '*' for all routes type: { type: String, enum: ['all', 'get', 'post', 'put', 'delete', 'patch', 'head', 'options', 'trace'] } }
- action foucs on specfic method on certien route
- you must put in your user schema the following
- role: { type: mongoose.Schema.Types.ObjectId, ref: 'Role', required: true }
Depndencies
- mongodb ( mongoose )
- express
Features
- the controller create routes with impelementaion from model [get all [Catalog class used here], get by id, create, create many , delete, delete many, update by id ]
- Catalog class make catalog and pagination
- validator for rules
- Router Guard by action
Controller (Model, options, rules)
- mongoose model
- options = { prefix: 'hidden', // default 'admin' ==> localhost:3000/admin notAllowedMethods: ['getAll'], urlTitleForMany: 'tests', default table name ===> localhost:3000/tests urlTitleForOne: 'test', default Model name hiddenFields: ['_id', '__v'], ===> projection of mongodb
Controller.app()
immutableFields: ['marks'] ===> to prevent edit on this field <br />
isAuth: isAuthFuntion ==> give me refrence for your is authentication function becuase i want to check if user are logged in or not by your function <br />
};
rules = { name: 'minLen:1|maxLen:255|required|string', year: 'minLen:1|maxLen:3|required||number|max:2023|min:1963', active: 'boolean', gender: 'enum:male,female', };
const pagination = new Catalog(this.Model, { page, pageSize }, filters, sortBy, this.projection());
Catalog (Model, paginationOption, filters, sortBy, projection)
- mongoose model
- paginationOption = { page: 1, pageSize: 10 // [5, 10, 25, 50, 100] }
- filters = { name: 'mohamed' // insenstive case and use regex for searcing }
- sortBy = 'name' for asc '-name' for desc
guardAppRoutes
- use this function to guard your routes by role id
- usage => guardAppRoutes(req.user.roleId)
guardMiddleware (roleId)
- use this middleware to gaurd specic route
guardByReq
- use this middleware without pass roleId becuase i take it from req.user.role
appRouter (prefix='admin')
- usage app.use(appRouter());
- this middleware give you endpoints for actions and roles
- role endpoints
- Get => prefix/roles/:id ===> get specifc role
- Get => prefix/roles/ ===> get all roles
- Put => prefix/roles/ ===> update role
- delete => prefix/roles/:id ===> delete role
- post => prefix/roles/:id/clone-actions ===> to clone actions from role id to another and you can add custom actions
- actions endpoints
- Get => prefix/actionss/:id ===> get specifc actions
- Get => prefix/actionss/ ===> get all actionss
- Put => prefix/actionss/ ===> update actions
- delete => prefix/actionss/:id ===> delete actions