mongoose-metadata

1.0.3 • Public • Published

mongoose-metadata

Mongoose Metadata API Helpers

Install mongoose-metadata :

npm install --save mongoose-metadata

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"
  }
})
  • Email
// In mongoose model
let petSchema = new Schema({
  name : {  
    type: String,
    required: true,
    placeholder: "Nom de l'animal",
    forceField: "email"
  }
})
  • 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
//TODO

Create Forms

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

// In your routes.js
const path = require('path');
const _MMetadata = require('mongoose-metadata');
const Metadata = _MMetadata(mongoose);


// 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('/<modelName>/meta_add', Metadata.meta('<Model>'));

Filter fields

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

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

// Here, we remove the name field from the meta so it will not be rendered
// So, for those fields, you would not get a form input client side.
app.get('/<modelName>/meta_edit', Metadata.meta('<Model>', {
  filter: ['name']
}));

Readme

Keywords

Package Sidebar

Install

npm i mongoose-metadata

Weekly Downloads

0

Version

1.0.3

License

MIT

Last publish

Collaborators

  • aluzed