
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
mongoose-formulate
Advanced tools
Mongoose plugins to faciliate dynamic form based input based on custom templates
Mongoose Formulate is a package of 2 mongoose plugins that allow you to bake a programmatical CMS into your existing applications. It's intended to be used to build a framework that allows dynamic creation of form templates that in turn structure input that is stored in entries.
If you need a system for allowing users of your application to define custom form templates based on common input formats and a flexible way to store data submitted against those templates, Formulate is for you. Think of it as a pluggable CMS. If your looking for a full-blown CMS solution then it's not for you. There is no UI component, everything is powered programmtically and the UIs for the fieldtypes, templates and forms must be implemented by you. This is by design.
npm install mongooose-formulate
First you need to setup the template plugin:
// install the template plugin on an existing collection, or create a generic 'templates collection'
var mongoose = require('mongoose');
var formulate = require('mongoose-formulate')
var Schema = mongoose.Schema;
// the plugin only adds additional fields to your schema, so you can add
// specific custom schema fields as you wish. Name would be an obvious one
var schema = {
name: String
};
schema.plugin(formulate.templatePlugin);
var TemplateModel = mongoose.model('template', schema);
Next setup the entry plugin. This is where your form content will be stored. Because this is just a mongoose plugin you add it to any of your exisiting collections (giving your form content 'context') or create a generic entries collection for ultimate flexiblity.
var mongoose = require('mongoose');
var formulate = require('mongoose-formulate')
var Schema = mongoose.Schema;
var myExistingSchema = {
name: String,
created: Date
};
schema.plugin(formulate.entryPlugin);
var MyModel = mongoose.model('my-model', schema);
Once the two plugins are setup, you can start adding custom templates. Here is an example of a user profile template:
TemplateModel.new({
name: 'Example Template',
template: {
fieldGroups: [
{
name: 'Full Name',
options: {
customOption: true
},
fields: [
{
fieldtype: 'text'
}
]
},
{
name: 'Date of Birth',
options: {
customOption: true
},
fields: [
{
fieldtype: 'datetime'
}
]
},
{
name: 'Employment Status',
fields: [
{
fieldtype: 'select',
validation: {
enum: ['Employed', 'Self-Employed', 'Unemployed']
}
}
]
},
]
}
})
You could then use this resuable template to generate 'entries' via your client applications, e.g. Mobile App or Web App. Your client will need to interpret these templates (this is the part you have to write yourself) so they can display some kind of form to the end user.
The entry mimicks the schema of the template, with the addition of a value property on the fields. An entry document might be created like so:
MyModel.new({
name: 'Custom entry name',
content: {
fieldGroups: [
{
name: 'Full Name',
options: {
customOption: true
},
fields: [
{
fieldtype: 'text',
value: 'John Smith' // the submitted value for the field
}
]
},
{
name: 'Date of Birth',
options: {
customOption: true
},
fields: [
{
fieldtype: 'datetime',
value: Thu Apr 24 1987 00:00:00 GMT+0000 (UTC)
}
]
},
{
name: 'Employment Status',
fields: [
{
fieldtype: 'select',
value: 'Employed', // any value not found in enum will invalidate
validation: {
enum: ['Employed', 'Self-Employed', 'Unemployed']
}
}
]
},
]
}
})
As you can see it is a very flexible way to store data based on freeform data (e.g. data you will not define a schema for).
There are 3 components to Formulate: Fieldtypes, Templates and Entries.
Fieldtypes are the building blocks of your templates. They are a basic from of common inputs that should be flexible enough to handle most 'form content'.
Currently Formulate has the follow fieldtypes:
Each of the fields has validation options (which, as they stand, need improving) which allows customisation of input you store in your entries.
Templates can be thought of as a Form Builder. Imagine templates as a way to programmatically store your content structures as you would in your CMS – Heads up: Formulate handles only the logic of structuring and storing your content. There is no UI component to this package, that part is up to you!
Templates contain an Array of fieldGroups
, which you can imagine as a single line of input in a traditional form entry. They have the following properties:
Entries are where the data for a submitted template is stored. Entries mimick the schema used for the template they were based on, but there is no definite relationship between the two. E.g. Once an entry is created, it holds a copy of the template it was based on, so existing templates can be safely updated without affecting historical entries.
FAQs
Mongoose plugins to faciliate dynamic form based input based on custom templates
The npm package mongoose-formulate receives a total of 2 weekly downloads. As such, mongoose-formulate popularity was classified as not popular.
We found that mongoose-formulate demonstrated a not healthy version release cadence and project activity because the last version was released 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
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.