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

express-admin-pannel

v1.0.2

Published

Admin pannel for express app

Downloads

271

Readme

express-admin-pannel

Library to help with curd operations

USAGE

Installation

   npm i express-admin-pannel

These must be implement to use the library.

   const express = require('express')
   const cors = require('cors')
   const {AdminPannel} = require("express-admin-pannel")
   
   
   const app = express()

   app.use(express.json()) /* will be used to parse josn data */
   app.use(express.urlencoded({ extended: true })) /* will be used to parse form data */
   app.use(cors()) /* To prevent cors errors NOTE: cors must be installed */

Initializing sequelize (only sequelize is supported as of now)

  const {Sequelize,DataTypes} = require('sequelize')

  const sequelize = new Sequelize('database', 'username', 'password', {
    host: 'localhost',
    dialect: /* one of 'mysql' | 'postgres' | 'sqlite' | 'mariadb' | 'mssql' | 'db2' | 'snowflake' | 'oracle' */
   });

Creating a dummy models

  const ActorProfile = sequelize.define("ActorProfile", {
      photo: {type:DataTypes.STRING},
      description: DataTypes.TEXT,
  },
  
  const Actor = sequelize.define("Actor", {
      name: { type: DataTypes.STRING, allowNull: false },
      
       ActorProfileId: {
        type: DataTypes.INTEGER,
        references: {
          model: ActorProfile, // 'ActorProfiles' would also work
          key: "id",
      },
    },
  });

  ActorProfile.hasOne(Actor)
  Actor.belongsTo(ActorProfile)
  /* after defining association the field must be specified in the model or else our library will have no way to know existance of that relation */
  /* See the ActorProfileId in Actor model that is added by sequelize but for the admin pannel you have to specify it in the model*/
  });
  

Initializing admin pannel

  const {AdminPannel} = require("express-admin-pannel")
  
  const adminPannel = new AdminPannel("sequelize", sequelize, app}
  adminPannel.initialize("admin");//The argument is the route to adminpannel
  
  /* adminpannel can be seen at http://localhost:port/admin */
  

new AdminPannel needs at least three arguments with a optional argument to customize admin pannel. 1)First argument is the name of ORM you are using. Currently only sequelize is supported. 2)Second argunemt takes the instance of db. In this case it is sequelize 3)Third argument is the express app 4)Options to customize admin panel

adminPannel.initialize take one argument that is route to admin pannel */

Listening at a port

  app.listen(8000, () => console.log("listening at port 8000"));

now visit http://localhost:8000/admin to see the admin pannel.

Customization

Admin Pannel can be customized by passing options while instantiating admin pannel

    const adminPannel = new AdminPannel("sequelize", sequelize, app,{
       titleFields: /* fields mentioned here will be shown in admin pannel */,
       imageFields: /* fields mentioned here should be a link and it will be shown as image in admin pannel */,
       noOfItemsPerPage: /* shows this mentioned of items before showing pagination default is 30 */;
     }}

Lets look at them individually:

titleFields

     const adminPannel = new AdminPannel("sequelize", sequelize, app,{
       titleFields: { Actor: ["id", "name","ActorProfileId"], ActorProfile: ["photo","description"] },
                     /* {model1:["field1","field2"],model2:["field1","field2"]} */
     }}

this will show id,name,ActorProfileId in admin pannel.

optionally if you add a __title__ virtual field and return something then it will be shown in the admin pannel automatically if you have not customized title fields For Example: if we change our Actor model as below and add a virtual field called __title__


  const Actor = sequelize.define("Actor", {
     name: { type: DataTypes.STRING, allowNull: false },
     
      ActorProfileId: {
       type: DataTypes.INTEGER,
       references: {
         model: ActorProfile, // 'ActorProfiles' would also work
         key: "id",
     },
     __title__: {
      type: DataTypes.VIRTUAL,
      get() {
        return `${this.getDataValue("name")}`;
      },
     },
   },
 });

whatever you return from the __title__ virtual field will be automatically shown in admin pannel unless you have customized the titleFields if you still want to show __title__ then you have to mention in the titleFields.

imageFields

If you have a link to a image and you want to see it as a image then you can also do that. In our case we have a field called photo in ActorProfile so we will show it as image in admin pannel

   const adminPannel = new AdminPannel("sequelize", sequelize, app,{
      titleFields: { ActorProfile: ["photo"] },//First we mention that we want to show photo in admin pannel
      imageFields: {ActorProfile:["photo"]} // Then we mention it in the image field
    }}

noOfItemsPerPage

If you want to show a specific number of items before showing pagination then specify noOfItemsPerPage property:

   const adminPannel = new AdminPannel("sequelize", sequelize, app,{
      noOfItemsPerPage:10,
    }}

this will show 10 items before showing pagination.