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

react-mongoose-form-maker

v0.0.2

Published

Generate a form from Mongoose Metadata

Downloads

2

Readme

FormMaker

Preparation

Work with mongoose model. On your api, create a route 'metadata' for a given model :

// First of all, import the module
const path = require('path')
const Metadata = require('mongoose-metadata')

// Load Models
Metadata.loadModels(path.join(__dirname, './models'))

// Let's use a model pet
// We need to use the meta middleware : Metadata.meta('<modelName>')
app.get('/pets/metadata', Metadata.meta('Pet'))

Disabled fields

By default not all kind of field will be displayed on client side. Here is the list of disabled fields :

  • __v
  • _id
  • created_at
  • updated_at

So, for those fields, you would not get a form input client side.

Model Options

In the mongoose model :

let petSchema = new Schema({
  name        : {type: String, required: true, placeholder: "Name of the animal"},
  kind        : {
    type: String,
    enum: ['Lion', 'Cat', 'Dog', 'Rabbit', 'Bird', 'Duck'],
    placeholder: "Type of animal" // Placeholder
  },
  description : {
    type: String,
    placeholder: "Enter the description",
    forceField: "textarea" // Force generation of a textarea instead of a input type text
  }
  weight      : {type: Number},
  vaccined    : {type: Boolean},
  created_at  : {type: Date, default: Date.now},
  updated_at  : {type: Date}
})

module.exports = mongoose.model('Pet', petSchema)

List of field types

  • Text
// In mongoose model
let petSchema = new Schema({
  name : {  
    type: String,
    required: true,
    placeholder: "Nom de l'animal"
  }
})
  • Numeric
// In mongoose model
let petSchema = new Schema({
  weight : { type: Number }
})
  • type: Boolean, checkbox
// In mongoose model
let petSchema = new Schema({
  weight : { type: Number }
})
  • Enum : displays select input as default
// In mongoose model
let petSchema = new Schema({
  kind : {
    type: String,
    enum: ['Lion', 'Cat', 'Dog', 'Rabbit', 'Bird', 'Duck'],
    placeholder: "Type"
  }
})
  • Textarea
// In mongoose model
let petSchema = new Schema({
  description : {
    type: String,
    forceField: "textarea"
  }
})
  • Radio
// In mongoose model
let petSchema = new Schema({
  kind : {
    type: String,
    enum: ['Lion', 'Cat', 'Dog', 'Rabbit', 'Bird', 'Duck'],
    placeholder: "Type",
    forceField: "radio"
  }
})
  • Foreign Key

Create multiple forms

// First of all, import the module
const Metadata = require('/path/to/metadata.js')

// Add meta fields
// Here, we disabled kind and vaccined fields
app.get('/pets/meta_add', Metadata.meta('Pet', { filter: ['kind', 'vaccined'] }))

// Edit meta fields
// Here, we disabled name field
app.get('/pets/meta_edit', Metadata.meta('Pet', { filter: ['name'] }))

Views

That's it for the API.

Now on the client side react project :

Add Form

import FormMaker from '../Forms/formMaker'

// Now in the render
render() {
return <FormMaker
  title="Add a Pet"
  metaUrl="http://server/pets/metadata"
  onSubmit={ (values)=>{ console.log(values) } }
  onCancel={ ()=>{ console.log('cancel callback') } } />
}

// Now you can handle each action submit and cancel by binding your callback here

Edit Form

import FormMaker from '../Forms/formMaker'

// Now in the render
// We just have to set values and data, will be matched automatically
render() {
return <FormMaker
  title="Add a Pet"
  metaUrl="http://server/pets/metadata"
  values={{
    name: "Toto",
    vaccined: true,
    kind: "Cat"
  }}
  onSubmit={ (values)=>{ console.log(values) } }
  onCancel={ ()=>{ console.log('cancel callback') } } />
}

Customize style

Each element got a customizable Css style or caption, look in www/src/Forms/formStyles, by default we use Bootstrap 4 classes.